156 - new primitive for lifting into tagged-types: 'save-type'

This commit is contained in:
Kartik K. Agaram 2014-10-24 11:38:02 -07:00
parent 4a0751d80b
commit b046ed735b
2 changed files with 19 additions and 1 deletions

4
mu.arc
View File

@ -371,6 +371,10 @@
array-len.base
-1))
; tagged-values require one primitive
save-type
(annotate 'record `(,(ty arg.0) ,(v arg.0)))
; multiprocessing
run
(run (v arg.0))

View File

@ -638,7 +638,11 @@
; A special kind of record is the 'tagged type'. It lets us represent
; dynamically typed values, which save type information in memory rather than
; in the code to use them. This will let us do things like create heterogenous
; lists containing both integers and strings.
; lists containing both integers and strings. Tagged values admit two
; operations:
;
; 'save-type' - turns a regular value into a tagged-value of the appropriate type
; 'maybe-coerce' - turns a tagged value into a regular value if the type matches
(reset)
(new-trace "tagged-value")
@ -666,6 +670,16 @@
(if (or (~is memory*.3 0) (~is memory*.4 nil))
(prn "F - 'maybe-coerce' doesn't copy value when type tag doesn't match"))
(reset)
(new-trace "save-type")
(add-fns
'((main
((1 tagged-value) <- save-type (34 integer-address))))) ; pointer to nowhere
(run 'main)
;? (prn memory*)
(if (~iso memory* (obj 1 'integer-address 2 34))
(prn "F - 'save-type' saves the type of a value at runtime, turning it into a tagged-value"))
(reset)
(new-trace "new-tagged-value")
;? (set dump-trace*)