Execute Brain****: Difference between revisions

m
m (→‎{{header|TRS-80 BASIC}}: Add missing </syntaxhighlight> tag)
 
(12 intermediate revisions by 7 users not shown)
Line 2,466:
270 NEXT I
280 IF O THEN PRINT "UNMATCHED BRACKETS AT EOF. ABORTING.":END
290 REM SET MS TO NUMBER OF MEMORY CELLS NEEDED.
300 REM THE BF SPEC REQUIRES 30000, WHICH DOESWILL WORK ON AC64 SYSTEM WITHOR 48K+ RAMPET.
310 AN UNEXPANDED VIC-20 WILL HANDLE 1000, A C-16 9000. THE DEMO ONLY NEEDS 4.
310 REM THE DEMO HELLO-WORLD PROGRAM ONLY REQUIRES 4 CELLS.
320 MS=4:DIM M%(MS/2-1):MP=0
330 REM FUNCTION TO READ BYTE AT CELL N
Line 2,515:
{{Out}}
<pre>HELLO, WORLD!</pre>
 
 
==={{header|FreeBASIC}}===
Line 2,951 ⟶ 2,950:
<pre>Filename? hello.bf
Hello World!</pre>
 
=={{header|Binary Lambda Calculus}}==
 
The following 224-byte program
 
<pre>0000000 44 51 a1 01 84 55 d5 02 b7 70 30 22 ff 32 f0 00
0000020 bf f9 85 7f 5e e1 6f 95 7f 7d ee c0 e5 54 68 00
0000040 58 55 fd fb e0 45 57 fd eb fb f0 b6 f0 2f d6 07
0000060 e1 6f 73 d7 f1 14 bc c0 0b ff 2e 1f a1 6f 66 17
0000100 e8 5b ef 2f cf ff 13 ff e1 ca 34 20 0a c8 d0 0b
0000120 99 ee 1f e5 ff 7f 5a 6a 1f ff 0f ff 87 9d 04 d0
0000140 ab 00 05 db 23 40 b7 3b 28 cc c0 b0 6c 0e 74 10
0000160 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 5b 3e 2b 2b 2b 2b
0000200 2b 2b 2b 3e 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 3e 2b
0000220 2b 2b 3e 2b 3c 3c 3c 3c 2d 5d 3e 2b 2b 2e 3e 2b
0000240 2e 2b 2b 2b 2b 2b 2b 2b 2e 2e 2b 2b 2b 2e 3e 2b
0000260 2b 2e 3c 3c 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b 2b
0000300 2b 2b 2b 2e 3e 2e 2b 2b 2b 2e 2d 2d 2d 2d 2d 2d
0000320 2e 2d 2d 2d 2d 2d 2d 2d 2d 2e 3e 2b 2e 3e 2e 5d</pre>
consists of the 112-byte brainfuck interpreter https://github.com/tromp/AIT/blob/master/bf.blc8 followed by the 112-byte brainfuck hello world program
 
<pre>++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.]</pre>
 
and produces output
 
<pre>Hello World!
</pre>
 
=={{header|Brainf***}}==
 
Line 3,996 ⟶ 4,023:
=={{header|E}}==
[[/E|Implementation in E]].
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc exec code$ . .
len mem[] 100
dp = 1
code$[] = strchars code$
ip = 1
while ip <= len code$[]
if dp > len mem[]
len mem[] len mem[] + 100
.
if dp < 1
print "programm error"
return
.
c$ = code$[ip]
if c$ = "+"
mem[dp] += 1
elif c$ = "-"
mem[dp] -= 1
elif c$ = ">"
dp += 1
elif c$ = "<"
dp -= 1
elif c$ = "."
write strchar mem[dp]
elif c$ = ","
print "input not implemented"
elif c$ = "["
if mem[dp] = 0
br = 1
repeat
ip += 1
if code$[ip] = "["
br += 1
elif code$[ip] = "]"
br -= 1
.
until br = 0
.
else
br[] &= ip
.
elif c$ = "]"
ip = br[len br[]] - 1
len br[] -1
.
ip += 1
.
.
func syntax code$ .
for i to len code$
h$ = substr code$ i 1
if h$ = "["
br += 1
elif h$ = "]"
br -= 1
.
if br < 0
return 0
.
.
return if br = 0
.
repeat
inp$ = input
until inp$ = ""
code$ &= inp$
.
if syntax code$ <> 1
print "syntax error"
return
.
exec code$
#
input_data
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
 
</syntaxhighlight>
 
 
=={{header|Elena}}==
Line 6,757 ⟶ 6,866:
...
Print this backwards!</pre>
 
=={{header|R}}==
Unfortunately doesn't support the "," operator.
 
