e9301a71768d — Leonard Ritter 23 days ago
* pointers can be explicitly cast to intptr and back
3 files changed, 35 insertions(+), 0 deletions(-)

M lib/scopes/core.sc
M testing/test_all.sc
A => testing/test_pointer.sc
M lib/scopes/core.sc +8 -0
@@ 2708,12 2708,20 @@ fn pointer-as (vT T)
         if (icmp== ('kind ST) type-kind-pointer)
             if (icmp== ('pointer-storage-class vT) ('pointer-storage-class T))
                 return `(inline (self) (bitcast self T))
+    elseif (type== T intptr)
+        return `(inline (self) (ptrtoint self T))
+    `()
+
+fn pointer-ras (T vT)
+    if (type== T intptr)
+        return `(inline (self) (inttoptr self vT))
     `()
 
 'set-symbols pointer
     __call = coerce-call-arguments
     __imply = (box-pointer (spice-cast-macro pointer-imply))
     __as = (box-pointer (spice-cast-macro pointer-as))
+    __ras = (box-pointer (spice-cast-macro pointer-ras))
 
 # dotted symbol expander
 # --------------------------------------------------------------------------

          
M testing/test_all.sc +1 -0
@@ 66,6 66,7 @@ test-modules
     .test_option
     .test_overload
     .test_parser
+    .test_pointer
     .test_property
     .test_quote
     .test_rc

          
A => testing/test_pointer.sc +26 -0
@@ 0,0 1,26 @@ 
+
+using import testing
+
+# testing pointer cast
+
+local value = 1
+local value2 = 2
+
+# reference to pointer
+let value = &value
+
+# implicitly convert stack pointer to universal/heap pointer
+let value = (imply value @i32)
+# implicitly convert pointer to void pointer
+let anyvalue = (imply value voidstar)
+
+# explicitly convert pointer to another pointer
+value := value as @f32
+
+# explicitly convert pointer to intptr and back
+ivalue := value as intptr
+vvalue := ivalue as voidstar
+test (vvalue == value)
+test (vvalue != &value2)
+
+;
  No newline at end of file