Skip to content

Commit

Permalink
fix brainfuck loop, fixed #112
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya committed Dec 22, 2016
1 parent c6d9ec2 commit 387b17d
Show file tree
Hide file tree
Showing 15 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion brainfuck2/bf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Program {
switch (op.op) {
case INC: tape.inc(op.val); break;
case MOVE: tape.move(op.val); break;
case LOOP: while (tape.get() != 0) _run(op.loop, tape); break;
case LOOP: while (tape.get() > 0) _run(op.loop, tape); break;
case PRINT: printf("%c", tape.get()); fflush(stdout); break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Program
when Op::Move
tape.move(op.val)
when Array(Op::T)
while tape.get != 0
while tape.get > 0
_run(op, tape)
end
when Op::Print
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Program
switch (op.op) {
case OpT.INC: tape.Inc(op.v); break;
case OpT.MOVE: tape.Move(op.v); break;
case OpT.LOOP: while (tape.Get() != 0) _run(op.loop, tape); break;
case OpT.LOOP: while (tape.Get() > 0) _run(op.loop, tape); break;
case OpT.PRINT: Console.Write((char)tape.Get()); break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.d
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct Program
mixin("tape." ~ type.to!string ~ "(); continue loop;");

case OpT.loop:
while (tape.get() != 0)
while (tape.get() > 0)
run(op.loop, tape);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.d8.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var Brainfuck = function(text) {
switch(op.op) {
case INC: tape.inc(op.v); break;
case MOVE: tape.move(op.v); break;
case LOOP: while (tape.get() != 0) _run(op.v, tape); break;
case LOOP: while (tape.get() > 0) _run(op.v, tape); break;
case PRINT: write(String.fromCharCode(tape.get())); break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func _run(program []Op, tape *Tape) {
switch op.O {
case INC: tape.Inc(op.V)
case MOVE: tape.Move(op.V)
case LOOP: for tape.Get() != 0 { _run(op.Loop, tape) }
case LOOP: for tape.Get() > 0 { _run(op.Loop, tape) }
case PRINT: fmt.Printf("%c", tape.Get())
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void _run(List<Op> program, Tape tape) {
switch (op.op) {
case INC: tape.inc(op.v); break;
case MOVE: tape.move(op.v); break;
case LOOP: while (tape.get() != 0) _run(op.loop, tape); break;
case LOOP: while (tape.get() > 0) _run(op.loop, tape); break;
case PRINT: System.out.print( (char) tape.get() ); break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var Brainfuck = function(text) {
switch(op.op) {
case INC: tape.inc(op.v); break;
case MOVE: tape.move(op.v); break;
case LOOP: while (tape.get() != 0) _run(op.v, tape); break;
case LOOP: while (tape.get() > 0) _run(op.v, tape); break;
case PRINT: process.stdout.write(String.fromCharCode(tape.get())); break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ proc runops(program: seq[Op], tape: var Tape) =
of INC: tape.inc(op.v)
of MOVE: tape.move(op.v)
of LOOP:
while tape.get != 0:
while tape.get > 0:
runops(op.loop, tape)
of PRINT:
write stdout, tape.get.chr
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _run(program, tape):
if op.op == INC: tape.inc(op.val)
elif op.op == MOVE: tape.move(op.val)
elif op.op == LOOP:
while tape.get() != 0:
while tape.get() > 0:
_run(op.val, tape)
elif op.op == PRINT:
sys.stdout.write(chr(tape.get()))
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _run(program, tape)
case op.op
when :inc; tape.inc(op.val)
when :move; tape.move(op.val)
when :loop; _run(op.val, tape) while tape.get != 0
when :loop; _run(op.val, tape) while tape.get > 0
when :print; print(tape.get.chr)
end
end
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn _run(program: &[Op], tape: &mut Tape) {
match *op {
Inc(x) => tape.inc(x),
Move(x) => tape.mov(x),
Loop(ref program) => while tape.get() != 0 {
Loop(ref program) => while tape.get() > 0 {
_run(program, tape);
},
Print => {
Expand Down
2 changes: 1 addition & 1 deletion brainfuck2/bf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Program(text: String) {
for (op <- program) op match {
case Inc(x) => tape.inc(x)
case Move(x) => tape.move(x)
case Loop(loop) => while (tape.get != 0) _run(loop, tape)
case Loop(loop) => while (tape.get > 0) _run(loop, tape)
case Print() => print(tape.get.toChar)
}
}
Expand Down
4 changes: 2 additions & 2 deletions brainfuck2/bf2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Program(code: String) {
when (op) {
is Op.Inc -> tape.inc(op.v)
is Op.Move -> tape.move(op.v)
is Op.Loop -> while (tape.get() != 0) {
is Op.Loop -> while (tape.get() > 0) {
_run(op.loop, tape)
}
is Op.Print -> print(tape.get().toChar())
Expand Down Expand Up @@ -93,4 +93,4 @@ fun main(args: Array<String>) {
val program = Program(code)
program.run()
System.err.println("time: ${(System.currentTimeMillis() - start_time) / 1e3}s")
}
}
2 changes: 1 addition & 1 deletion brainfuck2/bf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _run(program, tape):
if op.op == INC: tape.inc(op.val)
elif op.op == MOVE: tape.move(op.val)
elif op.op == LOOP:
while tape.get() != 0:
while tape.get() > 0:
_run(op.val, tape)
elif op.op == PRINT:
sys.stdout.write(chr(tape.get()))
Expand Down

0 comments on commit 387b17d

Please sign in to comment.