Skip to content

Commit

Permalink
Have preprocessor and assembler print to stdout instead of stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
iafisher committed Feb 13, 2019
1 parent 28cd4d5 commit c199df2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Changed
- The `break` debugging command now prints the location of the breakpoint that it set.
- The preprocess subcommand now prints its normal output to standard out instead of standard error.

### Fixed
- When no file path is provided, the `break` debugging command defaults to the current file (whereas behavior was unpredictable before).
Expand Down
30 changes: 16 additions & 14 deletions hera/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,22 @@ def main_execute(path: str, settings: Settings) -> VirtualMachine:


def main_preprocess(path: str, settings: Settings) -> None:
"""Preprocess the program and print it to standard output."""
"""Preprocess the program and print it to stdout."""
program = load_program_from_file(path, settings)
if program.data:
sys.stderr.write("[DATA]\n")
print("[DATA]")
for data_op in program.data:
sys.stderr.write(" {}\n".format(data_op))
print(" {}".format(data_op))

if program.code:
sys.stderr.write("\n[CODE]\n")
print("\n[CODE]")

for i, op in enumerate(program.code):
sys.stderr.write(" {:0>4} {}\n".format(i, op))
print(" {:0>4} {}".format(i, op))


def main_assemble(path: str, settings: Settings) -> None:
"""Assemble the program into machine code and print the hex output to stdout."""
program = load_program_from_file(path, settings)
raw_code, raw_data = assemble(program)

Expand All @@ -108,17 +109,18 @@ def main_assemble(path: str, settings: Settings) -> None:

if settings.stdout:
if settings.data:
sys.stderr.write(data)
sys.stderr.write("\n")
print(data)
elif settings.code:
sys.stderr.write(code)
sys.stderr.write("\n")
print(code)
else:
sys.stderr.write("[DATA]\n")
sys.stderr.write(textwrap.indent(data, " "))
sys.stderr.write("\n[CODE]\n")
sys.stderr.write(textwrap.indent(code, " "))
print("[DATA]")
print(textwrap.indent(data, " "))
print("\n[CODE]")
print(textwrap.indent(code, " "))
else:
if path == "-":
path = "stdin"

with open(path + ".lcode", "w", encoding="ascii") as f:
f.write(code)

Expand Down Expand Up @@ -256,7 +258,7 @@ def dump_state(vm, settings):
nprint("\n{} warning{} emitted.".format(c, "" if c == 1 else "s"))


def bytes_to_hex(b):
def bytes_to_hex(b: bytes) -> str:
try:
return b.hex()
except AttributeError:
Expand Down
24 changes: 12 additions & 12 deletions test/test_assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@ def test_assemble_set_inc(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/set_inc.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/set_inc.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()


def test_assemble_binary_op(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/binary_op.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/binary_op.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()


def test_assemble_branch(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/branch.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/branch.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()


def test_assemble_flag(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/flag.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/flag.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()


def test_assemble_shift(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/shift.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/shift.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()


def test_assemble_misc(capsys):
main(["assemble", "--code", "--stdout", "test/assets/asm/misc.hera"])

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""

with open("test/assets/asm/misc.hera.lcode") as f:
assert captured.err == f.read()
assert captured.out == f.read()
20 changes: 9 additions & 11 deletions test/test_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def test_preprocess_simple_program(capsys):
preprocess_program_helper("SET(R1, 10)")

captured = capsys.readouterr()
assert captured.out == ""
# TODO: Have it print the newline to stdout as well.
assert captured.err == "\n"
assert (
captured.err
captured.out
== """\
0000 SETLO(R1, 10)
0001 SETHI(R1, 0)
"""
Expand All @@ -20,11 +20,10 @@ def test_preprocess_data(capsys):
preprocess_program_helper('LP_STRING("hello")')

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == "\n"
assert (
captured.err
captured.out
== """\
[DATA]
LP_STRING("hello")
"""
Expand All @@ -35,11 +34,10 @@ def test_preprocess_data_and_code(capsys):
preprocess_program_helper('DLABEL(s) LP_STRING("hello") SET(R1, s)')

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == "\n"
assert (
captured.err
captured.out
== """\
[DATA]
LP_STRING("hello")
Expand All @@ -54,5 +52,5 @@ def test_preprocess_character_literal(capsys):
preprocess_program_helper("SET(R1, 'A')")

captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == "\n 0000 SETLO(R1, 65)\n 0001 SETHI(R1, 0)\n"
assert captured.err == "\n"
assert captured.out == " 0000 SETLO(R1, 65)\n 0001 SETHI(R1, 0)\n"

0 comments on commit c199df2

Please sign in to comment.