~ntgg/PGLang

5b56eb7b47cdd3260552a99334bef31afe56c4c1 — Noah Graff 11 months ago e0e88e3
Fixed isuse with loop parsing logic
2 files changed, 25 insertions(+), 8 deletions(-)

M src/main.zig
M src/parse.zig
M src/main.zig => src/main.zig +7 -3
@@ 16,7 16,12 @@ pub const VM = struct {
    pub const StdInStream = std.fs.File.InStream.Stream;
    pub const StdOutStream = std.fs.File.OutStream.Stream;

    pub fn init(allocator: *Allocator, stdin: *StdInStream, stdout: *StdOutStream, source: []const u8,) !VM {
    pub fn init(
        allocator: *Allocator,
        stdin: *StdInStream,
        stdout: *StdOutStream,
        source: []const u8,
    ) !VM {
        var memory = try allocator.alloc(u8, 30000);
        std.mem.secureZero(u8, memory);
        return VM{


@@ 47,7 52,6 @@ pub const VM = struct {
            vm.memory = try vm.allocator.realloc(vm.memory, std.math.max(vm.memory.len * 2, vm.current_cell));
        }


        const instr = vm.instructions.toSliceConst()[vm.current_instr];
        const cell = &vm.memory[vm.current_cell];
        switch (instr) {


@@ 91,7 95,7 @@ pub fn main() anyerror!u8 {
            \\Usage:
            \\  {} <file>
            \\
            ,
        ,
            exe_path,
        );
        return 1;

M src/parse.zig => src/parse.zig +18 -5
@@ 140,7 140,7 @@ pub fn parse(allocator: *Allocator, source: []const u8) !std.ArrayList(Instructi
                }
                state = State{ .Scanning = {} };
                const loop_start_idx = loop_stack.popOrNull() orelse return error.MismatchedLoop;
                instructions.set(loop_start_idx, Instruction{ .LoopStart = instructions.len - 1 });
                instructions.set(loop_start_idx, Instruction{ .LoopStart = instructions.len });
                try instructions.append(Instruction{ .LoopEnd = loop_start_idx });
            },
            else => {},


@@ 154,14 154,14 @@ test "parse basic" {
    const program = "++++++++++[>+++++++++>+<<-]>+++++++.>.";
    const expected = [_]Instruction{
        Instruction{ .Incr = 10 },
        Instruction{ .LoopStart = {} },
        Instruction{ .LoopStart = 8 },
        Instruction{ .Next = 1 },
        Instruction{ .Incr = 9 },
        Instruction{ .Next = 1 },
        Instruction{ .Incr = 1 },
        Instruction{ .Prev = 2 },
        Instruction{ .Decr = 1 },
        Instruction{ .LoopEnd = {} },
        Instruction{ .LoopEnd = 1 },
        Instruction{ .Next = 1 },
        Instruction{ .Incr = 7 },
        Instruction{ .Output = {} },


@@ 169,6 169,19 @@ test "parse basic" {
        Instruction{ .Output = {} },
    };

    const parsed = try parse(program);
    for (expected) |i, exp_istr| {}
    const parsed = (try parse(std.heap.direct_allocator, program)).toOwnedSlice();
    std.testing.expectEqual(parsed[0].Incr, expected[0].Incr);
    std.testing.expectEqual(parsed[1].LoopStart, expected[1].LoopStart);
    std.testing.expectEqual(parsed[2].Next, expected[2].Next);
    std.testing.expectEqual(parsed[3].Incr, expected[3].Incr);
    std.testing.expectEqual(parsed[4].Next, expected[4].Next);
    std.testing.expectEqual(parsed[5].Incr, expected[5].Incr);
    std.testing.expectEqual(parsed[6].Prev, expected[6].Prev);
    std.testing.expectEqual(parsed[7].Decr, expected[7].Decr);
    std.testing.expectEqual(parsed[8].LoopEnd, expected[8].LoopEnd);
    std.testing.expectEqual(parsed[9].Next, expected[9].Next);
    std.testing.expectEqual(parsed[10].Incr, expected[10].Incr);
    std.testing.expectEqual(parsed[11].Output, expected[11].Output);
    std.testing.expectEqual(parsed[12].Next, expected[12].Next);
    std.testing.expectEqual(parsed[13].Output, expected[13].Output);
}