Implemented tests for int token and char token
authorLukas Krickl <lukas@krickl.dev>
Fri, 17 Nov 2023 15:42:18 +0000 (16:42 +0100)
committerLukas Krickl <lukas@krickl.dev>
Fri, 17 Nov 2023 15:42:18 +0000 (16:42 +0100)
include/ulas.h
makefile
src/test.c
src/ulas.c

index f5a87c281c4faa02f0b5553a1fc5ee6dc4cc9095..0ef18029a8adc5523b3ef05ea5b531b1862fedcb 100644 (file)
@@ -97,8 +97,8 @@ enum ulas_type { ULAS_INT, ULAS_STR };
 
 // data type value
 union ulas_val {
-  int int_value;
-  char *str_value;
+  int intv;
+  char *strv;
 };
 
 // literal value
index 0fed56feac8c529f57c0db637e9793e799038ae4..e2755f7fce6f9f66f973ebb3ed76ce2196841db0 100644 (file)
--- a/makefile
+++ b/makefile
@@ -53,7 +53,7 @@ install:
 
 .PHONY: tags 
 tags:
-       ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --exclude=scripts
+       ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --extras=*  --fields=*  --c-kinds=* --language-force=C 
 
 .PHONY:
 ccmds:
index 2433618e14e5d9696e355f33fd13fe2a2a0cf26b..77900832287cc37a9c3960d3f2f18489ee39c6b4 100644 (file)
@@ -142,8 +142,35 @@ void test_preproc(void) {
   ulascfg.preproc_only = 0;
 }
 
+#define ASSERT_INT_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_TOKLITERAL);                                       \
+    assert(tok.lit.type == ULAS_INT);                                          \
+    assert(tok.lit.val.intv == (expected_val));                                \
+  }
+
 void test_totok(void) {
   TESTBEGIN("totok");
+
+  // regular ints
+  ASSERT_INT_TOTOK(10, 0, "10");
+  ASSERT_INT_TOTOK(0x1A, 0, "0x1A");
+  ASSERT_INT_TOTOK(5, 0, "0b101");
+
+  // chars
+  ASSERT_INT_TOTOK('a', 0, "'a'");
+  ASSERT_INT_TOTOK('\n', 0, "'\\n'");
+  ASSERT_INT_TOTOK('\\', 0, "'\\\\'");
+  // char - not terminated
+  ASSERT_INT_TOTOK('a', -1, "'a");
+  // bad escape
+  ASSERT_INT_TOTOK(0, -1, "'\\z'");
+  // unterminated escape
+  ASSERT_INT_TOTOK('\n', -1, "'\\n");
+
   TESTEND("totok");
 }
 
index 55102273f7b9270ea95dc6298fd21a3dea81b3df..763079b23f91f795150224433a84e767018e4d5a 100644 (file)
@@ -269,22 +269,30 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) {
       // integer
       tok.type = ULAS_TOKLITERAL;
       tok.lit.type = ULAS_INT;
-      tok.lit.val.int_value = (int)strtol(buf, &buf, 0);
+
+      // 0b prefix is not supported in strtol... so we implement it by hand
+      if (*buf == 'b') {
+        buf++;
+        tok.lit.val.intv = (int)strtol(buf, &buf, 2);
+      } else {
+        tok.lit.val.intv = (int)strtol(buf - 1, &buf, 0);
+      }
     } else if (first == '\'') {
       tok.type = ULAS_TOKLITERAL;
       tok.lit.type = ULAS_INT;
-      buf++;
       if (*buf == '\\') {
         buf++;
-        tok.lit.val.int_value = ulas_unescape(*buf, rc);
+        tok.lit.val.intv = ulas_unescape(*buf, rc);
       } else {
-        tok.lit.val.int_value = (int)*buf;
+        tok.lit.val.intv = (int)*buf;
       }
       buf++;
       if (*buf != '\'') {
         *rc = -1;
         ULASERR("Unterminated character sequence\n");
+        goto end;
       }
+      buf++;
       break;
     } else if (ulas_isname(buf, n)) {
       // literal. we can resolve it now
@@ -294,7 +302,6 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) {
       tok.type = ULAS_TOKSYMBOL;
       tok.lit.type = ULAS_STR;
     } else {
-
       ULASERR("Unexpected token: %s\n", buf);
       *rc = -1;
       goto end;
@@ -821,7 +828,7 @@ int ulas_litint(struct ulas_lit *lit, int *rc) {
     return 0;
   }
 
-  return lit->val.int_value;
+  return lit->val.intv;
 }
 
 char *ulas_litchar(struct ulas_lit *lit, int *rc) {
@@ -830,7 +837,7 @@ char *ulas_litchar(struct ulas_lit *lit, int *rc) {
     return NULL;
   }
 
-  return lit->val.str_value;
+  return lit->val.strv;
 }
 
 struct ulas_tokbuf ulas_tokbuf(void) {