Added local scope resolver test
authorLukas Krickl <lukas@krickl.dev>
Mon, 4 Dec 2023 07:12:30 +0000 (08:12 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 4 Dec 2023 07:12:30 +0000 (08:12 +0100)
src/test.c
src/ulas.c
tests/t0.bin
tests/t0.s

index 55fd116e9d24f99ed601d1d20f43bab8e6e9103d..e89e39a88745ab66327191cc87cae605526df33d 100644 (file)
@@ -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");
 }
 
index 5ca228fe3de2858e658196f913e2cc6e28a8f469..60cc6f650d18f96964f68ab16305acd9cc129130 100644 (file)
@@ -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;
 }
index b752c2e50338234207c621f181d226c45683c051..04a9544411aa838e08d9db0135683215d0c8050c 100644 (file)
Binary files a/tests/t0.bin and b/tests/t0.bin differ
index 4abdad79eceeebb3a2288908bd0327bedd9d3940..9dc13210265c20daff372105b27483b284f697a1 100644 (file)
@@ -74,4 +74,7 @@ l1:
   bit 0, [hl]
 
   ld bc, l1 + 1
-@local1:
+@local:
+  ld bc, @local
+l2:
+@local: ld bc, @local