chore: added test cases for parsing simple stitches

This commit is contained in:
Brett Broadhurst 2026-03-26 09:01:56 -06:00
parent 6f54e6fbcd
commit 37ab29947a
Failed to generate hash of commit
4 changed files with 50 additions and 38 deletions

View file

@ -184,7 +184,7 @@ pub const Module = struct {
};
pub const Stitch = struct {
knot_index: Knot.Index,
knot_index: ?Knot.Index,
code_index: CodeChunk.Index,
name_index: InternPool.Constant.Index,
@ -252,6 +252,7 @@ pub const Module = struct {
}
while (mod.work_queue.pop()) |work_unit| {
const chunk_index = mod.intern_pool.code_chunks.items.len;
var knot_index: ?Knot.Index = null;
var chunk: Sema.Chunk = .{
.sema = &sema,
.code = try mod.createCodeChunk(),
@ -266,6 +267,7 @@ pub const Module = struct {
try sema.analyzeKnot(&chunk, work_unit.inst_index);
try chunk.finalize();
knot_index = @enumFromInt(mod.knots.items.len);
try mod.intern_pool.code_chunks.append(gpa, chunk.code);
try mod.knots.append(gpa, .{
.name_index = work_unit.decl_name,
@ -273,13 +275,12 @@ pub const Module = struct {
});
},
.stitch => {
const knot_index = mod.knots.items.len - 1;
try sema.analyzeStitch(&chunk, work_unit.inst_index);
try chunk.finalize();
try mod.intern_pool.code_chunks.append(gpa, chunk.code);
try mod.stitches.append(gpa, .{
.knot_index = @enumFromInt(knot_index),
.knot_index = knot_index,
.name_index = work_unit.decl_name,
.code_index = @enumFromInt(chunk_index),
});
@ -406,11 +407,12 @@ pub const Module = struct {
.code_bytes = try code_chunk.bytecode.toOwnedSlice(gpa),
}),
});
const parent_knot = mod.knots.items[@intFromEnum(stitch.knot_index)];
const parent_knot_name = mod.intern_pool.getStrBytes(mod.ir, parent_knot.name_index);
const parent_knot_obj: *Object.Knot = @ptrCast(story.globals.get(parent_knot_name).?);
try parent_knot_obj.members.put(story.allocator, name_bytes, &stitch_obj.base);
if (stitch.knot_index) |index| {
const parent_knot = mod.knots.items[@intFromEnum(index)];
const parent_knot_name = mod.intern_pool.getStrBytes(mod.ir, parent_knot.name_index);
const parent_knot_obj: *Object.Knot = @ptrCast(story.globals.get(parent_knot_name).?);
try parent_knot_obj.members.put(story.allocator, name_bytes, &stitch_obj.base);
}
}
story.string_bytes = mod.ir.string_bytes;
mod.ir.string_bytes = &.{};

View file

@ -225,6 +225,46 @@ test "parser: empty knot declarations" {
);
}
test "parser: stitch declarations, inside knot" {
try testEqual(
\\=== knot ===
\\= stitch
\\Hello, world!
,
\\File "<STDIN>"
\\`--KnotDecl <line:1, line:3>
\\ |--KnotProto <col:1, col:13>
\\ | `--Identifier `knot` <col:5, col:9>
\\ `--StitchDecl <line:2, line:3>
\\ |--StitchProto <col:1, col:9>
\\ | `--Identifier `stitch` <col:3, col:9>
\\ `--BlockStmt <line:3, line:3>
\\ `--ContentStmt <line:3, col:1:14>
\\ `--Content <col:1, col:14>
\\ `--StringLiteral `Hello, world!` <col:1, col:14>
\\
,
);
}
test "parser: stitch declarations, simple" {
try testEqual(
\\= stitch
\\Hello, world!
,
\\File "<STDIN>"
\\`--StitchDecl <line:1, line:2>
\\ |--StitchProto <col:1, col:9>
\\ | `--Identifier `stitch` <col:3, col:9>
\\ `--BlockStmt <line:2, line:2>
\\ `--ContentStmt <line:2, col:1:14>
\\ `--Content <col:1, col:14>
\\ `--StringLiteral `Hello, world!` <col:1, col:14>
\\
,
);
}
fn testEqual(source_bytes: [:0]const u8, expected_ast: []const u8) !void {
const gpa = std.testing.allocator;
var arena_allocator = std.heap.ArenaAllocator.init(gpa);

View file

@ -1,17 +0,0 @@
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
// CHECK: File "<STDIN>"
// CHECK-NEXT: `--KnotDecl <line:15, line:17>
// CHECK-NEXT: |--KnotProto <col:1, col:13>
// CHECK-NEXT: | `--Identifier `knot` <col:5, col:9>
// CHECK-NEXT: `--StitchDecl <line:16, line:17>
// CHECK-NEXT: |--StitchProto <col:1, col:9>
// CHECK-NEXT: | `--Identifier `stitch` <col:3, col:9>
// CHECK-NEXT: `--BlockStmt <line:17, line:17>
// CHECK-NEXT: `--ContentStmt <line:17, col:1:14>
// CHECK-NEXT: `--Content <col:1, col:14>
// CHECK-NEXT: `--StringLiteral `Hello, world!` <col:1, col:14>
=== knot ===
= stitch
Hello, world!

View file

@ -1,13 +0,0 @@
// RUN: %ink-compiler --stdin --compile-only --dump-ast < %s | FileCheck %s
// CHECK: File "<STDIN>"
// CHECK-NEXT: `--StitchDecl <line:12, line:13>
// CHECK-NEXT: |--StitchProto <col:1, col:9>
// CHECK-NEXT: | `--Identifier `stitch` <col:3, col:9>
// CHECK-NEXT: `--BlockStmt <line:13, line:13>
// CHECK-NEXT: `--ContentStmt <line:13, col:1:14>
// CHECK-NEXT: `--Content <col:1, col:14>
// CHECK-NEXT: `--StringLiteral `Hello, world!` <col:1, col:14>
= stitch
Hello, world!