about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-08-21 00:57:57 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-08-21 00:57:57 -0700
commit689a480e1e0f7aca0f406bfad90a52dcef01d878 (patch)
tree16056e3425356b0831059e2e35c2e1e084265ef2
parent0c57bf0a1bfababf3f4b661c500f9ef7aae56776 (diff)
downloadmu-689a480e1e0f7aca0f406bfad90a52dcef01d878.tar.gz
61 - 'get' for array access
get _ 0 => retrieves array length
get _ n => retrieves index n-1
-rw-r--r--mu.arc19
-rw-r--r--mu.arc.t20
2 files changed, 34 insertions, 5 deletions
diff --git a/mu.arc b/mu.arc
index d5d1484f..e88c4825 100644
--- a/mu.arc
+++ b/mu.arc
@@ -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))
diff --git a/mu.arc.t b/mu.arc.t
index a7003c6e..a385164a 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -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
@@ -346,6 +349,23 @@
 (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
       ((1 integer) <- literal 34)
       ((2 boolean) <- literal nil)
       ((4 boolean) <- literal t)