about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-10-05 11:34:23 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-10-05 11:34:23 -0700
commit35366e91d911295c60de5be873be0f5d59f32478 (patch)
treed45ccdcaf339d7c4e7475484e9e0898d83e1cdeb
parent5858585951b145b2a4fe0e691b7dffbe47c49f98 (diff)
downloadmu-35366e91d911295c60de5be873be0f5d59f32478.tar.gz
104 - writing to fields/indices
-rw-r--r--mu.arc20
-rw-r--r--mu.arc.t25
2 files changed, 45 insertions, 0 deletions
diff --git a/mu.arc b/mu.arc
index 8379f778..cbbb1839 100644
--- a/mu.arc
+++ b/mu.arc
@@ -133,6 +133,13 @@
 (def array-len (operand)
   (m `(,v.operand integer)))
 
+(def array-ref-addr (operand idx)
+  (assert typeinfo.operand!array)
+  (assert (< -1 idx (array-len operand)))
+  (withs (elem  typeinfo.operand!elem
+          offset  (+ 1 (* idx sz.elem)))
+    (+ v.operand offset)))
+
 (def array-ref (operand idx)
   (assert typeinfo.operand!array)
   (assert (< -1 idx (array-len operand)))
@@ -291,6 +298,19 @@
                                  ,typeinfo.base!elems.idx)))
                       :else
                         (assert nil "get on invalid type @base")))
+                get-address
+                  (with (base arg.0
+                         idx (v arg.1))
+                    (if typeinfo.base!array
+                          (array-ref-addr base idx)
+                        typeinfo.base!record
+                          (do (assert (< -1 idx (len typeinfo.base!elems)))
+                              (+ v.base
+                                 (apply + (map sz
+                                               (firstn idx typeinfo.base!elems)))))
+                        :else
+                          (assert nil "get-address on invalid type @base")))
+
                 new
                   (let type (v arg.0)
                     (if types*.type!array
diff --git a/mu.arc.t b/mu.arc.t
index 829eb8ca..9f7365c9 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -411,6 +411,17 @@
 (reset)
 (add-fns
   '((main
+      ((1 integer) <- literal 34)
+      ((2 integer) <- literal t)
+      ((3 integer-boolean-pair-address) <- get-address (1 integer-point-pair) (1 offset)))))
+(run 'main)
+;? (prn memory*)
+(if (~iso memory* (obj 1 34  2 t  3 2))
+  (prn "F - 'get-address' returns address of fields of records"))
+
+(reset)
+(add-fns
+  '((main
       ((1 integer) <- literal 2)
       ((2 integer) <- literal 23)
       ((3 boolean) <- literal nil)
@@ -430,6 +441,20 @@
       ((3 boolean) <- literal nil)
       ((4 integer) <- literal 24)
       ((5 boolean) <- literal t)
+      ((6 integer-boolean-pair-address) <- get-address (1 integer-boolean-pair-array) (1 offset)))))
+(run 'main)
+;? (prn memory*)
+(if (~iso memory* (obj 1 2  2 23 3 nil  4 24 5 t  6 4))
+  (prn "F - 'get-address' returns addresses of indices of arrays"))
+
+(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) <- len (1 integer-boolean-pair-array)))))
 (run 'main)
 ;? (prn memory*)