Added tests for symbol resolver
authorLukas Krickl <lukas@krickl.dev>
Sat, 18 Nov 2023 06:27:38 +0000 (07:27 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 18 Nov 2023 06:27:38 +0000 (07:27 +0100)
src/test.c
src/ulas.c

index ce9d929c946cee431847e791f81f98167fe56f80..c8b18d53ec284b2bd7f5920051235c1b2f71a4bc 100644 (file)
@@ -163,6 +163,25 @@ void test_preproc(void) {
     free(tok.lit.val.strv);                                                    \
   }
 
+#define ASSERT_SYMBOL_TOTOK(expected_val, expected_rc, token)                  \
+  {                                                                            \
+    int rc = 0;                                                                \
+    struct ulas_tok tok = ulas_totok((token), strlen(token), &rc);             \
+    assert((expected_rc) == rc);                                               \
+    assert(tok.type == ULAS_TOKSYMBOL);                                        \
+    assert(tok.lit.type == ULAS_STR);                                          \
+    assert(strcmp((expected_val), tok.lit.val.strv) == 0);                     \
+    free(tok.lit.val.strv);                                                    \
+  }
+
+#define ASSERT_UNEXPECTED_TOTOK(expected_rc, token)                  \
+  {                                                                            \
+    int rc = 0;                                                                \
+    ulas_totok((token), strlen(token), &rc);             \
+    assert((expected_rc) == rc);                                               \
+  }
+
+
 void test_totok(void) {
   TESTBEGIN("totok");
 
@@ -189,6 +208,12 @@ void test_totok(void) {
   // unterminated string
   ASSERT_STR_TOTOK("test\n\"123\"", -1, "\"test\\n\\\"123\\\"");
 
+  // symbols
+  ASSERT_SYMBOL_TOTOK("_symbol123", 0, "_symbol123");
+  ASSERT_SYMBOL_TOTOK("symbol123", 0, "symbol123");
+
+  ASSERT_UNEXPECTED_TOTOK(-1, "1symbol123");
+
   TESTEND("totok");
 }
 
index 2031b746faa83255c6ce485e7c5fde989a933fc0..b61fe4ef01db1efc062519d70ccac1b68d15c83c 100644 (file)
@@ -336,13 +336,13 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) {
       }
       buf++;
       break;
-    } else if (ulas_isname(buf, n)) {
-      // literal. we can resolve it now
-      // because literals need to be able to be resolved
-      // for every line, unless they are a label!
-      // TODO: read and unescape striing between " and "
+    } else if (ulas_isname(buf - 1, n)) {
+      // literal token
+      // we resolve it later, will need to malloc here for now
       tok.type = ULAS_TOKSYMBOL;
       tok.lit.type = ULAS_STR;
+      tok.lit.val.strv = strndup(buf - 1, n);
+      buf += n - 1;
     } else {
       ULASERR("Unexpected token: %s\n", buf);
       *rc = -1;