add checking of numeric types around boolean operators

This commit is contained in:
Luna 2019-09-27 13:49:53 -03:00
parent 68b9855309
commit 466243fc2b
2 changed files with 28 additions and 5 deletions

View File

@ -3,14 +3,19 @@ const (
)
fn f() i32 {
//var a = 3;
// var a = 3;
// return a;
return 2;
}
fn f2() i32 {
return 1301;
return f() + 2;
}
//fn f2() bool {
// return 1 > 10;
//}
enum B {
a
b

View File

@ -84,8 +84,6 @@ pub const TypeSolver = struct {
.Struct => SymbolUnderlyingType{ .Struct = val },
.Enum => SymbolUnderlyingType{ .Enum = val },
// TODO name resolution
else => blk: {
self.doError(
"expected struct or enum for '{}', got {}",
@ -115,6 +113,21 @@ pub const TypeSolver = struct {
}
}
/// Check if the given symbol is of a numeric type.
pub fn expectSymUnTypeNumeric(
self: *@This(),
symbol_type: comp.SymbolUnderlyingType,
) !void {
switch (symbol_type) {
.Integer32, .Integer64, .Double => {},
else => {
var actual_enum = comp.SymbolUnderlyingTypeEnum(symbol_type);
std.debug.warn("Expected numeric, got {}\n", actual_enum);
return CompileError.TypeError;
},
}
}
/// Compare if the given type names are equal.
fn compositeIdentifierEqual(
self: *@This(),
@ -188,7 +201,12 @@ pub const TypeSolver = struct {
.Add, .Sub, .Mul, .Div, .Mod => left_type,
// TODO check left and right as numeric
.Greater, .GreaterEqual, .Less, .LessEqual => SymbolUnderlyingType{ .Bool = {} },
.Greater, .GreaterEqual, .Less, .LessEqual => blk: {
try self.expectSymUnTypeNumeric(left_type);
try self.expectSymUnTypeNumeric(right_type);
break :blk SymbolUnderlyingType{ .Bool = {} };
},
// all boolean ops return bools
.Equal, .And, .Or => SymbolUnderlyingType{ .Bool = {} },