summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-10 05:02:28 +0200
committerkdx <kikoodx@paranoici.org>2023-06-10 05:06:42 +0200
commiteadc11a1ed56241e565e6ff9744fa692ed8ccd09 (patch)
tree23c4608aa6acb9aabea1638fbf69b535a1b63696
parentc18eda9ff2377f02727910b7cf54b286ea35fa4d (diff)
downloadgolem-eadc11a1ed56241e565e6ff9744fa692ed8ccd09.tar.gz
local variables should be done
-rw-r--r--src/main.c22
-rwxr-xr-xtesting.sh1
2 files changed, 16 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 10e8d5a..4bbe051 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}
diff --git a/testing.sh b/testing.sh
index b1db4ea..3acaafe 100755
--- a/testing.sh
+++ b/testing.sh
@@ -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.*