WIP: asm step tokenizer
authorLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 19:38:46 +0000 (20:38 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 20 Nov 2023 19:38:46 +0000 (20:38 +0100)
src/test.c
src/ulas.c

index d1cce77f52cddb27af3f85447ee0d40c2ba04227..e96a103f635df6c144fdb6a031db0b7146e36be8 100644 (file)
@@ -272,6 +272,7 @@ void test_intexpr(void) {
 
   ASSERT_INTEXPR(-1, -1, "((2 + 3) * 5");
   ASSERT_INTEXPR(25, 0, "(2 + 3) * 5");
+  ASSERT_INTEXPR(4, 0, "1 + 3 ; comment");
 
   TESTEND("intexpr");
 }
index ad4e4a8e3e9b84f6fefee14c891ac388e6ed3862..90112fb63821195580f8ead865dc112372b4092d 100644 (file)
@@ -1040,7 +1040,8 @@ void ulas_exprbuffree(struct ulas_exprbuf *eb) { free(eb->buf); }
  * Assembly step
  */
 
-int ulas_tokexpr(const char **line, unsigned long n) {
+// tokenize all until a terminator token or comment is reached
+int ulas_tokall(const char **line, unsigned long n, int term) {
   ulas_tokbufclear(&ulas.toks);
 
   int tokrc = 0;
@@ -1062,7 +1063,7 @@ int ulas_tokexpr(const char **line, unsigned long n) {
     }
 
     // check for any expression terminators here
-    if (tok.type == ',') {
+    if (tok.type == term || tok.type == ULAS_TOK_COMMENT) {
       goto end;
     }
 
@@ -1074,6 +1075,10 @@ end:
   return tokrc;
 }
 
+int ulas_tokexpr(const char **line, unsigned long n) {
+  return ulas_tokall(line, n, ',');
+}
+
 /**
  * Parse expressions from tokens
  * return tree-like structure
@@ -1338,10 +1343,7 @@ int ulas_intexpr(const char **line, unsigned long n, int *rc) {
   return ulas_intexpreval(expr, rc);
 }
 
-int ulas_asmimisc(char *dst, unsigned long max, const char *line,
-                  unsigned long n) {
-  return 0;
-}
+#define ULAS_ISINSTR(tok, name, n) (strncmp(tok, name, n) == 0)
 
 // assembles an instruction, writes bytes into dst
 // returns bytes written or -1 on error
@@ -1349,16 +1351,27 @@ int ulas_asminstr(char *dst, unsigned long max, const char *line,
                   unsigned long n) {
   int rc = 0;
 
-  if ((rc = ulas_asmimisc(dst, max, line, n))) {
-  }
-
   return rc;
 }
 
 void ulas_asmlst(const char *line, char *outbuf, unsigned long n) {
   if (ulaslstout) {
-    // TODO: verbose output <address> <bytes>\tline
-    fprintf(ulaslstout, "%08X\t%s", ulas.address, line);
+    fprintf(ulaslstout, "%08X", ulas.address);
+
+    // always pad at least n bytes
+    fputs("\t", ulaslstout);
+    const int pad = 8;
+    int outwrt = 0;
+
+    for (long i = 0; i < n; i++) {
+      outwrt += fprintf(ulaslstout, "%02x ", (int)outbuf[i]);
+    }
+
+    for (long i = outwrt; i < pad; i++) {
+      fputs(".", ulaslstout);
+    }
+
+    fprintf(ulaslstout, "\t%s", line);
   }
 }
 
@@ -1424,9 +1437,11 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
       goto fail;
     }
 
-    // TODO: place marker when a label was unresolved
-    // write down byte location in dst as well as line number in verbout so we
-    // can fix them later
+    // TODO:
+    // place marker when a label was unresolved
+    // write down byte location in dst as well as the byte offset in lstout so
+    // we can fix them later labels can only be unresolved when they are the
+    // only node in an expression!
   }
 
   ulas_asmout(dst, outbuf, towrite);