WIP: symbols
authorLukas Krickl <lukas@krickl.dev>
Thu, 30 Nov 2023 19:00:17 +0000 (20:00 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 30 Nov 2023 19:00:17 +0000 (20:00 +0100)
include/ulas.h
src/ulas.c

index 049038aabec1d7cbd08fae89fd1c42236b314c4d..4dd9166fddd28bed005bb99c7c54236510aad8e1 100644 (file)
@@ -410,7 +410,7 @@ char *ulas_strndup(const char *src, unsigned long n);
 // returns -1 if any flagged symbol was not found
 // if flagged symbols remain unresolved (e.g. global or locals) rc is set to the
 // respective flag value
-struct ulas_tok *ulas_symbolresolve(const char *name, int *rc);
+struct ulas_sym *ulas_symbolresolve(const char *name, int *rc);
 
 // define a new symbol
 // scope 0 indicates global scope
index 29cd3e623d1cf81b46661e03d6dd96b80221dbdc..4f4b394a84911f502b659ccb29534fd04c019c7d 100644 (file)
@@ -211,19 +211,32 @@ int ulas_islabelname(const char *tok, unsigned long n) {
   return tok[n - 1] == ':' && ulas_isname(tok, n - 1);
 }
 
-struct ulas_tok *ulas_symbolresolve(const char *name, int *rc) {
+struct ulas_sym *ulas_symbolresolve(const char *name, int *rc) {
   for (int i = 0; i < ulas.syms.len; i++) {
     struct ulas_sym *sym = &ulas.syms.buf[i];
     // when scope is the same as the current one, or scope 0 (global)
     if ((sym->scope & ulas.scope) == 0 && strcmp(name, sym->name) == 0) {
-      return &sym->tok;
+      return sym;
     }
   }
+  *rc = -1;
   return NULL;
 }
 
 int ulas_symboldef(const char *name, int scope, struct ulas_tok token) {
-  return 0;
+  int rc = 0;
+  int resolve_rc = 0;
+  struct ulas_sym *exisitng = ulas_symbolresolve(name, &resolve_rc);
+  // define new
+  if (!exisitng) {
+  } else if (exisitng->lastdefin != ulas.pass) {
+    // redefine if not defined this pass
+  } else {
+    // exists.. cannot have duplicates!
+    rc = -1;
+    ULASERR("Redefenition of symbol '%s'\n", name);
+  }
+  return rc;
 }
 
 #define WELD_TOKISTERM write
@@ -995,12 +1008,16 @@ fail:
 
 int ulas_valint(struct ulas_tok *lit, int *rc) {
   if (lit->type == ULAS_SYMBOL) {
-    struct ulas_tok *stok = ulas_symbolresolve(lit->val.strv, rc);
+    struct ulas_sym *stok = ulas_symbolresolve(lit->val.strv, rc);
     // bail if symbol is not resolvable
     if (*rc > 0) {
       return 0;
     }
-    return ulas_valint(stok, rc);
+    if (!stok || *rc == -1) {
+      *rc = -1;
+      return 0;
+    }
+    return ulas_valint(&stok->tok, rc);
   }
 
   if (!lit || lit->type != ULAS_INT) {