Skip to content
Rebecca G. Bettencourt edited this page Aug 28, 2014 · 1 revision

BF

Interpreter for a Turing-complete esoteric programming language, written in XION.

Note: This will only work properly in OpenXION 1.4 or later, where the existence test for chunk types has been fixed.

on bf program as string
    local tape as binary is empty
    local pointer as integer is zero
    local pc as integer is one
    local depth as integer
    open file "/dev/stdin" as "binary"
    open file "/dev/stdout" as "binary"
    repeat while there is a char pc of program
        put char pc of program into instruction
        add 1 to pc
        switch instruction
        case ">"
            add 1 to pointer
        case "<"
            subtract 1 from pointer
        case "+"
            add 1 to tinyInt pointer of tape
        case "-"
            subtract 1 from tinyInt pointer of tape
        case "."
            write byte pointer of tape to file "/dev/stdout"
        case ","
            read from file "/dev/stdin" for 1
            put it into byte pointer of tape
        case "["
            if tinyInt pointer of tape is zero then
                put zero into depth
                repeat while there is a char pc of program
                    put char pc of program into tmp
                    add 1 to pc
                    if tmp is "[" then
                        add 1 to depth
                    else if tmp is "]" then
                        if depth is zero then
                            exit repeat
                        end if
                        subtract 1 from depth
                    end if
                end repeat
            end if
        case "]"
            if tinyInt pointer of tape is not zero then
                put zero into depth
                repeat while there is a char pc-1 of program
                    subtract 1 from pc
                    put char pc of program into tmp
                    if tmp is "]" then
                        add 1 to depth
                    else if tmp is "[" then
                        subtract 1 from depth
                        if depth is zero then
                            exit repeat
                        end if
                    end if
                end repeat
            end if
        end switch
    end repeat
    close file "/dev/stdin"
    close file "/dev/stdout"
end bf

-- Hello World
bf "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
Clone this wiki locally