summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c4
-rw-r--r--copy.c10
-rw-r--r--decls_cgen.c2
-rw-r--r--eval.c2
-rw-r--r--identifiers.c15
-rw-r--r--parse.c12
-rw-r--r--sdecls_cgen.c2
-rw-r--r--test.toc12
-rw-r--r--types.c44
-rw-r--r--types.h8
10 files changed, 47 insertions, 64 deletions
diff --git a/cgen.c b/cgen.c
index e187f11..9b4ff3a 100644
--- a/cgen.c
+++ b/cgen.c
@@ -2027,7 +2027,7 @@ static bool cgen_stmt(CGenerator *g, Statement *s) {
*/
switch (s->kind) {
case STMT_DECL:
- if (!cgen_decl(g, &s->decl)) return false;
+ if (!cgen_decl(g, s->decl)) return false;
break;
case STMT_EXPR:
if (!cgen_expr_pre(g, &s->expr)) return false;
@@ -2099,7 +2099,7 @@ static bool cgen_defs_decl(CGenerator *g, Declaration *d) {
static bool cgen_defs_stmt(CGenerator *g, Statement *s) {
switch (s->kind) {
case STMT_DECL:
- if (!cgen_defs_decl(g, &s->decl))
+ if (!cgen_defs_decl(g, s->decl))
return false;
break;
case STMT_EXPR:
diff --git a/copy.c b/copy.c
index c7d8406..4a69967 100644
--- a/copy.c
+++ b/copy.c
@@ -223,13 +223,13 @@ static void copy_expr(Copier *c, Expression *out, Expression *in) {
if (fout->index) {
copier_ident_translate(c, &fout->index);
- fout->index->decl_kind = IDECL_EXPR;
- fout->index->expr = out;
+ fout->index->decl_kind = IDECL_FOR;
+ fout->index->for_ = fout;
}
if (fout->value) {
copier_ident_translate(c, &fout->value);
- fout->value->decl_kind = IDECL_EXPR;
- fout->value->expr = out;
+ fout->value->decl_kind = IDECL_FOR;
+ fout->value->for_ = fout;
}
if (fin->flags & FOR_ANNOTATED_TYPE)
copy_type(c, &fout->type, &fin->type);
@@ -360,7 +360,7 @@ static void copy_stmt(Copier *c, Statement *out, Statement *in) {
copy_expr(c, &out->expr, &in->expr);
break;
case STMT_DECL:
- copy_decl(c, &out->decl, &in->decl);
+ copy_decl(c, out->decl = allocr_malloc(c->allocr, sizeof *out->decl), in->decl);
break;
}
}
diff --git a/decls_cgen.c b/decls_cgen.c
index 6bb8754..d5a2836 100644
--- a/decls_cgen.c
+++ b/decls_cgen.c
@@ -226,7 +226,7 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d) {
static bool cgen_decls_stmt(CGenerator *g, Statement *s) {
switch (s->kind) {
case STMT_DECL:
- if (!cgen_decls_decl(g, &s->decl))
+ if (!cgen_decls_decl(g, s->decl))
return false;
break;
case STMT_EXPR:
diff --git a/eval.c b/eval.c
index 458a0cd..6f6412a 100644
--- a/eval.c
+++ b/eval.c
@@ -1654,7 +1654,7 @@ static bool eval_decl(Evaluator *ev, Declaration *d) {
static bool eval_stmt(Evaluator *ev, Statement *stmt) {
switch (stmt->kind) {
case STMT_DECL:
- if (!eval_decl(ev, &stmt->decl)) return false;
+ if (!eval_decl(ev, stmt->decl)) return false;
break;
case STMT_EXPR: {
Value unused;
diff --git a/identifiers.c b/identifiers.c
index 7d975fe..73bc413 100644
--- a/identifiers.c
+++ b/identifiers.c
@@ -200,18 +200,3 @@ static int ident_index_in_decl(Identifier i, Declaration *d) {
}
return -1;
}
-
-static Location ident_decl_location(Identifier i) {
-
- switch (i->decl_kind) {
- case IDECL_DECL:
- return i->decl->where;
- case IDECL_EXPR:
- return i->expr->where;
- case IDECL_NONE:
- break;
- }
- assert(0);
- Location def = {0};
- return def;
-}
diff --git a/parse.c b/parse.c
index 56f495d..397666a 100644
--- a/parse.c
+++ b/parse.c
@@ -1190,8 +1190,8 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) {
&& token_is_kw(t->token + 3, KW_COLON))))) {
if (t->token->kind == TOKEN_IDENT) {
fo->value = parser_ident_insert(p, t->token->ident);
- fo->value->decl_kind = IDECL_EXPR;
- fo->value->expr = e;
+ fo->value->decl_kind = IDECL_FOR;
+ fo->value->for_ = fo;
if (ident_eq_str(fo->value, "_")) /* ignore value */
fo->value = NULL;
++t->token;
@@ -1199,8 +1199,8 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) {
++t->token;
if (t->token->kind == TOKEN_IDENT) {
fo->index = parser_ident_insert(p, t->token->ident);
- fo->index->decl_kind = IDECL_EXPR;
- fo->index->expr = e;
+ fo->index->decl_kind = IDECL_FOR;
+ fo->index->for_ = fo;
if (ident_eq_str(fo->index, "_")) /* ignore index */
fo->index = NULL;
++t->token;
@@ -2106,7 +2106,7 @@ static bool parse_stmt(Parser *p, Statement *s, bool *was_a_statement) {
}
if (is_decl(t)) {
s->kind = STMT_DECL;
- if (!parse_decl(p, &s->decl, DECL_END_SEMICOLON, PARSE_DECL_ALLOW_EXPORT)) {
+ if (!parse_decl(p, s->decl = parser_malloc(p, sizeof *s->decl), DECL_END_SEMICOLON, PARSE_DECL_ALLOW_EXPORT)) {
return false;
}
} else {
@@ -2448,7 +2448,7 @@ static void fprint_stmt(FILE *out, Statement *s) {
PARSE_PRINT_LOCATION(s->where);
switch (s->kind) {
case STMT_DECL:
- fprint_decl(out, &s->decl);
+ fprint_decl(out, s->decl);
fprintf(out, ";\n");
break;
case STMT_EXPR:
diff --git a/sdecls_cgen.c b/sdecls_cgen.c
index 529041a..9ca41c4 100644
--- a/sdecls_cgen.c
+++ b/sdecls_cgen.c
@@ -95,7 +95,7 @@ static bool cgen_sdecls_decl(CGenerator *g, Declaration *d) {
static bool cgen_sdecls_stmt(CGenerator *g, Statement *s) {
switch (s->kind) {
case STMT_DECL:
- if (!cgen_sdecls_decl(g, &s->decl))
+ if (!cgen_sdecls_decl(g, s->decl))
return false;
break;
case STMT_EXPR:
diff --git a/test.toc b/test.toc
index ac60b69..05b3095 100644
--- a/test.toc
+++ b/test.toc
@@ -1,7 +1,7 @@
-getstdin ::= fn(){
- buffer : int;
- while true {
- while buffer {
- }
- }
+foo ::= nms {
+ bar ::= 5;
};
+
+main ::= fn() {
+ foo.bar;
+}; \ No newline at end of file
diff --git a/types.c b/types.c
index a5c30c0..9cedc5f 100644
--- a/types.c
+++ b/types.c
@@ -483,29 +483,24 @@ static bool type_of_ident(Typer *tr, Location where, Identifier *ident, Type *t)
}
}
} break;
- case IDECL_EXPR: {
- Expression *e = i->expr;
- /* are we inside this expression? */
- typedef Expression *ExpressionPtr;
- arr_foreach(tr->in_expr_decls, ExpressionPtr, in_e) {
- if (*in_e == e) {
+ case IDECL_FOR: {
+ ForExpr *fo = i->for_;
+ /* are we inside this for? */
+ typedef ForExpr *ForExprPtr;
+ arr_foreach(tr->in_fors, ForExprPtr, in_f) {
+ if (*in_f == fo) {
char *s = ident_to_str(i);
err_print(where, "Use of identifier %s in its own declaration.", s);
free(s);
return false;
}
}
- switch (e->kind) {
- case EXPR_FOR:
- if (i == e->for_->index) {
- t->kind = TYPE_BUILTIN;
- t->builtin = BUILTIN_I64;
- } else {
- assert(i == e->for_->value);
- *t = e->for_->type;
- }
- break;
- default: assert(0); return false;
+ if (i == fo->index) {
+ t->kind = TYPE_BUILTIN;
+ t->builtin = BUILTIN_I64;
+ } else {
+ assert(i == fo->value);
+ *t = fo->type;
}
} break;
case IDECL_NONE: {
@@ -1079,7 +1074,7 @@ static bool nms_translate_idents_in_stmts(Namespace *nms, Statement *stmts) {
return false;
break;
case STMT_DECL: {
- Declaration *d = &s->decl;
+ Declaration *d = s->decl;
arr_foreach(d->idents, Identifier, i) {
*i = ident_translate_forced(*i, &nms->idents);
}
@@ -1146,7 +1141,8 @@ static bool types_expr(Typer *tr, Expression *e) {
break;
case EXPR_FOR: {
ForExpr *fo = e->for_;
- *(Expression **)typer_arr_add(tr, &tr->in_expr_decls) = e;
+ bool in_header = true;
+ *(ForExpr **)typer_arr_add(tr, &tr->in_fors) = fo;
typer_block_enter(tr, &fo->body); /* while this block is being typed, fo->body will be in tr->blocks twice. hopefully that doesn't mess anything up! */
if (fo->flags & FOR_IS_RANGE) {
/* TODO: allow user-defined numerical types */
@@ -1263,8 +1259,8 @@ static bool types_expr(Typer *tr, Expression *e) {
fo->range.stepval = stepval;
}
- arr_remove_lasta(&tr->in_expr_decls, tr->allocr);
-
+ arr_remove_lasta(&tr->in_fors, tr->allocr);
+ in_header = false;
if (!types_block(tr, &fo->body)) goto for_fail;
if (fo->body.ret_expr) {
@@ -1276,6 +1272,8 @@ static bool types_expr(Typer *tr, Expression *e) {
typer_block_exit(tr);
break;
for_fail:
+ if (in_header)
+ arr_remove_lasta(&tr->in_fors, tr->allocr);
typer_block_exit(tr);
return false;
};
@@ -2429,7 +2427,7 @@ static bool types_stmt(Typer *tr, Statement *s) {
}
break;
case STMT_DECL:
- if (!types_decl(tr, &s->decl)) {
+ if (!types_decl(tr, s->decl)) {
return false;
}
break;
@@ -2504,7 +2502,7 @@ static void typer_create(Typer *tr, Evaluator *ev, ErrCtx *err_ctx, Allocator *a
tr->evalr = ev;
tr->err_ctx = err_ctx;
tr->in_decls = NULL;
- tr->in_expr_decls = NULL;
+ tr->in_fors = NULL;
tr->allocr = allocr;
tr->globals = idents;
tr->is_reference_stack = NULL;
diff --git a/types.h b/types.h
index 94a59c5..446de4c 100644
--- a/types.h
+++ b/types.h
@@ -174,7 +174,7 @@ enum {
typedef enum {
IDECL_NONE,
IDECL_DECL,
- IDECL_EXPR
+ IDECL_FOR
} IdentDeclKind;
@@ -185,7 +185,7 @@ typedef struct IdentSlot {
IdentDeclKind decl_kind;
union {
struct Declaration *decl;
- struct Expression *expr; /* for example, this identifier is declared in a for expression */
+ struct ForExpr *for_;
};
struct Identifiers *idents;
Value val;
@@ -842,7 +842,7 @@ typedef struct Statement {
StatementKind kind;
U8 flags;
union {
- Declaration decl;
+ Declaration *decl; /* we want the pointer to be fixed so that we can refer to it from an identifier */
Expression expr;
Return ret;
Include inc;
@@ -896,7 +896,7 @@ typedef struct Typer {
Allocator *allocr;
Evaluator *evalr;
Identifiers *globals;
- Expression **in_expr_decls; /* an array of expressions whose declarations (e.g. for **x := foo**) we are currently inside */
+ ForExpr **in_fors; /* which fors we are currently inside the header of */
Declaration **in_decls; /* array of declarations we are currently inside */
Block *block;
Block **blocks; /* dyn array of all the block's we're in ([0] = NULL for global scope) */