From: NeilBrown Date: Sat, 11 Dec 2021 23:42:48 +0000 (+1100) Subject: oceani: allow field references on references. X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=6e9a6bc829c370f60148deb155c3952edc6895f2 oceani: allow field references on references. If a ref (pointer) is asked for a field, pass the request on to the thing that is pointed to. Signed-off-by: NeilBrown --- diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index d672cff..655a2c9 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -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. diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index e1a2cb0..1e2bfde 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -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*), };