summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c22
-rw-r--r--main.c2
-rw-r--r--scope.c2
-rw-r--r--test.toc9
-rw-r--r--types.c2
-rw-r--r--types.h1
6 files changed, 28 insertions, 10 deletions
diff --git a/cgen.c b/cgen.c
index 57d9d9d..b2cb042 100644
--- a/cgen.c
+++ b/cgen.c
@@ -88,12 +88,15 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d);
if (!block_f(g, &e->block)) \
return false; \
break; \
- case EXPR_NMS: \
+ case EXPR_NMS: { \
+ Namespace *prev = g->nms; \
cgen_nms_enter(g, &e->nms); \
- if (!block_f(g, &e->nms.body)) \
+ if (!block_f(g, &e->nms.body)) { \
+ cgen_nms_exit(g, &e->nms, prev); \
return false; \
- cgen_nms_exit(g, &e->nms); \
- break; \
+ } \
+ cgen_nms_exit(g, &e->nms, prev); \
+ } break; \
case EXPR_IF: \
if (e->if_.cond) \
if (!f(g, e->if_.cond)) \
@@ -270,6 +273,12 @@ static void cgen_ident(CGenerator *g, Identifier i) {
if (g->block && (g->block->flags & BLOCK_IS_NMS) && !g->fn) {
/* namespace prefix */
cgen_write(g, "%s", g->nms_prefix);
+ } else {
+ /* do prefix for references to siblings */
+ IdentDecl *idecl = ident_decl(i);
+ if (g->nms && idecl->scope == &g->nms->body) {
+ cgen_write(g, "%s", g->nms_prefix);
+ }
}
if (i == g->main_ident && ident_decl(i) && ident_decl(i)->scope == NULL) {
/* don't conflict with C's main! */
@@ -306,6 +315,7 @@ static char *cgen_nms_prefix(CGenerator *g, Namespace *n) {
}
static void cgen_nms_enter(CGenerator *g, Namespace *n) {
+ g->nms = n;
char *s = cgen_nms_prefix(g, n);
size_t chars_so_far = arr_len(g->nms_prefix) - 1; /* -1 for '\0' byte */
size_t new_chars = strlen(s) + 1; /* + 1 for '\0' byte */
@@ -316,9 +326,10 @@ static void cgen_nms_enter(CGenerator *g, Namespace *n) {
free(s);
}
-static void cgen_nms_exit(CGenerator *g, Namespace *n) {
+static void cgen_nms_exit(CGenerator *g, Namespace *n, Namespace *prev) {
char *s = cgen_nms_prefix(g, n);
arr_set_len(&g->nms_prefix, arr_len(g->nms_prefix) - strlen(s));
+ g->nms = prev;
free(s);
}
@@ -2180,6 +2191,7 @@ static bool cgen_defs_block(CGenerator *g, Block *b) {
static bool cgen_file(CGenerator *g, ParsedFile *f) {
g->block = NULL;
+ g->nms = NULL;
g->fn = NULL;
g->file = f;
diff --git a/main.c b/main.c
index 6f1ac55..5500512 100644
--- a/main.c
+++ b/main.c
@@ -19,7 +19,7 @@
/*
TODO:
try to remember why arr_set_len doesn't shrink, then write that reason there
-make eval_ptr_to_struct_field return a bool
+make eval_ptr_to_struct_field return a bool (just in case it successfully returns a NULL pointer)
nms["foo"]
make sure #export still works properly
fix cgen_ident_to_str for unicode idents
diff --git a/scope.c b/scope.c
index ed70875..d04d1d8 100644
--- a/scope.c
+++ b/scope.c
@@ -65,7 +65,7 @@ static bool block_enter(Block *b, Statement *stmts, U16 flags) {
Declaration *decl = &stmt->decl;
if (!add_ident_decls(b, decl, flags))
ret = false;
- } else if (stmt->kind == STMT_INCLUDE) {
+ } else if ((stmt->flags & STMT_TYPED) && stmt->kind == STMT_INCLUDE) {
if (!block_enter(b, stmt->inc.stmts, flags))
return false;
}
diff --git a/test.toc b/test.toc
index fffb4e4..b1aefcf 100644
--- a/test.toc
+++ b/test.toc
@@ -1,3 +1,6 @@
+io ::= nms {
+ #include "std/io.toc";
+};
n ::= nms {
x := 1;
@@ -10,7 +13,7 @@ main ::= fn() {
b := n.counter();
n.counter();
c := n.counter();
- // puti(a);
- // puti(b);
- // puti(c);
+ io.puti(a);
+ io.puti(b);
+ io.puti(c);
};
diff --git a/types.c b/types.c
index 73c0da0..c2b5437 100644
--- a/types.c
+++ b/types.c
@@ -2395,6 +2395,7 @@ static bool types_decl(Typer *tr, Declaration *d) {
}
static bool types_stmt(Typer *tr, Statement *s) {
+ if (s->flags & STMT_TYPED) return true;
switch (s->kind) {
case STMT_EXPR:
if (!types_expr(tr, &s->expr)) {
@@ -2472,6 +2473,7 @@ static bool types_stmt(Typer *tr, Statement *s) {
}
} break;
}
+ s->flags |= STMT_TYPED;
return true;
}
diff --git a/types.h b/types.h
index 158fd04..ba4bcc2 100644
--- a/types.h
+++ b/types.h
@@ -914,6 +914,7 @@ typedef struct CGenerator {
bool will_indent; /* will the next thing be indented? */
ParsedFile *file;
Block *block;
+ Namespace *nms;
FnExpr *fn; /* which function are we in? (NULL for none) - not used during decls */
Evaluator *evalr;
Identifier main_ident;