Added basic symbol output
authorLukas Krickl <lukas@krickl.dev>
Thu, 14 Dec 2023 04:50:07 +0000 (05:50 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 14 Dec 2023 04:50:07 +0000 (05:50 +0100)
include/ulas.h
src/test.c
src/ulas.c

index 4c65f0369c90ad01377047408310e384251aede4..1b9ec115c8b8997feae9382aa0b1cde83e039efd 100644 (file)
@@ -460,6 +460,8 @@ struct ulas_sym *ulas_symbolresolve(const char *name, int scope, int *rc);
 int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok,
                    int constant);
 
+int ulas_symbolout(FILE *dst, struct ulas_sym *s);
+
 // tokenisze according to pre-defined rules
 // returns the amount of bytes of line that were
 // consumed or -1 on error
index 8948322ef501e27aa2e757704021b43d535bb434..6a19d540c79ad66787a821dfe690ad776049ea8f 100644 (file)
@@ -366,6 +366,7 @@ void test_symscope(void) {
   {                                                                            \
     printf("[source: %s; expect: %s]\n", in_path, expect_path);                \
     ulaslstout = stdout;                                                       \
+    ulassymout = stdout;                                                       \
     struct ulas_config cfg = ulas_cfg_from_env();                              \
     cfg.verbose = 1;                                                           \
     char dstbuf[ULAS_FULLEN];                                                  \
index 507e0b0bcca760ea2c6a4368e69edd8f9bfa6d3d..09cae350e577b9c6b5bf8a8321365e6c9e0c23ad 100644 (file)
@@ -295,12 +295,16 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok,
     // def new symbol
     struct ulas_sym new_sym = {strdup(name), tok, scope, ulas.pass, constant};
     ulas_symbufpush(&ulas.syms, new_sym);
+    
+    rc = ulas_symbolout(ulassymout, &new_sym);
   } else if (existing->lastdefin != ulas.pass || !existing->constant) {
     // redefine if not defined this pass
     existing->lastdefin = ulas.pass;
     ulas_tokfree(&existing->tok);
     existing->tok = tok;
     existing->constant = constant;
+
+    rc = ulas_symbolout(ulassymout, existing);
   } else {
     // exists.. cannot have duplicates!
     rc = -1;
@@ -309,6 +313,29 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok,
   return rc;
 }
 
+int ulas_symbolout(FILE *dst, struct ulas_sym *s) {
+  if (!dst || ulas.pass != ULAS_PASS_FINAL) {
+    return 0;
+  }
+
+  int rc = 0;
+  fprintf(dst, "%s\t\t= ", s->name);
+  switch (s->tok.type) {
+  case ULAS_INT:
+    fprintf(dst, "%d", ulas_valint(&s->tok, &rc));
+    break;
+  case ULAS_STR:
+    fprintf(dst, "%s", ulas_valstr(&s->tok, &rc));
+    break;
+  default:
+    fprintf(dst, "<Unknown type>");
+    break;
+  }
+  fprintf(dst, "\n");
+
+  return rc;
+}
+
 #define WELD_TOKISTERM write
 #define WELD_TOKCOND (i < n && write < n && line[i])
 
@@ -2400,8 +2427,7 @@ int ulas_asmdirincbin(FILE *dst, const char **line, unsigned long n, int *rc) {
 }
 
 int ulas_asmdiradv(FILE *dst, const char **line, unsigned long n, int *rc) {
-  ULAS_EVALEXPRS(ulas.address +=
-                         ulas_intexpr(line, strnlen(*line, n), rc));
+  ULAS_EVALEXPRS(ulas.address += ulas_intexpr(line, strnlen(*line, n), rc));
   return 0;
 }
 
@@ -2439,15 +2465,21 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
   }
 
   if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) {
-    const char *dirstrs[] = {ULAS_ASMSTR_ORG,    ULAS_ASMSTR_SET,
-                             ULAS_ASMSTR_BYTE,   ULAS_ASMSTR_STR,
-                             ULAS_ASMSTR_FILL,   ULAS_ASMSTR_PAD,
-                             ULAS_ASMSTR_INCBIN, ULAS_ASMSTR_DEF,
-                             ULAS_ASMSTR_CHKSM, ULAS_ASMSTR_ADV,  NULL};
+    const char *dirstrs[] = {ULAS_ASMSTR_ORG,
+                             ULAS_ASMSTR_SET,
+                             ULAS_ASMSTR_BYTE,
+                             ULAS_ASMSTR_STR,
+                             ULAS_ASMSTR_FILL,
+                             ULAS_ASMSTR_PAD,
+                             ULAS_ASMSTR_INCBIN,
+                             ULAS_ASMSTR_DEF,
+                             ULAS_ASMSTR_CHKSM,
+                             ULAS_ASMSTR_ADV,
+                             NULL};
     enum ulas_asmdir dirs[] = {
-        ULAS_ASMDIR_ORG,    ULAS_ASMDIR_SET,  ULAS_ASMDIR_BYTE,
-        ULAS_ASMDIR_STR,    ULAS_ASMDIR_FILL, ULAS_ASMDIR_PAD,
-        ULAS_ASMDIR_INCBIN, ULAS_ASMDIR_DEF,  ULAS_ASMDIR_CHKSM, ULAS_ASMDIR_ADV};
+        ULAS_ASMDIR_ORG,   ULAS_ASMDIR_SET, ULAS_ASMDIR_BYTE,   ULAS_ASMDIR_STR,
+        ULAS_ASMDIR_FILL,  ULAS_ASMDIR_PAD, ULAS_ASMDIR_INCBIN, ULAS_ASMDIR_DEF,
+        ULAS_ASMDIR_CHKSM, ULAS_ASMDIR_ADV};
 
     enum ulas_asmdir dir = ULAS_ASMDIR_NONE;