about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-11-28 16:49:11 -0800
committerKartik K. Agaram <vc@akkartik.com>2014-11-28 16:49:11 -0800
commit6831aaabb98a0b52516c1e83f6b9d25e00505555 (patch)
treeb3d87a1cdef0ac434f94217ca5da5433ef5ac7b4
parent1f56c99181f31314b26757307170fca0c9808cf3 (diff)
downloadmu-6831aaabb98a0b52516c1e83f6b9d25e00505555.tar.gz
360 - back up, let's create a new 'deref' helper
-rw-r--r--mu.arc13
-rw-r--r--mu.arc.t8
2 files changed, 21 insertions, 0 deletions
diff --git a/mu.arc b/mu.arc
index 5c1b0455..6f214be1 100644
--- a/mu.arc
+++ b/mu.arc
@@ -703,6 +703,19 @@
         :else
           (err "sizeof can't handle @type (arrays require a specific variable)")))))
 
+(def deref (operand)
+  (assert (pos 'deref metadata.operand))
+  (assert typeinfo.operand!address)
+  (apply list (memory* v.operand)
+              typeinfo.operand!elem
+              (drop-one 'deref (cut operand 2))))
+
+(def drop-one (f x)
+  (when acons.x  ; proper lists only
+    (if (testify.f car.x)
+      cdr.x
+      (cons car.x (drop-one f x)))))
+
 ;; desugar structured assembly based on blocks
 
 (def convert-braces (instrs)
diff --git a/mu.arc.t b/mu.arc.t
index 0c85f4a8..87926a9e 100644
--- a/mu.arc.t
+++ b/mu.arc.t
@@ -743,6 +743,14 @@
 (if (~is 23 (addr '(4 integer-address deref)))
   (prn "F - 'addr' adds default-scope before 'deref', not after"))
 
+; unit tests for 'deref' helper
+(reset)
+(= memory*.3 4)
+(if (~iso '(4 integer) (deref '(3 integer-address deref)))
+  (prn "F - 'deref' handles simple addresses"))
+(if (~iso '(4 integer deref) (deref '(3 integer-address deref deref)))
+  (prn "F - 'deref' deletes just one deref"))
+
 ; unit tests for 'sizeof' helper
 (reset)
 (if (~is 1 sizeof!integer)