~ntgg/PGLang

633e512be44183be428d0df5373659d0aa38dc81 — Noah Graff 7 months ago 22f028b
It works on wasm now, but real slow.

Removed essentially all old functionalty and now just does brainfuck
for wasm. Needs a bunch of improvements on the js side to work, but
prints out simple.bf, so I consider it a kinda working product.
M .gitignore => .gitignore +1 -1
@@ 1,2 1,2 @@
zig-cache/
*.bf
*.wasm

A brainfuck-examples/cat.bf => brainfuck-examples/cat.bf +1 -0
@@ 0,0 1,1 @@
,[.,]

A brainfuck-examples/fib.bf => brainfuck-examples/fib.bf +10 -0
@@ 0,0 1,10 @@
>++++++++++>+>+[
    [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
        [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
            [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
    ]<<<
]
This program doesn't terminate; you will have to kill it.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/


A brainfuck-examples/game-of-life.bf => brainfuck-examples/game-of-life.bf +50 -0
@@ 0,0 1,50 @@

                            Linus Akesson presents:
                   The Game Of Life implemented in Brainfuck

       +>>++++[<++++>-]<[<++++++>-]+[<[>>>>+<<<<-]>>>>[<<<<+>>>>>>+<<-]<+
   +++[>++++++++<-]>.[-]<+++[>+++<-]>+[>>.+<<-]>>[-]<<<++[<+++++>-]<.<<[>>>>+
 <<<<-]>>>>[<<<<+>>>>>>+<<-]<<[>>>>.+<<<++++++++++[<[>>+<<-]>>[<<+>>>>>++++++++
 +++<<<-]<[>+<-]>[<+>>>>+<<<-]>>>[>>>>>>>>>>>>+>+<<     <<<<<<<<<<<-]>>>>>>>>>>
>>[-[>>>>+<<<<-]>[>>>>+<<<<-]>>>]>      >>[<<<+>>  >-    ]<<<[>>+>+<<<-]>[->[<<<
<+>>>>-]<[<<<  <+>      >>>-]<<<< ]<     ++++++  ++       +[>+++++<-]>>[<<+>>-]<
<[>---<-]>.[- ]         <<<<<<<<< <      <<<<<< <         -]++++++++++.[-]<-]>>>
>[-]<[-]+++++           +++[>++++        ++++<     -     ]>--.[-]<,----------[<+
>-]>>>>>>+<<<<< <     <[>+>>>>>+>[      -]<<<      <<   <<-]>++++++++++>>>>>[[-]
<<,<<<<<<<->>>> >    >>[<<<<+>>>>-]<<<<[>>>>+      >+<<<<<-]>>>>>----------[<<<<
<<<<+<[>>>>+<<<      <-]>>>>[<<<<+>>>>>>+<<-      ]>[>-<-]>++++++++++[>+++++++++
++<-]<<<<<<[>>>      >+<<<<-]>>>>[<<<<+>>>>>      >+<<-]>>>>[<<->>-]<<++++++++++
[>+<-]>[>>>>>>>      >>>>>+>+<<<<      <<<<<      <<<<-]>>> >>     >>>>>>>[-[>>>
>+<<<<-]>[>>>>       +<<<<-]>> >       ]>> >           [<< <        +>>>-]+<<<[>
>>-<<<-]>[->[<      <<<+>>>>-]         <[ <            < <           <+>>>>-]<<<
<]<<<<<<<<<<<, [    -]]>]>[-+++        ++               +    +++     ++[>+++++++
++++>+++++++++ +    +<<-]>[-[>>>      +<<<-      ]>>>[ <    <<+      >>>>>>>+>+<
<<<<-]>>>>[-[> >    >>+<<<<-]>[>      >>>+< <    <<-]> >    >]>      >>[<<<+>>>-
]<<<[>>+>+<<< -     ]>[->[<<<<+>      >>>-] <    [<<< <    +>>       >>-]<<<<]<<
<<<<<<[>>>+<< <     -]>>>[<<<+>>      >>>>> +    >+<< <             <<-]<<[>>+<<
-]>>[<<+>>>>>      >+>+<<<<<-]>>      >>[-[ >    >>>+ <            <<<-]>[>>>>+<
<<<-]>[>>>>+<      <<<-]>>]>>>[ -    ]<[>+< -    ]<[ -           [<<<<+>>>>-]<<<
<]<<<<<<<<]<<      <<<<<<<<++++ +    +++++  [   >+++ +    ++++++[<[>>+<<-]>>[<<+
>>>>>++++++++ +    ++<<<     -] <    [>+<- ]    >[<+ >    >>>+<<<-]>>>[<<<+>>>-]
<<<[>>>+>>>>  >    +<<<<     <<      <<-]> >    >>>>       >>>[>>+<<-]>>[<<+<+>>
>-]<<<------ -    -----[     >>      >+<<< -    ]>>>       [<<<+> > >>>>>+>+<<<<
<-]>>>>[-[>> >    >+<<<<    -] >     [>>>> +    <<<<-       ]>>> ]  >>>[<<<+>>>-
]<<<[>>+>+<< <    -]>>>     >>           > >    [<<<+               >>>-]<<<[>>>
+<<<<<+>>-                  ]>           >     >>>>>[<             <<+>>>-]<<<[>
>>+<<<<<<<                  <<+         >      >>>>>-]<          <<<<<<[->[<<<<+
>>>>-]<[<<<<+>>>>-]<<<<]>[<<<<<<    <+>>>      >>>>-]<<<<     <<<<<+++++++++++[>
>>+<<<-]>>>[<<<+>>>>>>>+>+<<<<<-]>>>>[-[>     >>>+<<<<-]>[>>>>+<<<<-]>>>]>>>[<<<
+>>>-]<<<[>>+>+<<<-]>>>>>>>[<<<+>>>-]<<<[     >>>+<<<<<+>>-]>>>>>>>[<<<+>>>-]<<<
[>>>+<<<<<<<<<+>>>>>>-]<<<<<<<[->[< <  <     <+>>>>-]<[<<<<+>>>>-]<<<<]>[<<<<<<<
+>>>>>>>-]<<<<<<<<<+++++++++++[>>> >        >>>+>+<<<<<<<<-]>>>>>>>[-[>>>>+<<<<-
]>[>>>>+<<<<-]>>>]>>>[<<<+>>>-]<<< [       >>+>+<<<-]>>>>>>>[<<<+>>>-]<<<[>>>+<<
<<<+>>-]>>>>>>>[<<<+>>>-]<<<[>>>+<        <<<<<<<<+>>>>>>-]<<<<<<<[->[<<<<+>>>>-
 ]<[<<<<+>>>>-]<<<<]>[<<<<<<<+>>>>>      >>-]<<<<<<<----[>>>>>>>+<<<<<<<+[>>>>>
 >>-<<<<<<<[-]]<<<<<<<[>>>>>>>>>>>>+>+<<<<<<<<<<<<<-][   lft@df.lth.se   ]>>>>>
   >>>>>>>[-[>>>>+<<<<-]>[>>>>+<<<<-]>[>>>>+<<<<-]>>]>>>[-]<[>+<-]<[-[<<<<+>>
       >>-]<<<<]<<<<<<[-]]<<<<<<<[-]<<<<-]<-]>>>>>>>>>>>[-]<<]<<<<<<<<<<]

        Type for instance "fg" to toggle the cell at row f and column g
                   Hit enter to calculate the next generation
                                 Type q to quit


A brainfuck-examples/mandalbrot.bf => brainfuck-examples/mandalbrot.bf +146 -0
@@ 0,0 1,146 @@
 A mandelbrot set fractal viewer in brainfuck written by Erik Bosman
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+[>>>>>>>>[-]>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+
<<<<<<<+++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>>+>>>>>>>>>>>>>>>>>>>>>>>>>>
>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+[>>>>>>[>>>>>>>[-]>>]<<<<<<<<<[<<<<<<<<<]>>
>>>>>[-]+<<<<<<++++[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<+++++++[-[->>>
>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>>+<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[[-]>>>>>>[>>>>>
>>[-<<<<<<+>>>>>>]<<<<<<[->>>>>>+<<+<<<+<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>
[>>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<+<<]>>>>>>>>]<<<<<<<<<[<<<<<<<
<<]>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<[->>>>>>>+<<+<<<<<]>>>>>>>>>+++++++++++++++[[
>>>>>>>>>]+>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]>[-]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>-]+[
>+>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>>[>->>>>[-<<<<+>>>>]<<<<[->>>>+<<<<<[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<]>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>[-]<<<++++
+[-[->>>>>>>>>+<<<<<<<<<]>>>>>>>>>]>>>>>->>>>>>>>>>>>>>>>>>>>>>>>>>>-<<<<<<[<<<<
<<<<<]]>>>]


A brainfuck-examples/numwarp.bf => brainfuck-examples/numwarp.bf +35 -0
@@ 0,0 1,35 @@
>>>>+>+++>+++>>>>>+++[
  >,+>++++[>++++<-]>[<<[-[->]]>[<]>-]<<[
    >+>+>>+>+[<<<<]<+>>[+<]<[>]>+[[>>>]>>+[<<<<]>-]+<+>>>-[
      <<+[>]>>+<<<+<+<--------[
        <<-<<+[>]>+<<-<<-[
          <<<+<-[>>]<-<-<<<-<----[
            <<<->>>>+<-[
              <<<+[>]>+<<+<-<-[
                <<+<-<+[>>]<+<<<<+<-[
                  <<-[>]>>-<<<-<-<-[
                    <<<+<-[>>]<+<<<+<+<-[
                      <<<<+[>]<-<<-[
                        <<+[>]>>-<<<<-<-[
                          >>>>>+<-<<<+<-[
                            >>+<<-[
                              <<-<-[>]>+<<-<-<-[
                                <<+<+[>]<+<+<-[
                                  >>-<-<-[
                                    <<-[>]<+<++++[<-------->-]++<[
                                      <<+[>]>>-<-<<<<-[
                                        <<-<<->>>>-[
                                          <<<<+[>]>+<<<<-[
                                            <<+<<-[>>]<+<<<<<-[
                                              >>>>-<<<-<-
  ]]]]]]]]]]]]]]]]]]]]]]>[>[[[<<<<]>+>>[>>>>>]<-]<]>>>+>>>>>>>+>]<
]<[-]<<<<<<<++<+++<+++[
  [>]>>>>>>++++++++[<<++++>++++++>-]<-<<[-[<+>>.<-]]<<<<[
    -[-[>+<-]>]>>>>>[.[>]]<<[<+>-]>>>[<<++[<+>--]>>-]
    <<[->+<[<++>-]]<<<[<+>-]<<<<
  ]>>+>>>--[<+>---]<.>>[[-]<<]<
]
[Enter a number using ()-./0123456789abcdef and space, and hit return.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]


A brainfuck-examples/simple.bf => brainfuck-examples/simple.bf +13 -0
@@ 0,0 1,13 @@
++++++++++  set cell #0 to 10
[           loop 10 times based on cell #0
>+++++++++  add 9 to cell #1
>+          add 1 to cell #2
<<
-           go back to cell #0 for the loop
]
>
+++++++     add 7 to cell #1 to increase it to 97
.           output cell #1 (97 = ascii a)
>
.           output cell #2 (10 = ascii newline)


M build.zig => build.zig +4 -11
@@ 2,15 2,8 @@ const Builder = @import("std").build.Builder;

pub fn build(b: *Builder) void {
    const mode = b.standardReleaseOptions();
    const target = b.standardTargetOptions(null);
    const exe = b.addExecutable("bfzk", "src/main.zig");
    exe.setBuildMode(mode);
    exe.setTheTarget(target);
    exe.install();

    const run_cmd = exe.run();
    run_cmd.step.dependOn(b.getInstallStep());

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
    const lib = b.addStaticLibrary("bfzk", "src/main.zig");
    lib.setBuildMode(mode);
    lib.setTarget(.wasm32, .freestanding, .none);
    lib.install();
}

A index.html => index.html +13 -0
@@ 0,0 1,13 @@
<html>
  <head>
    <meta charset='utf-8'>
    <title>Brainfuck WASM in Zig</title>
  </head>

  <body>
    <textarea id='bfzk-input' placeholder='BrainFuck source'></textarea>
    <input type='button' id='bfzk-run' value='Loading WASM module...'/>
    <div id='bfzk-output'></div>
    <script src='index.js'></script>
  </body>
</html>

A index.js => index.js +49 -0
@@ 0,0 1,49 @@
if (!('WebAssembly' in window)) {
    alert('You need a browser with WebAssembly support enabled');
    throw 'Missing WASM support';
}

function readByte() {
    console.log('err');
    return 0;
}

function writeByte(byte) {
    document.getElementById('bfzk-output').innerHTML += String.fromCharCode(byte);
}

var logBuffer = [];
const importObject = {
    env: {
        readByte,
        writeByte(byte) {
            document.getElementById('bfzk-output').innerHTML += String.fromCharCode(byte);
        },
        logBufferByte(byte) {
            logBuffer.push(byte);
        },
        logFlush() {
            const decoder = new TextDecoder();
            console.log(decoder.decode(new Uint8Array(logBuffer)));
            logBuffer = [];
        }
    }
};

WebAssembly.instantiateStreaming(fetch('bfzk.wasm'), importObject)
    .then(obj => {
        console.log(JSON.stringify(obj));
        const exports = obj.instance.exports;
        const runBrainFuckSource = exports.runBrainFuckSource;
        const runButton = document.getElementById('bfzk-run');
        runButton.value = 'Run';
        runButton.addEventListener('click', function() {
            const source = document.getElementById('bfzk-input').value;
            const encoder = new TextEncoder();
            const utf8 = encoder.encode(source);
            const ptr = exports.malloc(utf8.length);
            const passArray = new Uint8Array(exports.memory.buffer, ptr, utf8.length);
            passArray.set(utf8);
            runBrainFuckSource(ptr, passArray.length);
        }, false);
    });

A runserver.py => runserver.py +17 -0
@@ 0,0 1,17 @@
#!/usr/bin/env python3

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
    '.wasm': 'application/wasm',
})

socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    httpd.allow_reuse_address = True
    print("serving at port", PORT)
    httpd.serve_forever()

M src/main.zig => src/main.zig +32 -52
@@ 3,49 3,41 @@ const parse = @import("parse.zig").parse;
const Allocator = std.mem.Allocator;
const Instruction = @import("parse.zig").Instruction;

extern fn readByte() u8;
extern fn writeByte(byte: u8) void;

pub const VM = struct {
    const Self = @This();

    allocator: *Allocator,
    instructions: std.ArrayList(Instruction),
    current_instr: usize,
    memory: []u8,
    current_cell: usize,

    stdin: *StdInStream,
    stdout: *StdOutStream,

    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, source: []const u8) !Self {
        var memory = try allocator.alloc(u8, 30000);
        std.mem.secureZero(u8, memory);
        return VM{
        return Self{
            .allocator = allocator,
            .instructions = try parse(allocator, source),
            .current_instr = 0,
            .memory = memory,
            .current_cell = 0,
            .stdin = stdin,
            .stdout = stdout,
        };
    }

    pub fn deinit(vm: *VM) void {
    pub fn deinit(vm: *Self) void {
        vm.instructions.deinit();
        vm.allocator.free(vm.memory);
    }

    pub fn run(vm: *VM) !void {
    pub fn run(vm: *Self) !void {
        while (try vm.step()) {}
    }

    // false if should finish
    pub fn step(vm: *VM) !bool {
    pub fn step(vm: *Self) !bool {
        if (vm.current_instr >= vm.instructions.len) return false;

        if (vm.current_cell > vm.memory.len) {


@@ 65,10 57,10 @@ pub const VM = struct {
                vm.current_cell -= cells;
            },
            .Output => {
                try vm.stdout.print("{}", [1]u8{cell.*});
                writeByte(cell.*);
            },
            .Input => {
                cell.* = try vm.stdin.readByte();
                cell.* = readByte();
            },
            .LoopStart => |end| if (cell.* == 0) {
                // parse returns only vaild code, so .? is safe


@@ 84,43 76,31 @@ pub const VM = struct {
    }
};

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

    var args_iter = std.process.args();
    const exe_path = args_iter.next(allocator);
    const path = if (args_iter.next(allocator)) |arg| try arg else {
        std.debug.warn(
            \\Usage:
            \\  {} <file>
            \\
        ,
            exe_path,
        );
        return 1;
    };

    var file = try std.fs.File.openRead(path);
    defer file.close();
    const bfzk_src = try file.inStream().stream.readAllAlloc(allocator, std.mem.page_size * 1024);

    var stdin_file = try std.io.getStdIn();
    defer stdin_file.close();
    const stdin = &stdin_file.inStream().stream;
    var list = std.ArrayList(u8).init(allocator);
    var i = usize(0);
    while (i < length) : (i += 1) {
        list.append(source[i]) catch return;
    }

    var stdout_file = try std.io.getStdOut();
    defer stdout_file.close();
    const stdout = &stdout_file.outStream().stream;
    // 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.wasm_allocator.alloc(u8, size) catch return null;
    return result.ptr;
}

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

M src/parse.zig => src/parse.zig +1 -5
@@ 1,4 1,5 @@
const std = @import("std");
const jsLog = @import("root").jsLog;
const Allocator = std.mem.Allocator;

pub const Instruction = union(enum) {


@@ 18,11 19,6 @@ const State = union(enum) {
    Next: usize,
    Prev: usize,
    Scanning,

    pub const Loop = struct {
        inner_state: *State,
        start_cell: usize,
    };
};

pub fn parse(allocator: *Allocator, source: []const u8) !std.ArrayList(Instruction) {