add rudimentary return type analysis for call exprs

This commit is contained in:
Luna 2019-09-25 17:17:47 -03:00
parent 04c624e210
commit 5036be02e3
2 changed files with 27 additions and 1 deletions

View File

@ -1,7 +1,11 @@
// import std;
fn f() i32 {
return 2;
}
const (
AWOO = 1 + 2
piss = f() + 3
)
enum B {

View File

@ -88,6 +88,8 @@ pub const TypeSolver = struct {
}
}
// TODO make return type optional and so, skip exprs that
// fail to be fully resolved, instead of returning CompileError
pub fn resolveExprType(
self: *@This(),
ctx: *comp.CompilationContext,
@ -143,6 +145,26 @@ pub const TypeSolver = struct {
return typ.?;
},
.Call => |call| {
self.setErrToken(call.paren);
std.debug.assert(ast.ExprType(call.callee.*) == .Variable);
const func_name = call.callee.*.Variable.lexeme;
var sym_kv = ctx.symbol_table.get(func_name);
if (sym_kv == null) {
self.doError("Unknown function '{}'\n", func_name);
return CompileError.TypeError;
}
std.debug.assert(comp.SymbolType(sym_kv.?.value) == .Function);
var func_sym = sym_kv.?.value.Function;
// TODO check parameter type mismatches between
// call.arguments and func_sym.parameters
return func_sym.return_type;
},
// TODO variable resolution
// TODO Get (for structs and enums)