51 lines
1.8 KiB
Text
51 lines
1.8 KiB
Text
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]
|
|
|
|
--
|
|
-- 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
|
|
|
|
constant bf="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<++.>---------.>------.<----.++++++++.>>+.>++.+++."
|
|
constant fb="++++++++[>++++[>++>++++>+++>+<<<<-]>++>->+>>+[<]<-]>>.>>.+.<.>>.<<<+++.>---.>------.++++++++.<--.>>+.>++.+++.,"
|
|
|
|
bfi(bf)
|
|
bfi(fb)
|