| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- /* tripl/arch/arch_templ.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 <stdlib.h>
- #include <sys/types.h>
- #include <string.h>
- #include <errno.h>
- #include <klist.h>
- #include <tripl/tripl.h>
- #include <tripl/limits.h>
- #include <tripl/arch.h>
- char *psave;
- struct klist_node names_list = KLIST_NODE_INIT(&names_list);
- extern char *line_eof;
- __arch_templ static int new_var(char *p, struct arch *);
- __arch_templ static int set_name(char *p, struct arch *);
- __arch_templ static int math_set_name(char *p, struct arch *);
- __arch_templ static int declare_object(char *p, struct arch *);
- __arch_templ int arch_do_keyword(char *p, struct arch *arch_fn) {
- psave = p;
- if (p == line_eof)
- return 0;
- switch (*p) {
- case '$':
- /* '$ asdf = 0;' --- asdf = 0
- * '$ asdf 0;' --- asdf = 0
- * '$ asdf;' --- asdf = 0
- * */
- p++;
- _skip_spaces(p);
- new_var(p, arch_fn);
- break;
- case '!':
- if (!arch_fn->arch_stack_dump)
- exit(generic_error("unsupported 'stack-dump' in current mode", psave, lno));
- arch_fn->arch_stack_dump();
- break;
- case '<':
- declare_object(p, arch_fn);
- break;
- default:
- if (set_name(p, arch_fn) + math_set_name(p, arch_fn) == 0)
- exit(generic_error("unknow keyword", psave, lno));
- break;
- }
- return 0;
- }
- __arch_templ static int new_var(char *p, struct arch *arch_fn) {
- char *name;
- int type, out, attr;
-
- getword(p, &type, &out);
- if (type != WORDTYPE_NAME)
- exit(generic_error("a name for variable required", psave, lno));
- name = strndup(p, out);
- p += out;
- _skip_spaces(p);
- getword(p, &type, &out);
- switch (type) {
- case WORDTYPE_KWORD:
- if (out != '=')
- exit(generic_error("keyword but it's not a '='", psave, lno));
-
- ++p;
- _skip_spaces(p);
- getword(p, &type, &out);
- if (type != WORDTYPE_NR)
- exit(generic_error("a number required", psave, lno));
- _skip_word(p);
- break;
- case WORDTYPE_NR:
- /* to attribute (if have) */
- _skip_word(p);
- break;
- case 0:
- out = 0;
- break;
- default:
- exit(generic_error("bad word after variable name", psave, lno));
- }
- attr = getattrmask(p, vardef_attrtab);
- arch_fn->arch_new_var(name, out, attr);
- /* the name can't be free */
- return 0;
- }
- static int set_name(char *p, struct arch *arch_fn) {
- char *name;
- int type, out;
- getword(p, &type, &out);
- if (type != WORDTYPE_NAME)
- return 0;
- name = strndup(p, out);
- p += out;
- _skip_spaces(p);
- getword(p, &type, &out);
- if (type != WORDTYPE_KWORD || out != '=')
- return 0;
- p++;
- _skip_spaces(p);
- getword(p, &type, &out);
- if (type != WORDTYPE_NR)
- exit(generic_error("a number required", psave, lno));
- arch_fn->arch_set_var(name, out, 0);
- free(name);
- return 1;
- }
- /* 9 -> a; */
- static int math_set_name(char *p, struct arch *arch_fn) {
- char *name;
- int type, out, nr;
- getword(p, &type, &out);
- if (type != WORDTYPE_NR)
- return 0;
- nr = out;
- /* this requires a space */
- _skip_word(p);
- getword(p, &type, &out);
- if (type != WORDTYPE_KWORD || out != '-')
- exit(generic_error("a '->' required", psave, lno));
- p++;
- getword(p, &type, &out);
- if (type != WORDTYPE_KWORD || out != '>')
- exit(generic_error("a '->' required", psave, lno));
- p++;
- _skip_spaces(p);
- getword(p, &type, &out);
- if (type != WORDTYPE_NAME)
- exit(generic_error("a name required", psave, lno));
- name = strndup(p, out);
- arch_fn->arch_set_var(name, nr, 0);
- free(name);
- return 1;
- }
- /*
- * 1.
- * < "libfoo.a" @ dynalib;
- * 2.
- * < fn @ fn;
- *
- * 3.
- * < fn 2 @ fn;
- *
- * 4.
- * < fn (a, b) @ fn;
- *
- * 5.
- * < age @ var;
- * */
- static int declare_object(char *p, struct arch *arch_fn) {
- char *name;
- int type, out, attr;
- enum { SHARED, NAMES, FUNCTION } guess;
- struct klist_node *n;
- /* skip '<' */
- p++;
- _skip_spaces(p);
-
- if (!getword(p, &type, &out))
- exit(generic_error("a module name or object name required", psave, lno));
- switch (type) {
- case WORDTYPE_STR:
- /* it must be a dynamic library */
- name = strndup(p + 1, out);
- guess = SHARED;
- if (arch_fn->arch_import_shared)
- arch_fn->arch_import_shared(name);
- p += out + 2;
- free(name);
- break;
- case WORDTYPE_NAME:
- /* functions or variables */
- name = strndup(p, out);
- /* join to the name list */
-
- n = malloc(sizeof(struct klist_node));
- klist_init(n);
- n->payload = name;
- klist_add(&names_list, n);
- guess = NAMES;
- p += out;
- break;
- default:
- exit(generic_error("invalid type for '<'", psave, lno));
- }
- _skip_spaces(p);
- if (guess == SHARED)
- goto skip;
- /* try to getword */
- getword(p, &type, &out);
-
- if (type == WORDTYPE_KWORD && out == '@')
- goto skip;
- /* can just be a function define */
- guess = FUNCTION;
- if (type == WORDTYPE_NR) {
- /* only number of arguments */
- if (!out)
- /* special case */
- n->value = DECLARE_NOARG;
- else
- n->value = out;
- _skip_word(p);
- _skip_spaces(p);
- goto skip;
- }
- /* argument list */
- if (type == WORDTYPE_KWORD && out == '(') {
- n->value = 0;
- p++;
- _skip_spaces(p);
- for (;;) {
- if (!getword(p, &type, &out))
- /* end but no ')' */
- exit(generic_error("argument list terminated with no ')'", psave, lno));
- switch (type) {
- case WORDTYPE_KWORD:
- if (out != ')' && out != ',')
- exit(generic_error("invalid operators between arguments", psave, lno));
- if (out == ')') {
- if (!n->value)
- n->value = DECLARE_NOARG;
- p++;
- _skip_spaces(p);
- goto skip;
- }
- p++;
- _skip_spaces(p);
- break;
- case WORDTYPE_NAME:
- n->value++;
- p += out;
- _skip_spaces(p);
- break;
- default:
- exit(generic_error("invalid type between arguments", psave, lno));
- }
- }
- }
- if (!n->value)
- n->value = DECLARE_NOARG;
- skip: attr = getattrmask(p, declare_attrtab);
- if (!attr && guess != SHARED)
- exit(generic_error("you must give attributes for declares", psave, lno));
- if (guess != SHARED) {
- if (attr & 2)
- n->value |= DECLARE_FN;
- else if (attr & 4) {
- if (guess == FUNCTION)
- exit(generic_error("you gave us a function declare but told us this is a variable declare? ",
- psave, lno));
- /* variable */
- n->value = DECLARE_VAR;
- }
- else
- exit(generic_error("one of 'fn' or 'var' must be gave", psave, lno));
- }
- return 0;
- }
|