/* 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 */ #include #include #include #include #include #include #include #include #include char *linebuffer_save = NULL; char *line_buffer = NULL; int lno = 0; char *eof; char *line_eof; static int next_line(char *), parse_line(void); struct arch *arch_fn; extern struct arch interp_arch_fn, x86_64_arch_fn; struct context *current; struct context main_context = { .funcname = "main", .link = NULL, .private_data = NULL }; const static struct { char *name; struct arch *arch; } arch_list[] = { { "interp", &interp_arch_fn}, { "x86_64", &x86_64_arch_fn}, { NULL, NULL } }; const struct attr_table vardef_attrtab[] = { { "global", 1 }, { "rdonly", 4 }, { "ptr", 8 }, { NULL, 0 } }; const struct attr_table declare_attrtab[] = { { "shared", 1 }, { "fn", 2 }, { "var", 4 }, { "dynalib", 1 }, { "function", 2 }, { "variable", 4 }, { NULL, 0 } }; const struct attr_table dump_attrtab[] = { { "stack", 1 }, { "var", 2 }, { "fn", 4 }, { "names", 8 }, { "libs", 16 }, { "all", 32 }, { "variables", 2 }, { "functions", 4 }, { "libraries", 16 }, { NULL, 0 } }; int do_tripl_compile(char *fbuffer, off_t fsize) { char *archname = getenv("TRIPL_ARCH"); int i; current = &main_context; if (!archname) arch_fn = &interp_arch_fn; else { for (i = 0; arch_list[i].name; i++) if (!strcmp(arch_list[i].name, archname)) { arch_fn = arch_list[i].arch; goto ok; } fprintf(stderr, "*** unsupported arch %s (got from $TRIPL_ARCH). \n", archname); return 1; } ok: klist_init(&stack); eof = fbuffer + fsize; arch_fn->arch_init(); while (next_line(fbuffer)) parse_line(); arch_fn->arch_fin ? arch_fn->arch_fin() : 0; return 0; } /* skip the comment, from '# asdf\nasdf2' to 'asdf2' */ static char *__skip_comment(char *fbuffer) { if (likely(*fbuffer != '#')) return fbuffer; while (*fbuffer != '\n') fbuffer++; _skip_spaces(fbuffer); lno++; if (*fbuffer == '#') return __skip_comment(fbuffer); return fbuffer; } /* skip to the ';' */ static char *to_eol(char *p, char *eof) { int in = 0; while (p < eof) { if (*p == '\"') { if (in) in = 0; else in = 1; } if (!in && *p == ';') return p; p++; } return NULL; } static int next_line(char *fbuffer) { ++lno; /* first time we call this, initial */ if (unlikely(!line_buffer)) { line_eof = line_buffer = fbuffer = __skip_comment(fbuffer); if (unlikely(fbuffer >= eof)) /* only comment in this file */ return 0; line_eof = to_eol(line_eof, eof); linebuffer_save = line_buffer; return 1; } /* move line_buffer to ';' + 1 and line_eof to line_buffer -> ';' */ line_buffer = fbuffer = __skip_comment(line_buffer); if (unlikely(fbuffer >= eof)) return 0; /* skip current line */ line_buffer = to_eol(line_buffer, eof); line_buffer++; _skip_spaces(line_buffer) line_buffer = __skip_comment(line_buffer); line_eof = line_buffer; line_eof = to_eol(line_eof, eof); linebuffer_save = line_buffer; if (unlikely(line_buffer >= eof)) return 0; return 1; } static int getnumber(char *p) { int dec = 0, log = 1, neg = 0; if (!isnr(*p)) { if (*p == '+') neg = 0; else if (*p == '-') neg = 1; else exit(generic_error("a dec number required, but: ", linebuffer_save, lno)); p++; } if (unlikely(*p == '0' && isnr(p[1]))) exit(generic_error("number starting with '0'", linebuffer_save, lno)); /* never use eof because this is calling from getword() */ while (isnr(*p)) p++; --p; while (p >= line_buffer) { dec += itoa(*p) * log; log *= 10; p--; if (!isnr(*p)) /* for - and + */ break; } return neg ? -dec : dec; } static inline int getwordtype(char c) { if (isnr(c)) return WORDTYPE_NR; else if (ischr(c)) return WORDTYPE_NAME; else if (isspace(c)) return 0; return WORDTYPE_KWORD; } /* NEVER MODIFY THIS! MAGIC! * THIS IS REALLY THE UGLY CODE, MOST UGLY CODE ALL OVER THE WORLD. * * BUT IT WORKS NOW, SO WE DO NOT TO CHANGE THIS AND WE WILL CHANGE * A LOT BECAUSE ALL OF THE TRIPL REQUIRES THIS FUNCTION. * * f**k up. i will fix this up. --- 2021.1.29 rain * */ #define isfirstchr() !len #define notfirstchr() len char *getword(char *p, int *type, int *out) { int last_word_type, curr_word_type; int len, in = 0; char *back = p; last_word_type = 0; if (p >= line_eof) return NULL; for (len = 0; p < line_eof; p++, len++) { if (*p == '\"') { if (in) { in = 0; *type = WORDTYPE_STR; *out = len - 1; return p; } if (!in) in = 1; } if (!in) { curr_word_type = getwordtype(*p); if (!curr_word_type) break; if (isfirstchr() && curr_word_type == WORDTYPE_KWORD) { *type = WORDTYPE_KWORD; *out = *p; return p; } if (notfirstchr() && curr_word_type != last_word_type) { if (curr_word_type == WORDTYPE_NR && !isnr(p[-1])) continue; break; } last_word_type = curr_word_type; } } *type = last_word_type; switch (last_word_type) { case WORDTYPE_NR: *out = getnumber(back); break; case WORDTYPE_NAME: *out = len; break; } return back; } static inline int find_attr(char *attrname, const struct attr_table *tab) { int i; for (i = 0; tab[i].attrname; i++) if (!strcmp(tab[i].attrname, attrname)) return tab[i].attrmask; return 0; } int getattrmask(char *p, const struct attr_table *tab) { char *attrname; int type, out, mask = 0, t; if (!getword(p, &type, &out)) return 0; if (unlikely(type != WORDTYPE_KWORD)) exit(generic_error("non-attribute ('@') character after that", linebuffer_save, lno)); if (unlikely(out != '@')) exit(generic_error("not a '@' after string", linebuffer_save, lno)); ++p; _skip_spaces(p); while (getword(p, &type, &out)) { if (unlikely(type != WORDTYPE_NAME)) exit(generic_error("an attribute name required", linebuffer_save, lno)); attrname = strndup(p, out); if (unlikely(!(t = find_attr(attrname, tab)))) exit(generic_error("unknow attribute", linebuffer_save, lno)); mask |= t; free(attrname); _skip_word(p); } return mask; } static int parse_line(void) { char *p; int type, out; if ((p = getword(line_buffer, &type, &out)) >= line_eof) /* empty line */ return 0; return arch_do_keyword(p, arch_fn); }