<syntaxhighlight lang="r">
bf <- function(code) {
instructions <- strsplit(code, "")[[1]]
tape <- c()
visited <- c()
 
pset <- function(n) {
if (n %in% visited)
p <<- n
else {
visited[length(visited)+1] <<- n
tape[as.character(n)] <<- 0
pset(n)
}
}
 
bracket <- function(b1, b2, x) {
nest <- 1
j <- i + x
while (nest != 0) {
if (instructions[j] == b1)
nest <- nest + 1
if (instructions[j] == b2)
nest <- nest - 1
j <- j + x
}
i <<- j
}
 
pset(0)
i <- 1
while (i <= length(instructions)) {
p_ <- as.character(p)
c <- instructions[i]
switch(c,
">" = pset(p + 1),
"<" = pset(p - 1),
"+" = tape[p_] <- tape[p_] + 1,
"-" = tape[p_] <- tape[p_] - 1,
"." = cat(intToUtf8(tape[p_])),
# TODO: IMPLEMENT ","
"[" = if (tape[p_] == 0) {
bracket("[", "]", 1)
i <- i - 1 # off by one error
},
"]" = bracket("]", "[", -1))
i <- i + 1
}
}
 
bf("++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++.>+.+++++++..+++.<<++.>+++++++++++++++.>.+++.------.--------.<<+.<.")
</syntaxhighlight>
 
To run:
<pre>
R -s --vanilla < bf.r
</pre>
 
=={{header|Racket}}==
Line 6,866 ⟶ 7,036:
bf/run
</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Arg 1>: e.File
, <ReadFile 1 e.File>: e.Source
, <ParseBF e.Source>: {
F e.Error = <Prout e.Error>;
T e.Prog = <RunProgram e.Prog>;
};
};
 
ReadFile {
s.Chan e.File = <Open 'r' s.Chan e.File>
<ReadFile (s.Chan)>;
(s.Chan), <Get s.Chan>: {
0 = <Close s.Chan>;
e.Line = <SanitizeBF e.Line> <ReadFile (s.Chan)>
};
}
 
SanitizeBF {
= ;
s.C e.X, '+-<>.,[]': e.L s.C e.R = s.C <SanitizeBF e.X>;
s.C e.X = <SanitizeBF e.X>;
};
 
ParseBF {
e.X, <CheckLoops e.X>: {
T = T <ParseLoops () () e.X>;
e.Err = e.Err;
};
};
 
CheckLoops {
(0) = T;
(s.N) = F 'Mismatched [';
(0) ']' e.X = F 'Mismatched ]';
(s.N) '[' e.X = <CheckLoops (<+ s.N 1>) e.X>;
(s.N) ']' e.X = <CheckLoops (<- s.N 1>) e.X>;
(s.N) s.I e.X = <CheckLoops (s.N) e.X>;
e.X = <CheckLoops (0) e.X>;
};
 
ParseLoops {
(e.X) (e.C) = e.X e.C;
(e.R) (e.Cur) '[' e.Prog = <ParseLoops (e.R (e.Cur)) () e.Prog>;
(e.R (e.Last)) (e.Cur) ']' e.Prog = <ParseLoops (e.R) (e.Last (e.Cur)) e.Prog>;
(e.R) (e.Cur) s.Instr e.Prog = <ParseLoops (e.R) (e.Cur s.Instr) e.Prog>;
}
 
RunProgram {
e.Prog, (() 0 ()): t.Tape,
(() ()): t.IObuf,
(t.Tape t.IObuf): t.State,
<RunBF t.State e.Prog>: (t.TapeOut t.IObufOut),
t.IObufOut: ((e.In) (e.Out)),
e.Out: {
= ;
e.X = <Prout e.X>;
};
};
 
RunBF {
t.State = t.State;
t.State t.Step e.Prog = <RunBF <StepBF t.State t.Step> e.Prog>;
};
 
StepBF {
(t.Tape t.IObuf) '+' = (<TapeF Inc t.Tape> t.IObuf);
(t.Tape t.IObuf) '-' = (<TapeF Dec t.Tape> t.IObuf);
(t.Tape t.IObuf) '<' = (<TapeLeft t.Tape> t.IObuf);
(t.Tape t.IObuf) '>' = (<TapeRight t.Tape> t.IObuf);
t.State ',' = <BFIn t.State>;
t.State '.' = <BFOut t.State>;
t.State (e.Loop), t.State: ((t.L 0 t.R) t.IObuf) = t.State;
t.State (e.Loop), <RunBF t.State e.Loop>: t.Newstate = <StepBF t.Newstate (e.Loop)>;
};
 
TapeLeft {
((e.L s.N) s.C (e.R)) = ((e.L) s.N (s.C e.R));
(() s.C (e.R)) = (() 0 (s.C e.R));
};
 
TapeRight {
((e.L) s.C (s.N e.R)) = ((e.L s.C) s.N (e.R));
((e.L) s.C ()) = ((e.L s.C) 0 ());
};
 
TapeF {
s.F ((e.L) s.C (e.R)) = ((e.L) <Mu s.F s.C> (e.R));
};
 
