ast_printer: print scopes inside functions

- comp_ctx: add children attr for debug info
This commit is contained in:
Luna 2019-09-26 16:58:57 -03:00
parent 97c2437d97
commit 64e39a6f1e
3 changed files with 25 additions and 3 deletions

View file

@ -3,7 +3,7 @@ const (
)
fn f() i32 {
var a = 3;
//var a = 3;
return 2;
}

View file

@ -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| {

View file

@ -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,