arch_templ.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <sys/types.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <klist.h>
  24. #include <tripl/tripl.h>
  25. #include <tripl/limits.h>
  26. #include <tripl/arch.h>
  27. char *psave;
  28. struct klist_node names_list = KLIST_NODE_INIT(&names_list);
  29. extern char *line_eof;
  30. __arch_templ static int new_var(char *p, struct arch *);
  31. __arch_templ static int set_name(char *p, struct arch *);
  32. __arch_templ static int math_set_name(char *p, struct arch *);
  33. __arch_templ static int declare_object(char *p, struct arch *);
  34. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  35. psave = p;
  36. if (p == line_eof)
  37. return 0;
  38. switch (*p) {
  39. case '$':
  40. /* '$ asdf = 0;' --- asdf = 0
  41. * '$ asdf 0;' --- asdf = 0
  42. * '$ asdf;' --- asdf = 0
  43. * */
  44. p++;
  45. _skip_spaces(p);
  46. new_var(p, arch_fn);
  47. break;
  48. case '!':
  49. if (!arch_fn->arch_stack_dump)
  50. exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno));
  51. arch_fn->arch_stack_dump();
  52. break;
  53. case '<':
  54. declare_object(p, arch_fn);
  55. break;
  56. default:
  57. if (set_name(p, arch_fn) + math_set_name(p, arch_fn) == 0)
  58. exit(generic_error("unknow keyword", psave, lno));
  59. break;
  60. }
  61. return 0;
  62. }
  63. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  64. char *name;
  65. int type, out, attr;
  66. getword(p, &type, &out);
  67. if (type != WORDTYPE_NAME)
  68. exit(generic_error("a name for variable required", psave, lno));
  69. name = strndup(p, out);
  70. p += out;
  71. _skip_spaces(p);
  72. getword(p, &type, &out);
  73. switch (type) {
  74. case WORDTYPE_KWORD:
  75. if (out != '=')
  76. exit(generic_error("keyword but it's not a '='", psave, lno));
  77. ++p;
  78. _skip_spaces(p);
  79. getword(p, &type, &out);
  80. if (type != WORDTYPE_NR)
  81. exit(generic_error("a number required", psave, lno));
  82. _skip_word(p);
  83. break;
  84. case WORDTYPE_NR:
  85. /* to attribute (if have) */
  86. _skip_word(p);
  87. break;
  88. case 0:
  89. out = 0;
  90. break;
  91. default:
  92. exit(generic_error("bad word after variable name", psave, lno));
  93. }
  94. attr = getattrmask(p, vardef_attrtab);
  95. arch_fn->arch_new_var(name, out, attr);
  96. /* the name can't be free */
  97. return 0;
  98. }
  99. static int set_name(char *p, struct arch *arch_fn) {
  100. char *name;
  101. int type, out;
  102. getword(p, &type, &out);
  103. if (type != WORDTYPE_NAME)
  104. return 0;
  105. name = strndup(p, out);
  106. p += out;
  107. _skip_spaces(p);
  108. getword(p, &type, &out);
  109. if (type != WORDTYPE_KWORD || out != '=')
  110. return 0;
  111. p++;
  112. _skip_spaces(p);
  113. getword(p, &type, &out);
  114. if (type != WORDTYPE_NR)
  115. exit(generic_error("a number required", psave, lno));
  116. arch_fn->arch_set_var(name, out, 0);
  117. free(name);
  118. return 1;
  119. }
  120. /* 9 -> a; */
  121. static int math_set_name(char *p, struct arch *arch_fn) {
  122. char *name;
  123. int type, out, nr;
  124. getword(p, &type, &out);
  125. if (type != WORDTYPE_NR)
  126. return 0;
  127. nr = out;
  128. /* this requires a space */
  129. _skip_word(p);
  130. getword(p, &type, &out);
  131. if (type != WORDTYPE_KWORD || out != '-')
  132. exit(generic_error("a '->' required", psave, lno));
  133. p++;
  134. getword(p, &type, &out);
  135. if (type != WORDTYPE_KWORD || out != '>')
  136. exit(generic_error("a '->' required", psave, lno));
  137. p++;
  138. _skip_spaces(p);
  139. getword(p, &type, &out);
  140. if (type != WORDTYPE_NAME)
  141. exit(generic_error("a name required", psave, lno));
  142. name = strndup(p, out);
  143. arch_fn->arch_set_var(name, nr, 0);
  144. free(name);
  145. return 1;
  146. }
  147. /*
  148. * 1.
  149. * < "libfoo.a" @ dynalib;
  150. * 2.
  151. * < fn @ fn;
  152. *
  153. * 3.
  154. * < fn 2 @ fn;
  155. *
  156. * 4.
  157. * < fn (a, b) @ fn;
  158. *
  159. * 5.
  160. * < age @ var;
  161. * */
  162. static int declare_object(char *p, struct arch *arch_fn) {
  163. char *name;
  164. int type, out, attr;
  165. enum { SHARED, NAMES, FUNCTION } guess;
  166. struct klist_node *n;
  167. /* skip '<' */
  168. p++;
  169. _skip_spaces(p);
  170. if (!getword(p, &type, &out))
  171. exit(generic_error("a module name or object name required", psave, lno));
  172. switch (type) {
  173. case WORDTYPE_STR:
  174. /* it must be a dynamic library */
  175. name = strndup(p + 1, out);
  176. guess = SHARED;
  177. if (arch_fn->arch_import_shared)
  178. arch_fn->arch_import_shared(name);
  179. p += out + 2;
  180. free(name);
  181. break;
  182. case WORDTYPE_NAME:
  183. /* functions or variables */
  184. name = strndup(p, out);
  185. /* join to the name list */
  186. n = malloc(sizeof(struct klist_node));
  187. klist_init(n);
  188. n->payload = name;
  189. klist_add(&names_list, n);
  190. guess = NAMES;
  191. p += out;
  192. break;
  193. default:
  194. exit(generic_error("invalid type for '<'", psave, lno));
  195. }
  196. _skip_spaces(p);
  197. if (guess == SHARED)
  198. goto skip;
  199. /* try to getword */
  200. getword(p, &type, &out);
  201. if (type == WORDTYPE_KWORD && out == '@')
  202. goto skip;
  203. /* can just be a function define */
  204. guess = FUNCTION;
  205. if (type == WORDTYPE_NR) {
  206. /* only number of arguments */
  207. if (!out)
  208. /* special case */
  209. n->value = DECLARE_NOARG;
  210. else
  211. n->value = out;
  212. _skip_word(p);
  213. _skip_spaces(p);
  214. goto skip;
  215. }
  216. /* argument list */
  217. if (type == WORDTYPE_KWORD && out == '(') {
  218. n->value = 0;
  219. p++;
  220. _skip_spaces(p);
  221. for (;;) {
  222. if (!getword(p, &type, &out))
  223. /* end but no ')' */
  224. exit(generic_error("argument list terminated with no ')'", psave, lno));
  225. switch (type) {
  226. case WORDTYPE_KWORD:
  227. if (out != ')' && out != ',')
  228. exit(generic_error("invalid operators between arguments", psave, lno));
  229. if (out == ')') {
  230. if (!n->value)
  231. n->value = DECLARE_NOARG;
  232. p++;
  233. _skip_spaces(p);
  234. goto skip;
  235. }
  236. p++;
  237. _skip_spaces(p);
  238. break;
  239. case WORDTYPE_NAME:
  240. n->value++;
  241. p += out;
  242. _skip_spaces(p);
  243. break;
  244. default:
  245. exit(generic_error("invalid type between arguments", psave, lno));
  246. }
  247. }
  248. }
  249. if (!n->value)
  250. n->value = DECLARE_NOARG;
  251. skip: attr = getattrmask(p, declare_attrtab);
  252. if (!attr && guess != SHARED)
  253. exit(generic_error("you must give attributes for declares", psave, lno));
  254. if (guess != SHARED) {
  255. if (attr & 2)
  256. n->value |= DECLARE_FN;
  257. else if (attr & 4) {
  258. if (guess == FUNCTION)
  259. exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
  260. psave, lno));
  261. /* variable */
  262. n->value = DECLARE_VAR;
  263. }
  264. else
  265. exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
  266. }
  267. return 0;
  268. }