analysis: add contextual checks for Assign expr

This commit is contained in:
Luna 2019-10-05 10:26:47 -03:00
parent 809dad1095
commit 5c58ac0238
2 changed files with 15 additions and 4 deletions

View file

@ -329,8 +329,8 @@ pub const TypeSolver = struct {
}, },
else => { else => {
std.debug.warn( self.doError(
"Expected Struct/Enum as get target, got {}\n", "Expected Struct/Enum as get target, got {}",
comp.SymbolUnderlyingTypeEnum(global_typ), comp.SymbolUnderlyingTypeEnum(global_typ),
); );
@ -340,12 +340,22 @@ pub const TypeSolver = struct {
}, },
.Assign => |assign| { .Assign => |assign| {
if (ctx.current_scope == null) {
self.doError("Can't assign without a scope");
return CompileError.Invalid;
}
var var_type = ctx.current_scope.?.env.get( var var_type = ctx.current_scope.?.env.get(
assign.name.lexeme, assign.name.lexeme,
).?.value; );
if (var_type == null) {
self.doError("Assign target variable not found");
return CompileError.Invalid;
}
var value_type = try self.resolveExprType(ctx, assign.value); var value_type = try self.resolveExprType(ctx, assign.value);
try self.expectSymUnTypeEqual(var_type, value_type); try self.expectSymUnTypeEqual(var_type.?.value, value_type);
return var_type; return var_type;
}, },

View file

@ -13,6 +13,7 @@ pub const CompileError = error{
LLVMError, LLVMError,
EmitError, EmitError,
TypeError, TypeError,
Invalid,
}; };
fn mkLLVMBool(val: bool) llvm.LLVMValueRef { fn mkLLVMBool(val: bool) llvm.LLVMValueRef {