feat: code generation and execution for simple choice statements

This commit is contained in:
Brett Broadhurst 2026-03-02 21:16:24 -07:00
parent 55346fcd85
commit 889f678dd8
Failed to generate hash of commit
4 changed files with 389 additions and 179 deletions

View file

@ -73,6 +73,9 @@ fn mainArgs(
};
};
const stdin = std.fs.File.stdin();
var stdin_reader = stdin.reader(&stdin_buffer);
const stdout = std.fs.File.stdout();
var stdout_writer = stdout.writer(&stdout_buffer);
@ -89,11 +92,26 @@ fn mainArgs(
try story.dump(&stderr_writer.interface);
if (compile_only) return;
while (story.can_advance) {
const content = try story.advance();
defer gpa.free(content);
while (!story.is_exited and story.can_advance) {
while (story.can_advance) {
const content_text = try story.advance(gpa);
defer gpa.free(content_text);
std.debug.print("{s}\n", .{content_text});
}
if (story.current_choices.items.len > 0) {
for (story.current_choices.items, 0..) |*choice, index| {
const choice_text = try choice.text.toOwnedSlice(gpa);
defer gpa.free(choice_text);
std.debug.print("[{d}]: {s}\n", .{ index + 1, choice_text });
}
std.debug.print("> ", .{});
std.debug.print("{s}\n", .{content});
const input_line = try stdin_reader.interface.takeDelimiter('\n');
if (input_line) |bytes| {
const choice_index = try std.fmt.parseUnsigned(usize, bytes, 10);
try story.selectChoiceIndex(if (choice_index == 0) 0 else choice_index - 1);
}
}
}
}