From 0b02df1ab65c51e9727fe44d75d84e687c4c7e40 Mon Sep 17 00:00:00 2001 From: Mau Magnaguagno Date: Wed, 3 Jan 2024 19:46:14 -0300 Subject: [PATCH] Prefer input read instead of pos STDIN is not compatible with pos. Revert 01dc656. --- MindFreak.rb | 6 +++--- README.md | 3 +-- tests/rorschach.rb | 15 +++++++++------ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/MindFreak.rb b/MindFreak.rb index f804d17..3e3276d 100644 --- a/MindFreak.rb +++ b/MindFreak.rb @@ -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 @@ -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 @@ -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 diff --git a/README.md b/README.md index a7f239a..93fbb0d 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/tests/rorschach.rb b/tests/rorschach.rb index c31a85f..1b3ffff 100644 --- a/tests/rorschach.rb +++ b/tests/rorschach.rb @@ -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