A downloadable tool

10 Line BASIC 

=============

Category : SCHAU PUR-80 

TITLE    : Brainf*ck interpreter

PLATFORM : Oric Atmos

LANGUAGE : BASIC 1.1b

AUTHOR   : Rax

VIDEO    :


Parameters:

    Maximum program size: 5000 bytes

    Memory: 300 cells

    Stack size: 99

These values can be changed on line 0 of the program.


Features:

    Full-fledged Brainfuck interpreter

    Supports program input and keyboard input

    You can also paste a program using the emulator’s Paste function (right-click → Paste)

    Turbo Mode (F6) can be enabled for faster execution

    Note: Long programs, like “99 bottles of beer” (~1.8k bytes), will still require patience even in Turbo Mode.


Extras:

    A few test programs are included for experimentation

    Online Brainfuck generators can also be used for testing

Sometimes the interpreter freezes and may appear to be stuck, but at that moment the BASIC interpreter is actually optimizing its arrays. You just need to wait for this process to finish.

The program, interpreter memory, and stack are stored in arrays. It would have been possible to use direct memory access via POKE and PEEK, which could improve performance, but I chose to use only BASIC variables.

The only shorthand in ORIC is for the PRINT command, represented by the ? symbol.

When the ? character is entered in the code, it is displayed as the PRINT token when the LIST command is used.

Code:

0---------1---------2---------3---------4---------5---------6---------7-----78
0CLS:DIMP$(5000),M(300),S(99):R=20:?:?"Brainf*ck":?"Entr prg - ESC for exit"
1GOSUB8:?"Output":REPEAT:C$=P$(PC):IFC$=","THENGETZ$:Z=ASC(Z$):M(R)=Z*-(Z<>13)
2M(R)=M(R)+(C$="-")-(C$="+"):IFM(R)>255THENM(R)=0ELSE:IFM(R)<0THENM(R)=255
3IFC$="."ORC$=","THENIFM(R)=10ORM(R)=0THEN?CHR$(10)CHR$(13);ELSE?CHR$(M(R));
4R=R-(C$=">")+(C$="<"):IFC$="["THENIFM(R)>0THENS(S)=PC-1:S=S+1ELSEGOSUB7
5IFC$="]"THENIFM(R)THENS=S-1:PC=S(S)ELSES=S-1
6PLOT2,0,STR$(I):I=I+1:PC=PC+1:UNTILPC=L:END
7D=1:REPEAT:PC=PC+1:Z$=P$(PC):D=D-(Z$="[")+(Z$="]"):UNTILD=0:RETURN
8GETA$:P$(L)=A$:A=ASC(A$):IFA=127ANDL>0THENL=L-2ELSE:IFA=13THEN?CHR$(10);
9IFA<>27THEN?A$;:L=L+1:PLOT2,0,STR$(L): GOTO8ELSE:?:?:PLOT2,0,"      ":RETURN


CODE EXPLANATION:

0CLS:DIMM(300),S(99),P$(9000):R=20:?:?"Brainf*ck":?"Entr prg - ESC for exit"

    CLS                                                         - clear the screen

    DIM P$(5000)                                                - array for storing the program source code

    DIM M(300)                                                  - memory array (Brainfuck tape cells)

    DIM S(99)                                                   - stack array used for loop addresses

    R=20                                                        - initial tape pointer position

    PRINT                                                       - blank line

    PRINT "Brainf*ck"                                           - print interpreter name

    PRINT "Entr prg - ESC for exit"                             - prompt user to enter program

1GOSUB8:?"Output":REPEAT:C$=P$(PC):IFC$=","THENGETZ$:Z=ASC(Z$):M(R)=Z*-(Z<>13)

    GOSUB8                                                      - call routine to read Brainfuck program from keyboard

    PRINT "Output"                                              - print output header

    REPEAT                                                      - start interpreter loop

        C$=P$(PC)                                               - get current instruction from program memory

        IF C$="," THEN                                          - if instruction is input

            GET Z$                                              - read one character from keyboard

            Z=ASC(Z$)                                           - convert to ASCII value

            M(R)=Z*-(Z<>13)                                     - store value in current memory cell (ignore ENTER)

2M(R)=M(R)+(C$="-")-(C$="+"):IFM(R)>255THENM(R)=0ELSE:IFM(R)<0THENM(R)=255

        M(R)=M(R)+(C$="-")-(C$="+")                             - decrement or increment memory cell depending on command

        IF M(R)>255 THEN                                        - wrap value if it exceeds byte range

            M(R)=0

        ELSE

            :                                                   - nothing

        IF M(R)<0 THEN                                          - wrap if negative

            M(R)=255

