about summary refs log tree commit diff stats
path: root/mu.arc.t
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-07-11 22:26:19 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-07-11 22:26:19 -0700
commit368e76cdf685e589f807755bdd39a31e8ccd2c83 (patch)
tree1d476d80f443a2743d43ce3482b444570764a98f /mu.arc.t
parentbaccb5d569ddb9d835b1b837ab3139abe7bbde8d (diff)
downloadmu-368e76cdf685e589f807755bdd39a31e8ccd2c83.tar.gz
19 - example function with type-based dispatch
I imagined we could just push new clauses at the top, but that isn't realistic;
it would mess up all the jump locations.
Either we need to append clauses, or we need some sort of relative adddressing
for locations. Can't think of a third idea at the moment.
Appending clauses is fine as long as functions are restrictive about what they
accept.
Diffstat (limited to 'mu.arc.t')
-rw-r--r--mu.arc.t32
1 files changed, 29 insertions, 3 deletions
diff --git a/mu.arc.t b/mu.arc.t
index 5bca40b5..d35bddf0 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -180,7 +180,8 @@
       ((integer 1) <- loadi 8)
       (jmp (location 3))
       ((integer 2) <- loadi 3)
-      (reply))))
+      (reply)
+      ((integer 3) <- loadi 34))))
 (run function*!main)
 ;? (prn memory*)
 (if (~iso memory* (obj 1 8))
@@ -192,7 +193,8 @@
       ((integer 1) <- loadi 0)
       (jifz (integer 1) (location 3))
       ((integer 2) <- loadi 3)
-      (reply))))
+      (reply)
+      ((integer 3) <- loadi 34))))
 (run function*!main)
 ;? (prn memory*)
 (if (~iso memory* (obj 1 0))
@@ -204,8 +206,32 @@
       ((integer 1) <- loadi 1)
       (jifz (integer 1) (location 3))
       ((integer 2) <- loadi 3)
-      (reply))))
+      (reply)
+      ((integer 3) <- loadi 34))))
 (run function*!main)
 ;? (prn memory*)
 (if (~iso memory* (obj 1 1  2 3))
   (prn "F - jifz works - 2"))
+
+(clear)
+(add-fns
+  '((add-fn
+      ((integer 4) <- otype 0)
+      ((integer 5) <- loadi 0)  ; type index corresponding to 'integer'
+      ((integer 6) <- sub (integer 4) (integer 5))
+      (jifz (integer 6) (location 5))
+      (reply)
+      ((integer 7) <- arg)
+      ((integer 8) <- arg)
+      ((integer 9) <- add (integer 7) (integer 8))
+      (reply (integer 9)))
+    (main
+      ((integer 1) <- loadi 1)
+      ((integer 2) <- loadi 3)
+      ((integer 3) <- add-fn (integer 1) (integer 2)))))
+(run function*!main)
+;? (prn memory*)
+(if (~iso memory* (obj 1 1  2 3                     3 4
+                         ; add-fn's temporaries
+                         4 0  5 0  6 0  7 1  8 3  9 4))
+  (prn "F - user-defined function with clauses"))