Skip to content

Commit

Permalink
Refactor 'read-program'
Browse files Browse the repository at this point in the history
Use as(<program>, string) to parse strings and read-program for files
  • Loading branch information
fraya committed May 13, 2024
1 parent 1ad9f10 commit 953f8de
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 43 deletions.
3 changes: 1 addition & 2 deletions sources/brainfuck-app.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ define function main
optimization-level := string-to-integer(arguments[1]);
end;

let locator = as(<file-locator>, program-name);
let program = read-program(locator);
let program = read-program(program-name);
let optimized = optimize-program(program, optimization-level);
run!(optimized);
format-out("\n");
Expand Down
2 changes: 1 addition & 1 deletion sources/library.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ define module brainfuck-impl

// Instruction
export
parse-character;
parse-instruction;

// Memory

Expand Down
2 changes: 1 addition & 1 deletion sources/optimizations.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ end function;

define function reset-to-zero
(origin :: <program>) => (optimized :: <program>)
let pattern = read-program("[-]");
let pattern = as(<program>, "[-]");
let optimization = make(<reset-to-zero>);
let optimized = make(<program>);
let _size = origin.size;
Expand Down
46 changes: 20 additions & 26 deletions sources/program.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ define constant <program-pointer>
define constant <program>
= limited(<vector>, of: <instruction>);

define method \=
(this :: <program>, that :: <program>)
=> (equals? :: <boolean>)
next-method()
end;

define sealed domain \= (<program>, <program>);

define constant $brainfuck-table
= instruction-table(<memory-pointer-increment>,
<memory-pointer-decrement>,
Expand All @@ -20,36 +28,22 @@ define constant $brainfuck-table
<jump-backward>,
<reset-to-zero>);

define function parse-character
define function parse-instruction
(char :: <character>) => (instruction :: <instruction>)
let type = element($brainfuck-table, char, default: <comment>);
make(type)
end;

define generic read-program
(object :: <object>) => (program :: <program>);

define method read-program
(sequence :: <sequence>) => (program :: <program>)
map-as(<program>, parse-character, sequence)
end;

define method read-program
(stream :: <stream>) => (program :: <program>)
read-program(read-to-end(stream))
end;

define method read-program
(locator :: <locator>) => (program :: <program>)
with-open-file (fs = locator, element-type: <byte-character>)
read-program(fs)
end;
end;

define method \=
(this :: <program>, that :: <program>)
=> (equals? :: <boolean>)
next-method()
define method as
(type == <program>, string :: <string>)
=> (program :: <program>)
map-as(<program>, parse-instruction, string)
end as;

define function read-program
(filename :: <string>) => (program :: <program>)
with-open-file (fs = filename, element-type: <byte-character>)
as(<program>, stream-contents(fs))
end
end;

define sealed domain \= (<program>, <program>);
26 changes: 13 additions & 13 deletions tests/brainfuck-test-suite.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Module: brainfuck-test-suite
//////////////////////////////////////////////////////////////////////////////

define test memory-data-increment-test ()
let bf = run!(read-program("+"));
let bf = run!(as(<program>, "+"));
expect-equal(1, bf.bf-pp);
expect-equal(1, bf.bf-memory[bf.bf-mp]);
end test;
Expand All @@ -23,14 +23,14 @@ end suite;
//////////////////////////////////////////////////////////////////////////////

define test test-parse-instructions ()
expect-instance?(<memory-data-increment>, parse-character('+'));
expect-instance?(<memory-data-decrement>, parse-character('-'));
expect-instance?(<memory-pointer-increment>, parse-character('>'));
expect-instance?(<memory-pointer-decrement>, parse-character('<'));
expect-instance?(<jump-forward>, parse-character('['));
expect-instance?(<jump-backward>, parse-character(']'));
expect-instance?(<input>, parse-character(','));
expect-instance?(<output>, parse-character('.'));
expect-instance?(<memory-data-increment>, parse-instruction('+'));
expect-instance?(<memory-data-decrement>, parse-instruction('-'));
expect-instance?(<memory-pointer-increment>, parse-instruction('>'));
expect-instance?(<memory-pointer-decrement>, parse-instruction('<'));
expect-instance?(<jump-forward>, parse-instruction('['));
expect-instance?(<jump-backward>, parse-instruction(']'));
expect-instance?(<input>, parse-instruction(','));
expect-instance?(<output>, parse-instruction('.'));
end;

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -40,14 +40,14 @@ end;
//////////////////////////////////////////////////////////////////////////////

define test test-reset-to-zero ()
let source = read-program("[-]");
let source = as(<program>, "[-]");
let optimized = reset-to-zero(source);
let expected = read-program("Z");
let expected = vector(make(<reset-to-zero>));
assert-equal(expected, optimized, "Replace '[-]' with 'Z'");
end;

define test test-group-instructions ()
let program = read-program("[[+++<<<>>>---]]");
let program = as(<program>, "[[+++<<<>>>---]]");
let expected = vector(make(<jump-forward>),
make(<jump-forward>),
make(<memory-data-increment>, amount: 3),
Expand All @@ -60,7 +60,7 @@ define test test-group-instructions ()
end;

define test test-precalculate-jumps ()
let program = read-program("[++++]");
let program = as(<program>, "[++++]");
let optimized = precalculate-jumps(program);
assert-equal(make(<jump-forward>, address: 5), optimized.first);
end;
Expand Down

0 comments on commit 953f8de

Please sign in to comment.