~ntgg/PGLang

ref: 97fab4ca046e0180ccfa8653858c19fa6dc6cdd3 PGLang/src/main.zig -rw-r--r-- 1.4 KiB View raw
97fab4caNoah Graff moved to stream based io, removed wasm temporarily 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const std = @import("std");
const Allocator = std.mem.Allocator;
const VM = @import("vm.zig").VM;

fn runBrainFuckSourceSlice(allocator: *Allocator, source: []const u8) !void {
    const stdin = std.io.getStdIn();
    const stdout = std.io.getStdOut();

    var vm = try VM(
        std.fs.File.InStream.Error,
        std.fs.File.OutStream.Error,
    ).init(
        allocator,
        source,
        &stdin.inStream().stream,
        &stdout.outStream().stream,
    );
    defer vm.deinit();
    try vm.run();
}

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = &arena.allocator;

    var args = std.process.args();
    var exe_path = try args.next(allocator) orelse unreachable;
    defer allocator.free(exe_path);
    var brainfuck_source_file_path = try args.next(allocator) orelse {
        std.debug.warn("Usage: {} FILE\n", .{exe_path});
        std.process.exit(1);
    };
    defer allocator.free(brainfuck_source_file_path);

    var brainfuck_source_file = try std.fs.cwd().openFile(brainfuck_source_file_path, .{});
    defer brainfuck_source_file.close();

    var brainfuck_source = try brainfuck_source_file.inStream().stream.readAllAlloc(allocator, 1024 * 1024 * 1024);
    defer allocator.free(brainfuck_source);

    try runBrainFuckSourceSlice(allocator, brainfuck_source);
}