~ntgg/PGLang

ref: 5b64a42f5b965a902bb3d349ffb583b4738b8dac PGLang/src/main.zig -rw-r--r-- 1.0 KiB View raw
5b64a42fNoah Graff moved VM to seperate file 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
const std = @import("std");
const Allocator = std.mem.Allocator;
const VM = @import("vm.zig").VM;

pub export fn runBrainFuckSource(source: [*c]const u8, length: usize) void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();
    const allocator = &arena.allocator;

    var list = std.ArrayList(u8).init(allocator);
    var i = @as(usize, 0);
    while (i < length) : (i += 1) {
        list.append(source[i]) catch return;
    }

    // const source_slice = source[0..length];
    const source_slice = list.toOwnedSlice();
    runBrainFuckSourceSlice(allocator, source_slice) catch {
        return;
    };
}
// from https://github.com/fengb/fundude/blob/master/src/wasm.zig
pub export fn malloc(size: usize) ?*c_void {
    const result = std.heap.page_allocator.alloc(u8, size) catch return null;
    return result.ptr;
}

fn runBrainFuckSourceSlice(allocator: *Allocator, source: []const u8) !void {
    var vm = try VM.init(allocator, source);
    defer vm.deinit();
    try vm.run();
}