WIP: symbol scoping tests
authorLukas Krickl <lukas@krickl.dev>
Sun, 3 Dec 2023 06:37:59 +0000 (07:37 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 3 Dec 2023 06:37:59 +0000 (07:37 +0100)
src/test.c
src/ulas.c

index 6f23ad3e92ec935e7514d2337a13226b87831cb8..df50573345b9fa9c18a1a5171d227039372eac1b 100644 (file)
@@ -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();
 
index 33be3a28dc5c170f88766159c7be92e38bb27b36..6654413c7fb670e4b867554492abd5c5ed00fe1e 100644 (file)
@@ -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) {