diff options
-rw-r--r-- | cgen.c | 22 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | scope.c | 2 | ||||
-rw-r--r-- | test.toc | 9 | ||||
-rw-r--r-- | types.c | 2 | ||||
-rw-r--r-- | types.h | 1 |
6 files changed, 28 insertions, 10 deletions
@@ -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; @@ -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 @@ -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; } @@ -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); }; @@ -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; } @@ -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; |