Added test for scoped redefinition
authorLukas Krickl <lukas@krickl.dev>
Sun, 3 Dec 2023 12:40:07 +0000 (13:40 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 3 Dec 2023 12:40:07 +0000 (13:40 +0100)
include/ulas.h
src/test.c
src/ulas.c

index dba2476674723a296256cb8a5042fbf1061dbe4f..4d575e96aaec9607fcf3004ef543d5260294f618 100644 (file)
@@ -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
index b939583234ae10d4d7eae2e128e1db7addd4725a..483d451f96ff2237f811a9ec00339ea62dc8824e 100644 (file)
@@ -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");
 }
 
index 833b1e83d8828c3e2dcef74ea0cb04e7989f4e25..e8b4009aae979eb9fe265b6896ca0f97ec0c0898 100644 (file)
@@ -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;