BFIn {
(t.Tape t.IObuf), t.Tape: (t.L s.C t.R),
t.IObuf: (t.In t.Out),
t.In: {
(s.Char e.Rest), (t.L s.Char t.R): t.Newtape,
((e.Rest) t.Out): t.NewIO
= (t.Newtape t.NewIO);
(), <Card>: {
0 = ((t.L 0 t.R) t.IObuf);
e.Line = <BFIn (t.Tape ((<Ord e.Line> 10) t.Out))>;
};
};
};
 
BFOut {
(t.Tape t.IObuf), t.Tape: (t.L s.C t.R),
t.IObuf: (t.In t.Out),
s.C: {
10, t.Out: (e.Line) = <Prout <Chr e.Line>> (t.Tape (t.In ()));
s.C, t.Out: (e.Line) = (t.Tape (t.In (e.Line s.C)));
};
};
 
Inc { s.X = <Mod <+ 1 s.X> 256>; };
Dec { s.X = <Mod <+ 255 s.X > 256>; };</syntaxhighlight>
{{out}}
<pre>$ cat hello.bf
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
$ refgo bf hello.bf
Hello World!</pre>
 
=={{header|REXX}}==
Line 6,927 ⟶ 7,219:
<pre>
Hello World!
</pre>
 
=={{header|RPL}}==
« 3000 DUP { } + 0 CON 'Tape' STO "" 'StdOut' STO 1
{ « 1 + »
« 1 - »
« 'Tape' OVER DUP2 GET 1 + PUT »
« 'Tape' OVER DUP2 GET 1 - PUT »
« StdOut 'Tape' 3 PICK GET CHR + 'StdOut' STO »
« 'Tape' OVER '''DO UNTIL''' KEY '''END''' PUT »
« '''IF''' 'Tape' OVER GET NOT '''THEN'''
1 CF
'''DO''' pgm pptr 1 + DUP 'pptr' STO DUP SUB
'''IF''' DUP "" == OVER "]" == OR '''THEN''' 1 SF '''END'''
'''UNTIL''' 1 FS? '''END END''' »
« '''IF''' 'Tape' OVER GET '''THEN'''
1 CF
'''DO''' pgm pptr 1 - DUP 'pptr' STO DUP SUB
'''IF''' DUP "" == '''THEN''' 1 SF pgm SIZE 'pptr' STO '''END'''
'''IF''' "[" == '''THEN''' 1 SF '''END'''
'''UNTIL''' 1 FS? '''END END''' »
}
→ pgm mmax pptr code
« 1
'''DO''' "><+-.,[]" pgm pptr DUP SUB POS
'''IF''' DUP '''THEN''' code SWAP GET EVAL '''ELSE''' DROP '''END'''
pptr 1 + 'pptr' STO
'''UNTIL''' DUP NOT OVER mmax > OR pptr pgm SIZE > OR '''END'''
DROP StdOut
{ 'Tape' 'StdOut'} PURGE
» » '<span style="color:blue">BRAIN</span>' STO
 
"++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." <span style="color:blue">BRAIN</span>
{{out}}
<pre>
1: "Hello world!"
</pre>
 
Line 7,169 ⟶ 7,497:
 
Original source [http://seed7.sourceforge.net/algorith/puzzles.htm#brainf7].
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program brainfuck;
if command_line(1) = om then
print("error: no program file given");
stop;
end if;
if (f := open(command_line(1), "r")) = om then
print("error: cannot open file");
stop;
end if;
[pgm, loopmap] := read_program(f);
close(f);
mem_left := [];
mem_right := [];
mem_cur := 0;
pc := 1;
loop while pc <= #pgm do
case pgm(pc) of
("+"): mem_cur +:= 1;
mem_cur mod:= 256;
("-"): mem_cur -:= 1;
mem_cur mod:= 256;
(">"): mem_left with:= mem_cur;
mem_cur frome mem_right;
mem_cur ?:= 0;
("<"): mem_right with:= mem_cur;
mem_cur frome mem_left;
mem_cur ?:= 0;
("."): putchar(char mem_cur);
(","): mem_cur := ichar (getchar ? '\x00');
("["): if mem_cur = 0 then pc := loopmap(pc); end if;
("]"): if mem_cur /= 0 then pc := loopmap(pc); end if;
end case;
pc +:= 1;
end loop;
proc read_program(f);
pgm := [];
loop doing ch := getc(f); while ch /= om do
if ch in "+-<>.,[]" then
pgm with:= ch;
end if;
end loop;
stack := [];
loopmap := {};
loop for i in [1..#pgm] do
case pgm(i) of
("["):
stack with:= i;
("]"):
j frome stack;
if j=om then
print("mismatched brackets");
stop;
end if;
loopmap(i) := j;
loopmap(j) := i;
end case;
end loop;
if stack /= [] then
print("mismatched brackets");
stop;
end if;
return [pgm, loopmap];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>$ cat hello.bf
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
$ setl brainfuck.setl hello.bf
Hello World!</pre>
 
=={{header|Sidef}}==
Line 7,574 ⟶ 7,980:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdin
 
class Brainf__k {
56

edits