WIP: Added test for tokline
authorLukas Krickl <lukas@krickl.dev>
Mon, 6 Nov 2023 17:03:41 +0000 (18:03 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 6 Nov 2023 17:03:41 +0000 (18:03 +0100)
include/ulas.h
src/test.c
src/ulas.c

index ef7148b4f124a3a5dbf06847f9bec3a4ce5f5335..10cf87a99ebd2843f67160df6c16cfb3af0a21a5 100644 (file)
@@ -42,6 +42,21 @@ extern FILE *ulasin;
 extern FILE *ulasout;
 extern FILE *ulaserr;
 
+// NULL for int based lookup
+#define INULL (-1)
+
+// expression index in the expression list
+typedef int iexpr;
+
+// token index in the token list
+typedef int itok;
+
+// string index in the string list
+typedef int istr;
+
+struct ulas_expr;
+struct ulas_tok;
+
 struct ulas_config {
   // argv represents file names
   char **argv;
@@ -52,6 +67,15 @@ struct ulas_config {
   bool verbose;
 };
 
+/**
+ * Assembly context
+ */
+
+struct ulas {
+  char **strs;
+  size_t strslen;
+};
+
 /**
  * Tokens
  */
@@ -65,7 +89,7 @@ enum ulas_toks {
 };
 
 struct ulas_tokliteral {
-  const char *literal;
+  istr literal;
 };
 
 union ulas_tokdat {
@@ -82,30 +106,34 @@ struct ulas_tok {
  */
 
 struct ulas_sym {
-  const char *name;
+  istr name;
 };
 
 /**
  * Expressions
+ *
+ * Expressions use an index based lookup instead of
+ * actual pointers to allow easy dumping of the structure to a file
+ * as well as realloc calls. As an index the integer based iexpr and itok
+ * denote which data type is supposed to be looked up. An iexpr or itok value of
+ * -1 denotes a NULL value
  */
 
-struct ulas_expr;
-
 enum ulas_exprs { ULAS_EXPUNARY, ULAS_EXPBINARY, ULAS_EXPLITERAL };
 
 struct ulas_expunary {
-  struct ulas_expr *left;
-  struct ulas_tok *op;
+  iexpr left;
+  itok op;
 };
 
 struct ulas_expbinary {
-  struct ulas_expr *left;
-  struct ulas_expr *right;
-  struct ulas_tok *op;
+  iexpr left;
+  iexpr right;
+  itok op;
 };
 
 struct ulas_expliteral {
-  struct ulas_tok *tok;
+  itok tok;
 };
 
 union ulas_expdat {
index 280dd44471115ebaeef69a4dae169d191c80e291..aae3cad5a8263d5290f0b3306f32c55f1df260ea 100644 (file)
     assert(strcmp(buf, expected_tok) == 0);                                    \
   }
 
-#define assert_tokline(expected_toks, expected_n, line, rule)                  \
-  {}
+#define assert_tokline(expected_n, line, rule, ...)                            \
+  {                                                                            \
+    char *expect[] = __VA_ARGS__;                                              \
+    size_t n = 0;                                                              \
+    char **toks = ulas_tokline(line, &n, rule);                                \
+    assert(toks);                                                              \
+    assert(n == expected_n);                                                   \
+    for (size_t i = 0; i < n; i++) {                                           \
+      assert(strcmp(toks[i], expect[i]) == 0);                                 \
+    }                                                                          \
+    ulas_toklinefree(toks, n);                                                 \
+  }
 
 void test_tok(void) {
   TESTBEGIN("tok");
@@ -26,8 +36,8 @@ void test_tok(void) {
   assert_tok("", 0, "", ulas_tokrulespace);
   assert_tok("", -1, NULL, ulas_tokrulespace);
 
-  assert_tokline(({"test", "tokens", "with", "line"}), 4,
-                 "  test  tokens   with   line", ulas_tokrulespace);
+  assert_tokline(4, "  test  tokens   with   line", ulas_tokrulespace,
+                 {"test", "tokens", "with", "line"});
 
   TESTEND("tok");
 }
index a80853c2fc32a7e675d2b8825a296e6d692020a5..aa5b0a52d7fa1cae5e1e5ca94172ebe9f8560eed 100644 (file)
@@ -83,7 +83,7 @@ int ulas_tok(char *dst, const char *line, size_t n, ulas_tokrule rule) {
   }
 #undef weld_tokcond
 
-  dst[write + 1] = '\0';
+  dst[write] = '\0';
   return i;
 }
 
@@ -108,12 +108,13 @@ char **ulas_tokline(const char *line, size_t *n, ulas_tokrule rule) {
     }
     dst = newdst;
 
-    dst[*n - 1] = strndup(buf, ULAS_TOKMAX);
+    dst[(*n) - 1] = strndup(buf, ULAS_TOKMAX);
   }
 
   return dst;
 fail:
   ulas_toklinefree(dst, *n);
+  *n = 0;
   return NULL;
 }
 
@@ -122,7 +123,7 @@ void ulas_toklinefree(char **data, size_t n) {
     return;
   }
   for (size_t i = 0; i < n; i++) {
-    free(data[n]);
+    free(data[i]);
   }
 
   free(data);