tripl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <klist.h>
  23. #include <tripl/tripl.h>
  24. #include <tripl/limits.h>
  25. static char *linebuffer_save = NULL;
  26. static char *line_buffer = NULL;
  27. static int lno = 0;
  28. static char *eof;
  29. static char *line_eof;
  30. struct klist_node stack;
  31. static int next_line(char *),
  32. parse_line(void),
  33. getnumber(void);
  34. int do_tripl_compile(char *fbuffer, off_t fsize) {
  35. klist_init(&stack);
  36. eof = fbuffer + fsize;
  37. while (next_line(fbuffer))
  38. if (parse_line())
  39. /* don't flush streams */
  40. _exit(1);
  41. return 0;
  42. }
  43. /* move line pointer to next line */
  44. static int next_line(char *fbuffer) {
  45. char *p;
  46. ++lno;
  47. if (!line_buffer) {
  48. p = line_eof = line_buffer = fbuffer;
  49. while (*p != '\n')
  50. line_eof++, p++;
  51. linebuffer_save = line_buffer;
  52. return 1;
  53. }
  54. for (; (*line_buffer != '\n') && (line_buffer < eof); ++line_buffer);
  55. if (line_buffer >= eof)
  56. return 0;
  57. p = ++line_buffer;
  58. while (*p != '\n')
  59. p++, line_eof++;
  60. linebuffer_save = line_buffer;
  61. return line_eof++, 1;
  62. }
  63. static int parse_line(void) {
  64. struct klist_node *node;
  65. int a, strstat, strallocated, strindex, stacklvl;
  66. char *p, *string, *putbuf;
  67. skip_spaces();
  68. if ((p = line_buffer) >= line_eof)
  69. /* empty line */
  70. return 0;
  71. switch (*p) {
  72. case '$':
  73. line_buffer++;
  74. push_nr(getnumber());
  75. break;
  76. case '@':
  77. /* function: not support */
  78. return generic_error("tripl doesn't support functions now. ",
  79. linebuffer_save, lno);
  80. case '#':
  81. return 0;
  82. case '+':
  83. if (klist_empty(&stack))
  84. return generic_error("stack underflow. ",
  85. linebuffer_save, lno);
  86. a = _pop_nr();
  87. if (klist_empty(&stack))
  88. return generic_error("stack underflow. ",
  89. linebuffer_save, lno);
  90. push_nr(a + _pop_nr());
  91. break;
  92. case '!':
  93. /* show stack */
  94. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  95. printf("stack dump: \n");
  96. ansi_csi_ed(stdout);
  97. fflush(stdout);
  98. for (node = stack.next; node != &stack; node = node->next)
  99. printf("\t%09d\t0x%016x\n", node->value, node->value);
  100. if (klist_empty(&stack))
  101. printf("\t(nothing)\n");
  102. break;
  103. case '%':
  104. return generic_error("tripl doesn't support variables now. ",
  105. linebuffer_save, lno);
  106. case '~':
  107. /* \ */
  108. strstat = 0;
  109. strindex = 0;
  110. strallocated = 1;
  111. putbuf = malloc(1);
  112. line_buffer++;
  113. /* output */
  114. skip_spaces();
  115. string = line_buffer;
  116. while (*string != '\n') {
  117. if (strstat) {
  118. if (*string == 'n')
  119. putbuf[strindex++] = '\n';
  120. else if (*string == 't')
  121. putbuf[strindex++] = '\t';
  122. else
  123. return generic_error("unsupported character: "
  124. "'\\t' or '\\n' are supported. ",
  125. linebuffer_save, lno);
  126. strstat = 0;
  127. }
  128. else if (*string == '\\')
  129. strstat = 1;
  130. else
  131. putbuf[strindex++] = *string;
  132. string++;
  133. putbuf = realloc(putbuf, ++strallocated);
  134. }
  135. putbuf[strindex] = 0;
  136. printf("%s", putbuf);
  137. break;
  138. case ':':
  139. /* output number */
  140. line_buffer++;
  141. skip_spaces();
  142. stacklvl = getnumber() - 1;
  143. if (stacklvl < 0)
  144. return generic_error("invalid stack index. ",
  145. linebuffer_save, lno);
  146. for (strindex = 0, node = stack.next; node != &stack && strindex < stacklvl;
  147. node = node->next)
  148. ;
  149. if (node == &stack)
  150. return generic_error("stack overflow. ",
  151. linebuffer_save, lno);
  152. printf("%d", node->value);
  153. break;
  154. default:
  155. return generic_error("the first character must be one of $@#+%!~:. ",
  156. linebuffer_save, lno);
  157. }
  158. return 0;
  159. }
  160. static int getnumber(void) {
  161. char *p;
  162. int dec = 0, log = 1, neg = 0;
  163. skip_spaces();
  164. if (!isnr(*line_buffer) && *line_buffer != '-')
  165. exit(generic_error("'$' requires a dec number, but: ", linebuffer_save, lno));
  166. if (*line_buffer == '-') {
  167. neg = 1;
  168. line_buffer++;
  169. }
  170. if (*line_buffer == '0' && isnr(line_buffer[1]))
  171. exit(generic_error("invalid dec number: starting with zero. ", linebuffer_save, lno));
  172. p = line_eof;
  173. while (!isnr(*p)) {
  174. if (!isspace(*p))
  175. exit(generic_error("invalid dec number: character at end of string. ",
  176. linebuffer_save, lno));
  177. p--;
  178. }
  179. while (p >= line_buffer) {
  180. dec += itoa(*p) * log;
  181. log *= 10;
  182. p--;
  183. }
  184. return neg ? -dec : dec;
  185. }