From 3f9408247767769c16c951bc433fe74affa015a0 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 27 Sep 2019 14:07:42 -0300 Subject: [PATCH] analyze: make VarDecl statements insert symbols to current scope --- examples/hello.ry | 4 +++- src/analysis.zig | 11 ++++++++++- src/ast_printer.zig | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/hello.ry b/examples/hello.ry index d0d9c28..6e79137 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -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; } } diff --git a/src/analysis.zig b/src/analysis.zig index 1b427ac..31e9762 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -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 diff --git a/src/ast_printer.zig b/src/ast_printer.zig index 1eb5407..a5d23e3 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -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| {