analyze: make VarDecl statements insert symbols to current scope

This commit is contained in:
Luna 2019-09-27 14:07:42 -03:00
parent 466243fc2b
commit 3f94082477
3 changed files with 14 additions and 3 deletions

View File

@ -3,7 +3,7 @@ const (
)
fn f() i32 {
// var a = 3;
var a = 3;
// return a;
return 2;
}
@ -34,8 +34,10 @@ fn test_function() B {
fn multwo(num: i32, double_flag: bool) i32 {
// TODO resolve expr variables
if (true) {
var truthy = true;
return 1 * 2;
} else {
var falsey = false;
return 1;
}
}

View File

@ -344,7 +344,16 @@ pub const TypeSolver = struct {
// insert it into the context, however we need to know a pointer
// to where we are, scope-wise, we don't have that info here,
// so it should be implicit into the context.
.VarDecl => @panic("TODO vardecl"),
.VarDecl => |vardecl| {
self.setErrToken(vardecl.name);
const name = vardecl.name.lexeme;
var var_type = try self.resolveExprType(ctx, vardecl.value);
// TODO check current_scope being null
_ = try ctx.current_scope.?.env.put(name, var_type);
},
// Returns dont cause any type system things as they deal with
// values, however, we must ensure that the expression type

View File

@ -376,7 +376,7 @@ pub fn printScope(scope: *Scope, ident: usize) void {
var it = scope.env.iterator();
while (it.next()) |kv| {
print(ident, "sym: {}, typ: {}\n", kv.key, prettyType(kv.value));
print(ident + 1, "sym: {}, typ: {}\n", kv.key, prettyType(kv.value));
}
for (scope.children.toSlice()) |child| {