From 466243fc2bf4fcc6859eae25f9903eaa29662e3b Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 27 Sep 2019 13:49:53 -0300 Subject: [PATCH] add checking of numeric types around boolean operators --- examples/hello.ry | 9 +++++++-- src/analysis.zig | 24 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/examples/hello.ry b/examples/hello.ry index 482c1f7..d0d9c28 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -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 diff --git a/src/analysis.zig b/src/analysis.zig index c6ef1f8..1b427ac 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -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 = {} },