local function bf(...)
    local file = ...

    local fs = require("fs")
    local script = fs.readAllText(file)
    local program = {}
    print("[Brainfuck] Loaded script")

    --pre parse script
    for c in script:gmatch(".") do
        if c:match("[%+%-%<%>%[%]%,%.]") then
            table.insert(program, c)
        end
    end
    print("[Brainfuck] Script parsed")

    local jumps = {}
    local stack = {}

    for i, c in ipairs(program) do
        if c == "[" then
            table.insert(stack, i)
        elseif c == "]" then
            assert(#stack > 0, "Unmatched ]")
            local start = table.remove(stack)
            jumps[start] = i
            jumps[i] = start
        end
    end
    print("[Brainfuck] Jumptable made")
    print("[Brainfuck] Initailizing...")
    local startime = syscall.getUptime()

    assert(#stack == 0, "Unmatched [")

    local tape = {0}
    local ptr = 1
    local ip = 1
    local itrs = 0
    while ip <= #program do
        local char = program[ip]
        if char == ">" then
            ptr=ptr+1
        elseif char == "<" then
            ptr=ptr-1
            if ptr<=0 then
                print("[Brainfuck] FATAL ERROR: OOB ERR")
                syscall.exit()
            end
        elseif char == "+" then
            local cur=tape[ptr] or 0
            tape[ptr]=(cur+1)%256
        elseif char == "-" then
            local cur=tape[ptr] or 0
            tape[ptr]=(cur-1)%256
        elseif char == "." then
            printInline(string.char(tape[ptr] or 0))
        elseif char == "," then
            local data = syscall.read(0, 1)
            if not data or data == "" then data = "\0" end
            tape[ptr]=data:byte()
        elseif char == "[" then
            if (tape[ptr] or 0)==0 then
                ip=jumps[ip]
            end
        elseif char == "]" then
            if (tape[ptr] or 0)~=0 then
                ip=jumps[ip]
            end
        end
        ip=ip+1
        itrs=itrs+1
    end
    print("[Brainfuck] Ran in "..tostring(itrs).." itrations")
    print("[Brainfuck] and "..tostring(syscall.getUptime()-startime))
    syscall.exit()
end

local ok,err = xpcall(bf, debug.traceback, ...)
if not ok then
    print("[Brainfuck] FATAL ERROR: "..err)
end