arch_templ.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* tripl/arch/arch_templ.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 <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <klist.h>
  26. #include <tripl/tripl.h>
  27. #include <tripl/limits.h>
  28. #include <tripl/arch.h>
  29. char *psave;
  30. struct klist_node names_list = KLIST_NODE_INIT(&names_list);
  31. extern char *line_buffer;
  32. extern char *line_eof;
  33. __arch_templ static int new_var(char *p, struct arch *);
  34. __arch_templ static int set_name(char *p, struct arch *, long *out);
  35. __arch_templ static int math_set_name(char *p, struct arch *, long *out);
  36. __arch_templ static int declare_object(char *p, struct arch *);
  37. __arch_templ static int interp_in_interp(char *p, struct arch *, long *out);
  38. __arch_templ int cstyle_declare(char *p, struct arch *);
  39. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  40. long ignored;
  41. psave = p;
  42. if (unlikely(p == line_eof))
  43. return 0;
  44. switch (*p) {
  45. case '$':
  46. /* '$ asdf = 0;' --- asdf = 0
  47. * '$ asdf 0;' --- asdf = 0
  48. * '$ asdf;' --- asdf = 0
  49. * */
  50. p++;
  51. _skip_spaces(p);
  52. new_var(p, arch_fn);
  53. break;
  54. case '!':
  55. p++;
  56. _skip_spaces(p);
  57. if (likely(!arch_fn->arch_dump))
  58. exit(generic_error("unsupported dumping in current mode", psave, lno));
  59. arch_fn->arch_dump(getattrmask(p, dump_attrtab));
  60. break;
  61. case '<':
  62. declare_object(p, arch_fn);
  63. break;
  64. case '`':
  65. /* running code in another interpreter */
  66. interp_in_interp(p, arch_fn, &ignored);
  67. break;
  68. case '+':
  69. case '-':
  70. if (!math_set_name(p, arch_fn, &ignored))
  71. exit(generic_error("unknow keyword", psave, lno));
  72. break;
  73. default:
  74. if (unlikely(!set_name(p, arch_fn, &ignored) &&
  75. !math_set_name(p, arch_fn, &ignored) &&
  76. !cstyle_declare(p, arch_fn)))
  77. exit(generic_error("unknow keyword", psave, lno));
  78. break;
  79. }
  80. return 0;
  81. }
  82. struct klist_node *find_name(char *name) {
  83. struct klist_node *n;
  84. for (n = names_list.next; n != &names_list; n = n->next)
  85. if (!strcmp(n->payload, name))
  86. return n;
  87. return NULL;
  88. }
  89. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  90. char *name;
  91. int type, out, attr;
  92. struct klist_node *t;
  93. getword(p, &type, &out);
  94. if (unlikely(type != WORDTYPE_NAME))
  95. exit(generic_error("a name for variable required", psave, lno));
  96. name = strndup(p, out);
  97. p += out;
  98. _skip_spaces(p);
  99. getword(p, &type, &out);
  100. switch (type) {
  101. case WORDTYPE_KWORD:
  102. if (unlikely(out != '='))
  103. exit(generic_error("keyword but it's not a '='", psave, lno));
  104. ++p;
  105. _skip_spaces(p);
  106. getword(p, &type, &out);
  107. if (unlikely(type != WORDTYPE_NR))
  108. exit(generic_error("a number required", psave, lno));
  109. _skip_word(p);
  110. break;
  111. case WORDTYPE_NR:
  112. /* to attribute (if have) */
  113. _skip_word(p);
  114. break;
  115. case 0:
  116. out = 0;
  117. break;
  118. default:
  119. exit(generic_error("bad word after variable name", psave, lno));
  120. }
  121. attr = getattrmask(p, vardef_attrtab);
  122. if (unlikely((t = find_name(name)) && !(t->xmask & XMASK_WEAK))) {
  123. error(name);
  124. exit(generic_error("name already defined", psave, lno));
  125. }
  126. arch_fn->arch_new_var(name, out, attr);
  127. /* the name can't be free */
  128. return 0;
  129. }
  130. static int getrightvalue(char *p, struct arch *arch_fn) {
  131. long res;
  132. if (*p == '`') {
  133. if (!interp_in_interp(p, arch_fn, &res))
  134. /* actually, it never be here */
  135. exit(generic_error("invalid right-value", psave, lno));
  136. }
  137. else if (!set_name(p, arch_fn, &res)
  138. && !math_set_name(p, arch_fn, &res))
  139. exit(generic_error("invalid right-value", psave, lno));
  140. return res;
  141. }
  142. static int set_name(char *p, struct arch *arch_fn, long *res) {
  143. char *name;
  144. int type, out;
  145. getword(p, &type, &out);
  146. if (type != WORDTYPE_NAME)
  147. return 0;
  148. name = strndup(p, out);
  149. p += out;
  150. _skip_spaces(p);
  151. getword(p, &type, &out);
  152. if (type != WORDTYPE_KWORD || out != '=')
  153. return 0;
  154. p++;
  155. _skip_spaces(p);
  156. getword(p, &type, &out);
  157. if (type != WORDTYPE_NR)
  158. out = getrightvalue(p, arch_fn);
  159. arch_fn->arch_set_var(name, out, 0);
  160. *res = out;
  161. free(name);
  162. return 1;
  163. }
  164. /* 9 -> a; and warning that math_set_name doesn't support instructions
  165. * such as 'a = 0 -> b' */
  166. static int math_set_name(char *p, struct arch *arch_fn, long *res) {
  167. char *name;
  168. int type, out, nr;
  169. getword(p, &type, &out);
  170. if (type != WORDTYPE_NR)
  171. return 0;
  172. nr = out;
  173. /* this requires a space */
  174. _skip_word(p);
  175. getword(p, &type, &out);
  176. if (unlikely(type != WORDTYPE_KWORD || out != '-'))
  177. exit(generic_error("a '->' required", psave, lno));
  178. p++;
  179. getword(p, &type, &out);
  180. if (unlikely(type != WORDTYPE_KWORD || out != '>'))
  181. exit(generic_error("a '->' required", psave, lno));
  182. p++;
  183. _skip_spaces(p);
  184. getword(p, &type, &out);
  185. if (unlikely(type != WORDTYPE_NAME))
  186. exit(generic_error("a name required", psave, lno));
  187. name = strndup(p, out);
  188. arch_fn->arch_set_var(name, nr, 0);
  189. *res = nr;
  190. free(name);
  191. return 1;
  192. }
  193. /*
  194. * 1.
  195. * < "libfoo.a" @ dynalib;
  196. * 2.
  197. * < fn @ fn;
  198. *
  199. * 3.
  200. * < fn 2 @ fn;
  201. *
  202. * 4.
  203. * < fn (a, b) @ fn;
  204. *
  205. * 5.
  206. * < age @ var;
  207. * */
  208. static int declare_object(char *p, struct arch *arch_fn) {
  209. char *name;
  210. int type, out, attr;
  211. enum { SHARED, NAMES, FUNCTION } guess;
  212. struct klist_node *n, *t;
  213. /* skip '<' */
  214. p++;
  215. _skip_spaces(p);
  216. if (unlikely(!getword(p, &type, &out)))
  217. exit(generic_error("a module name or object name required", psave, lno));
  218. switch (type) {
  219. case WORDTYPE_STR:
  220. /* it must be a dynamic library */
  221. name = strndup(p + 1, out);
  222. guess = SHARED;
  223. if (arch_fn->arch_import_shared)
  224. arch_fn->arch_import_shared(name);
  225. p += out + 2;
  226. /* we will not free the name because interp_libs_dump uses it */
  227. return 0;
  228. case WORDTYPE_NAME:
  229. /* functions or variables */
  230. name = strndup(p, out);
  231. /* join to the name list */
  232. n = malloc(sizeof(struct klist_node));
  233. klist_init(n);
  234. n->payload = name;
  235. guess = NAMES;
  236. p += out;
  237. break;
  238. default:
  239. exit(generic_error("invalid type for '<'", psave, lno));
  240. }
  241. _skip_spaces(p);
  242. /* try to getword */
  243. getword(p, &type, &out);
  244. if (type == WORDTYPE_KWORD && out == '@')
  245. goto skip;
  246. /* can just be a function define */
  247. guess = FUNCTION;
  248. if (type == WORDTYPE_NR) {
  249. /* only number of arguments */
  250. if (!out)
  251. /* special case */
  252. n->value = DECLARE_NOARG;
  253. else
  254. n->value = out;
  255. _skip_word(p);
  256. _skip_spaces(p);
  257. goto skip;
  258. }
  259. /* argument list */
  260. if (type == WORDTYPE_KWORD && out == '(') {
  261. n->value = 0;
  262. p++;
  263. _skip_spaces(p);
  264. for (;;) {
  265. if (unlikely(!getword(p, &type, &out)))
  266. /* end but no ')' */
  267. exit(generic_error("argument list terminated with no ')'", psave, lno));
  268. switch (type) {
  269. case WORDTYPE_KWORD:
  270. if (unlikely(out != ')' && out != ','))
  271. exit(generic_error("invalid operators between arguments", psave, lno));
  272. if (out == ')') {
  273. if (!n->value)
  274. n->value = DECLARE_NOARG;
  275. p++;
  276. _skip_spaces(p);
  277. goto skip;
  278. }
  279. p++;
  280. _skip_spaces(p);
  281. break;
  282. case WORDTYPE_NAME:
  283. n->value++;
  284. p += out;
  285. _skip_spaces(p);
  286. break;
  287. default:
  288. exit(generic_error("invalid type between arguments", psave, lno));
  289. }
  290. }
  291. }
  292. if (!n->value)
  293. n->value = DECLARE_NOARG;
  294. skip: attr = getattrmask(p, declare_attrtab);
  295. if (unlikely(!attr && guess != SHARED))
  296. exit(generic_error("you must give attributes for declares", psave, lno));
  297. if (attr & 2) {
  298. n->value |= DECLARE_FN;
  299. n->xmask = XMASK_FN;
  300. }
  301. else if (attr & 4) {
  302. if (unlikely(guess == FUNCTION))
  303. exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
  304. psave, lno));
  305. /* variable */
  306. n->value = DECLARE_VAR;
  307. }
  308. else
  309. exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
  310. if (unlikely((t = find_name(n->payload))) && !(t->xmask & XMASK_WEAK)) {
  311. error((char *) n->payload);
  312. exit(generic_error("name already defined", psave, lno));
  313. }
  314. if (t && t->xmask == XMASK_WEAK)
  315. if (((t->value & DECLARE_FN) && !(n->value & DECLARE_FN)) ||
  316. ((n->value & DECLARE_FN) && !(t->value & DECLARE_FN))) {
  317. error((char *) n->payload);
  318. exit(generic_error("name defined in different type", psave, lno));
  319. }
  320. n->xmask |= XMASK_WEAK;
  321. klist_add(&names_list, n);
  322. return 0;
  323. }
  324. static int iini_run(char *interp_path, char *code) {
  325. int pid, stat;
  326. /* we use the command-line '-c' */
  327. printf("\nRun %s as interpreter-in-interpreter: \n", interp_path);
  328. if (!(pid = fork())) {
  329. /* child */
  330. execl(interp_path, interp_path, "-c", code, NULL);
  331. error("execl: %s\n", strerror(errno));
  332. exit(generic_error("interp-in-interp failed", psave, lno));
  333. }
  334. waitpid(pid, &stat, 0);
  335. printf("\n");
  336. if (WIFEXITED(stat))
  337. printf("%s exited normally with status %d\n", interp_path, WEXITSTATUS(stat));
  338. else if (WIFSIGNALED(stat)) {
  339. error("%s terminated by signal %d%s\n", interp_path, WTERMSIG(stat),
  340. #ifdef WCOREDUMP
  341. WCOREDUMP(stat) ? " (core dumped)" : ""
  342. #else
  343. ""
  344. #endif
  345. );
  346. exit(generic_error("interp-in-interp failed", psave, lno));
  347. }
  348. return WEXITSTATUS(stat);
  349. }
  350. static int interp_in_interp(char *p, struct arch *arch_fn, long *res) {
  351. int type, out;
  352. char *interp, *code, *end;
  353. /* first get the interpreter name */
  354. p++;
  355. _skip_spaces(p);
  356. if (!getword(p, &type, &out))
  357. exit(generic_error("interpreter name required", psave, lno));
  358. interp = strndup(p + 1, out);
  359. p += out + 2;
  360. _skip_spaces(p);
  361. if (!getword(p, &type, &out) || out != '{')
  362. exit(generic_error("start mark '{' required", psave, lno));
  363. /* get the end mark '}' */
  364. end = read_code_block(p);
  365. _skip_spaces(end);
  366. /* ; -> next_line */
  367. end++;
  368. p++;
  369. code = strndup(p, end - p - 1);
  370. /* now let's do something interesting */
  371. *res = iini_run(interp, code);
  372. free(code);
  373. free(interp);
  374. _skip_spaces(end);
  375. /* this, parse_line will not bark. */
  376. line_buffer = end;
  377. return 1;
  378. }