refactor: working towards a better astgen patterns
This commit is contained in:
parent
d6ff3a40bd
commit
95d89b7bf9
7 changed files with 922 additions and 596 deletions
|
|
@ -497,11 +497,48 @@ pub fn loadFromString(
|
|||
return error.Invalid;
|
||||
}
|
||||
|
||||
var story = try AstGen.generate(gpa, &ast);
|
||||
const comp_unit = try AstGen.generate(gpa, &ast);
|
||||
defer comp_unit.deinit(gpa);
|
||||
comp_unit.dumpStringsWithHex();
|
||||
|
||||
var story: Story = .{
|
||||
.allocator = gpa,
|
||||
.can_advance = false,
|
||||
.dump_writer = options.dump_writer,
|
||||
};
|
||||
errdefer story.deinit();
|
||||
|
||||
try story.divert("@main@");
|
||||
story.dump_writer = options.dump_writer;
|
||||
for (comp_unit.knots) |compiled_chunk| {
|
||||
const chunk_name = comp_unit.resolveString(compiled_chunk.name_ref);
|
||||
var constant_pool: std.ArrayList(*Object) = .empty;
|
||||
try constant_pool.ensureUnusedCapacity(gpa, compiled_chunk.constants.len);
|
||||
defer constant_pool.deinit(gpa);
|
||||
|
||||
for (comp_unit.resolveConstants(compiled_chunk.constants)) |constant| {
|
||||
switch (constant) {
|
||||
.number => |value| {
|
||||
const object: *Object.Number = try .create(&story, .{ .integer = value });
|
||||
constant_pool.appendAssumeCapacity(&object.base);
|
||||
},
|
||||
.string => |ref| {
|
||||
const bytes = comp_unit.resolveString(ref);
|
||||
const object: *Object.String = try .create(&story, bytes);
|
||||
constant_pool.appendAssumeCapacity(&object.base);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const runtime_chunk: *Object.ContentPath = try .create(&story, .{
|
||||
.name = try .create(&story, chunk_name),
|
||||
.arity = @intCast(compiled_chunk.arity),
|
||||
.locals_count = @intCast(compiled_chunk.stack_size - compiled_chunk.arity),
|
||||
.const_pool = try constant_pool.toOwnedSlice(gpa),
|
||||
.bytes = try gpa.dupe(u8, comp_unit.resolveInstructions(compiled_chunk.instructions)),
|
||||
});
|
||||
try story.paths.append(gpa, &runtime_chunk.base);
|
||||
}
|
||||
|
||||
try story.divert("$__main__$");
|
||||
story.can_advance = true;
|
||||
return story;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue