analysis: add name type resolution
this only adds resolution for the types. for the actual metadata, such as e.g parameters, that isn't available, so codegen isn't helped by this.
This commit is contained in:
parent
4346636cfa
commit
362fc7e3ef
3 changed files with 44 additions and 4 deletions
|
@ -43,8 +43,7 @@ fn multwo(num: i32, double_flag: bool) i32 {
|
|||
}
|
||||
|
||||
fn multwo_with_one(b: i32) i32 {
|
||||
// TODO replace by b
|
||||
return multwo(2, false) + 2;
|
||||
return multwo(b, false) + b;
|
||||
}
|
||||
|
||||
fn add(a: i32, b: i32) i32 {
|
||||
|
|
|
@ -275,7 +275,11 @@ pub const TypeSolver = struct {
|
|||
return func_sym.return_type;
|
||||
},
|
||||
|
||||
// TODO analysis for .Variable
|
||||
.Variable => |vari| {
|
||||
// TODO maybe we can modify vari and add the current scope
|
||||
// it was found for codegen purposes
|
||||
return try ctx.resolveVarType(vari.lexeme);
|
||||
},
|
||||
|
||||
.Get => |get| {
|
||||
var target = get.target.*;
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
|
||||
pub const CompilationError = error{TypeError};
|
||||
pub const CompilationError = error{
|
||||
TypeError,
|
||||
UnknownName,
|
||||
};
|
||||
|
||||
pub const SymbolTable = std.hash_map.StringHashMap(SymbolData);
|
||||
pub const TypeList = std.ArrayList(SymbolUnderlyingType);
|
||||
|
@ -289,4 +292,38 @@ pub const CompilationContext = struct {
|
|||
|
||||
return sym_kv.?.value;
|
||||
}
|
||||
|
||||
fn resolveVarTypeInScope(
|
||||
self: *@This(),
|
||||
scope_opt: ?*Scope,
|
||||
name: []const u8,
|
||||
) ?SymbolUnderlyingType {
|
||||
if (scope_opt == null) return null;
|
||||
var scope = scope_opt.?;
|
||||
|
||||
var kv_opt = scope.env.get(name);
|
||||
if (kv_opt) |kv| {
|
||||
// TODO maybe return the scope it was found in
|
||||
return kv.value;
|
||||
} else {
|
||||
return self.resolveVarTypeInScope(scope.parent, name);
|
||||
}
|
||||
}
|
||||
|
||||
fn resolveVarType(self: *@This(), name: []const u8) !SymbolUnderlyingType {
|
||||
var var_type: ?SymbolUnderlyingType = null;
|
||||
|
||||
if (self.current_scope) |scope| {
|
||||
var_type = self.resolveVarTypeInScope(scope, name);
|
||||
if (var_type) |typ| return typ;
|
||||
}
|
||||
|
||||
if (self.cur_function) |cur_function| {
|
||||
var kv_opt = cur_function.parameters.get(name);
|
||||
if (kv_opt) |kv| return kv.value;
|
||||
}
|
||||
|
||||
std.debug.warn("Unknown name {}\n", name);
|
||||
return CompilationError.UnknownName;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue