6 - compound functions now return values

This commit is contained in:
Kartik K. Agaram 2014-07-06 02:34:03 -07:00
parent 4363daba1f
commit ceb4710530
2 changed files with 33 additions and 6 deletions

16
mu.arc
View File

@ -12,11 +12,13 @@
(each instr instrs
(unless returned
;? (prn instr)
;? (prn memory*)
(let delim (or (pos '<- instr) -1)
(with (oarg (cut instr 0 delim)
(with (oarg (if (>= delim 0)
(cut instr 0 delim))
op (instr (+ delim 1))
arg (cut instr (+ delim 2)))
;? (prn op)
;? (prn op " " oarg)
(case op
loadi
(= (memory* oarg.0) arg.0)
@ -28,12 +30,16 @@
; hardcoded channel for now
(memory* pop.fn-args))
return
(set returned)
(= returned (annotate 'result arg))
; else user-defined function
(run function*.op arg)
(let results (run function*.op arg)
;? (prn "== " memory*)
(each o oarg
;? (prn o)
(= memory*.o (memory* pop.results))))
)))))
;? (prn "return")
)
rep.returned)
(awhen cdr.argv
(each file it

View File

@ -36,6 +36,7 @@
;? (prn memory*)
(if (~iso memory* (obj 1 1 2 3 3 4))
(prn "F - early return works"))
;? (quit)
(clear)
(add-fns
@ -48,10 +49,30 @@
(main
(1 <- loadi 1)
(2 <- loadi 3)
(add-fn 1 2))))
(add-fn 1 2)
)))
(run function*!main)
;? (prn memory*)
(if (~iso memory* (obj 1 1 2 3 3 4
; add-fn's temporaries
4 1 5 3))
(prn "F - parameterized compound fn"))
(clear)
(add-fns
'((add-fn
(4 <- read)
(5 <- read)
(6 <- add 4 5)
(return 6)
(4 <- loadi 34))
(main
(1 <- loadi 1)
(2 <- loadi 3)
(3 <- add-fn 1 2))))
(run function*!main)
;? (prn memory*)
(if (~iso memory* (obj 1 1 2 3 3 4
; add-fn's temporaries
4 1 5 3 6 4))
(prn "F - parameterized compound fn with return value"))