about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--shell/evaluate.mu47
-rw-r--r--shell/global.mu15
2 files changed, 62 insertions, 0 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu
index 4066db20..729fdfe9 100644
--- a/shell/evaluate.mu
+++ b/shell/evaluate.mu
@@ -88,6 +88,53 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     trace-higher trace
     return
   }
+  $evaluate:set: {
+    # trees starting with "set" define globals
+    var expr/esi: (addr cell) <- copy in-addr
+    # if its first elem is not "set", break
+    var first-ah/ecx: (addr handle cell) <- get in-addr, left
+    var rest-ah/edx: (addr handle cell) <- get in-addr, right
+    var first/eax: (addr cell) <- lookup *first-ah
+    var first-type/ecx: (addr int) <- get first, type
+    compare *first-type, 2/symbol
+    break-if-!=
+    var sym-data-ah/eax: (addr handle stream byte) <- get first, text-data
+    var sym-data/eax: (addr stream byte) <- lookup *sym-data-ah
+    var set?/eax: boolean <- stream-data-equal? sym-data, "set"
+    compare set?, 0/false
+    break-if-=
+    #
+    trace-text trace, "eval", "set"
+    trace-text trace, "eval", "evaluating second arg"
+    var rest/eax: (addr cell) <- lookup *rest-ah
+    var first-arg-ah/ecx: (addr handle cell) <- get rest, left
+    {
+      var first-arg/eax: (addr cell) <- lookup *first-arg-ah
+      var first-arg-type/eax: (addr int) <- get first-arg, type
+      compare *first-arg-type, 2/symbol
+      break-if-=
+      error trace, "first arg to set must be a symbol"
+      trace-higher trace
+      return
+    }
+    rest-ah <- get rest, right
+    rest <- lookup *rest-ah
+    var second-arg-ah/edx: (addr handle cell) <- get rest, left
+    evaluate second-arg-ah, out, env-h, globals, trace
+    trace-text trace, "eval", "saving global binding"
+    var first-arg/eax: (addr cell) <- lookup *first-arg-ah
+    var first-arg-data-ah/eax: (addr handle stream byte) <- get first-arg, text-data
+    var first-arg-data/eax: (addr stream byte) <- lookup *first-arg-data-ah
+    var tmp-string: (handle array byte)
+    var tmp-ah/edx: (addr handle array byte) <- address tmp-string
+    rewind-stream first-arg-data
+    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
+    trace-higher trace
+    return
+  }
   trace-text trace, "eval", "function call"
   trace-text trace, "eval", "evaluating list elements"
   var evaluated-list-storage: (handle cell)
diff --git a/shell/global.mu b/shell/global.mu
index 5204ffc6..c87198ee 100644
--- a/shell/global.mu
+++ b/shell/global.mu
@@ -37,6 +37,21 @@ 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) {
+  var self/esi: (addr global-table) <- copy _self
+  var final-index-addr/ecx: (addr int) <- get self, final-index
+  increment *final-index-addr
+  var curr-index/ecx: int <- copy *final-index-addr
+  var data-ah/eax: (addr handle array global) <- get self, data
+  var data/eax: (addr array global) <- lookup *data-ah
+  var curr-offset/esi: (offset global) <- compute-offset data, curr-index
+  var curr/esi: (addr global) <- index data, curr-offset
+  var curr-name-ah/eax: (addr handle array byte) <- get curr, name
+  copy-array-object name, curr-name-ah
+  var curr-value-ah/eax: (addr handle cell) <- get curr, value
+  copy-handle value, curr-value-ah
+}
+
 fn lookup-symbol-in-globals _sym: (addr cell), out: (addr handle cell), _globals: (addr global-table), trace: (addr trace) {
   var sym/eax: (addr cell) <- copy _sym
   var sym-data-ah/eax: (addr handle stream byte) <- get sym, text-data
committer Kartik K. Agaram <vc@akkartik.com> 2021-03-26 23:07:35 -0700 explicitly pass screen and keyboard to main' href='/akkartik/mu/commit/ex7.mu?h=main&id=1a43d12b15c11c1fb1686369665f48d87f350f37'>1a43d12b ^
c1e841fc ^
328d76e4 ^

74f1512f ^
328d76e4 ^
1a43d12b ^
c1e841fc ^
328d76e4 ^

74f1512f ^
328d76e4 ^
08c55cb2 ^
1a43d12b ^
c1e841fc ^
328d76e4 ^

ec0d5bb1 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46