tripl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* tripl/tripl.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *linebuffer_save = NULL;
  28. char *line_buffer = NULL;
  29. int lno = 0;
  30. char *eof;
  31. char *line_eof;
  32. static int next_line(char *),
  33. parse_line(void);
  34. struct arch *arch_fn;
  35. extern struct arch interp_arch_fn,
  36. x86_64_arch_fn;
  37. struct context *current;
  38. struct context main_context = {
  39. .funcname = "main",
  40. .link = NULL,
  41. .private_data = NULL
  42. };
  43. const static struct {
  44. char *name;
  45. struct arch *arch;
  46. } arch_list[] = {
  47. { "interp", &interp_arch_fn},
  48. { "x86_64", &x86_64_arch_fn},
  49. { NULL, NULL }
  50. };
  51. const struct attr_table vardef_attrtab[] = {
  52. { "global", 1 },
  53. { "rdonly", 4 },
  54. { "ptr", 8 },
  55. { NULL, 0 }
  56. };
  57. const struct attr_table declare_attrtab[] = {
  58. { "shared", 1 },
  59. { "fn", 2 },
  60. { "var", 4 },
  61. { "dynalib", 1 },
  62. { "function", 2 },
  63. { "variable", 4 },
  64. { NULL, 0 }
  65. };
  66. const struct attr_table dump_attrtab[] = {
  67. { "var", 1 },
  68. { "fn", 2 },
  69. { "names", 4 },
  70. { "libs", 8 },
  71. { "all", 16 },
  72. { "variables", 2 },
  73. { "functions", 2 },
  74. { "libraries", 8 },
  75. { NULL, 0 }
  76. };
  77. int do_tripl_compile(char *fbuffer, off_t fsize) {
  78. char *archname = getenv("TRIPL_ARCH");
  79. int i;
  80. current = &main_context;
  81. if (!archname)
  82. arch_fn = &interp_arch_fn;
  83. else {
  84. for (i = 0; arch_list[i].name; i++)
  85. if (!strcmp(arch_list[i].name, archname)) {
  86. arch_fn = arch_list[i].arch;
  87. goto ok;
  88. }
  89. fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", archname);
  90. return 1;
  91. }
  92. ok: eof = fbuffer + fsize;
  93. arch_fn->arch_init();
  94. while (next_line(fbuffer))
  95. parse_line();
  96. arch_fn->arch_fin ? arch_fn->arch_fin() : 0;
  97. return 0;
  98. }
  99. /* skip the comment, from '# asdf\nasdf2' to 'asdf2' */
  100. static char *__skip_comment(char *fbuffer) {
  101. if (likely(*fbuffer != '#'))
  102. return fbuffer;
  103. while (*fbuffer != '\n')
  104. fbuffer++;
  105. _skip_spaces(fbuffer);
  106. lno++;
  107. if (*fbuffer == '#')
  108. return __skip_comment(fbuffer);
  109. return fbuffer;
  110. }
  111. /* skip to the ';' */
  112. static char *to_eol(char *p, char *eof) {
  113. int in = 0;
  114. while (p < eof) {
  115. if (*p == '\"') {
  116. if (in)
  117. in = 0;
  118. else
  119. in = 1;
  120. }
  121. if (!in && *p == ';')
  122. return p;
  123. p++;
  124. }
  125. return NULL;
  126. }
  127. static int next_line(char *fbuffer) {
  128. ++lno;
  129. /* first time we call this, initial */
  130. if (unlikely(!line_buffer)) {
  131. line_eof = line_buffer = fbuffer = __skip_comment(fbuffer);
  132. if (unlikely(fbuffer >= eof))
  133. /* only comment in this file */
  134. return 0;
  135. line_eof = to_eol(line_eof, eof);
  136. linebuffer_save = line_buffer;
  137. return 1;
  138. }
  139. /* move line_buffer to ';' + 1 and line_eof to line_buffer -> ';' */
  140. line_buffer = fbuffer = __skip_comment(line_buffer);
  141. if (unlikely(fbuffer >= eof))
  142. return 0;
  143. /* skip current line */
  144. if (unlikely(!(line_buffer = to_eol(line_buffer, eof))))
  145. return 0;
  146. line_buffer++;
  147. _skip_spaces(line_buffer)
  148. line_buffer = __skip_comment(line_buffer);
  149. line_eof = line_buffer;
  150. line_eof = to_eol(line_eof, eof);
  151. linebuffer_save = line_buffer;
  152. if (unlikely(line_buffer >= eof))
  153. return 0;
  154. return 1;
  155. }
  156. static int getnumber(char *p) {
  157. int dec = 0, log = 1, neg = 0;
  158. if (!isnr(*p)) {
  159. if (*p == '+')
  160. neg = 0;
  161. else if (*p == '-')
  162. neg = 1;
  163. else
  164. exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
  165. p++;
  166. }
  167. if (unlikely(*p == '0' && isnr(p[1])))
  168. exit(generic_error("number starting with '0'", linebuffer_save, lno));
  169. /* never use eof because this is calling from getword() */
  170. while (isnr(*p)) p++;
  171. --p;
  172. while (p >= line_buffer) {
  173. dec += itoa(*p) * log;
  174. log *= 10;
  175. p--;
  176. if (!isnr(*p))
  177. /* for - and + */
  178. break;
  179. }
  180. return neg ? -dec : dec;
  181. }
  182. static inline int getwordtype(char c) {
  183. if (isnr(c))
  184. return WORDTYPE_NR;
  185. else if (ischr(c))
  186. return WORDTYPE_NAME;
  187. else if (isspace(c))
  188. return 0;
  189. return WORDTYPE_KWORD;
  190. }
  191. /* NEVER MODIFY THIS! MAGIC!
  192. * THIS IS REALLY THE UGLY CODE, MOST UGLY CODE ALL OVER THE WORLD.
  193. *
  194. * BUT IT WORKS NOW, SO WE DO NOT TO CHANGE THIS AND WE WILL CHANGE
  195. * A LOT BECAUSE ALL OF THE TRIPL REQUIRES THIS FUNCTION.
  196. *
  197. * f**k up. i will fix this up. --- 2021.1.29 rain
  198. * */
  199. #define isfirstchr() !len
  200. #define notfirstchr() len
  201. char *getword(char *p, int *type, int *out) {
  202. int last_word_type, curr_word_type;
  203. int len, in = 0;
  204. char *back = p;
  205. last_word_type = 0;
  206. *type = 0;
  207. if (p >= line_eof)
  208. return NULL;
  209. for (len = 0; p < line_eof; p++, len++) {
  210. if (*p == '\"') {
  211. if (in) {
  212. in = 0;
  213. *type = WORDTYPE_STR;
  214. *out = len - 1;
  215. return p;
  216. }
  217. if (!in)
  218. in = 1;
  219. }
  220. if (!in) {
  221. curr_word_type = getwordtype(*p);
  222. if (!curr_word_type)
  223. break;
  224. if ((*p == '-' || *p == '+') && isnr(p[1])) {
  225. *type = WORDTYPE_NR;
  226. *out = getnumber(p);
  227. return p;
  228. }
  229. if (isfirstchr() && curr_word_type == WORDTYPE_KWORD) {
  230. *type = WORDTYPE_KWORD;
  231. *out = *p;
  232. return p;
  233. }
  234. if (notfirstchr() && curr_word_type != last_word_type) {
  235. if (curr_word_type == WORDTYPE_NR && !isnr(p[-1]))
  236. continue;
  237. break;
  238. }
  239. last_word_type = curr_word_type;
  240. }
  241. }
  242. *type = last_word_type;
  243. switch (last_word_type) {
  244. case WORDTYPE_NR:
  245. *out = getnumber(back);
  246. break;
  247. case WORDTYPE_NAME:
  248. *out = len;
  249. break;
  250. }
  251. return back;
  252. }
  253. char *read_code_block(char *p) {
  254. int level = 0, in = 0;
  255. /* skip to next '{' (not just next, but end of this code-block) */
  256. for (; p < eof; p++) {
  257. /* strings and comments... */
  258. if (*p == '\"') {
  259. if (in)
  260. in = 0;
  261. else
  262. in = 1;
  263. }
  264. if (!in) {
  265. __skip_comment(p);
  266. if (*p == '{')
  267. level++;
  268. else if (*p == '}') {
  269. level--;
  270. if (!level)
  271. return p;
  272. }
  273. }
  274. }
  275. /* eof */
  276. return exit(generic_error("1 or more than 1 '}'s require", linebuffer_save, lno)), NULL;
  277. }
  278. static inline int find_attr(char *attrname, const struct attr_table *tab) {
  279. int i;
  280. for (i = 0; tab[i].attrname; i++)
  281. if (!strcmp(tab[i].attrname, attrname))
  282. return tab[i].attrmask;
  283. return 0;
  284. }
  285. int getattrmask(char *p, const struct attr_table *tab) {
  286. char *attrname;
  287. int type, out, mask = 0, t;
  288. if (!getword(p, &type, &out))
  289. return 0;
  290. if (unlikely(type != WORDTYPE_KWORD))
  291. exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno));
  292. if (unlikely(out != '@'))
  293. exit(generic_error("not a '@' after string", linebuffer_save, lno));
  294. ++p;
  295. _skip_spaces(p);
  296. while (getword(p, &type, &out)) {
  297. if (unlikely(type != WORDTYPE_NAME))
  298. exit(generic_error("an attribute name required", linebuffer_save, lno));
  299. attrname = strndup(p, out);
  300. if (unlikely(!(t = find_attr(attrname, tab))))
  301. exit(generic_error("unknow attribute", linebuffer_save, lno));
  302. mask |= t;
  303. free(attrname);
  304. _skip_word(p);
  305. }
  306. return mask;
  307. }
  308. static int parse_line(void) {
  309. char *p;
  310. int type, out;
  311. if ((p = getword(line_buffer, &type, &out)) >= line_eof)
  312. /* empty line */
  313. return 0;
  314. return arch_do_keyword(p, arch_fn);
  315. }