diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-10 05:02:28 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-10 05:06:42 +0200 |
commit | eadc11a1ed56241e565e6ff9744fa692ed8ccd09 (patch) | |
tree | 23c4608aa6acb9aabea1638fbf69b535a1b63696 | |
parent | c18eda9ff2377f02727910b7cf54b286ea35fa4d (diff) | |
download | golem-eadc11a1ed56241e565e6ff9744fa692ed8ccd09.tar.gz |
local variables should be done
-rw-r--r-- | src/main.c | 22 | ||||
-rwxr-xr-x | testing.sh | 1 |
2 files changed, 16 insertions, 7 deletions
@@ -131,7 +131,7 @@ tokenize(char *p) continue; } - if (isalpha(*p)) { + if (isalpha(*p) || *p == '_') { char *q = p; while (isalnum(*p) || *p == '_') p += 1; @@ -549,7 +549,7 @@ static int depth; static void gen_expr(Node *node); static void gen_globaldec(Node *node); static void gen_fncall(Node *node); -static void gen_globalget(Node *node); +static void gen_variableget(Node *node); static void gen_expr(Node *node) @@ -566,7 +566,7 @@ gen_expr(Node *node) } if (node->type == NOD_WORD) { - gen_globalget(node); + gen_variableget(node); return; } @@ -628,10 +628,18 @@ gen_fncall(Node *node) } static void -gen_globalget(Node *node) +gen_variableget(Node *node) { - printf("\tLIT ,__gl_%.*s\n", node->len, node->loc); - printf("\tLDA\n"); + const int found = node_find(locals, node); + if (found != -1) { + printf("\tLIT ,__stack_ptr LDA\n"); + if (locals_size - found - 1) + printf("\tLIT %04x SUB\n", locals_size - found - 1); + printf("\tLDA\n"); + } else { + printf("\tLIT ,__gl_%.*s\n", node->len, node->loc); + printf("\tLDA\n"); + } depth += 1; } @@ -668,7 +676,7 @@ gen_function(Node *node) const int offset = locals_size + i - argcount; if (offset) printf("\tLIT %04x SUB\n", offset); - printf("\tSWP STA\n"); + printf("\tSTA\n"); cur = cur->next; } @@ -21,4 +21,5 @@ test "$1" "global abc = 5; main(){ return abc; }" test "$1" "main(){ return abc; } global abc = 5;" test "$1" "global abc = 5; main(){ inc(); return abc; } inc() { abc = abc + 1; }" test "$1" "main() { wrt('H'); wrt('e'); wrt('l'); wrt('l'); wrt('o'); wrt('!'); wrt('\n'); }" +test "$1" "main() { wrt(up('u')); wrt('w'); wrt(up('u')); wrt('\n'); } up(c) { return c + 'A' - 'a'; }" rm -f build/tmp.* |