From: Lukas Krickl Date: Sun, 3 Dec 2023 12:40:07 +0000 (+0100) Subject: Added test for scoped redefinition X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=85d93085f3af2e0f20756b8e9bd06c8dc3500757;p=ulas%2F.git Added test for scoped redefinition --- diff --git a/include/ulas.h b/include/ulas.h index dba2476..4d575e9 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -413,7 +413,7 @@ char *ulas_strndup(const char *src, unsigned long n); // returns -1 if any flagged symbol was not found // if flagged symbols remain unresolved (e.g. global or locals) rc is set to the // respective flag value -struct ulas_sym *ulas_symbolresolve(const char *name, int *rc); +struct ulas_sym *ulas_symbolresolve(const char *name, int scope, int *rc); // define a new symbol // scope 0 indicates global scope. a scope of -1 instructs diff --git a/src/test.c b/src/test.c index b939583..483d451 100644 --- a/src/test.c +++ b/src/test.c @@ -317,6 +317,12 @@ void test_symscope(void) { ASSERT_SYMSCOPE(0, "@t1:", -1, 1); ASSERT_SYMSCOPE(-1, "@t1:", -1, 1); + // manual scoping + ASSERT_SYMSCOPE(0, "t2:", 5, 1); + ASSERT_SYMSCOPE(-1, "t2:", 5, 1); + ASSERT_SYMSCOPE(0, "t2:", 6, 1); + ASSERT_SYMSCOPE(-1, "t2:", 6, 1); + TESTEND("symscope"); } diff --git a/src/ulas.c b/src/ulas.c index 833b1e8..e8b4009 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -211,11 +211,11 @@ int ulas_islabelname(const char *tok, unsigned long n) { return tok[n - 1] == ':' && ulas_isname(tok, n - 1); } -struct ulas_sym *ulas_symbolresolve(const char *name, int *rc) { +struct ulas_sym *ulas_symbolresolve(const char *name, int scope, int *rc) { for (int i = 0; i < ulas.syms.len; i++) { struct ulas_sym *sym = &ulas.syms.buf[i]; // when scope is the same as the current one, or scope 0 (global) - if ((sym->scope == 0 || sym->scope == ulas.scope) && + if ((sym->scope == 0 || sym->scope == scope) && strcmp(name, sym->name) == 0) { return sym; } @@ -247,7 +247,7 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, } } - struct ulas_sym *exisitng = ulas_symbolresolve(name, &resolve_rc); + struct ulas_sym *exisitng = ulas_symbolresolve(name, scope, &resolve_rc); if (!exisitng) { // inc scope when symbol is global if (name[0] != ULAS_TOK_SCOPED_SYMBOL_BEGIN) { @@ -1036,7 +1036,7 @@ fail: int ulas_valint(struct ulas_tok *lit, int *rc) { if (lit->type == ULAS_SYMBOL) { - struct ulas_sym *stok = ulas_symbolresolve(lit->val.strv, rc); + struct ulas_sym *stok = ulas_symbolresolve(lit->val.strv, ulas.scope, rc); if (!stok || *rc == -1) { ULASERR("Unabel to resolve '%s'\n", lit->val.strv); *rc = -1;