| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /* tripl/tripl.c
- *
- * This file is part of tripl.
- *
- * tripl is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * tripl is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with tripl. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <klist.h>
- #include <tripl/tripl.h>
- #include <tripl/limits.h>
- static char *linebuffer_save = NULL;
- static char *line_buffer = NULL;
- static int lno = 0;
- static char *eof;
- static char *line_eof;
- struct klist_node stack;
- static int next_line(char *),
- parse_line(void),
- getnumber(void);
- int do_tripl_compile(char *fbuffer, off_t fsize) {
- klist_init(&stack);
- eof = fbuffer + fsize;
- while (next_line(fbuffer))
- if (parse_line())
- /* don't flush streams */
- _exit(1);
- return 0;
- }
- /* move line pointer to next line */
- static int next_line(char *fbuffer) {
- char *p;
- ++lno;
- if (!line_buffer) {
- p = line_eof = line_buffer = fbuffer;
- while (*p != '\n')
- line_eof++, p++;
- linebuffer_save = line_buffer;
- return 1;
- }
- for (; (*line_buffer != '\n') && (line_buffer < eof); ++line_buffer);
- if (line_buffer >= eof)
- return 0;
- p = ++line_buffer;
- while (*p != '\n')
- p++, line_eof++;
- linebuffer_save = line_buffer;
- return line_eof++, 1;
- }
- static int parse_line(void) {
- struct klist_node *node;
- int a, strstat, strallocated, strindex, stacklvl;
- char *p, *string, *putbuf;
- skip_spaces();
- if ((p = line_buffer) >= line_eof)
- /* empty line */
- return 0;
- switch (*p) {
- case '$':
- line_buffer++;
- push_nr(getnumber());
- break;
- case '@':
- /* function: not support */
- return generic_error("tripl doesn't support functions now. ",
- linebuffer_save, lno);
- case '#':
- return 0;
- case '+':
- if (klist_empty(&stack))
- return generic_error("stack underflow. ",
- linebuffer_save, lno);
- a = _pop_nr();
- if (klist_empty(&stack))
- return generic_error("stack underflow. ",
- linebuffer_save, lno);
- push_nr(a + _pop_nr());
- break;
- case '!':
- /* show stack */
- ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
- printf("stack dump: \n");
- ansi_csi_ed(stdout);
- fflush(stdout);
- for (node = stack.next; node != &stack; node = node->next)
- printf("\t%09d\t0x%016x\n", node->value, node->value);
- if (klist_empty(&stack))
- printf("\t(nothing)\n");
- break;
- case '%':
- return generic_error("tripl doesn't support variables now. ",
- linebuffer_save, lno);
- case '~':
- /* \ */
- strstat = 0;
- strindex = 0;
- strallocated = 1;
- putbuf = malloc(1);
- line_buffer++;
- /* output */
- skip_spaces();
- string = line_buffer;
- while (*string != '\n') {
- if (strstat) {
- if (*string == 'n')
- putbuf[strindex++] = '\n';
- else if (*string == 't')
- putbuf[strindex++] = '\t';
- else
- return generic_error("unsupported character: "
- "'\\t' or '\\n' are supported. ",
- linebuffer_save, lno);
- strstat = 0;
- }
- else if (*string == '\\')
- strstat = 1;
- else
- putbuf[strindex++] = *string;
- string++;
- putbuf = realloc(putbuf, ++strallocated);
- }
- putbuf[strindex] = 0;
- printf("%s", putbuf);
- break;
- case ':':
- /* output number */
- line_buffer++;
- skip_spaces();
- stacklvl = getnumber() - 1;
- if (stacklvl < 0)
- return generic_error("invalid stack index. ",
- linebuffer_save, lno);
- for (strindex = 0, node = stack.next; node != &stack && strindex < stacklvl;
- node = node->next)
- ;
- if (node == &stack)
- return generic_error("stack overflow. ",
- linebuffer_save, lno);
- printf("%d", node->value);
- break;
- default:
- return generic_error("the first character must be one of $@#+%!~:. ",
- linebuffer_save, lno);
- }
- return 0;
- }
- static int getnumber(void) {
- char *p;
- int dec = 0, log = 1, neg = 0;
- skip_spaces();
- if (!isnr(*line_buffer) && *line_buffer != '-')
- exit(generic_error("'$' requires a dec number, but: ", linebuffer_save, lno));
- if (*line_buffer == '-') {
- neg = 1;
- line_buffer++;
- }
- if (*line_buffer == '0' && isnr(line_buffer[1]))
- exit(generic_error("invalid dec number: starting with zero. ", linebuffer_save, lno));
- p = line_eof;
- while (!isnr(*p)) {
- if (!isspace(*p))
- exit(generic_error("invalid dec number: character at end of string. ",
- linebuffer_save, lno));
- p--;
- }
- while (p >= line_buffer) {
- dec += itoa(*p) * log;
- log *= 10;
- p--;
- }
- return neg ? -dec : dec;
- }
|