about summary refs log tree commit diff stats
path: root/shell/eval.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-03-07 08:53:31 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-03-07 08:53:31 -0800
commit410782c79e66fa97c44f487cef0400632edbbff1 (patch)
tree090a7460ade5cb736f8f1f12c8e88ab7730900c7 /shell/eval.mu
parent477e58f0b27ad622a6fb2486a167b132389c1f44 (diff)
downloadmu-410782c79e66fa97c44f487cef0400632edbbff1.tar.gz
7861 - shell: anonymous fn calls without args
((fn () (+ 1 1)))
Diffstat (limited to 'shell/eval.mu')
-rw-r--r--shell/eval.mu31
1 files changed, 30 insertions, 1 deletions
diff --git a/shell/eval.mu b/shell/eval.mu
index 9a338a1f..813b1b87 100644
--- a/shell/eval.mu
+++ b/shell/eval.mu
@@ -114,7 +114,36 @@ fn apply _f-ah: (addr handle cell), args-ah: (addr handle cell), out: (addr hand
   error trace, "unknown function"
 }
 
-fn apply-function _params-ah: (addr handle cell), _args-ah: (addr handle cell), _body-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), trace: (addr trace) {
+fn apply-function params-ah: (addr handle cell), args-ah: (addr handle cell), _body-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), trace: (addr trace) {
+  # push bindings for params to env
+  var new-env-storage: (handle cell)
+  var new-env-ah/esi: (addr handle cell) <- address new-env-storage
+  push-bindings params-ah, args-ah, env-h, new-env-ah, trace
+  # eval all expressions in body, writing result to `out` each time
+  var body-ah/ecx: (addr handle cell) <- copy _body-ah
+  $apply-function:body: {
+    var body/eax: (addr cell) <- lookup *body-ah
+    # stop when body is nil
+    {
+      var body-is-nil?/eax: boolean <- is-nil? body
+      compare body-is-nil?, 0/false
+      break-if-!= $apply-function:body
+    }
+    # evaluate each expression, writing result to `out`
+    {
+      var curr-ah/eax: (addr handle cell) <- get body, left
+      evaluate curr-ah, out, *new-env-ah, trace
+    }
+    #
+    body-ah <- get body, right
+    loop
+  }
+  # `out` contains result of evaluating final expression
+}
+
+fn push-bindings _params-ah: (addr handle cell), _args-ah: (addr handle cell), old-env-h: (handle cell), env-ah: (addr handle cell), trace: (addr trace) {
+  # no bindings for now
+  copy-handle old-env-h, env-ah
 }
 
 fn apply-primitive _f: (addr cell), args-ah: (addr handle cell), out: (addr handle cell), env-h: (handle cell), trace: (addr trace) {