arch_templ.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. __arch_templ static int new_var(char *p, struct arch *);
  29. __arch_templ static int set_name(char *p, struct arch *);
  30. __arch_templ static int math_set_name(char *p, struct arch *);
  31. __arch_templ static int check_condition(char *p, struct arch *);
  32. __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
  33. psave = p;
  34. switch (*p) {
  35. case '$':
  36. /* '$ asdf = 0;' --- asdf = 0
  37. * '$ asdf 0;' --- asdf = 0
  38. * '$ asdf;' --- asdf = 0
  39. * */
  40. p++;
  41. _skip_spaces(p);
  42. new_var(p, arch_fn);
  43. break;
  44. case '!':
  45. if (!arch_fn->arch_stack_dump)
  46. exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno));
  47. arch_fn->arch_stack_dump();
  48. break;
  49. default:
  50. if (set_name(p, arch_fn) + math_set_name(p, arch_fn) + check_condition(p, arch_fn) == 0)
  51. exit(generic_error("unknow keyword", psave, lno));
  52. break;
  53. }
  54. return 0;
  55. }
  56. __arch_templ static int new_var(char *p, struct arch *arch_fn) {
  57. char *name;
  58. int type, out, attr;
  59. getword(p, &type, &out);
  60. if (type != WORDTYPE_NAME)
  61. exit(generic_error("a name for variable required", psave, lno));
  62. name = strndup(p, out);
  63. p += out;
  64. _skip_spaces(p);
  65. getword(p, &type, &out);
  66. switch (type) {
  67. case WORDTYPE_KWORD:
  68. if (out != '=')
  69. exit(generic_error("keyword but it's not a '='", psave, lno));
  70. ++p;
  71. _skip_spaces(p);
  72. getword(p, &type, &out);
  73. if (type != WORDTYPE_NR)
  74. exit(generic_error("a number required", psave, lno));
  75. _skip_word(p);
  76. break;
  77. case WORDTYPE_NR:
  78. /* to attribute (if have) */
  79. _skip_word(p);
  80. break;
  81. case 0:
  82. out = 0;
  83. break;
  84. default:
  85. exit(generic_error("bad word after variable name", psave, lno));
  86. }
  87. attr = getattrmask(p, vardef_attrtab);
  88. arch_fn->arch_new_var(name, out, attr);
  89. /* the name can't be free */
  90. return 0;
  91. }
  92. static int set_name(char *p, struct arch *arch_fn) {
  93. char *name;
  94. int type, out;
  95. getword(p, &type, &out);
  96. if (type != WORDTYPE_NAME)
  97. return 0;
  98. name = strndup(p, out);
  99. p += out;
  100. _skip_spaces(p);
  101. getword(p, &type, &out);
  102. if (type != WORDTYPE_KWORD || out != '=')
  103. return 0;
  104. p++;
  105. _skip_spaces(p);
  106. getword(p, &type, &out);
  107. if (type != WORDTYPE_NR)
  108. exit(generic_error("a number required", psave, lno));
  109. arch_fn->arch_set_var(name, out, 0);
  110. free(name);
  111. return 1;
  112. }
  113. /* 9 -> a; */
  114. static int math_set_name(char *p, struct arch *arch_fn) {
  115. char *name;
  116. int type, out, nr;
  117. getword(p, &type, &out);
  118. if (type != WORDTYPE_NR)
  119. return 0;
  120. nr = out;
  121. /* this requires a space */
  122. _skip_word(p);
  123. getword(p, &type, &out);
  124. if (type != WORDTYPE_KWORD || out != '-')
  125. exit(generic_error("a '->' required", psave, lno));
  126. p++;
  127. getword(p, &type, &out);
  128. if (type != WORDTYPE_KWORD || out != '>')
  129. exit(generic_error("a '->' required", psave, lno));
  130. p++;
  131. _skip_spaces(p);
  132. getword(p, &type, &out);
  133. if (type != WORDTYPE_NAME)
  134. exit(generic_error("a name required", psave, lno));
  135. name = strndup(p, out);
  136. arch_fn->arch_set_var(name, nr, 0);
  137. free(name);
  138. return 1;
  139. }
  140. /* # a - 10
  141. * a : 10 {
  142. * # = 1
  143. * 1: xxx;
  144. * # = 2
  145. * 2: yyy;
  146. * # = 4
  147. * 4: zzz;
  148. * } */
  149. static int check_condition(char *p, struct arch *arch_fn) {
  150. struct condition cond;
  151. int type, out;
  152. getword(p, &type, &out);
  153. if (type == WORDTYPE_NR) {
  154. cond.nra = out;
  155. _skip_word(p);
  156. }
  157. else if (type == WORDTYPE_NAME) {
  158. cond.namea = strndup(p, out);
  159. p += out;
  160. }
  161. else
  162. return 0;
  163. _skip_spaces(p);
  164. getword(p, &type, &out);
  165. getword(p, &type, &out);
  166. if (type != WORDTYPE_KWORD || out != ':')
  167. return 0;
  168. p++;
  169. _skip_spaces(p);
  170. getword(p, &type, &out);
  171. if (type == WORDTYPE_NR) {
  172. cond.nrb = out;
  173. _skip_word(p);
  174. }
  175. else if (type == WORDTYPE_NAME) {
  176. cond.nameb = strndup(p, out);
  177. p += out;
  178. }
  179. else
  180. return 0;
  181. printf("condition: %d %d %s %s\n",
  182. cond.nra, cond.nrb,
  183. cond.namea, cond.nameb);
  184. return 1;
  185. }