Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,89 +1,27 @@
|
|||
# Brain**** interpreter
|
||||
scope # Simple BF interpreter, based on the Lua sample: "Simple meta-implementation using load"
|
||||
# reads the BF code from stdin, builds an equivalent Agena program and executes it
|
||||
|
||||
# execute the Brain**** program in the code string
|
||||
bf := proc( code :: string ) is
|
||||
local address := 1; # current data address
|
||||
local pc := 1; # current position in code
|
||||
local data := []; # data - initially empty
|
||||
local input := ""; # user input - initially empty
|
||||
local bfOperations := # table of operations and their implemntations
|
||||
[ ">" ~ proc() is inc address, 1 end
|
||||
, "<" ~ proc() is dec address, 1 end
|
||||
, "+" ~ proc() is inc data[ address ], 1 end
|
||||
, "-" ~ proc() is dec data[ address ], 1 end
|
||||
, "." ~ proc() is io.write( char( data[ address ] ) ) end
|
||||
, "," ~ proc() is
|
||||
# get next input character, converted to an integer
|
||||
while input = ""
|
||||
do
|
||||
# no input left - get the next line
|
||||
input := io.read()
|
||||
od;
|
||||
data[ address ] := abs( input[ 1 ] );
|
||||
# remove the latest character from the input
|
||||
if size input < 2
|
||||
then
|
||||
input := ""
|
||||
else
|
||||
input := input[ 2 to -1 ]
|
||||
fi
|
||||
end
|
||||
, "[" ~ proc() is
|
||||
if data[ address ] = 0
|
||||
then
|
||||
# skip to the end of the loop
|
||||
local depth := 0;
|
||||
do
|
||||
inc pc, 1;
|
||||
if code[ pc ] = "["
|
||||
then
|
||||
inc depth, 1
|
||||
elif code[ pc ] = "]"
|
||||
then
|
||||
dec depth, 1
|
||||
fi
|
||||
until depth < 0
|
||||
fi
|
||||
end
|
||||
, "]" ~ proc() is
|
||||
if data[ address ] <> 0
|
||||
then
|
||||
# skip to the start of the loop
|
||||
local depth := 0;
|
||||
do
|
||||
dec pc, 1;
|
||||
if code[ pc ] = "["
|
||||
then
|
||||
dec depth, 1
|
||||
elif code[ pc ] = "]"
|
||||
then
|
||||
inc depth, 1
|
||||
fi
|
||||
until depth < 0
|
||||
fi
|
||||
end
|
||||
];
|
||||
# execute the operations - ignore anything invalid
|
||||
while pc <= size code
|
||||
do
|
||||
if data[ address ] = null
|
||||
then
|
||||
data[ address ] := 0
|
||||
fi;
|
||||
if bfOperations[ code[ pc ] ] <> null
|
||||
then
|
||||
bfOperations[ code[ pc ] ]()
|
||||
fi;
|
||||
inc pc, 1
|
||||
od
|
||||
end;
|
||||
local constant bfOp := [ '>' ~ 'ptr +:= 1; '
|
||||
, '<' ~ 'ptr -:= 1; '
|
||||
, '+' ~ 'mem[ptr] +:= 1; '
|
||||
, '-' ~ 'mem[ptr] -:= 1; '
|
||||
, '[' ~ 'while mem[ptr] <> 0 do '
|
||||
, ']' ~ 'od; '
|
||||
, '.' ~ 'io.write(char(mem[ptr])); '
|
||||
, ',' ~ 'mem[ptr] := abs(io.read(io.stdin,1) or "\\0"); '
|
||||
];
|
||||
|
||||
# prompt for Brain**** code and execute it, repeating until an empty code string is entered
|
||||
scope
|
||||
local code;
|
||||
do
|
||||
io.write( "BF> " );
|
||||
code := io.read();
|
||||
bf( code )
|
||||
until code = ""
|
||||
epocs;
|
||||
local bfProgram := "local mem := setmetatable([], [ '__index' ~ proc() return 0 end ]);\n"
|
||||
& "local ptr := 1;\n"
|
||||
;
|
||||
|
||||
for line in io.lines() do
|
||||
for p to size line do
|
||||
local snippet := bfOp[ line[ p ] ];
|
||||
if snippet then bfProgram &:= snippet & "\n" fi
|
||||
od
|
||||
od;
|
||||
|
||||
loadstring( bfProgram, "bfCode" )()
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,21 +4,21 @@
|
|||
;
|
||||
|
||||
Tape: [0]
|
||||
DataPointer: new 0
|
||||
InstructionPointer: new 0
|
||||
DataPointer: 0
|
||||
InstructionPointer: 0
|
||||
|
||||
; Look for jumps in Code an register them
|
||||
; in the Jumps table
|
||||
|
||||
precomputeJumps: function [][
|
||||
vstack: new []
|
||||
jumphash: new #[]
|
||||
vstack: []
|
||||
jumphash: #[]
|
||||
instrPointer: 0
|
||||
|
||||
while [instrPointer<CodeLength] [
|
||||
command: get split Code instrPointer
|
||||
if? command="[" -> 'vstack ++ instrPointer
|
||||
else [
|
||||
switch command="[" -> 'vstack ++ instrPointer
|
||||
[
|
||||
if command="]" [
|
||||
target: last vstack
|
||||
chop 'vstack
|
||||
|
|
@ -47,36 +47,37 @@ StateIsValid: function [][
|
|||
interpret: function [].export:[DataPointer,InstructionPointer,Tape][
|
||||
while [StateIsValid][
|
||||
command: get split Code InstructionPointer
|
||||
case [command=]
|
||||
when? ["+"] -> Tape\[DataPointer]: Tape\[DataPointer]+1
|
||||
when? ["-"] -> Tape\[DataPointer]: Tape\[DataPointer]-1
|
||||
when? [">"] [
|
||||
case command [
|
||||
"+" -> Tape\[DataPointer]: Tape\[DataPointer]+1
|
||||
"-" -> Tape\[DataPointer]: Tape\[DataPointer]-1
|
||||
">" [
|
||||
inc 'DataPointer
|
||||
if DataPointer = size Tape -> Tape: Tape ++ 0
|
||||
]
|
||||
when? ["<"] -> dec 'DataPointer
|
||||
when? ["."] -> prints to :string to :char Tape\[DataPointer]
|
||||
when? [","][
|
||||
"<" -> dec 'DataPointer
|
||||
"." -> prints to :string to :char Tape\[DataPointer]
|
||||
"," [
|
||||
inp: to :integer input ""
|
||||
if inp=13 -> inp: 10
|
||||
if inp=3 -> panic "something went wrong!"
|
||||
set Tape DataPointer inp
|
||||
]
|
||||
when? ["["] ->
|
||||
if 0 = get Tape DataPointer [ InstructionPointer: new get Jumps InstructionPointer ]
|
||||
"[" ->
|
||||
if 0 = get Tape DataPointer [ InstructionPointer: new Jumps\[InstructionPointer] ]
|
||||
|
||||
when? ["]"] ->
|
||||
"]" ->
|
||||
if 0 <> get Tape DataPointer [
|
||||
InstructionPointer: new get Jumps InstructionPointer
|
||||
InstructionPointer: new Jumps\[InstructionPointer]
|
||||
]
|
||||
]
|
||||
|
||||
inc 'InstructionPointer
|
||||
]
|
||||
]
|
||||
|
||||
Code: ""
|
||||
if? 1>size arg -> Code: "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
|
||||
else -> Code: read arg\0
|
||||
switch 1>size arg -> Code: "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
|
||||
-> Code: read arg\0
|
||||
|
||||
CodeLength: size Code
|
||||
Jumps: precomputeJumps
|
||||
|
|
|
|||
|
|
@ -1,134 +0,0 @@
|
|||
; AutoFucck
|
||||
; A AutoIt Brainfuck Interpreter
|
||||
; by minx
|
||||
; AutoIt Version: 3.3.8.x
|
||||
|
||||
; Commands:
|
||||
; - DEC
|
||||
; + INC
|
||||
; [ LOOP START
|
||||
; ] LOOP END
|
||||
; . Output cell value as ASCII Chr
|
||||
; , Input a ASCII char (cell value = ASCII code)
|
||||
; : Ouput cell value as integer
|
||||
; ; Input a Integer
|
||||
; _ Output a single whitespace
|
||||
; / Output an Carriage Return and Line Feed
|
||||
|
||||
; You can load & save .atf Files.
|
||||
|
||||
#include <WindowsConstants.au3>
|
||||
#include <EditConstants.au3>
|
||||
#include <Array.au3>
|
||||
#include <GUIConstants.au3>
|
||||
#include <StaticCOnstants.au3>
|
||||
|
||||
HotKeySet("{F5}", "_Runn")
|
||||
|
||||
$hMain = GUICreate("Autofuck - Real Brainfuck Interpreter", 600, 525)
|
||||
$mMain = GUICtrlCreateMenu("File")
|
||||
Global $mCode = GUICtrlCreateMenu("Code")
|
||||
$mInfo = GUICtrlCreateMenu("Info")
|
||||
$mCredits = GUICtrlCreateMenuItem("Credits", $mInfo)
|
||||
$mFile_New = GUICtrlCreateMenuItem("New", $mMain)
|
||||
$mFile_Open = GUICtrlCreateMenuItem("Open", $mMain)
|
||||
$mFile_Save = GUICtrlCreateMenuItem("Save", $mMain)
|
||||
Global $mCode_Run = GUICtrlCreateMenuItem("Run [F5]", $mCode)
|
||||
Global $lStatus = GUICtrlCreateLabel("++ Autofuck started...", 5, 480, 590, 20, $SS_SUNKEN)
|
||||
GUICtrlSetFont(-1, Default, Default, Default, "Courier New")
|
||||
$eCode = GUICtrlCreateEdit("", 5, 5, 590, 350)
|
||||
GUICtrlSetFont(-1, Default, Default, Default, "Courier New")
|
||||
$eConsole = GUICtrlCreateEdit("", 5, 360, 590, 115, $ES_WANTRETURN)
|
||||
GUICtrlSetFont(-1, Default, Default, Default, "Courier New")
|
||||
GUISetState()
|
||||
|
||||
While 1
|
||||
$nMsg = GUIGetMsg()
|
||||
Switch $nMsg
|
||||
Case $mFile_New
|
||||
GUICtrlSetData($eCode, "")
|
||||
Case $mFile_Open
|
||||
GUICtrlSetData($eCode, FileRead(FileOpenDialog("Open Autofuck script", @DesktopDir, "Autofuck (*.atf)")))
|
||||
Case $mFile_Save
|
||||
FileWrite(FileOpen(StringReplace(FileSaveDialog("Save Autofuck script", @DesktopDir, "Autofuck (*.atf)"), ".atf", "") &".atf", 2), GUICtrlRead($eCode))
|
||||
Case $GUI_EVENT_CLOSE
|
||||
Exit
|
||||
Case $mCredits
|
||||
MsgBox(0, "Autofuck", "Copyright by: "&@CRLF&"minx (autoit.de)"&@CRLF&"crashdemons (autoitscript.com)")
|
||||
EndSwitch
|
||||
WEnd
|
||||
|
||||
Func _Runn()
|
||||
$Timer = TimerInit()
|
||||
GUICtrlSetData($lStatus, "++ Program started")
|
||||
Global $tData=DllStructCreate('BYTE[65536]')
|
||||
Global $pData=0
|
||||
GUICtrlSetData($eConsole, "")
|
||||
Local $aError[6]=['','Unmatched closing bracket during search','Unmatched opening bracket during search','Unexpected closing bracket','Data pointer passed left boundary','Data pointer passed right boundary']
|
||||
Local $sError=''
|
||||
Local $i=_Run(GUICtrlRead($eCode))
|
||||
If @error>=0 And @error<6 Then $sError=$aError[@error]
|
||||
If StringLen($sError) Then GUICtrlSetData($eConsole, 'ERROR: '&$sError&'.'&@CRLF&'Ending Instruction Pointer: '&($i-1)&@CRLF&'Current Data Pointer: '&$pData)
|
||||
GUICtrlSetData($lStatus, "++ Program terminated. Runtime: "& Round( TimerDiff($Timer) / 1000, 4) &"s")
|
||||
EndFunc
|
||||
|
||||
Func _Run($Code,$iStart=1,$iEnd=0)
|
||||
If $iEnd<1 Then $iEnd=StringLen($Code)
|
||||
For $i = $iStart to $iEnd
|
||||
Switch StringMid($Code, $i, 1)
|
||||
Case ">"
|
||||
$pData+=1
|
||||
If $pData=65536 Then Return SetError(5,0,$i)
|
||||
Case "<"
|
||||
$pData-=1
|
||||
If $pData<0 Then Return SetError(4,0,$i)
|
||||
Case "+"
|
||||
DllStructSetData($tData,1,DllStructGetData($tData,1,$pData+1)+1,$pData+1)
|
||||
Case "-"
|
||||
DllStructSetData($tData,1,DllStructGetData($tData,1,$pData+1)-1,$pData+1)
|
||||
Case ":"
|
||||
GUICtrlSetData($eConsole, GUICtrlRead($eConsole) & (DllStructGetData($tData,1,$pData+1)))
|
||||
Case "."
|
||||
GUICtrlSetData($eConsole, GUICtrlRead($eConsole) & Chr(DllStructGetData($tData,1,$pData+1)))
|
||||
Case ";"
|
||||
Local $cIn=StringMid(InputBox('Autofuck','Enter Number'),1)
|
||||
DllStructSetData($tData,1,Number($cIn),$pData+1)
|
||||
Case ","
|
||||
Local $cIn=StringMid(InputBox('Autofuck','Enter one ASCII character'),1,1)
|
||||
DllStructSetData($tData,1,Asc($cIn),$pData+1)
|
||||
Case "["
|
||||
Local $iStartSub=$i
|
||||
Local $iEndSub=_MatchBracket($Code,$i,$iEnd)
|
||||
If @error<>0 Then Return SetError(@error,0,$iEndSub)
|
||||
While DllStructGetData($tData,1,$pData+1)<>0
|
||||
Local $iRet=_Run($Code,$iStartSub+1,$iEndSub-1)
|
||||
If @error<>0 Then Return SetError(@error,0,$iRet)
|
||||
WEnd
|
||||
$i=$iEndSub
|
||||
Case ']'
|
||||
Return SetError(3,0,$i)
|
||||
Case "_"
|
||||
GUICtrlSetData($eConsole, GUICtrlRead($eConsole)&" ")
|
||||
Case "/"
|
||||
GUICtrlSetData($eConsole, GUICtrlRead($eConsole)&@CRLF)
|
||||
EndSwitch
|
||||
Next
|
||||
Return 0
|
||||
EndFunc
|
||||
|
||||
Func _MatchBracket($Code,$iStart=1,$iEnd=0)
|
||||
If $iEnd<1 Then $iEnd=StringLen($Code)
|
||||
Local $Open=0
|
||||
For $i=$iStart To $iEnd
|
||||
Switch StringMid($Code,$i,1)
|
||||
Case '['
|
||||
$Open+=1
|
||||
Case ']'
|
||||
$Open-=1
|
||||
If $Open=0 Then Return $i
|
||||
If $Open<0 Then Return SetError(1,0,$i)
|
||||
EndSwitch
|
||||
Next
|
||||
If $Open>0 Then Return SetError(2,0,$i)
|
||||
Return 0
|
||||
EndFunc
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
USE: brainf***
|
||||
USE: brainfuck
|
||||
|
||||
"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." run-brainf***
|
||||
"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." run-brainfuck
|
||||
|
|
|
|||
|
|
@ -1,54 +1,51 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">bfi</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">pgm</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">jumptable</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pgm</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">loopstack</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
|
||||
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- size??</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">skip</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">loopstart</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dp</span>
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- compile (pack/strip comments and link jumps)
|
||||
--</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pgm</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pgm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">switch</span> <span style="color: #000000;">ch</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'['</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">loopstack</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">loopstack</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">);</span>
|
||||
<span style="color: #000000;">pgm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">']'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">loopstart</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">loopstack</span><span style="color: #0000FF;">[$];</span>
|
||||
<span style="color: #000000;">loopstack</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">loopstack</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">];</span>
|
||||
<span style="color: #000000;">jumptable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">loopstart</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #000000;">jumptable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">loopstart</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #008080;">fallthrough</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'+'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'<'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'>'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">','</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">pgm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #008080;">default</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">skip</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">loopstack</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">pgm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pgm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">]</span>
|
||||
procedure bfi(string pgm)
|
||||
sequence jumptable = repeat(0,length(pgm)),
|
||||
loopstack = {},
|
||||
data = repeat(0,10) -- size??
|
||||
integer skip = 0, ch
|
||||
--
|
||||
-- compile (pack/strip comments and link jumps)
|
||||
--
|
||||
for i=1 to length(pgm) do
|
||||
ch = pgm[i]
|
||||
switch ch do
|
||||
case '[': loopstack = append(loopstack,i-skip);
|
||||
pgm[i-skip] = ch;
|
||||
case ']': integer loopstart = loopstack[$];
|
||||
loopstack = loopstack[1..-2];
|
||||
jumptable[i-skip] = loopstart;
|
||||
jumptable[loopstart] = i-skip;
|
||||
fallthrough
|
||||
case '+','-','<','>',',','.': pgm[i-skip] = ch;
|
||||
default: skip += 1
|
||||
end switch
|
||||
end for
|
||||
if length(loopstack) then ?9/0 end if
|
||||
pgm = pgm[1..-1-skip]
|
||||
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- main execution loop
|
||||
--</span>
|
||||
<span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">pc</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pgm</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pgm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">switch</span> <span style="color: #000000;">ch</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'>'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">if</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'<'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">if</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'+'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'-'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">','</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #008000;">'?'</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">getc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'.'</span><span style="color: #0000FF;">:</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">'['</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">if</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">jumptable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">case</span> <span style="color: #008000;">']'</span><span style="color: #0000FF;">:</span> <span style="color: #008080;">if</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">jumptable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pc</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">default</span><span style="color: #0000FF;">:</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
|
||||
<span style="color: #000000;">pc</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
--
|
||||
-- main execution loop
|
||||
--
|
||||
integer pc = 1, dp = 1
|
||||
while pc<=length(pgm) do
|
||||
ch = pgm[pc]
|
||||
switch ch do
|
||||
case '>': dp += 1 if dp>length(data) then dp = 1 end if
|
||||
case '<': dp -= 1 if dp<1 then dp = length(data) end if
|
||||
case '+': data[dp] += 1
|
||||
case '-': data[dp] -= 1
|
||||
case ',': data[dp] = iff(platform()=JS?'?':getc(0))
|
||||
case '.': puts(1,data[dp])
|
||||
case '[': if data[dp]=0 then pc = jumptable[pc] end if
|
||||
case ']': if data[dp]!=0 then pc = jumptable[pc] end if
|
||||
default: ?9/0
|
||||
end switch
|
||||
pc += 1
|
||||
end while
|
||||
end procedure
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">bf</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<++.>---------.>------.<----.++++++++.>>+.>++.+++."</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">fb</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<+++.>---.>------.++++++++.<--.>>+.>++.+++.,"</span>
|
||||
constant bf="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<++.>---------.>------.<----.++++++++.>>+.>++.+++."
|
||||
constant fb="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<+++.>---.>------.++++++++.<--.>>+.>++.+++.,"
|
||||
|
||||
<span style="color: #000000;">bfi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bf</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">bfi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fb</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
bfi(bf)
|
||||
bfi(fb)
|
||||
|
|
|
|||
|
|
@ -1,83 +0,0 @@
|
|||
REBOL [Title: "Brainfuck interpreter"]
|
||||
|
||||
tape: make object! [
|
||||
pos: 1
|
||||
data: [0]
|
||||
inc: does [
|
||||
data/:pos: data/:pos + 1
|
||||
]
|
||||
dec: does [
|
||||
data/:pos: data/:pos - 1
|
||||
]
|
||||
advance: does [
|
||||
pos: pos + 1
|
||||
if (length? data) <= pos [
|
||||
append data 0
|
||||
]
|
||||
]
|
||||
devance: does [
|
||||
if pos > 1 [
|
||||
pos: pos - 1
|
||||
]
|
||||
]
|
||||
get: does [
|
||||
data/:pos
|
||||
]
|
||||
]
|
||||
|
||||
brainfuck: make object! [
|
||||
data: string!
|
||||
code: ""
|
||||
init: func [instr] [
|
||||
self/data: instr
|
||||
]
|
||||
bracket-map: func [text] [
|
||||
leftstack: []
|
||||
bm: make map! []
|
||||
pc: 1
|
||||
for i 1 (length? text) 1 [
|
||||
c: text/:i
|
||||
if not find "+-<>[].," c [
|
||||
continue
|
||||
]
|
||||
if c == #"[" [
|
||||
append leftstack pc
|
||||
]
|
||||
if c == #"]" & ((length? leftstack) > 0) [
|
||||
left: last leftstack
|
||||
take/last leftstack
|
||||
append bm reduce [left pc]
|
||||
append bm reduce [pc left]
|
||||
]
|
||||
append code c
|
||||
pc: pc + 1
|
||||
]
|
||||
return bm
|
||||
]
|
||||
run: function [] [
|
||||
pc: 0
|
||||
tp: make tape []
|
||||
bm: bracket-map self/data
|
||||
while [pc <= (length? code)] [
|
||||
switch/default code/:pc [
|
||||
#"+" [tp/inc]
|
||||
#"-" [tp/dec]
|
||||
#">" [tp/advance]
|
||||
#"<" [tp/devance]
|
||||
#"[" [if tp/get == 0 [
|
||||
pc: bm/:pc
|
||||
]]
|
||||
#"]" [if tp/get != 0 [
|
||||
pc: bm/:pc
|
||||
]]
|
||||
#"." [prin to-string to-char tp/get]
|
||||
] []
|
||||
pc: pc + 1
|
||||
]
|
||||
print newline
|
||||
]
|
||||
]
|
||||
|
||||
bf: make brainfuck []
|
||||
bf/init input
|
||||
bf/run
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
'Execute BrainFuck
|
||||
'VBScript Implementation
|
||||
|
||||
'The Main Interpreter
|
||||
Function BFInpt(s, sp, d, dp, i, ip, o)
|
||||
While sp < Len(s)
|
||||
Select Case Mid(s, sp + 1, 1)
|
||||
Case "+"
|
||||
newd = Asc(d(dp)) + 1
|
||||
If newd > 255 Then newd = newd Mod 256 'To take account of values over 255
|
||||
d(dp) = Chr(newd)
|
||||
Case "-"
|
||||
newd = Asc(d(dp)) - 1
|
||||
If newd < 0 Then newd = (newd Mod 256) + 256 'To take account of negative values
|
||||
d(dp) = Chr(newd)
|
||||
Case ">"
|
||||
dp = dp + 1
|
||||
If dp > UBound(d) Then
|
||||
ReDim Preserve d(UBound(d) + 1)
|
||||
d(dp) = Chr(0)
|
||||
End If
|
||||
Case "<"
|
||||
dp = dp - 1
|
||||
Case "."
|
||||
o = o & d(dp)
|
||||
Case ","
|
||||
If ip = Len(i) Then d(dp) = Chr(0) Else ip = ip + 1 : d(dp) = Mid(i, ip, 1)
|
||||
Case "["
|
||||
If Asc(d(dp)) = 0 Then
|
||||
bracket = 1
|
||||
While bracket And sp < Len(s)
|
||||
sp = sp + 1
|
||||
If Mid(s, sp + 1, 1) = "[" Then
|
||||
bracket = bracket + 1
|
||||
ElseIf Mid(s, sp + 1, 1) = "]" Then
|
||||
bracket = bracket - 1
|
||||
End If
|
||||
WEnd
|
||||
Else
|
||||
pos = sp - 1
|
||||
sp = sp + 1
|
||||
If BFInpt(s, sp, d, dp, i, ip, o) Then sp = pos
|
||||
End If
|
||||
Case "]"
|
||||
BFInpt = Asc(d(dp)) <> 0
|
||||
Exit Function
|
||||
End Select
|
||||
sp = sp + 1
|
||||
WEnd
|
||||
End Function
|
||||
|
||||
'This Prepares the Intepreter
|
||||
Function BFuck(source, input)
|
||||
Dim data() : ReDim data(0)
|
||||
data(0) = Chr(0)
|
||||
DataPtr = 0
|
||||
SrcPtr = 0
|
||||
InputPtr = 0
|
||||
output = ""
|
||||
|
||||
BFInpt source , SrcPtr , _
|
||||
data , DataPtr , _
|
||||
input , InputPtr , _
|
||||
output
|
||||
BFuck = output
|
||||
End Function
|
||||
|
||||
|
||||
'Sample Run
|
||||
'The input is a string. The first character will be scanned by the first comma
|
||||
'in the code, the next character will be scanned by the next comma, and so on.
|
||||
|
||||
code = ">++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>" & _
|
||||
">+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++."
|
||||
inpstr = ""
|
||||
WScript.StdOut.Write BFuck(code, inpstr)
|
||||
Loading…
Add table
Add a link
Reference in a new issue