about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-07-06 01:57:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-07-06 01:57:47 -0700
commit8fafaf8982e3262409ee87b977ea04034334dd04 (patch)
tree90459683ab74eda760ab2293ae7aa3f8c1aec519
parenta1fe0ff3c70de1acc3a34d7afaacf362b4fbb7ca (diff)
downloadmu-8fafaf8982e3262409ee87b977ea04034334dd04.tar.gz
3 - early return
-rw-r--r--mu.arc29
-rw-r--r--mu.arc.t15
2 files changed, 31 insertions, 13 deletions
diff --git a/mu.arc b/mu.arc
index 204d0b74..87c69eaf 100644
--- a/mu.arc
+++ b/mu.arc
@@ -8,20 +8,23 @@
   (each (name . body) fns
     (= function*.name body)))
 
-(def run (instrs)
+(def run (instrs (o returned))
   (each instr instrs
-;?     (prn instr)
-    (let (oarg1 <- op arg1 arg2) instr
-;?       (prn op)
-      (case op
-        loadi
-          (= memory*.oarg1 arg1)
-        add
-          (= memory*.oarg1
-             (+ memory*.arg1 memory*.arg2))
-        ; else user-defined function
-          (run function*.op)
-        )))
+    (unless returned
+;?       (prn instr)
+      (let (oarg1 <- op arg1 arg2) instr
+;?         (prn op)
+        (case op
+          loadi
+            (= memory*.oarg1 arg1)
+          add
+            (= memory*.oarg1
+               (+ memory*.arg1 memory*.arg2))
+          return
+            (set returned)
+          ; else user-defined function
+            (run function*.op)
+          ))))
 ;?   (prn "return")
   )
 
diff --git a/mu.arc.t b/mu.arc.t
index be4075ec..ff63ebdc 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -21,3 +21,18 @@
 ;? (prn memory*)
 (if (~iso memory* (obj 1 1  2 3  3 4))
   (prn "F - compound functions work"))
+
+(clear)
+(add-fns
+  '((add-fn
+      (3 <- add 1 2)
+      (_ <- return)
+      (4 <- loadi 34))
+    (main
+      (1 <- loadi 1)
+      (2 <- loadi 3)
+      (_ <- add-fn))))
+(run function*!main)
+;? (prn memory*)
+(if (~iso memory* (obj 1 1  2 3  3 4))
+  (prn "F - early return works"))