209 lines
9.9 KiB
Python
209 lines
9.9 KiB
Python
|
"""Simple brainfuck interpreter.
|
||
|
|
||
|
Runs very slowly... prints sierpinski triangle"""
|
||
|
|
||
|
___name___ = "bf interpreter"
|
||
|
___license___ = "MIT"
|
||
|
___dependencies___ = ["sleep", "app"]
|
||
|
___categories___ = ["Other"]
|
||
|
|
||
|
import ugfx, os, time, sleep, app
|
||
|
from tilda import Buttons
|
||
|
from time import sleep_ms
|
||
|
|
||
|
# initialize screen
|
||
|
ugfx.init()
|
||
|
ugfx.clear()
|
||
|
|
||
|
ugfx.set_default_font(ugfx.FONT_TITLE)
|
||
|
|
||
|
|
||
|
Prog="""
|
||
|
+>-[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>++[-]->>>>+>>>+>>>+>>>+>>>+>
|
||
|
>>+>>>+>>>+>>>++[-<+]-<<<<+<<++++[->++++++++<]<++[------>+<]>++<<+[--->++<]>++
|
||
|
<<-[--->+<]>------<+[-<+]-<[>>+[->+]-<[-]<<[-]+++[>[-]++++++++++.[-]+++[>+[>>+<<
|
||
|
-]>>[<<++[-<+]->++[->+]->-]<<+[-<+]->-[-[-[-[-[-[-[-[->>>]>>>]>>>]>>>]>>>]>>>]>>
|
||
|
>]>>>]>>>>>>>>>>>>>>>>>>>>> > > > > > > > > > > > > >>>>>>>>>>[+[-<+]-<<<<<<.>>>
|
||
|
>>>>]>[+[-<+]-<<<< <<<.>>>>>>>>]>[+[-
|
||
|
<+]-<<<<<<<<.>>> tic tac toe >>>>>>]+[-<+]-<<
|
||
|
<<<.>>>-]<-]+++ to play: type a number (1 to 9) to +++++++.[-]<<<<
|
||
|
<<[<<<<<<<<<<<+ place an X at that grid location [--->++<]>+++.[
|
||
|
->+++++++<]>.++ ++++.-[---->+<]
|
||
|
>+++.---[->+++<] [ http://mitxela.com/ ] >.+++[->++++<]>+
|
||
|
.+++++.-[->+++++<] >.[--->+<]>-.+[-<+
|
||
|
]-<[-]>>>>]<[<<<<<<<++++[++++>---<]>+.[++++>---<]>-.+++[->+++<]>++.+[--->+<]>+.+
|
||
|
[---->+<]>+++.[--->+<]>-.[-]+[-<+]-<[-]>>>>]<[<<<<<<<<<<+[--->++<]>+++.[->++++++
|
||
|
+<]>.++++++.-[---->+<]>+++.++++++[->++<]>.+[--->+<]>.++++.++++[->+++<]>.--[--->+
|
||
|
<]>.[--->+<]>-.+[-<+]-<[-]>>>>]<+[-<+]-<[>>->>>>>>+[-<<<<[-]<<[-]>>>>-[>>[-]+<<+
|
||
|
<[-]<[-]<[-]<[-]-[----->+<]>---<,>[-<->]<[>>+>+<<<-]>>[<<+>>-]+++++++++[->-[<<]>
|
||
|
]>>-]<<<<[-]>>>>[-]+<<<<<<[>>+>+<<<-]>>[<<+>>-]>>]>>-<<<[-]<<[<->-]<-[-[-[-[-[-[
|
||
|
-[-[->>>]>>>]>>>]>>>]>>>]>>>]>>>]>>>]]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||
|
>[->++[-<+]->>>>>[>>>[>>>[+[-<+]-<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>>>>[>
|
||
|
>>[>>>[+[-<+]-<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>>>>>>>>>>>>>[>>>[>>>[+[-
|
||
|
<+]-<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<[-]
|
||
|
++[->+]->]]]+[-<+]->>>>>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<[-]++[->+]->]]]+
|
||
|
[-<+]->>>>>>>>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>
|
||
|
>[>>>>>>>>>>>>[>>>>>>>>>>>>[+[-<+]-<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>[>>
|
||
|
>>>>[>>>>>>[+[-<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]<[-]+++++++
|
||
|
++[->+]->]]]>+[-<+]->>>>>>>>>>>>>>>>>>>>>>[>>>>[>>>[+[-<+]-<[-]<[-]++++++++[->+]
|
||
|
->]]]>+[-<+]->>>>>[>>>>>>>>>[>>>>>>>>[+[-<+]-<[-]<[-]++++++++[->+]->]]]>+[-<+]->
|
||
|
>>>>[>>>>>>>>[>>>>>>>>>>[+[-<+]-<[-]<[-]+++++[->+]->]]]>+[-<+]->>>>[>>>>>>>>>>[>
|
||
|
>>>>>>>>[+[-<+]-<[-]<[-]++[->+]->]]]>+[-<+]->>>>>>>>[>>>>>>>>>[>>>>>>>>[+[-<+]-<
|
||
|
[-]<[-]+++++++++[->+]->]]]>+[-<+]->>>>>>>>[>>>>>>>>[>>>>>>>>>>[+[-<+]-<[-]<[-]++
|
||
|
++++[->+]->]]]>+[-<+]->>>>>>>[>>>>>>>>>>[>>>>>>>>>[+[-<+]-<[-]<[-]+++[->+]->]]]>
|
||
|
+[-<+]->>>>>>>>>>>[>>>>>>>>>[>>>>>>>>[+[-<+]-<[-]<[-]++++++++++[->+]->]]]>+[-<+]
|
||
|
->>>>>>>>>>>[>>>>>>>>[>>>>>>>>>>[+[-<+]-<[-]<[-]+++++++[->+]->]]]>+[-<+]->>>>>>>
|
||
|
>>>[>>>>>>>>>>[>>>>>>>>>[+[-<+]-<[-]<[-]++++[->+]->]]]>+[-<+]->>>>>[>>>>>>>>>>>>
|
||
|
[>>>>>>>>>>>[+[-<+]-<[-]<[-]++++++++++[->+]->]]]>+[-<+]->>>>[>>>>>>>>>>>>>[>>>>>
|
||
|
>>>>>>>[+[-<+]-<[-]<[-]++[->+]->]]]>+[-<+]->>>>>>>>>>>[>>>>>>[>>>>>[+[-<+]-<[-]<
|
||
|
[-]++++++++[->+]->]]]>+[-<+]->>>>>>>>>>[>>>>>>>[>>>>>>[+[-<+]-<[-]<[-]++++[->+]-
|
||
|
>]]]>+[-<+]->>>>>>[>>>[>[+[-<+]-<[-]<[-]++++[->+]->]]]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>[+[-<+]-<[-]<[-]+++++++[->+]->]]]>+[-<+]->>
|
||
|
>>>>>>>>[>>>>>>>>>>>[>>>>>>>>>[+[-<+]-<[-]<[-]++++[->+]->]]]>+[-<+]->>>>>>[>>>>>
|
||
|
>>>>>>>[>>>>>>>>>>[+[-<+]-<[-]<[-]++++++++++[->+]->]]]>+[-<+]->>>>[>>>>>>>>>>>>>
|
||
|
>[>>>>>>>>>>>>[+[-<+]-<[-]<[-]++[->+]->]]]>+[-<+]->>>>>>>>>>>>[>>>>>>[>>>>[+[-<+
|
||
|
]-<[-]<[-]++++++++[->+]->]]]>+[-<+]->>>>>>>>>>[>>>>>>>>[>>>>>>[+[-<+]-<[-]<[-]++
|
||
|
++[->+]->]]]>+[-<+]-<[>>+[-<+]->>>>>>>>>>>>>>>>>>>>>>>>>>>>[+[-<+]-<[-]<[-]+++++
|
||
|
+++++[->+]->]+[-<+]->>>>>>>>>>>>>>>>>>>>>>[+[-<+]-<[-]<[-]++++++++[->+]->]+[-<+]
|
||
|
->>>>>>>>>>[+[-<+]-<[-]<[-]++++[->+]->]+[-<+]->>>>[+[-<+]-<[-]<[-]++[->+]->]+[-<
|
||
|
+]->>>>>>>>>>>>>>>>>>>>>>>>>[+[-<+]-<[-]<[-]+++++++++[->+]->]+[-<+]->>>>>>>>>>>>
|
||
|
>>>>>>>[+[-<+]-<[-]<[-]+++++++[->+]->]+[-<+]->>>>>>>>>>>>>[+[-<+]-<[-]<[-]+++++[
|
||
|
->+]->]+[-<+]->>>>>>>[+[-<+]-<[-]<[-]+++[->+]->]+[-<+]->>>>>>>>>>>>>>>>[+[-<+]-<
|
||
|
[-]<[-]++++++[->+]->]+[-<+]->]>>+[-<+]-<<<<[+[->+]->>>>>>>>>>>>>>>>>[+[-<+]-<[-]
|
||
|
<[-]++[->+]->]+[-<+]->]>>>>+[-<+]-<<[>>>+[-<+]-<[-]<[+[-<+]->++[->+]-<<-]+[-<+]-
|
||
|
>-[-[-[-[-[-[-[-[->>>]>>>]>>>]>>>]>>>]>>>]>>>]>>>]>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||
|
>>>>>>>>>>>>>->>++[-<+]->]>>>>+[-<+]-<<[-]>>>+[-<+]-<<<<[-]>>>>>+[-<+]->>>>>>[>>
|
||
|
>[>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>>>>>[>>>[>>>[+[-<+]-<<<
|
||
|
<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>>>>>>>>>>>>>>[>>>[>>>[+[-<+]-<<<<<<<<<<
|
||
|
<[-]++[->+]->]]]+[-<+]->>>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->
|
||
|
]]]+[-<+]->>>>>>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->]]]+[-<+]-
|
||
|
>>>>>>>>>>>>[>>>>>>>>>[>>>>>>>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>[
|
||
|
>>>>>>>>>>>>[>>>>>>>>>>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->]]]+[-<+]->>>>>>>>>>>>[>
|
||
|
>>>>>[>>>>>>[+[-<+]-<<<<<<<<<<<[-]++[->+]->]]]+[-<+]-<[-]]++[->+]->]+[-<+]-<+[
|
||
|
-<+]-<]>>+[->+]->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>+[-[-]<+]-<+[-[-]<+]-<+>]
|
||
|
"""
|
||
|
|
||
|
|
||
|
|
||
|
# Hello World
|
||
|
#Prog="++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
|
||
|
|
||
|
# Sierpinski
|
||
|
Prog="""
|
||
|
++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[
|
||
|
-<<<[
|
||
|
->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<
|
||
|
]>.>+[>>]>+
|
||
|
]"""
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
buf=""
|
||
|
def output(t):
|
||
|
global buf
|
||
|
buf+=t
|
||
|
buf=buf[-(16*80):]
|
||
|
ugfx.clear()
|
||
|
lines=buf.split("\n")
|
||
|
lines=lines[-16:]
|
||
|
for i,v in enumerate(lines):
|
||
|
ugfx.text(5,i*20+5, v+" ", ugfx.BLACK)
|
||
|
|
||
|
lastpushed=0
|
||
|
def pushed(n):
|
||
|
global Tape, TP, waiting
|
||
|
if (waiting):
|
||
|
output(n+" \n")
|
||
|
Tape[TP]=ord(n)
|
||
|
waiting=False
|
||
|
|
||
|
|
||
|
|
||
|
Buttons.enable_interrupt(Buttons.BTN_1, lambda button_id:pushed("1"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_2, lambda button_id:pushed("2"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_3, lambda button_id:pushed("3"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_4, lambda button_id:pushed("4"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_5, lambda button_id:pushed("5"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_6, lambda button_id:pushed("6"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_7, lambda button_id:pushed("7"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_8, lambda button_id:pushed("8"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_9, lambda button_id:pushed("9"), on_press=True, on_release=False)
|
||
|
Buttons.enable_interrupt(Buttons.BTN_0, lambda button_id:pushed("0"), on_press=True, on_release=False)
|
||
|
|
||
|
|
||
|
|
||
|
output("Loading...")
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
waiting=False
|
||
|
Prog+='\0'
|
||
|
Tape=[0]*256
|
||
|
PP=-1
|
||
|
TP=0
|
||
|
while True:
|
||
|
if (waiting):
|
||
|
sleep_ms(200)
|
||
|
else:
|
||
|
PP=PP+1
|
||
|
if (PP>=len(Prog)):
|
||
|
waiting=True
|
||
|
output("END!")
|
||
|
elif (Prog[PP]=="+"):
|
||
|
Tape[TP]=Tape[TP]+1
|
||
|
elif (Prog[PP] =="-"):
|
||
|
Tape[TP]=Tape[TP]-1
|
||
|
elif (Prog[PP] ==">"):
|
||
|
TP=TP+1
|
||
|
elif (Prog[PP] =="<"):
|
||
|
TP=TP-1
|
||
|
elif (Prog[PP] =="."):
|
||
|
output(chr(Tape[TP]))
|
||
|
elif (Prog[PP] ==","):
|
||
|
waiting=True
|
||
|
elif (Prog[PP] =="["):
|
||
|
if (Tape[TP]==0):
|
||
|
depth=1
|
||
|
while (depth>0):
|
||
|
PP=PP+1
|
||
|
if (Prog[PP]=="]"):
|
||
|
depth = depth - 1
|
||
|
if (Prog[PP]=="["):
|
||
|
depth = depth + 1
|
||
|
elif (Prog[PP] =="]"):
|
||
|
if (Tape[TP]!=0):
|
||
|
depth=1
|
||
|
while (depth>0):
|
||
|
PP=PP-1
|
||
|
if (Prog[PP]=="]"):
|
||
|
depth = depth + 1
|
||
|
if (Prog[PP]=="["):
|
||
|
depth = depth - 1
|
||
|
|