commit 87fd2ecbebd45b40f373c6a88a78e28dd6ee23f0
parent ffcf5cfad776528d397dde33679f84b0e1754778
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Mon, 20 Dec 2021 00:17:00 -0600
typecheck assignments and declarations the same, and add a test
Diffstat:
2 files changed, 22 insertions(+), 30 deletions(-)
diff --git a/main.c b/main.c
@@ -264,63 +264,53 @@ typecheck(struct block items)
struct decl *decl;
struct type *type;
struct assgn *assgn;
+ size_t line, col;
switch (items.data[i].kind) {
+ case ITEM_ASSGN:
+ assgn = &assgns.data[item->idx];
+ decl = finddecl(assgn->s);
+ if (decl == NULL)
+ error(assgn->start->line, assgn->start->col, "unknown name");
+
+ type = &types.data[decl->type];
+ line = assgn->start->line;
+ col = assgn->start->col;
+ goto check;
case ITEM_DECL:
decl = &items.decls.data[item->idx];
type = &types.data[decl->type];
+ line = decl->start->line;
+ col = decl->start->col;
+check:
switch (type->class) {
case TYPE_I64:
expr = &exprs.data[decl->val];
// FIXME: we should be able to deal with ident or fcalls
if (expr->class != C_INT)
- error(decl->start->line, decl->start->col, "expected integer expression for integer declaration");
+ error(line, col, "expected integer expression for integer declaration");
break;
case TYPE_STR:
expr = &exprs.data[decl->val];
// FIXME: we should be able to deal with ident or fcalls
if (expr->class != C_STR)
- error(decl->start->line, decl->start->col, "expected string expression for string declaration");
+ error(line, col, "expected string expression for string declaration");
break;
case TYPE_PROC:
expr = &exprs.data[decl->val];
if (expr->class != C_PROC)
- error(decl->start->line, decl->start->col, "expected proc expression for proc declaration");
+ error(line, col, "expected proc expression for proc declaration");
if (expr->d.proc.in.len != type->d.params.in.len)
- error(decl->start->line, decl->start->col, "procedure expression takes %u parameters, but declaration has type which takes %u", expr->d.proc.in.len, type->d.params.in.len);
+ error(line, col, "procedure expression takes %u parameters, but declaration has type which takes %u", expr->d.proc.in.len, type->d.params.in.len);
for (size_t j = 0; j < expr->d.proc.in.len; j++) {
if (expr->d.proc.in.data[j].type != type->d.params.in.data[j])
- error(decl->start->line, decl->start->col, "unexpected type for parameter %u in procedure declaration", j);
+ error(line, col, "unexpected type for parameter %u in procedure declaration", j);
}
break;
default:
- error(decl->start->line, decl->start->col, "unknown decl type");
- }
- break;
- case ITEM_ASSGN:
- assgn = &assgns.data[item->idx];
- decl = finddecl(assgn->s);
- if (decl == NULL)
- error(assgn->start->line, assgn->start->col, "unknown name");
-
- type = &types.data[decl->type];
- switch (type->class) {
- case TYPE_I64:
- expr = &exprs.data[assgn->val];
- // FIXME: we should be able to deal with ident or fcalls
- if (expr->class != C_INT)
- error(assgn->start->line, assgn->start->col, "expected integer expression for integer variable");
- break;
- case TYPE_STR:
- expr = &exprs.data[assgn->val];
- // FIXME: we should be able to deal with ident or fcalls
- if (expr->class != C_STR)
- error(assgn->start->line, assgn->start->col, "expected string expression for string variable");
- break;
- default:
- error(assgn->start->line, assgn->start->col, "unknown decl type");
+ error(line, col, "unknown decl type");
}
break;
case ITEM_EXPR:
diff --git a/test/type.fail.nooc b/test/type.fail.nooc
@@ -0,0 +1,2 @@
+let num i64 = 15
+num = "a string"