From 4346636cfacfeaaded043e24df7781eace77f98b Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 27 Sep 2019 14:31:59 -0300 Subject: [PATCH] analysis: add check for call expr's arguments --- examples/hello.ry | 5 +++++ src/analysis.zig | 21 +++++++++++++++++---- src/comp_ctx.zig | 3 +++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/examples/hello.ry b/examples/hello.ry index 6e79137..0d93592 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -42,6 +42,11 @@ fn multwo(num: i32, double_flag: bool) i32 { } } +fn multwo_with_one(b: i32) i32 { + // TODO replace by b + return multwo(2, false) + 2; +} + fn add(a: i32, b: i32) i32 { return 69 + 69; } diff --git a/src/analysis.zig b/src/analysis.zig index 31e9762..e9cbf26 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -200,7 +200,6 @@ pub const TypeSolver = struct { // all numeric operations return numeric types .Add, .Sub, .Mul, .Div, .Mod => left_type, - // TODO check left and right as numeric .Greater, .GreaterEqual, .Less, .LessEqual => blk: { try self.expectSymUnTypeNumeric(left_type); try self.expectSymUnTypeNumeric(right_type); @@ -226,7 +225,8 @@ pub const TypeSolver = struct { return switch (literal) { .Bool => SymbolUnderlyingType{ .Bool = {} }, - // TODO determine its i64 depending of parseInt results + // TODO recast Integer32 as Integer64 if the type we're + // checking into is Integer64, but not the other way. .Integer32 => SymbolUnderlyingType{ .Integer32 = {} }, .Integer64 => SymbolUnderlyingType{ .Integer64 = {} }, .Float => SymbolUnderlyingType{ .Double = {} }, @@ -256,8 +256,21 @@ pub const TypeSolver = struct { var symbol = try ctx.fetchGlobalSymbol(func_name, .Function); var func_sym = symbol.Function; - // TODO check parameter type mismatches between - // call.arguments and func_sym.parameters + for (call.arguments.toSlice()) |arg_expr, idx| { + var param_type = func_sym.parameter_list.at(idx); + var arg_type = try self.resolveExprType(ctx, &arg_expr); + + self.expectSymUnTypeEqual(arg_type, param_type) catch { + self.doError( + "Expected parameter {} to be {}, got {}", + idx, + @tagName(comp.SymbolUnderlyingTypeEnum(param_type)), + @tagName(comp.SymbolUnderlyingTypeEnum(arg_type)), + ); + + return CompileError.TypeError; + }; + } return func_sym.return_type; }, diff --git a/src/comp_ctx.zig b/src/comp_ctx.zig index 0a407e3..749a518 100644 --- a/src/comp_ctx.zig +++ b/src/comp_ctx.zig @@ -76,6 +76,8 @@ pub const FunctionSymbol = struct { /// Parameters for a function are also a table instead of an ArrayList /// because we want to resolve identifiers to them. parameters: UnderlyingTypeMap, + parameter_list: TypeList, + scope: *Scope, /// Find a given identifier in the function. Can resolve to either a parameter @@ -239,6 +241,7 @@ pub const CompilationContext = struct { .decl = decl, .return_type = ret_type, .parameters = type_map, + .parameter_list = param_types, .scope = scope, }, });