add rudimentary type analysis for return, if, loop

This commit is contained in:
Luna 2019-09-26 14:23:15 -03:00
parent 8065d0d905
commit ae27995eb6
1 changed files with 39 additions and 11 deletions

View File

@ -189,21 +189,49 @@ pub const TypeSolver = struct {
// so it should be implicit into the context.
.VarDecl => @panic("TODO vardecl"),
// If create two scopes for each branch of the if
.If => @panic("TODO ifstmt"),
// Loop (creates 1 scope) asserts that the expression
// type is a bool
.Loop => @panic("TODO loop"),
// For (creates 1 scope) receives arrays, which we dont have yet
.For => @panic("TODO for"),
// Returns dont cause any type system things as they deal with
// values, however, we must ensure that the expression type
// matches the function type (must fetch from context, or we could
// pull a hack with err contexts, lol)
.Return => @panic("TODO return"),
.Return => |ret| {
var ret_stmt_type = try self.resolveExprType(ctx, ret.value);
// TODO check if ret_stmt_type == ctx.cur_function.return_type
},
// If create two scopes for each branch of the if
.If => |ifstmt| {
try self.resolveExprType(ifstmt.condition);
// TODO assert condition's type is bool
// TODO bump-dump scope
for (ifstmt.then_branch.toSlice()) |then_stmt| {
try self.stmtPass(ctx, &then_stmt);
}
if (ifstmt.else_branch) |else_branch| {
// TODO bump-dump scope
for (else_branch.toSlice()) |else_stmt| {
try self.stmtPass(ctx, &else_stmt);
}
}
},
// Loop (creates 1 scope) asserts that the expression
// type is a bool
.Loop => |loop| {
try self.resolveExprType(loop.condition);
// TODO assert condition's type is bool
// TODO bump-dump scope
for (loop.then_branch.toSlice()) |then_stmt| {
try self.stmtPass(ctx, &then_stmt);
}
},
// For (creates 1 scope) receives arrays, which we dont have yet
.For => @panic("TODO for"),
else => unreachable,
}