Skip to content

Commit

Permalink
More compact write/read in MindFreak to_c
Browse files Browse the repository at this point in the history
  • Loading branch information
Maumagnaguagno committed Jan 5, 2024
1 parent 9498b4a commit 7169b42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions MindFreak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,17 @@ def to_c(program, tape = TAPE_DEFAULT_SIZE, eof = 0, type = 'unsigned int')
code << "#{indent}pointer += #{arg};"
when WRITE # Write
c = "putchar(*(pointer#{"+#{offset}" if offset}));"
code << "#{indent}#{arg > 1 ? "for(unsigned int i = #{arg}; i; --i) #{c}" : c}"
code << "#{indent}#{arg > 1 ? "for(unsigned int i = #{arg}; i--;) #{c}" : c}"
when READ # Read
code << "#{indent}for(unsigned int i = #{arg-1}; i; --i) getchar();" if arg > 1
code << "#{indent}for(unsigned int i = #{arg-1}; i--;) getchar();" if arg > 1
case eof
when nil
code << "#{indent}c = getchar();#{indent}if(c != EOF) (*(pointer#{"+#{offset}" if offset})) = c;"
code << "#{indent}if((c = getchar()) != EOF) (*(pointer#{"+#{offset}" if offset})) = c;"
eof_variable ||= "\n int c;"
when -1
code << "#{indent}(*(pointer#{"+#{offset}" if offset})) = getchar();"
else
code << "#{indent}c = getchar();#{indent}(*(pointer#{"+#{offset}" if offset})) = c == EOF ? #{eof} : c;"
code << "#{indent}(*(pointer#{"+#{offset}" if offset})) = (c = getchar()) != EOF ? c : #{eof};"
eof_variable ||= "\n int c;"
end
when JUMP # Jump if zero
Expand Down
6 changes: 3 additions & 3 deletions tests/rorschach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ def test_to_c_sum
def test_to_c_read_consecutive
program = ',,,,,'
assert_nil(MindFreak.check(program))
eof_zero = "int c;\n for(unsigned int i = 4; i; --i) getchar();\n c = getchar();\n (*(pointer)) = c == EOF ? 0 : c;\n return 0;\n}"
eof_minus_one = "for(unsigned int i = 4; i; --i) getchar();\n (*(pointer)) = getchar();\n return 0;\n}"
eof_unchanged = "int c;\n for(unsigned int i = 4; i; --i) getchar();\n c = getchar();\n if(c != EOF) (*(pointer)) = c;\n return 0;\n}"
eof_zero = "int c;\n for(unsigned int i = 4; i--;) getchar();\n (*(pointer)) = (c = getchar()) != EOF ? c : 0;\n return 0;\n}"
eof_minus_one = "for(unsigned int i = 4; i--;) getchar();\n (*(pointer)) = getchar();\n return 0;\n}"
eof_unchanged = "int c;\n for(unsigned int i = 4; i--;) getchar();\n if((c = getchar()) != EOF) (*(pointer)) = c;\n return 0;\n}"
# Default tape
assert_equal(
c_header << eof_zero,
Expand Down

0 comments on commit 7169b42

Please sign in to comment.