From ae27995eb6bd734dbef3d6d866274b74a5897ca7 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 26 Sep 2019 14:23:15 -0300 Subject: [PATCH] add rudimentary type analysis for return, if, loop --- src/types.zig | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/types.zig b/src/types.zig index 0015b2c..733112a 100644 --- a/src/types.zig +++ b/src/types.zig @@ -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, }