From 64e39a6f1e55b6fe77886ce3520a0ec4a4eb2127 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 26 Sep 2019 16:58:57 -0300 Subject: [PATCH] ast_printer: print scopes inside functions - comp_ctx: add children attr for debug info --- examples/hello.ry | 2 +- src/ast_printer.zig | 17 +++++++++++++++++ src/comp_ctx.zig | 9 +++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/examples/hello.ry b/examples/hello.ry index 3c0c53c..886e348 100644 --- a/examples/hello.ry +++ b/examples/hello.ry @@ -3,7 +3,7 @@ const ( ) fn f() i32 { - var a = 3; + //var a = 3; return 2; } diff --git a/src/ast_printer.zig b/src/ast_printer.zig index 82ecfde..29e714d 100644 --- a/src/ast_printer.zig +++ b/src/ast_printer.zig @@ -369,6 +369,19 @@ fn prettyType(typ: SymbolUnderlyingType) []const u8 { }; } +pub fn printScope(scope: *Scope, ident: usize) void { + print(ident, "scope at addr {}\n", scope); + + var it = scope.env.iterator(); + while (it.next()) |kv| { + print(ident, "sym: {}, typ: {}\n", kv.key, prettyType(kv.value)); + } + + for (scope.children.toSlice()) |child| { + printScope(scope, ident + 1); + } +} + pub fn printContext(ctx: CompilationContext) void { var it = ctx.symbol_table.iterator(); @@ -389,6 +402,10 @@ pub fn printContext(ctx: CompilationContext) void { prettyType(param_kv.value), ); } + + // go through scopes + std.debug.warn("scope info:\n"); + printScope(fn_sym.scope, 0); }, .Struct => |typemap| { diff --git a/src/comp_ctx.zig b/src/comp_ctx.zig index 0668e8a..e645dd9 100644 --- a/src/comp_ctx.zig +++ b/src/comp_ctx.zig @@ -30,18 +30,23 @@ pub const SymbolUnderlyingType = union(SymbolUnderlyingTypeEnum) { }; pub const Environment = std.StringHashMap(SymbolUnderlyingType); +pub const ScopeList = std.ArrayList(*Scope); pub const Scope = struct { parent: ?*Scope, env: Environment, + /// Used for debug information. + children: ScopeList, + allocator: *std.mem.Allocator, pub fn create(allocator: *std.mem.Allocator, parent: ?*Scope) !*Scope { var scope = try allocator.create(Scope); scope.* = Scope{ - .parent = scope, + .parent = parent, .env = Environment.init(allocator), + .children = ScopeList.init(allocator), .allocator = allocator, }; return scope; @@ -58,7 +63,7 @@ pub const Scope = struct { // functions, for our purposes, other than symbols, have: // - a return type -// - TODO parameters +// - parameters pub const FunctionSymbol = struct { decl: ast.FnDecl, return_type: SymbolUnderlyingType,