commit f3684e186858adfbf2faa44afe6728ae78234433
parent 3fec1ee63332210509a4935ea87427e19f84a551
Author: Walther Chen <walther.chen@gmail.com>
Date:   Fri, 11 Nov 2022 21:46:59 -0500

add parse to runIntpreter params

Diffstat:
Msrc/common.zig | 11++++++-----
Msrc/og.zig | 4++--
Msrc/opt1.zig | 5++---
3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/common.zig b/src/common.zig @@ -7,7 +7,7 @@ const ArrayList = std.ArrayList; const MEMORY_SIZE = 30000; -pub fn runInterpreter(interpret: anytype) anyerror!void { +pub fn runInterpreter(parse: anytype, interpret: anytype) anyerror!void { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); const alloc = arena.allocator(); @@ -30,7 +30,7 @@ pub fn runInterpreter(interpret: anytype) anyerror!void { try interpret(program, &memory, stdin.reader(), stdout.writer(), alloc); } -fn parse(src: []const u8, alloc: std.mem.Allocator) !Program { +pub fn parseProgram(src: []const u8, alloc: std.mem.Allocator) !Program { var instructions = ArrayList(u8).init(alloc); for (src) |c| { @@ -51,10 +51,9 @@ pub const Program = struct { } }; -pub fn testHelloWorld(interpret: anytype) anyerror!void { +pub fn testHelloWorld(parse: anytype, interpret: anytype) anyerror!void { const expectEqualSlices = std.testing.expectEqualSlices; - // Doesn't test parsing stage (so this input cannot have comments) const hello_world = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; var list = ArrayList(u8).init(std.testing.allocator); @@ -67,7 +66,9 @@ pub fn testHelloWorld(interpret: anytype) anyerror!void { var rdr = empty_in.reader(); var wtr = list.writer(); - const program = Program{ .instructions = hello_world, .alloc = std.testing.allocator }; + const program = try parse(hello_world, std.testing.allocator); + defer program.deinit(); + try interpret(program, &memory, rdr, wtr, std.testing.allocator); try expectEqualSlices(u8, "Hello World!\n", list.items); } diff --git a/src/og.zig b/src/og.zig @@ -3,11 +3,11 @@ const common = @import("common.zig"); const Program = common.Program; pub fn main() anyerror!void { - try common.runInterpreter(interpret); + try common.runInterpreter(common.parseProgram, interpret); } test "og: interpret hello world" { - try common.testHelloWorld(interpret); + try common.testHelloWorld(common.parseProgram, interpret); } fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc: std.mem.Allocator) !void { diff --git a/src/opt1.zig b/src/opt1.zig @@ -15,15 +15,14 @@ pub fn main() anyerror!void { std.debug.print("Building with TRACE enabled\n", .{}); } - try common.runInterpreter(interpret); + try common.runInterpreter(common.parseProgram, interpret); } test "og: interpret hello world" { - try common.testHelloWorld(interpret); + try common.testHelloWorld(common.parseProgram, interpret); } fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc: Allocator) !void { - var instruction_count = if (TRACE) std.AutoHashMap(u8, usize).init(alloc) else undefined; comptime var instruction_count = if (TRACE) std.AutoHashMap(u8, usize).init(alloc) else undefined; if (TRACE) { defer instruction_count.deinit();