ast_printer: print scopes inside functions
- comp_ctx: add children attr for debug info
This commit is contained in:
parent
97c2437d97
commit
64e39a6f1e
|
@ -3,7 +3,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
fn f() i32 {
|
fn f() i32 {
|
||||||
var a = 3;
|
//var a = 3;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
pub fn printContext(ctx: CompilationContext) void {
|
||||||
var it = ctx.symbol_table.iterator();
|
var it = ctx.symbol_table.iterator();
|
||||||
|
|
||||||
|
@ -389,6 +402,10 @@ pub fn printContext(ctx: CompilationContext) void {
|
||||||
prettyType(param_kv.value),
|
prettyType(param_kv.value),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go through scopes
|
||||||
|
std.debug.warn("scope info:\n");
|
||||||
|
printScope(fn_sym.scope, 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
.Struct => |typemap| {
|
.Struct => |typemap| {
|
||||||
|
|
|
@ -30,18 +30,23 @@ pub const SymbolUnderlyingType = union(SymbolUnderlyingTypeEnum) {
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Environment = std.StringHashMap(SymbolUnderlyingType);
|
pub const Environment = std.StringHashMap(SymbolUnderlyingType);
|
||||||
|
pub const ScopeList = std.ArrayList(*Scope);
|
||||||
pub const Scope = struct {
|
pub const Scope = struct {
|
||||||
parent: ?*Scope,
|
parent: ?*Scope,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
|
|
||||||
|
/// Used for debug information.
|
||||||
|
children: ScopeList,
|
||||||
|
|
||||||
allocator: *std.mem.Allocator,
|
allocator: *std.mem.Allocator,
|
||||||
|
|
||||||
pub fn create(allocator: *std.mem.Allocator, parent: ?*Scope) !*Scope {
|
pub fn create(allocator: *std.mem.Allocator, parent: ?*Scope) !*Scope {
|
||||||
var scope = try allocator.create(Scope);
|
var scope = try allocator.create(Scope);
|
||||||
|
|
||||||
scope.* = Scope{
|
scope.* = Scope{
|
||||||
.parent = scope,
|
.parent = parent,
|
||||||
.env = Environment.init(allocator),
|
.env = Environment.init(allocator),
|
||||||
|
.children = ScopeList.init(allocator),
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
};
|
};
|
||||||
return scope;
|
return scope;
|
||||||
|
@ -58,7 +63,7 @@ pub const Scope = struct {
|
||||||
|
|
||||||
// functions, for our purposes, other than symbols, have:
|
// functions, for our purposes, other than symbols, have:
|
||||||
// - a return type
|
// - a return type
|
||||||
// - TODO parameters
|
// - parameters
|
||||||
pub const FunctionSymbol = struct {
|
pub const FunctionSymbol = struct {
|
||||||
decl: ast.FnDecl,
|
decl: ast.FnDecl,
|
||||||
return_type: SymbolUnderlyingType,
|
return_type: SymbolUnderlyingType,
|
||||||
|
|
Loading…
Reference in New Issue