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.
This commit is contained in:
parent
baccb5d569
commit
368e76cdf6
15
mu.arc
15
mu.arc
|
@ -9,6 +9,9 @@
|
||||||
(= function* (table)))
|
(= function* (table)))
|
||||||
(clear)
|
(clear)
|
||||||
|
|
||||||
|
; just a convenience until we get an assembler
|
||||||
|
(= type* (obj integer 0 location 1 address 2))
|
||||||
|
|
||||||
(mac aelse (test else . body)
|
(mac aelse (test else . body)
|
||||||
`(aif ,test
|
`(aif ,test
|
||||||
(do ,@body)
|
(do ,@body)
|
||||||
|
@ -37,8 +40,10 @@
|
||||||
loadi
|
loadi
|
||||||
(= (memory* oarg.0.1) arg.0)
|
(= (memory* oarg.0.1) arg.0)
|
||||||
add
|
add
|
||||||
|
;? (do (prn "add " arg.0.1 arg.1.1)
|
||||||
(= (memory* oarg.0.1)
|
(= (memory* oarg.0.1)
|
||||||
(+ (memory* arg.0.1) (memory* arg.1.1)))
|
(+ (memory* arg.0.1) (memory* arg.1.1)))
|
||||||
|
;? (prn "add2"))
|
||||||
sub
|
sub
|
||||||
(= (memory* oarg.0.1)
|
(= (memory* oarg.0.1)
|
||||||
(- (memory* arg.0.1) (memory* arg.1.1)))
|
(- (memory* arg.0.1) (memory* arg.1.1)))
|
||||||
|
@ -60,13 +65,17 @@
|
||||||
++.fn-arg-idx))
|
++.fn-arg-idx))
|
||||||
(= (memory* oarg.0.1)
|
(= (memory* oarg.0.1)
|
||||||
(memory* fn-args.idx.1)))
|
(memory* fn-args.idx.1)))
|
||||||
|
otype
|
||||||
|
(= (memory* oarg.0.1)
|
||||||
|
(type* (otypes arg.0)))
|
||||||
jmp
|
jmp
|
||||||
(do (= pc arg.0.1)
|
(do (= pc (- arg.0.1 1)) ; because continue still increments (bug)
|
||||||
;? (prn "jumping to " pc)
|
;? (prn "jumping to " pc)
|
||||||
(continue))
|
(continue))
|
||||||
jifz
|
jifz
|
||||||
(when (is 0 (memory* arg.0.1))
|
(when (is 0 (memory* arg.0.1))
|
||||||
(= pc arg.1.1)
|
;? (prn "jumping to " arg.1.1)
|
||||||
|
(= pc (- arg.1.1 1)) ; because continue still increments (bug)
|
||||||
(continue))
|
(continue))
|
||||||
reply
|
reply
|
||||||
(do (= result arg)
|
(do (= result arg)
|
||||||
|
@ -74,7 +83,7 @@
|
||||||
; else user-defined function
|
; else user-defined function
|
||||||
(aelse function*.op (prn "no definition for " op)
|
(aelse function*.op (prn "no definition for " op)
|
||||||
;? (prn "== " memory*)
|
;? (prn "== " memory*)
|
||||||
(let results (run it arg)
|
(let results (run it arg (map car oarg))
|
||||||
(each o oarg
|
(each o oarg
|
||||||
;? (prn o)
|
;? (prn o)
|
||||||
(= (memory* o.1) (memory* pop.results.1)))))
|
(= (memory* o.1) (memory* pop.results.1)))))
|
||||||
|
|
32
mu.arc.t
32
mu.arc.t
|
@ -180,7 +180,8 @@
|
||||||
((integer 1) <- loadi 8)
|
((integer 1) <- loadi 8)
|
||||||
(jmp (location 3))
|
(jmp (location 3))
|
||||||
((integer 2) <- loadi 3)
|
((integer 2) <- loadi 3)
|
||||||
(reply))))
|
(reply)
|
||||||
|
((integer 3) <- loadi 34))))
|
||||||
(run function*!main)
|
(run function*!main)
|
||||||
;? (prn memory*)
|
;? (prn memory*)
|
||||||
(if (~iso memory* (obj 1 8))
|
(if (~iso memory* (obj 1 8))
|
||||||
|
@ -192,7 +193,8 @@
|
||||||
((integer 1) <- loadi 0)
|
((integer 1) <- loadi 0)
|
||||||
(jifz (integer 1) (location 3))
|
(jifz (integer 1) (location 3))
|
||||||
((integer 2) <- loadi 3)
|
((integer 2) <- loadi 3)
|
||||||
(reply))))
|
(reply)
|
||||||
|
((integer 3) <- loadi 34))))
|
||||||
(run function*!main)
|
(run function*!main)
|
||||||
;? (prn memory*)
|
;? (prn memory*)
|
||||||
(if (~iso memory* (obj 1 0))
|
(if (~iso memory* (obj 1 0))
|
||||||
|
@ -204,8 +206,32 @@
|
||||||
((integer 1) <- loadi 1)
|
((integer 1) <- loadi 1)
|
||||||
(jifz (integer 1) (location 3))
|
(jifz (integer 1) (location 3))
|
||||||
((integer 2) <- loadi 3)
|
((integer 2) <- loadi 3)
|
||||||
(reply))))
|
(reply)
|
||||||
|
((integer 3) <- loadi 34))))
|
||||||
(run function*!main)
|
(run function*!main)
|
||||||
;? (prn memory*)
|
;? (prn memory*)
|
||||||
(if (~iso memory* (obj 1 1 2 3))
|
(if (~iso memory* (obj 1 1 2 3))
|
||||||
(prn "F - jifz works - 2"))
|
(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"))
|
||||||
|
|
Loading…
Reference in New Issue