Added comment support to instruction parser
authorLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 20:38:58 +0000 (21:38 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 20:38:58 +0000 (21:38 +0100)
src/ulas.c

index 33c0ae5011d731aa085839abc2e9e1c5b86018aa..b57f15da65322349b59c6f14cfafa929fd0efc0b 100644 (file)
@@ -1345,6 +1345,16 @@ int ulas_intexpr(const char **line, unsigned long n, int *rc) {
 
 #define ULAS_ISINSTR(tok, name, n) (strncmp(tok, name, n) == 0)
 
+int ulas_istokend(struct ulas_str *tok) {
+  long len = strnlen(tok->buf, tok->maxlen);
+  // skip comments though, they are not trailing tokens!
+  if (len > 0 && tok->buf[0] != ULAS_TOK_COMMENT) {
+    return 0;
+  }
+
+  return 1;
+}
+
 // assembles an instruction, writes bytes into dst
 // returns bytes written or -1 on error
 int ulas_asminstr(char *dst, unsigned long max, const char *line,
@@ -1355,6 +1365,10 @@ int ulas_asminstr(char *dst, unsigned long max, const char *line,
     return -1;
   }
 
+  if (ulas_istokend(&ulas.tok)) {
+    return 0;
+  }
+
   if (ulas_tok(&ulas.tok, &line, n) == -1) {
     ULASERR("Expected label or instruction\n");
     return -1;
@@ -1380,6 +1394,14 @@ int ulas_asminstr(char *dst, unsigned long max, const char *line,
     return -1;
   }
 
+  // check for trailing
+  if (ulas_tok(&ulas.tok, &line, n) > 0) {
+    if (!ulas_istokend(&ulas.tok)) {
+      ULASERR("Trailing token '%s'\n", ulas.tok.buf);
+      return -1;
+    }
+  }
+
   return rc;
 }