WIP: tokenizer
authorLukas Krickl <lukas@krickl.dev>
Wed, 15 Nov 2023 16:29:41 +0000 (17:29 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 15 Nov 2023 16:29:41 +0000 (17:29 +0100)
include/ulas.h
src/ulas.c

index 2fdabdaafff8851f6d20838c8f0e6b62bd7c33bb..6931ee28762367b6ef3ef0b146a7226ce79b8118 100644 (file)
@@ -89,7 +89,8 @@ struct ulas_str {
  * Tokens
  */
 
-enum ulas_toks { ULAS_TOKLITERAL, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING };
+// any token before 256 is just the literal char value 
+enum ulas_toks { ULAS_TOKLITERAL = 256, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING };
 
 // primitive data types
 enum ulas_type { ULAS_INT, ULAS_STR };
@@ -278,6 +279,9 @@ char *ulas_strndup(const char *src, size_t n);
 // returns 0 when no more tokens can be read
 int ulas_tok(struct ulas_str *dst, const char **out_line, size_t n);
 
+// converts a token string to a token struct
+struct ulas_tok ulas_totok(const char *buf, size_t n, int *rc);
+
 int ulas_tokuntil(struct ulas_str *dst, char c, const char **out_line,
                   size_t n);
 
index b8e853de5440b0e40724bf722c0da93b0c2ffaad..54e6c0d14e264cfa9b4963b1a98e439ad3a4ff1f 100644 (file)
@@ -217,6 +217,34 @@ int ulas_tokuntil(struct ulas_str *dst, char c, const char **out_line,
   return i;
 }
 
+struct ulas_tok ulas_totok(const char *buf, size_t n, int *rc) {
+  struct ulas_tok tok;
+  memset(&tok, 0, sizeof(tok));
+
+  if (n == 0) {
+    *rc = -1;
+    goto end;
+  }
+
+  unsigned char first = buf[0];
+
+  switch (first) {
+  case ';':
+    tok.type = first;
+    goto end;
+  case '"':
+    // string
+    break;
+  default:
+    ULASERR("Unexpected token: %s\n", buf);
+    *rc = -1;
+    goto end;
+  }
+
+end:
+  return tok;
+}
+
 #undef WELD_TOKCOND
 #undef WLED_TOKISTERM
 
@@ -773,7 +801,22 @@ void ulas_tokbuffree(struct ulas_tokbuf *tb) { free(tb->buf); }
  * Assembly step
  */
 
-int ulas_intexpr(const char **line, size_t n, int *rc) { return -1; }
+int ulas_intexpr(const char **line, size_t n, int *rc) {
+  // read tokens until the next token is end of line, ; or ,
+
+  int tokrc = 0;
+  while ((tokrc = ulas_tok(&ulas.tok, line, n) > 0)) {
+    if (tokrc == -1) {
+      *rc = -1;
+      goto fail;
+    }
+
+    // interpret the token
+  }
+
+fail:
+  return -1;
+}
 
 int ulas_asmline(FILE *dst, FILE *src, const char *line, size_t n) {
   const char *start = line;