March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,5 +1,6 @@
BEGIN {
compile(ARGV[1])
bf=ARGV[1]; ARGV[1] = ""
compile(bf)
execute()
}
@ -48,10 +49,30 @@ function execute( pc,p,i) {
else if(i == ".")
printf("%c", arena[p])
else if(i == ",") {
"dd bs=1 count=1 2>/dev/null | xd -1d" | getline
sub(/^0*/, "", $2)
arena[p] = 0 + $2
close("dd bs=1 count=1 2>/dev/null | xd -1d")
while(1) {
if (goteof) break
if (!gotline) {
gotline = getline
if(!gotline) goteof = 1
if (goteof) break
line = $0
}
if (line == "") {
gotline=0
m[p]=10
break
}
if (!genord) {
for(i=1; i<256; i++)
ord[sprintf("%c",i)] = i
genord=1
}
c = substr(line, 1, 1)
line=substr(line, 2)
arena[p] = ord[c]
break
}
} else if((i == "[" && arena[p] == 0) ||
(i == "]" && arena[p] != 0))
pc = jump[pc]

View file

@ -0,0 +1,125 @@
set codeString to text returned of (display dialog "Enter BF code:" buttons "OK" default answer "")
set inputString to text returned of (display dialog "Enter input string" buttons "OK" default answer "")
set codePointer to 1
set loopPosns to {}
set tape to {}
set tapePointer to 1
set output to {}
set inputPointer to 1
set step to 0
set thePath to (path to desktop as Unicode text) & "log.txt"
set debug to (open for access file thePath with write permission)
write (step as string) & " (" & ((codePointer - 1) as string) & "): (The program contains " & ((length of codeString) as string) & " instructions.)
" to debug
set step to 1
on betterMod(x, y) -- so -2 mod 256 is 254 instead of -2
local x
local y
try
return -y * (round (x / y) rounding down) + x
on error eMsg number eNum
error "Can't call betterMod() on " & eMsg number eNum
end try
end betterMod
repeat while codePointer length of codeString
set theChar to (get character codePointer of codeString)
if (theChar = "+") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
set item tapePointer of tape to betterMod(((get item tapePointer of tape) + 1), 256)
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | a[" & ((tapePointer - 1) as string) & "]= " & ((item tapePointer of tape) as string) & "
" to debug
else if (theChar = "-") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
set item tapePointer of tape to betterMod(((get item tapePointer of tape) - 1), 256)
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | a[" & ((tapePointer - 1) as string) & "]= " & ((item tapePointer of tape) as string) & "
" to debug
else if (theChar = "<") then
set tapePointer to tapePointer - 1
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | array pos. now " & ((tapePointer - 1) as string) & "
" to debug
else if (theChar = ">") then
set tapePointer to tapePointer + 1
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | array pos. now " & ((tapePointer - 1) as string) & "
" to debug
else if (theChar = "[") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | Array[" & ((tapePointer - 1) as string) & "] is '" & ((item tapePointer of tape) as string) & "'" to debug
if (item tapePointer of tape 0) then
set loopPosns to loopPosns & codePointer
write " ** Loop nesting level: " & (((length of loopPosns) - 1) as string) & ".
" to debug
else
write "
" & (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | Not entering a loop but skipping to instruction number " to debug
set matchLoops to 1
repeat while matchLoops 0
set codePointer to codePointer + 1
if (item codePointer of codeString = "[") then
set matchLoops to matchLoops + 1
else if (item codePointer of codeString = "]") then
set matchLoops to matchLoops - 1
end if
end repeat
write ((codePointer - 1) as string) & "
" to debug
end if
else if (theChar = "]") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | Array[" & ((tapePointer - 1) as string) & "] is '" & ((item tapePointer of tape) as string) & "'
" to debug
if (item tapePointer of tape 0) then
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | looping back to " & (((item (length of loopPosns) of loopPosns) - 1) as string) & "
" to debug
set codePointer to (item (length of loopPosns) of loopPosns) - 1
end if
if (length of loopPosns > 1) then
set loopPosns to items 1 thru ((length of loopPosns) - 1) of loopPosns
else
set loopPosns to {}
end if
else if (theChar = ".") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | output '" & ((item tapePointer of tape) as string) & "' " & string id (item tapePointer of tape) & "
" to debug
set output to output & item tapePointer of tape
else if (theChar = ",") then
repeat while (length of tape < tapePointer)
set tape to tape & 0
end repeat
if (inputPointer > length of inputString) then
set inputPointer to 1
end if
set item tapePointer of tape to id of item inputPointer of inputString
set inputPointer to inputPointer + 1
write (step as string) & " (" & ((codePointer - 1) as string) & "): " & (item codePointer of codeString) & " | read in " & string id (item tapePointer of tape) & " (" & ((item tapePointer of tape) as string) & ")
" to debug
end if
set codePointer to codePointer + 1
set step to step + 1
end repeat
set strout to string id output
display dialog strout
close access debug

View file

@ -43,4 +43,10 @@ function brainfuck($source, $input='') {
$output);
return $output;
}
?>
$code = "
>++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>
>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.
";
$inp = '123';
print brainfuck( $code, $inp );

View file

@ -2,7 +2,7 @@ $ include "seed7_05.s7i";
const proc: brainF (in string: source, inout file: input, inout file: output) is func
local
var array char: memory is 100000 times '\0\';
var array char: memory is 100000 times '\0;';
var integer: dataPointer is 50000;
var integer: instructionPointer is 1;
var integer: nestingLevel is 0;
@ -16,7 +16,7 @@ const proc: brainF (in string: source, inout file: input, inout file: output) is
when {'.'}: write(output, memory[dataPointer]);
when {','}: memory[dataPointer] := getc(input);
when {'['}: # Forward if zero at dataPointer
if memory[dataPointer] = '\0\' then
if memory[dataPointer] = '\0;' then
nestingLevel := 1;
repeat
incr(instructionPointer);
@ -27,7 +27,7 @@ const proc: brainF (in string: source, inout file: input, inout file: output) is
until nestingLevel = 0;
end if;
when {']'}: # Backward if non-zero at dataPointer
if memory[dataPointer] <> '\0\' then
if memory[dataPointer] <> '\0;' then
nestingLevel := 1;
repeat
decr(instructionPointer);