about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-04-21 20:54:18 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-04-21 20:54:18 -0700
commita5e1a220fde080c0eb7ef2a09ee0a7e9362e5de4 (patch)
tree3e3b5bbf1095fd286f31e3ff7d6ac89d160d4f01
parentc54b7e96307082fd3d67280acfcfc107553cda98 (diff)
downloadmu-a5e1a220fde080c0eb7ef2a09ee0a7e9362e5de4.tar.gz
shell: refuse to 'def' duplicate names
-rw-r--r--shell/evaluate.mu2
-rw-r--r--shell/global.mu46
2 files changed, 46 insertions, 2 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu
index 3e9cb6b6..e41459a4 100644
--- a/shell/evaluate.mu
+++ b/shell/evaluate.mu
@@ -191,7 +191,7 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     stream-to-array first-arg-data, tmp-ah
     var first-arg-data-string/eax: (addr array byte) <- lookup *tmp-ah
     var out2/edi: (addr handle cell) <- copy out
-    append-global globals, first-arg-data-string, *out2
+    append-global globals, first-arg-data-string, *out2, trace
     trace-higher trace
     return
   }
diff --git a/shell/global.mu b/shell/global.mu
index 6af04237..649964b7 100644
--- a/shell/global.mu
+++ b/shell/global.mu
@@ -256,8 +256,20 @@ fn append-primitive _self: (addr global-table), name: (addr array byte) {
   new-primitive-function curr-value-ah, curr-index
 }
 
-fn append-global _self: (addr global-table), name: (addr array byte), value: (handle cell) {
+fn append-global _self: (addr global-table), name: (addr array byte), value: (handle cell), trace: (addr trace) {
   var self/esi: (addr global-table) <- copy _self
+  {
+    var curr-index/ecx: int <- find-symbol-name-in-globals self, name
+    compare curr-index, -1/not-found
+    break-if-=
+    # otherwise error "global already exists: ", sym
+    var stream-storage: (stream byte 0x40)
+    var stream/ecx: (addr stream byte) <- address stream-storage
+    write stream, "global already exists: "
+    write stream, name
+    trace trace, "error", stream
+    return
+  }
   var final-index-addr/ecx: (addr int) <- get self, final-index
   increment *final-index-addr
   var curr-index/ecx: int <- copy *final-index-addr
@@ -368,6 +380,38 @@ fn find-symbol-in-globals _globals: (addr global-table), sym-name: (addr stream
   return -1/not-found
 }
 
+# return the index in globals containing 'sym'
+# or -1 if not found
+fn find-symbol-name-in-globals _globals: (addr global-table), sym-name: (addr array byte) -> _/ecx: int {
+  var globals/esi: (addr global-table) <- copy _globals
+  compare globals, 0
+  {
+    break-if-!=
+    return -1/not-found
+  }
+  var global-data-ah/eax: (addr handle array global) <- get globals, data
+  var global-data/eax: (addr array global) <- lookup *global-data-ah
+  var final-index/ecx: (addr int) <- get globals, final-index
+  var curr-index/ecx: int <- copy *final-index
+  {
+    compare curr-index, 0
+    break-if-<
+    var curr-offset/ebx: (offset global) <- compute-offset global-data, curr-index
+    var curr/ebx: (addr global) <- index global-data, curr-offset
+    var curr-name-ah/eax: (addr handle array byte) <- get curr, name
+    var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
+    var found?/eax: boolean <- string-equal? sym-name, curr-name
+    compare found?, 0/false
+    {
+      break-if-=
+      return curr-index
+    }
+    curr-index <- decrement
+    loop
+  }
+  return -1/not-found
+}
+
 fn mutate-binding-in-globals name: (addr stream byte), val: (addr handle cell), _globals: (addr global-table), trace: (addr trace) {
   var globals/esi: (addr global-table) <- copy _globals
   {