arch_templ.c 9.7 KB

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