Skip to content

Instantly share code, notes, and snippets.

@G33kDude
Created October 21, 2016 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G33kDude/1947cfb1c2a8e81118d805c7efee29f6 to your computer and use it in GitHub Desktop.
Save G33kDude/1947cfb1c2a8e81118d805c7efee29f6 to your computer and use it in GitHub Desktop.
BF Interpreter written in AutoHotkey
Tape := []
Inst := 1
Ptr := 0
Braces := []
Program =
(
-,+[ Read first character and start outer character reading loop
-[ Skip forward if character is 0
>>++++[>++++++++<-] Set up divisor (32) for division loop
(MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
<+<-[ Set up dividend (x minus 1) and enter division loop
>+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
<[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
<<<<<- Decrement dividend
] End division loop
]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
>--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
++++++++++++<[ If flag then set up divisor (13) for second division loop
(MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
>-[>+>>] Reduce divisor; Normal case: increase remainder
>[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
<<<<<- Decrease dividend
] End division loop
>>[<+>-] Add remainder back to divisor to get a useful 13
>[ Skip forward if quotient was 0
-[ Decrement quotient and skip forward if quotient was 1
-<<[-]>> Zero quotient and divisor if quotient was 2
]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
<[-] Clear remainder from first division if second division was skipped
<.[-] Output ROT13ed character from copy and clear it
<-,+ Read next character
] End character reading loop
)
Program := StrSplit(RegExReplace(Program, "[^<>+\-.,\[\]]"))
BraceStack := []
for i, c in Program
{
if (c == "[")
BraceStack.Push(i)
else if (c == "]")
{
Start := BraceStack.Pop()
Braces[i] := Start
Braces[Start] := i
}
}
DllCall("AllocConsole")
Loop
{
c := Program[Inst]
if (c == "")
break
else if (c == ">")
Ptr += 1
else if (c == "<")
Ptr -= 1
else if (c == "+")
Tape[Ptr] := Int(Tape[Ptr]) + 1
else if (c == "-")
Tape[Ptr] := Int(Tape[Ptr]) - 1
else if (c == ".")
FileOpen("CONOUT$", "w").Write(Chr(Int(Tape[Ptr])))
else if (c == ",")
{
loop
InputBox, Input
until StrLen(Input) == 1
Tape[Ptr] := Asc(Input)
}
else if (c == "[")
{
if (Int(Tape[Ptr]) == 0)
Inst := Braces[Inst]
}
else if (c == "]")
{
if (Int(Tape[Ptr]) != 0)
Inst := Braces[Inst]
}
else
throw Exception("Invalid character encountered") ; Shouldn't ever happen
Inst++
}
MsgBox, Finished
ExitApp
return
Int(Num)
{
return Round(Num)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment