tripl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. struct klist_node stack;
  33. extern int interp_init(void);
  34. static int next_line(char *),
  35. parse_line(void);
  36. int (* do_keyword) (char *, char);
  37. const static struct {
  38. char *name;
  39. int (* do_keyword) (char *, char);
  40. } arch_list[] = {
  41. { "interp", interp_do_keyword },
  42. { NULL, NULL }
  43. };
  44. int do_tripl_compile(char *fbuffer, off_t fsize) {
  45. char *arch = getenv("TRIPL_ARCH");
  46. int i;
  47. if (!arch)
  48. do_keyword = interp_do_keyword;
  49. else {
  50. for (i = 0; arch_list[i].name; i++)
  51. if (!strcmp(arch_list[i].name, arch)) {
  52. do_keyword = arch_list[i].do_keyword;
  53. goto ok;
  54. }
  55. printf("*** unsupported arch %s (got from $TRIPL_ARCH). \n", arch);
  56. exit(1);
  57. }
  58. ok: if (do_keyword == interp_do_keyword)
  59. interp_init();
  60. klist_init(&stack);
  61. eof = fbuffer + fsize;
  62. while (next_line(fbuffer))
  63. if (parse_line())
  64. /* don't flush streams */
  65. _exit(1);
  66. return 0;
  67. }
  68. /* move line pointer to next line */
  69. static int next_line(char *fbuffer) {
  70. char *p;
  71. ++lno;
  72. if (!line_buffer) {
  73. p = line_eof = line_buffer = fbuffer;
  74. while (*p != ';')
  75. line_eof++, p++;
  76. linebuffer_save = line_buffer;
  77. return 1;
  78. }
  79. for (; (*line_buffer != ';') && (line_buffer < eof); ++line_buffer);
  80. if (line_buffer >= eof)
  81. return 0;
  82. p = ++line_buffer;
  83. while (*p != ';' && p < eof)
  84. p++, line_eof++;
  85. if (p == eof)
  86. /* eof */
  87. exit(1);
  88. linebuffer_save = line_buffer;
  89. return line_eof++, 1;
  90. }
  91. static int getnumber(char *p) {
  92. int dec = 0, log = 1, neg = 0;
  93. if (!isnr(*p)) {
  94. if (*p == '+')
  95. neg = 0;
  96. else if (*p == '-')
  97. neg = 1;
  98. else
  99. exit(generic_error("a dec number required, but: ", linebuffer_save, lno));
  100. p++;
  101. }
  102. if (*p == '0' && isnr(p[1]))
  103. exit(generic_error("number starting with '0'", linebuffer_save, lno));
  104. /* never use eof because this is calling from getword() */
  105. while (isnr(*p)) p++;
  106. --p;
  107. while (p >= line_buffer) {
  108. dec += itoa(*p) * log;
  109. log *= 10;
  110. p--;
  111. if (!isnr(*p))
  112. /* for - and + */
  113. break;
  114. }
  115. return neg ? -dec : dec;
  116. }
  117. #define isfirstchr() (!i)
  118. #define notfirstchr() i
  119. char *getword(char *fbuffer, int *type, int *out) {
  120. *type = WORDTYPE_INVAL;
  121. char *p;
  122. int i, in = 0;
  123. _skip_spaces(fbuffer);
  124. for (i = 0, p = fbuffer; p < line_eof; i++, p++) {
  125. if (*p == '\"') {
  126. if (in) {
  127. /* end */
  128. *out = i;
  129. return fbuffer;
  130. }
  131. in = 1;
  132. *type = WORDTYPE_STR;
  133. }
  134. if (!in) {
  135. if (isnr(*p) || *p == '-' || *p == '+') {
  136. if (notfirstchr())
  137. /* name, but not a number */
  138. continue;
  139. /* get the number, and save to *out */
  140. *out = getnumber(p);
  141. /* line finished. */
  142. *type = WORDTYPE_NR;
  143. return p;
  144. }
  145. /* eg. "# " */
  146. if (i == 1 && isspace(*p)) {
  147. if (ischr(p[-1])) {
  148. /* name */
  149. *type = WORDTYPE_NAME;
  150. *out = 1;
  151. return p;
  152. }
  153. /* keyword */
  154. *type = WORDTYPE_KWORD;
  155. *out = *--p;
  156. return p;
  157. }
  158. /* it can only be a name */
  159. if (isspace(*p)) {
  160. /* end */
  161. *type = WORDTYPE_NAME;
  162. *out = i;
  163. return fbuffer;
  164. }
  165. }
  166. }
  167. /* there isn't a space at end of the line */
  168. if (in)
  169. exit(generic_error("no '\"' found", linebuffer_save, lno));
  170. if (i == 1) {
  171. if (ischr(p[-1])) {
  172. *type = WORDTYPE_NAME;
  173. *out = 1;
  174. return p;
  175. }
  176. *type = WORDTYPE_KWORD;
  177. *out = *--p;
  178. return p;
  179. }
  180. if (!i)
  181. return NULL;
  182. /* only can be a name */
  183. *type = WORDTYPE_NAME;
  184. *out = i;
  185. return fbuffer;
  186. }
  187. static int parse_line(void) {
  188. char *p;
  189. int type, out;
  190. if ((p = getword(line_buffer, &type, &out)) >= line_eof)
  191. /* empty line */
  192. return 0;
  193. if (type != WORDTYPE_KWORD)
  194. exit(generic_error("line starts with a non-keyword", linebuffer_save, lno));
  195. return do_keyword(p, *p);
  196. }