BF Interpreter
Übersicht
BlitzBasic
Codearchiv
|
Betreff: BF Interpreter
|
Do, Okt 16, 2008 17:51
Antworten mit Zitat
|
Da ich mich in letzter Zeit ein wenig mit Brainfuck beschäftigt habe, habe ich flugs mal einen Interpreter dafür geschrieben.
Da es nur 8 Befehle gibt, ist der Interpreter selbst auch nicht besonders komplex, aber es mag für den einen oder anderen nützlich zum rumspielen sein.
Für diejenigen, die nicht wissen, was Brainfuck ist, empfehle ich eine kurze Lektüre von Wikipedia
Zur Erklärung: Mit BF_Tokenize wird das Programm vorbereitet und mit BF_Execute ausgeführt.
Die Ausgabe des Programms steht später in StdOut$. Wenn man den Befehl ',' benutzt, sollte vorher natürlich noch etwas in StdIn$ stehen.
Dem Code liegt noch ein kurzes Beispiel mit einem "Hello World!" - Programm bei.
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Global Stack[ 255 ] Global StackPointer Global Instructions[ 255 ] Global InstructionPointer
Global StdOut$ Global StdIn$ = ""
Const BF_ADD = 1 Const BF_SUBSTRACT = 2 Const BF_PRVCELL = 3 Const BF_NEXTCELL = 4 Const BF_PUT = 5 Const BF_GET = 6 Const BF_WHILE = 7 Const BF_WEND = 8
BF_Tokenize( "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+." ) BF_Execute()
Print StdOut$ WaitKey() End
Function BF_Tokenize( Prgm$ ) For i = 1 To Len( Prgm$ ) Select Mid( Prgm$, i, 1 ) Case "+" Instructions[ i - 1 ] = BF_ADD Case "-" Instructions[ i - 1 ] = BF_SUBSTRACT Case "<" Instructions[ i - 1 ] = BF_PRVCELL Case ">" Instructions[ i - 1 ] = BF_NEXTCELL Case "." Instructions[ i - 1 ] = BF_PUT Case "," Instructions[ i - 1 ] = BF_GET Case "[" Instructions[ i - 1 ] = BF_WHILE Case "]" Instructions[ i - 1 ] = BF_WEND End Select Next Instructions[ Len( Prgm$ ) ] = 0 End Function
Function BF_Execute() While 1 Cmd = Instructions[ InstructionPointer ] Select Cmd Case 0 Exit Case BF_ADD Stack[ StackPointer ] = Stack[ StackPointer ] + 1 Case BF_SUBSTRACT Stack[ StackPointer ] = Stack[ StackPointer ] - 1 Case BF_PRVCELL StackPointer = StackPointer - 1 If StackPointer < 0 Then StackPointer = 0 Case BF_NEXTCELL StackPointer = StackPointer + 1 Case BF_PUT StdOut$ = StdOut$ + Chr( Stack[ StackPointer ] ) Case BF_GET If StdIn$ <> "" Then Stack[ StackPointer ] = Asc( Left( StdIn$, 1 ) ) StdIn$ = Right( StdIn$, Len( StdIn$ ) - 1 ) Else Stack[ StackPointer ] = 0 EndIf Case BF_WHILE If Not Stack[ StackPointer ] Then Index = InstructionPointer Level = 0 While 1 Index = Index + 1 If Instructions[ Index ] = BF_WHILE Then Level = Level + 1 ElseIf Instructions[ Index ] = BF_WEND Then If Not Level Then InstructionPointer = Index + 1 Exit Else Level = Level - 1 EndIf EndIf Wend EndIf Case BF_WEND If Stack[ StackPointer ] Then Index = InstructionPointer Level = 0 While 1 Index = Index - 1 If Instructions[ Index ] = BF_WHILE Then If Not Level Then InstructionPointer = Index - 1 Exit Else Level = Level + 1 EndIf ElseIf Instructions[ Index ] = BF_WEND Then Level = Level - 1 EndIf Wend EndIf End Select InstructionPointer = InstructionPointer + 1 Wend End Function
|
|
Übersicht
BlitzBasic
Codearchiv
Powered by phpBB © 2001 - 2006, phpBB Group