about summary refs log tree commit diff stats
path: root/shell
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-04-09 22:21:00 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-04-09 22:21:00 -0700
commitb3c6dddcd4b3c8579345050c67e6e00eff796c6e (patch)
treea37ec8ebdde1553ce81f4f7ca1d4bbe45d430aa9 /shell
parent630a7396c5626ce15f679754fa7b40f955b80483 (diff)
downloadmu-b3c6dddcd4b3c8579345050c67e6e00eff796c6e.tar.gz
shell: if
Diffstat (limited to 'shell')
-rw-r--r--shell/evaluate.mu41
1 files changed, 41 insertions, 0 deletions
diff --git a/shell/evaluate.mu b/shell/evaluate.mu
index 729fdfe9..00a75b9a 100644
--- a/shell/evaluate.mu
+++ b/shell/evaluate.mu
@@ -135,6 +135,47 @@ fn evaluate _in: (addr handle cell), out: (addr handle cell), env-h: (handle cel
     trace-higher trace
     return
   }
+  $evaluate:if: {
+    # trees starting with "if" are conditionals
+    var expr/esi: (addr cell) <- copy in-addr
+    # if its first elem is not "if", 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 if?/eax: boolean <- stream-data-equal? sym-data, "if"
+    compare if?, 0/false
+    break-if-=
+    #
+    trace-text trace, "eval", "if"
+    trace-text trace, "eval", "evaluating first arg"
+    var rest/eax: (addr cell) <- lookup *rest-ah
+    var first-arg-ah/ecx: (addr handle cell) <- get rest, left
+    var guard-h: (handle cell)
+    var guard-ah/esi: (addr handle cell) <- address guard-h
+    evaluate first-arg-ah, guard-ah, env-h, globals, trace
+    rest-ah <- get rest, right
+    rest <- lookup *rest-ah
+    var branch-ah/edi: (addr handle cell) <- get rest, left
+    var guard-a/eax: (addr cell) <- lookup *guard-ah
+    var skip-to-third-arg?/eax: boolean <- nil? guard-a
+    compare skip-to-third-arg?, 0/false
+    {
+      break-if-=
+      trace-text trace, "eval", "skipping to third arg"
+      var rest/eax: (addr cell) <- lookup *rest-ah
+      rest-ah <- get rest, right
+      rest <- lookup *rest-ah
+      branch-ah <- get rest, left
+    }
+    evaluate branch-ah, out, env-h, globals, trace
+    trace-higher trace
+    return
+  }
   trace-text trace, "eval", "function call"
   trace-text trace, "eval", "evaluating list elements"
   var evaluated-list-storage: (handle cell)