Skip to content

Instantly share code, notes, and snippets.

@nucular
Last active June 25, 2016 18:10
Show Gist options
  • Save nucular/a20c39a2bd90046dcce8ae8e7dacdf5a to your computer and use it in GitHub Desktop.
Save nucular/a20c39a2bd90046dcce8ae8e7dacdf5a to your computer and use it in GitHub Desktop.
Brainfuck interpreter written in TI-Basic
bf()
Prgm
Local mem, jmpstk, jsptr, memdim, memptr, pc, op, bfcdim, k, conrow, concol, conpic, sw, sh, pc2
ClrIO
StoPic conpic
XorPic conpic
240 -> sw
91 -> sh
255 -> memdim
dim(bfc) -> bfcdim
newList(memdim) -> mem
newList(dim(bfc)) -> jmpmap
newList(dim(bfc)) -> jmpstk
1 -> jsptr
For pc, 1, bfcdim
mid(bfc, pc, 1) -> op
If op = "[" Then
pc -> jmpstk[jsptr]
jsptr + 1 -> jsptr
ElseIf op = "]" Then
jsptr - 1 -> jsptr
jmpstk[jsptr] -> pc2
pc -> jmpmap[pc2]
pc2 -> jmpmap[pc]
EndIf
EndFor
1 -> memptr
1 -> pc
0 -> conrow
0 -> concol
While pc > 0 and pc < bfcdim
mid(bfc, pc, 1) -> op
If op = "+" Then
mod(mem[memptr] + 1, 256) -> mem[memptr]
ElseIf op = "-" Then
mod(mem[memptr] - 1, 256) -> mem[memptr]
ElseIf op = ">" Then
mod(memptr, memdim-1) + 1 -> memptr
ElseIf op = "<" Then
mod(memptr - 2, memdim - 1) + 1 -> memptr
ElseIf op = "[" Then
If mem[memptr] = 0 Then
jmpmap[pc] -> pc
EndIf
ElseIf op = "]" Then
If mem[memptr] > 0 Then
jmpmap[pc] -> pc
EndIf
ElseIf op = "." Then
If concol * 7 + 7 > sw or mem[memptr] = 13 Then
0 -> concol
conrow + 1 -> conrow
If conrow * 9 + 9 > sh Then
conrow - 1 -> conrow
StoPic conpic, 9
RplcPic conpic
StoPic conpic, sh - 9
XorPic conpic, sh - 9
0 -> concol
EndIf
EndIf
If mem[memptr] = 13 Then
PxlText char(mem[memptr]), conrow * 9, concol * 7
concol + 1 -> concol
EndIf
ElseIf op = "," Then
0 -> k
While k=0
getKey() -> k
EndWhile
If k > 255 Then
0 -> k
EndIf
k -> mem[memptr]
ElseIf op=":" Then
Disp mem[memptr]
Pause
DispG
ElseIf op = ";" Then
Disp
Input "", k
k -> mem[memptr]
DispG
EndIf
pc + 1 -> pc
EndWhile
PlotsOn
EndPrgm

Brainfuck in TI-Basic

Sorry.

Program name: bf()
BF code to run: bfc (as a string)

Special commands:
:: print the current cell to PrgmIO
;: read the current cell from PrgmIO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment