| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- /* 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 <string.h>
- #include <klist.h>
- #include <tripl/tripl.h>
- #include <tripl/limits.h>
- #include <tripl/arch.h>
- 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[] = {
- { "var", 1 },
- { "fn", 2 },
- { "names", 4 },
- { "libs", 8 },
- { "all", 16 },
- { "variables", 2 },
- { "functions", 2 },
- { "libraries", 8 },
- { 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: 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 */
- if (unlikely(!(line_buffer = to_eol(line_buffer, eof))))
- return 0;
- 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;
- *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 ((*p == '-' || *p == '+') && isnr(p[1])) {
- *type = WORDTYPE_NR;
- *out = getnumber(p);
- return p;
- }
- 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;
- }
- char *read_code_block(char *p) {
- int level = 0, in = 0;
- /* skip to next '{' (not just next, but end of this code-block) */
- for (; p < eof; p++) {
- /* strings and comments... */
- if (*p == '\"') {
- if (in)
- in = 0;
- else
- in = 1;
- }
- if (!in) {
- __skip_comment(p);
- if (*p == '{')
- level++;
- else if (*p == '}') {
- level--;
- if (!level)
- return p;
- }
- }
- }
- /* eof */
- return exit(generic_error("1 or more than 1 '}'s require", linebuffer_save, lno)), NULL;
- }
- 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);
- }
|