]> ocean-lang.org Git - ocean/commitdiff
oceani: allow field references on references.
authorNeilBrown <neil@brown.name>
Sat, 11 Dec 2021 23:42:48 +0000 (10:42 +1100)
committerNeilBrown <neil@brown.name>
Sat, 11 Dec 2021 23:42:48 +0000 (10:42 +1100)
If a ref (pointer) is asked for a field, pass the request on to the
thing that is pointed to.

Signed-off-by: NeilBrown <neil@brown.name>
csrc/oceani-tests.mdc
csrc/oceani.mdc

index d672cffc8a97d92b0edb8cc590feb052d5101e8d..655a2c95057a3cc686ddac87003acc10bbb88e91 100644 (file)
@@ -730,17 +730,17 @@ A simple linked list example
        func insert(list:@node; new:string):@node
                p:=list
                prev := @nil
-               while ?p and then p@.this < new:
+               while ?p and then p.this < new:
                        prev = p
-                       p = p@.next
+                       p = p.next
                if ?prev:
-                       prev@.next = @new()
-                       prev@.next@.next = p
-                       prev@.next@.this = new
+                       prev.next = @new()
+                       prev.next.next = p
+                       prev.next.this = new
                else
                        list = @new()
-                       list@.next = p
-                       list@.this = new
+                       list.next = p
+                       list.this = new
                use list
 
        func printlist(list:@node)
@@ -750,7 +750,7 @@ A simple linked list example
 
        func freelist(list:@node)
                if list != @nil:
-                       freelist(list@.next)
+                       freelist(list.next)
                        @free = list
 
        func main(argv:[ac::]string)
@@ -1195,7 +1195,7 @@ Test for errors with references
                ref2:@string
                num:number = @new()
                print num@
-               if num == @nil or ref == ref2 or ref == 2:
+               if num == @nil or ref == ref2 or ref == 2 or ref.foo:
                        @free = num
 
 ###### output: ref_err2
@@ -1206,6 +1206,8 @@ Test for errors with references
        .tmp.code:7:33: error: expected @number but variable 'ref2' is @string
        .tmp.code:4:8: info: this is where 'ref2' was set to @string
        .tmp.code:7:48: error: expected @number found number
+       .tmp.code:7:53: error: field reference on number is not supported
+       .tmp.code:7:56: error: have none but need Boolean
        .tmp.code:8:17: error: @free can only be assigned a reference, not number
        .tmp.code:8:17: error: @free can only be assigned a reference, not number
        oceani: type error in program - not running.
index e1a2cb076b49c43fc8c5dd95dc2e822929ff875c..1e2bfde472dc7ac727cb7ee2ab14137c6c8794e5 100644 (file)
@@ -3053,6 +3053,22 @@ anything in the heap or on the stack.  A reference can be assigned
                return val->ref != NULL;
        }
 
+       static struct type *reference_fieldref(struct type *t, struct parse_context *c,
+                                              struct fieldref *f, struct value **vp)
+       {
+               struct type *rt = t->reference.referent;
+
+               if (rt->fieldref) {
+                       if (vp)
+                               *vp = (*vp)->ref;
+                       return rt->fieldref(rt, c, f, vp);
+               }
+               type_err(c, "error: field reference on %1 is not supported",
+                                f->left, rt, 0, NULL);
+               return Tnone;
+       }
+
+
        static struct type reference_prototype = {
                .print_type = reference_print_type,
                .cmp_eq = reference_cmp,
@@ -3060,6 +3076,7 @@ anything in the heap or on the stack.  A reference can be assigned
                .test = reference_test,
                .free = reference_free,
                .compat = reference_compat,
+               .fieldref = reference_fieldref,
                .size = sizeof(void*),
                .align = sizeof(void*),
        };