From 5b077f5f80cd36fed3b006dd5b0b1cc9025c1b22 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 9 Jun 2019 15:55:21 +1000 Subject: [PATCH] oceani: labels only in 'use' statement. A variable is only set as a label if it appear at top level of "use" statement - otherwise it is "unknown". This makes error messages more meaningful. Signed-off-by: NeilBrown --- csrc/oceani-tests.mdc | 12 +++++------- csrc/oceani.mdc | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index a10d027..5366bf9 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -746,7 +746,8 @@ various places that `type_err()` are called. .tmp.code:28:16: error: expected string found Boolean .tmp.code:29:12: error: have number but need string .tmp.code:7:8: info: variable 'c' was set as string here. - .tmp.code:32:8: error: field reference attempted on label, not a struct + .tmp.code:32:8: error: variable used but not declared: foo + .tmp.code:32:8: error: field reference attempted on none, not a struct .tmp.code:32:16: error: expected none found number .tmp.code:33:14: error: field reference attempted on string, not a struct oceani: type error in program - not running. @@ -757,12 +758,9 @@ various places that `type_err()` are called. print a, b, c ###### output: type_err4 - .tmp.code:3:14: error: expected *unknown*type* (labels not permitted) but variable 'b' is label - .tmp.code:3:14: info: this is where 'b' was set to label - .tmp.code:3:16: error: expected label found number - .tmp.code:3:14: info: variable 'b' was set as label here. - .tmp.code:4:17: error: expected *unknown*type* (labels not permitted) but variable 'b' is label - .tmp.code:3:14: info: this is where 'b' was set to label + .tmp.code:3:14: error: variable used but not declared: b + .tmp.code:3:16: error: expected none found number + .tmp.code:3:14: info: variable 'b' was set as none here. oceani: type error in program - not running. ###### test list diff --git a/csrc/oceani.mdc b/csrc/oceani.mdc index a8653cc..f598344 100644 --- a/csrc/oceani.mdc +++ b/csrc/oceani.mdc @@ -1096,7 +1096,7 @@ list of in_scope names. v = t->previous; free_value(t->val); - if (t->min_depth == 0) + if (t->depth == 0) // This is a global constant free_exec(t->where_decl); free(t); @@ -2344,8 +2344,8 @@ link to find the primary instance. /* This might be a label - allocate a var just in case */ v = var_decl(c, $1.txt); if (v) { - v->val = val_prepare(Tlabel); - v->val.label = &v->val; + v->val = val_prepare(Tnone); + v->where_decl = $0; v->where_set = $0; } } @@ -2408,6 +2408,9 @@ link to find the primary instance. v->where_decl, NULL, 0, NULL); return v->val.type; } + if (v->val.type == Tnone && v->where_decl == prog) + type_err(c, "error: variable used but not declared: %v", + prog, NULL, 0, NULL); if (v->val.type == NULL) { if (type && *ok != 0) { v->val = val_prepare(type); @@ -3387,6 +3390,14 @@ function. $0 = new_pos(binode, $1); $0->op = Use; $0->right = $<2; + if ($0->right->type == Xvar) { + struct var *v = cast(var, $0->right); + if (v->var->val.type == Tnone) { + /* Convert this to a label */ + v->var->val = val_prepare(Tlabel); + v->var->val.label = &v->var->val; + } + } }$ ###### print binode cases -- 2.43.0