From: NeilBrown Date: Sat, 16 Oct 2021 05:09:59 +0000 (+1100) Subject: oceani: implement struct field initialisation properly. X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=f69af714036e7f24be0f5a8f6e4a054f35cefe9d oceani: implement struct field initialisation properly. I wasn't testing default initial values for struct fields, so of course it didn't work. Signed-off-by: NeilBrown --- diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index e7e9f22..d5541e9 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -555,7 +555,7 @@ Time to test if structure declarations and accesses work correctly. struct foo size:[three]number name:string - active:Boolean + active:Boolean = True struct baz { a:number; b:Boolean; } @@ -571,7 +571,8 @@ Time to test if structure declarations and accesses work correctly. info[i].name = nm info[i].size[0] = i*i - info[i].active = nm == "jane" + if nm != "jane": + info[i].active = False for i:=0; then i=i+1; while i < 4: print info[i].name, info[i].active, info[i].size[0] diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index 18e5930..416084e 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -1758,7 +1758,12 @@ function will be needed. for (i = 0; i < type->structure.nfields; i++) { struct value *v; v = (void*) val->ptr + type->structure.fields[i].offset; - val_init(type->structure.fields[i].type, v); + if (type->structure.fields[i].init) + dup_value(type->structure.fields[i].type, + type->structure.fields[i].init, + v); + else + val_init(type->structure.fields[i].type, v); } }