3IFC$="."ORC$=","THENIFM(R)=10ORM(R)=0THEN?CHR$(10)CHR$(13);ELSE?CHR$(M(R));

        IF C$="." OR C$="," THEN                                - if output command or after input

            IF M(R)=10 OR M(R)=0 THEN                           - if newline or zero

                PRINT CHR$(10)CHR$(13);                         - print newline

            ELSE

                PRINT CHR$(M(R));                               - print character from memory cell

4R=R-(C$=">")+(C$="<"):IFC$="["THENIFM(R)>0THENS(S)=PC-1:S=S+1ELSEGOSUB7

        R=R-(C$=">")+(C$="<")                                   - move memory pointer (left or right)

        IF C$="[" THEN                                          - start of loop

            IF M(R)>0 THEN                                      - if current cell is non-zero

                S(S)=PC-1                                       - push loop start address on stack

                S=S+1                                           - increase stack pointer

            ELSE

                GOSUB7                                          - skip loop if current cell is zero

5IFC$="]"THENIFM(R)THENS=S-1:PC=S(S)ELSES=S-1

        IF C$="]" THEN                                          - loop end

            IF M(R) THEN                                        - if current cell is non-zero

                S=S-1                                           - decrease stack pointer

                PC=S(S)                                         - jump back to matching "["

            ELSE

                S=S-1                                           - pop stack and continue execution

6PLOT2,0,STR$(I):I=I+1:PC=PC+1:UNTILPC=L:END

        PLOT2,0,STR$(I)                                         - display executed instruction counter

        I=I+1                                                   - increase instruction counter

        PC=PC+1                                                 - move to next instruction

    UNTIL PC=L                                                  - stop when end of program reached

    END                                                         - terminate interpreter

7D=1:REPEAT:PC=PC+1:Z$=P$(PC):D=D-(Z$="[")+(Z$="]"):UNTILD=0:RETURN

    D=1                                                         - nesting depth counter

    REPEAT                                                      - skip forward to matching ']'

        PC=PC+1                                                 - move to next instruction

        Z$=P$(PC)                                               - read instruction

        D=D-(Z$="[")+(Z$="]")                                   - adjust nesting depth

    UNTIL D=0                                                   - stop when matching bracket found

    RETURN                                                      - return to interpreter

8GETA$:P$(L)=A$:A=ASC(A$):IFA=127ANDL>0THENL=L-2ELSE:IFA=13THEN?CHR$(10);

    GET A$                                                      - read one character from keyboard

    P$(L)=A$                                                    - store character into program array

    A=ASC(A$)                                                   - get ASCII code

    IF A=127 AND L>0 THEN                                       - if BACKSPACE pressed

        L=L-2                                                   - remove previous character

    ELSE

        :                                                       - nothing

    IF A=13 THEN                                                - if ENTER pressed

        PRINT CHR$(10);                                         - print newline

9IFA<>27THEN?A$;:L=L+1:PLOT2,0,STR$(L): GOTO8ELSE:?:?:PLOT2,0,"      ":RETURN

    IF A<>27 THEN                                               - if key is not ESC

        PRINT A$;                                               - echo typed character

        L=L+1                                                   - increase program length

        PLOT2,0,STR$(L)                                         - show current program length

        GOTO8                                                   - continue reading program

    ELSE                                                        - if ESC pressed

        :                                                       - nothing

    PRINT                                                       - blank line

    PRINT                                                       - blank line

    PLOT2,0,"      "                                            - clear top line counter

    RETURN                                                      - finish program input


VARIABLES

A - ASCII code of the currently typed character when entering the Brainfuck program.

A$ - Single character read from the keyboard while entering the Brainfuck program.

C$ - Current Brainfuck instruction being executed (one character from the program).

D - Loop depth counter used while skipping a Brainfuck loop when the interpreter encounters "[" with a zero cell.

    It counts nested brackets until the matching "]" is found.

I - Instruction execution counter.

    Used only for debugging/visual feedback and shown on the screen while the interpreter runs.

L - Length of the Brainfuck program entered by the user.

    Also used as the current index while reading the program into memory.

M - Move counter (not used here; likely reused variable name but effectively unused in this interpreter).

M() - Main Brainfuck memory tape (array of cells).

    Each cell stores a byte value from 0–300.

PC - Program Counter.

    Points to the current instruction inside the Brainfuck program.

P$() - Array storing the Brainfuck program characters entered by the user.

R - Data pointer (tape pointer).

    Points to the current cell inside the memory array M().

S - Stack pointer used for nested loops.

S() - Stack storing addresses of "[" instructions.

    Used to jump back to the beginning of loops when "]" is executed.

