From 3ffd69a403150d8a81efea5213f8d4dee2de7451 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 4 Dec 2023 08:12:30 +0100 Subject: [PATCH] Added local scope resolver test --- src/test.c | 3 +-- src/ulas.c | 22 ++++++++++------------ tests/t0.bin | Bin 83 -> 89 bytes tests/t0.s | 5 ++++- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test.c b/src/test.c index 55fd116..e89e39a 100644 --- a/src/test.c +++ b/src/test.c @@ -323,11 +323,10 @@ void test_symscope(void) { ASSERT_SYMSCOPE(0, "t2:", 6, 1); ASSERT_SYMSCOPE(-1, "t2:", 6, 1); - // set + // set ASSERT_SYMSCOPE(0, "t3:", -1, 0); ASSERT_SYMSCOPE(0, "t3:", -1, 0); - TESTEND("symscope"); } diff --git a/src/ulas.c b/src/ulas.c index 5ca228f..60cc6f6 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -43,10 +43,11 @@ void ulas_init(struct ulas_config cfg) { ulas.exprs = ulas_exprbuf(); ulas.syms = ulas_symbuf(); ulas.pp = ulas_preprocinit(); + ulas.scope = 1; } void ulas_nextpass(void) { - ulas.scope = 0; + ulas.scope = 1; ulas.line = 0; ulas.icntr = 0; ulas.address = 0; @@ -199,7 +200,8 @@ int ulas_isname(const char *tok, unsigned long n) { for (unsigned long i = 0; i < n; i++) { char c = tok[i]; - if (c != '_' && !isalnum(c)) { + if (c != '_' && !isalnum(c) && + !(i == 0 && c == ULAS_TOK_SCOPED_SYMBOL_BEGIN)) { return 0; } } @@ -208,10 +210,6 @@ int ulas_isname(const char *tok, unsigned long n) { } int ulas_islabelname(const char *tok, unsigned long n) { - if (tok[0] == '@' && n > 0) { - tok+=1; - n--; - } return tok[n - 1] == ':' && ulas_isname(tok, n - 1); } @@ -252,12 +250,12 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, } struct ulas_sym *existing = ulas_symbolresolve(name, scope, &resolve_rc); - if (!existing) { - // inc scope when symbol is global - if (name[0] != ULAS_TOK_SCOPED_SYMBOL_BEGIN) { - ulas.scope++; - } + // inc scope when symbol is global + if (name[0] != ULAS_TOK_SCOPED_SYMBOL_BEGIN) { + ulas.scope++; + } + if (!existing) { // def new symbol struct ulas_sym new_sym = {strdup(name), tok, scope, ulas.pass, constant}; ulas_symbufpush(&ulas.syms, new_sym); @@ -270,7 +268,7 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, } else { // exists.. cannot have duplicates! rc = -1; - ULASERR("Redefenition of symbol '%s'\n", name); + ULASERR("Redefenition of symbol '%s' in scope %d\n", name, scope); } return rc; } diff --git a/tests/t0.bin b/tests/t0.bin index b752c2e50338234207c621f181d226c45683c051..04a9544411aa838e08d9db0135683215d0c8050c 100644 GIT binary patch delta 11 ScmWHJoDj^$7|h5R#s~lrK>|Yn delta 4 LcmazHo)8QG1JnVK diff --git a/tests/t0.s b/tests/t0.s index 4abdad7..9dc1321 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -74,4 +74,7 @@ l1: bit 0, [hl] ld bc, l1 + 1 -@local1: +@local: + ld bc, @local +l2: +@local: ld bc, @local -- 2.30.2