summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-04-04 19:50:57 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-04-04 19:50:57 -0400
commit98534503a8fdf8bcb5692c4e9c76adee4a70e5d3 (patch)
treefb69634accea689b32544dd4bb6d794f858fd6c6
parentaa8ae1c36663709befeff5693d5d6c0b2bcc450f (diff)
use with namespaces
-rw-r--r--test.toc22
-rw-r--r--tests.c1
-rw-r--r--types.c46
3 files changed, 55 insertions, 14 deletions
diff --git a/test.toc b/test.toc
index a1c28b1..d64d32e 100644
--- a/test.toc
+++ b/test.toc
@@ -1,16 +1,22 @@
-/*
#include "std/io.toc";
-*/
-Point ::= struct {
- x, y: int;
+
+
+n ::= nms {
+ x := 5;
}
-f ::= fn() p: Point {
- p.x = 13;
+m ::= nms {
+ x := 13;
+ y := 108;
}
main ::= fn() {
- p := f();
- use p;
+ use n;
+ use m;
+ //x = 7;
+ //puti(x);
+ puti(y);
+ y = 100;
+ puti(m.y);
}
diff --git a/tests.c b/tests.c
index 075aac0..d68d4a5 100644
--- a/tests.c
+++ b/tests.c
@@ -30,7 +30,6 @@ static void allocr_test(void) {
static void test_all(void) {
allocr_test();
arr_test();
- block_arr_test();
str_hash_table_test();
idents_test();
}
diff --git a/types.c b/types.c
index c75356f..b32daac 100644
--- a/types.c
+++ b/types.c
@@ -1799,24 +1799,54 @@ static Status types_expr(Typer *tr, Expression *e) {
printf(" to \n");
print_block_location(translated->idents->body);
#endif
- i = e->ident = translated;
+ i = translated;
undeclared = false;
}
Use **uses = b ? b->uses : tr->uses;
Use *previous_use_which_uses_i = NULL;
- (void)previous_use_which_uses_i;
arr_foreach(uses, UsePtr, usep) {
Use *use = *usep;
Expression *used = &use->expr;
+ Identifier translated_use;
if (type_is_builtin(&used->type, BUILTIN_NMS)) {
-
+ Value val;
+ if (!eval_expr(tr->evalr, used, &val))
+ return 0;
+ Namespace *nms = val.nms;
+ Block *body = &nms->body;
+ /* look up identifier in namespace */
+ translated_use = ident_translate(i, &body->idents);
} else {
/* it's a struct */
Type *struct_type = &used->type;
if (struct_type->kind == TYPE_PTR)
struct_type = struct_type->ptr;
assert(struct_type->kind == TYPE_STRUCT);
+ translated_use = NULL;
+ err_print(e->where, "not implemented yet");
+ return false;
+ }
+ if (ident_is_declared(translated_use)) {
+ if (undeclared) {
+ previous_use_which_uses_i = use;
+ undeclared = false;
+ i = translated_use;
+ } else {
+ char *s = ident_to_str(i);
+ err_print(e->where, "Conflicting declarations for identifier %s.", s);
+ char *also = "";
+ if (previous_use_which_uses_i) {
+ /* i was use'd twice */
+ info_print(previous_use_which_uses_i->expr.where, "%s was imported by this use statement.", s);
+ also = "also ";
+ } else {
+ /* i was declared then used. */
+ info_print(ident_decl_location(tr->file, translated), "%s was declared here.", s);
+ }
+ info_print(use->expr.where, "...and %simported by this use statement.", also);
+ return false;
+ }
}
}
if (!undeclared) break;
@@ -1826,6 +1856,7 @@ static Status types_expr(Typer *tr, Expression *e) {
break;
}
}
+ e->ident = i;
if (undeclared) {
char *s = ident_to_str(e->ident);
err_print(e->where, "Undeclared identifier \"%s\".", s);
@@ -3467,6 +3498,7 @@ static Status types_stmt(Typer *tr, Statement *s) {
file->filename = filename;
file->contents = contents;
file->ctx = tr->err_ctx;
+
if (!tokenize_file(&tokr, file))
return false;
Parser parser;
@@ -3481,11 +3513,15 @@ static Status types_stmt(Typer *tr, Statement *s) {
inc_f->stmts = stmts_inc;
}
inc->stmts = stmts_inc;
+ File *prev = tr->file;
+ tr->file = file;
arr_foreach(stmts_inc, Statement, s_incd) {
- if (!types_stmt(tr, s_incd))
+ if (!types_stmt(tr, s_incd)) {
+ tr->file = prev;
return false;
+ }
}
-
+ tr->file = prev;
} break;
case STMT_MESSAGE: {
Message *m = s->message;