Execute Brain****: Difference between revisions

Removed Awk code's dependency on external programs, fixed 'ARGV too long' error
(Update to use new numeric escape sequence)
(Removed Awk code's dependency on external programs, fixed 'ARGV too long' error)
Line 164:
 
=={{header|AWK}}==
 
Depends on dd and xd, but works!
 
Expects the program (not the program file) to be the first argument to the script. Cells don't wrap (trivial if desired) and the code and arena are unbounded.
 
<lang AWK>BEGIN {
compile(bf=ARGV[1]); ARGV[1] = ""
compile(bf)
execute()
}
 
# Strips non-instructions, builds the jump table.
function compile(s, i,j,k,f) {
Line 181 ⟶ 180:
if(src[i] ~ /[\-\+\[\]\<\>,\.]/)
code[j++] = src[i]
 
if(src[i] == "[") {
marks[j] = 1
Line 202 ⟶ 201:
}
}
 
function execute( pc,p,i) {
pc = p = 0
while(pc in code) {
i = code[pc]
 
if(i == "+")
arena[p]++
Line 219 ⟶ 218:
printf("%c", arena[p])
else if(i == ",") {
while(1) {
"dd bs=1 count=1 2>/dev/null | xd -1d" | getline
if (goteof) break
sub(/^0*/, "", $2)
if (!gotline) {
arena[p] = 0 + $2
gotline = getline
close("dd bs=1 count=1 2>/dev/null | xd -1d")
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] = 0 + $2ord[c]
break
}
 
} else if((i == "[" && arena[p] == 0) ||
(i == "]" && arena[p] != 0))
Anonymous user