From: Lukas Krickl Date: Sun, 3 Dec 2023 06:37:59 +0000 (+0100) Subject: WIP: symbol scoping tests X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=b81c1ea9b260c4f2a3a428f98439d3c12ca09a62;p=ulas%2F.git WIP: symbol scoping tests --- diff --git a/src/test.c b/src/test.c index 6f23ad3..df50573 100644 --- a/src/test.c +++ b/src/test.c @@ -303,6 +303,21 @@ void test_asminstr(void) { TESTEND("asminstr"); } +#define ASSERT_SYMSCOPE(expect_ret, name, scope, constant) \ + { \ + struct ulas_tok tok = {ULAS_INT, {0}}; \ + assert(ulas_symbolset((name), (scope), tok, constant) == (expect_ret)); \ + } + +void test_symscope(void) { + TESTBEGIN("symscope"); + + ASSERT_SYMSCOPE(0, "t1", -1, 1); + ASSERT_SYMSCOPE(-1, "t1", -1, 1); + + TESTEND("symscope"); +} + #define ULAS_FULLEN 0xFFFF #define ASSERT_FULL(expect_rc, in_path, expect_path) \ @@ -353,6 +368,7 @@ int main(int arc, char **argv) { test_totok(); test_intexpr(); test_asminstr(); + test_symscope(); ulas_free(); diff --git a/src/ulas.c b/src/ulas.c index 33be3a2..6654413 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -216,6 +216,7 @@ struct ulas_sym *ulas_symbolresolve(const char *name, int *rc) { struct ulas_sym *sym = &ulas.syms.buf[i]; // when scope is the same as the current one, or scope 0 (global) if ((sym->scope & ulas.scope) == 0 && strcmp(name, sym->name) == 0) { + puts("scope"); return sym; } } @@ -246,7 +247,7 @@ int ulas_symbolset(const char *name, int scope, struct ulas_tok tok, // def new symbol struct ulas_sym new_sym = {strdup(name), tok, scope, ulas.pass, constant}; - // last char of name has to be : so we trim it away + // last char of name has to be : so we trim it away new_sym.name[strlen(new_sym.name) - 1] = '\0'; ulas_symbufpush(&ulas.syms, new_sym); } else if (exisitng->lastdefin != ulas.pass) {