61 - 'get' for array access

get _ 0 => retrieves array length
get _ n => retrieves index n-1
This commit is contained in:
Kartik K. Agaram 2014-08-21 00:57:57 -07:00
parent 0c57bf0a1b
commit 689a480e1e
2 changed files with 34 additions and 5 deletions

19
mu.arc
View File

@ -18,6 +18,7 @@
block-address (obj size 1 address t elem 'block)
integer-boolean-pair (obj size 2 record t elems '(integer boolean))
integer-boolean-pair-address (obj size 1 address t elem 'integer-boolean-pair)
integer-boolean-pair-array (obj vector t elem 'integer-boolean-pair)
))
(= memory* (table))
(= function* (table)))
@ -152,11 +153,19 @@
copy
(m arg.0)
get
(withs (idx (v arg.1)
fields ((types* (ty arg.0)) 'elems)
offset (apply +
(map sz (firstn idx fields))))
(memory* (+ (v arg.0) offset)))
(if ((types* (ty arg.0)) 'vector)
(if (is 0 (v arg.1))
(m `(,(v arg.0) integer))
(withs (elem ((types* (ty arg.0)) 'elem)
offset (+ (* (- (v arg.1) 1)
sz.elem)
1))
(m `(,(+ (v arg.0) offset) ,elem))))
(withs (idx (v arg.1)
fields ((types* (ty arg.0)) 'elems)
offset (apply +
(map sz (firstn idx fields))))
(memory* (+ (v arg.0) offset))))
reply
(do (= result arg)
(break))

View File

@ -90,6 +90,9 @@
(prn "F - 'arg' with index can access function call arguments out of order"))
;? (quit)
; todo: test that too few args throws an error
; how should errors be handled? will be unclear until we support concurrency and routine trees.
(reset)
(add-fns
'((test1
@ -343,6 +346,23 @@
(if (~iso memory* (obj 1 34 2 nil 3 nil 4 34))
(prn "F - 'get' accesses fields of records"))
(reset)
(add-fns
'((main
((1 integer) <- literal 2)
((2 integer) <- literal 23)
((3 boolean) <- literal nil)
((4 integer) <- literal 24)
((5 boolean) <- literal t)
((6 integer) <- get (1 integer-boolean-pair-array) (0 offset))
((7 integer-boolean-pair) <- get (1 integer-boolean-pair-array) (1 offset)))))
(run function*!main)
;? (prn memory*)
(if (~iso memory* (obj 1 2 2 23 3 nil 4 24 5 t 6 2 7 23 8 nil))
(prn "F - 'get' accesses fields of arrays"))
; todo: test that out-of-bounds access throws an error
(reset)
(add-fns
'((main