Skip to content

Commit

Permalink
Prefer input read instead of pos
Browse files Browse the repository at this point in the history
STDIN is not compatible with pos.
Revert 01dc656.
  • Loading branch information
Maumagnaguagno committed Jan 3, 2024
1 parent 12c2e69 commit 0b02df1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions MindFreak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def run_bytecode(program, tape, eof = 0)
when WRITE # Write
arg > 1 ? @output.print(tape[@pointer].chr * arg) : @output.putc(tape[@pointer])
when READ # Read
@input.pos += arg - 1
@input.read(arg - 1)
tape[@pointer] = @input.getbyte || eof || next
when JUMP # Jump if zero
program_counter = arg if tape[@pointer] == 0
Expand Down Expand Up @@ -137,7 +137,7 @@ def run_bytecode2(program, tape, eof = 0)
c = tape[offset ? offset + @pointer : @pointer]
arg > 1 ? @output.print(c.chr * arg) : @output.putc(c)
when READ # Read
@input.pos += arg - 1
@input.read(arg - 1)
tape[offset ? offset + @pointer : @pointer] = @input.getbyte || eof || next
when JUMP # Jump if zero
program_counter = arg if tape[@pointer] == 0
Expand Down Expand Up @@ -175,7 +175,7 @@ def to_ruby(program, tape = TAPE_DEFAULT_SIZE, eof = 0, input = 'STDIN', output
c = "tape[#{pointer ? offset ? offset + pointer : pointer : "pointer#{"+#{offset}" if offset}"}]"
code << "#{indent}#{arg > 1 ? "#{output}.print #{c}.chr * #{arg}" : "#{output}.putc #{c}"}"
when READ # Read
code << "#{indent}#{input}.pos += #{arg - 1}" if arg > 1
code << "#{indent}#{input}.read(#{arg - 1})" if arg > 1
if eof
code << "#{indent}tape[#{pointer ? offset ? offset + pointer : pointer : "pointer#{"+#{offset}" if offset}"}] = #{input}.getbyte || #{eof}"
else
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ The main of this project is just an example of the API, all modes can be execute
## API
[**MindFreak**](MindFreak.rb) is a module with 4 attributes:
- ``attr_reader :pointer``, with the position of the current cell for interpreted execution modes, ``nil`` is the default.
- ``attr_accessor :input``, read external data from an object that responds to ``getbyte`` and ``pos``, ``STDIN`` is the default.
- ``attr_accessor :input``, read external data from an object that responds to ``getbyte`` and ``read``, ``STDIN`` is the default.
- ``attr_accessor :output``, write external data to an object that responds to ``putc`` and ``print``, ``STDOUT`` is the default.
- ``attr_accessor :debug``, print warnings when assigned to anything but ``false`` or ``nil``, ``nil`` is the default.

Expand Down Expand Up @@ -163,7 +163,6 @@ The [tests](tests/rorschach.rb) include several examples and can be used as a gu
- Feb 2023
- Support GCC and Clang
- Mar 2023
- Optimize consecutive read
- Optimize interpreter
- Custom EOF
- Apr 2023
Expand Down
15 changes: 9 additions & 6 deletions tests/rorschach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,30 +341,33 @@ def test_to_ruby_sum
def test_to_ruby_read_consecutive
program = ',,,,,'
assert_nil(MindFreak.check(program))
eof_zero = "\nSTDIN.read(4)\ntape[0] = STDIN.getbyte || 0"
eof_minus_one = "\nSTDIN.read(4)\ntape[0] = STDIN.getbyte || -1"
eof_unchanged = "\nSTDIN.read(4)\nc = STDIN.getbyte and tape[0] = c"
# Hash tape
assert_equal(
"tape = Hash.new(0)\npointer = 0\nSTDIN.pos += 4\ntape[0] = STDIN.getbyte || 0",
"tape = Hash.new(0)\npointer = 0" << eof_zero,
MindFreak.to_ruby(program, 0)
)
assert_equal(
"tape = Hash.new(0)\npointer = 0\nSTDIN.pos += 4\ntape[0] = STDIN.getbyte || -1",
"tape = Hash.new(0)\npointer = 0" << eof_minus_one,
MindFreak.to_ruby(program, 0, -1)
)
assert_equal(
"tape = Hash.new(0)\npointer = 0\nSTDIN.pos += 4\nc = STDIN.getbyte and tape[0] = c",
"tape = Hash.new(0)\npointer = 0" << eof_unchanged,
MindFreak.to_ruby(program, 0, nil)
)
# Array tape
assert_equal(
"\nSTDIN.pos += 4\ntape[0] = STDIN.getbyte || 0",
eof_zero,
MindFreak.to_ruby(program, [0])
)
assert_equal(
"\nSTDIN.pos += 4\ntape[0] = STDIN.getbyte || -1",
eof_minus_one,
MindFreak.to_ruby(program, [0], -1)
)
assert_equal(
"\nSTDIN.pos += 4\nc = STDIN.getbyte and tape[0] = c",
eof_unchanged,
MindFreak.to_ruby(program, [0], nil)
)
end
Expand Down

0 comments on commit 0b02df1

Please sign in to comment.