Z - ASCII value of input character read by the Brainfuck "," instruction.

Z$ - Character read from keyboard during Brainfuck input instruction.

Programs:

__________________________________________

hello world

>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.>++++++++++.

src: online gen

__________________________________________

BASIC 10Liner Contest

++++[++++>---<]>-.-.>-[--->+<]>--.----------.------.-[-->+<]>-.[-->+++<]>+.-.++[-->+++<]>+.++[--->++++<]>+.+++++.---------.+++++++++++++.[-->+++++<]>+++.+[->++<]>+.+[--->+<]>+++.-.++++++.+++[->+++<]>.[--->+<]>----.+.

src: online gen

__________________________________________

rose 

----[---->+<]>+.++[--->++<]>+..---[-->+++<]>-.--[---->+++<]>...---[-->+++<]>-.--[---->+++<]>...

src: online gen

__________________________________________

This program counts from 9 down to 0:

++++++++++++++++++++++++++++++++[>+>+<<-]

>>+++++++++++++++++++++++++<<

++++++++++[>>.-<.<-]

src: https://brainfuck.org/

__________________________________________

99 bottles of beer

>++++++++++[<++++++++++>-]<->>>>>+++[>+++>+++<<-]<<<<+<[>[>+>+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]>>>>>>[[-]<++++++++++<->>]<-[>+>+<<-]>[<+>-]+>[[-]<->]<<<<<<<<<->>]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>>>++++++++[>>++++++<<-]>[<++++++++[>++++++<-]>.<++++++++[>------<-]>[<<+>>-]]>.<<++++++++[>>------<<-]<[->>+<<]<++++++++[<++++>-]<.>+++++++[>+++++++++<-]>+++.<+++++[>+++++++++<-]>.+++++..--------.-------.++++++++++++++>>[>>>+>+<<<<-]>>>>[-<<<<+>>>>]>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<<[>>>+>+<<<<-]>>>>[-<<<<+>>>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+<<[-]]>[>+<[-]]++>>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<+<[[-]>-<]>[<<<<<<<.>>>>>>>[-]]<<<<<<<<<.>>----.---------.<<.>>----.+++..+++++++++++++.[-]<<[-]]<[>+>+<<-]>>[-<<+>>]+>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<<[>>+>+<<<-]>>>[-<<<+>>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[>+<[-]]<[>>+<<[-]]>>[<<+>>[-]]<<[[-]>++++++++[<++++>-]<.>++++++++++[>+++++++++++<-]>+.-.<<.>>++++++.------------.---.<<.>++++++[>+++<-]>.<++++++[>----<-]>++.+++++++++++..[-]<<[-]++++++++++.[-]]<[>+>+<<-]>>[-<<+>>]+++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.>+++++++++[>+++++++++<-]>+++.+++++++++++++.++++++++++.------.<++++++++[>>++++<<-]>>.<++++++++++.-.---------.>.<-.+++++++++++.++++++++.---------.>.<-------------.+++++++++++++.----------.>.<++++++++++++.---------------.<+++[>++++++<-]>..>.<----------.+++++++++++.>.<<+++[>------<-]>-.+++++++++++++++++.---.++++++.-------.----------.[-]>[-]<<<.[-]]<[>+>+<<-]>>[-<<+>>]++++>+<[-<->]<[[-]>>-<<]>>[[-]<<+>>]<<[[-]++++++++++.[-]<[-]>]<+<]

src: https://esolangs.org/wiki/99_bottles_of_beer

__________________________________________

bubble sort

>>,[>>,]<<[

[<<]>>>>[

<<[>+<<+>-]

>>[>+<<<<[->]>[<]>>-]

<<<[[-]>>[>+<-]>>[<<<+>>>-]]

>>[[<+>-]>>]<

]<<[>>+<<-]<<

]>>>>[.>>]

src:

[bsort.b -- bubble sort

(c) 2016 Daniel B. Cristofani

http://brainfuck.org/]

Download

Download
BF.tap 592 bytes
Download
brainfuck.txt 13 kB

Install instructions

INSTALLATION INSTRUCTIONS:

1. Download the zip file from here: http://www.petergordon.org.uk/oricutron/files/Oricutron_win32_v12.zip

2. Copy the file BF.TAP to the Oric\tapes folder

3. Start the emulator oricutron.exe

4. Type CLOAD"BF" and press enter

5. LIST to show the program

6. Type RUN to play the game

This is a small Brainfuck interpreter.

The idea was to see if it could fit into a 10-line program, with a maximum of 80 characters per line – turns out it can!

Yes, it runs slowly :)

Comments

Log in with itch.io to leave a comment.

very good