From 0c37c8fc1a2861b193b4c130d0df93b3df44956c Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 4 Apr 2020 15:55:48 -0400 Subject: fixed namespace problem, for real this time --- test.toc | 2 +- types.c | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test.toc b/test.toc index de22166..4c54b2c 100644 --- a/test.toc +++ b/test.toc @@ -7,5 +7,5 @@ main ::= fn() { p ::= nms { y := 5; } - puti(p.y); + puti(p["y"]); } diff --git a/types.c b/types.c index 9539170..7844871 100644 --- a/types.c +++ b/types.c @@ -388,6 +388,13 @@ static char *eval_expr_as_cstr(Typer *tr, Expression *e, const char *what_is_thi return str; } +static char *slice_to_cstr(Slice s) { + char *ret = malloc((size_t)s.n + 1); + memcpy(ret, s.data, (size_t)s.n); + ret[s.n] = 0; + return ret; +} + enum { /* is f an instance? (changes behaviour a bit) */ TYPE_OF_FN_IS_INSTANCE = 0x01 @@ -2926,6 +2933,12 @@ static Status types_expr(Typer *tr, Expression *e) { e->binary.op = BINARY_DOT; e->binary.rhs->kind = EXPR_IDENT; e->binary.rhs->ident = ident_get_with_len(&nms->body.idents, member_name.slice.data, (size_t)member_name.slice.n); + if (!ident_is_declared(e->binary.rhs->ident)) { + char *s = slice_to_cstr(member_name.slice); + err_print(e->where, "\"%s\" is not a member of this namespace.", s); + free(s); + return false; + } if (!type_of_ident(tr, rhs->where, &e->binary.rhs->ident, t, TYPE_OF_IDENT_BLOCK_IS_CORRECT)) { return false; } @@ -3053,7 +3066,14 @@ static Status types_expr(Typer *tr, Expression *e) { Namespace *nms = nms_val.nms; lhs->kind = EXPR_VAL; lhs->val.nms = nms; - rhs->ident = ident_translate(rhs->ident, &nms->body.idents); + Identifier original = rhs->ident; + rhs->ident = ident_translate(original, &nms->body.idents); + if (!ident_is_declared(rhs->ident)) { + char *s = ident_to_str(original); + err_print(e->where, "\"%s\" is not a member of this namespace.", s); + free(s); + return false; + } if (!type_of_ident(tr, rhs->where, &rhs->ident, t, TYPE_OF_IDENT_BLOCK_IS_CORRECT)) { return false; } -- cgit v1.2.3