Skip to content

Commit

Permalink
Print an error when a data statement passes the end of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
iafisher committed Dec 13, 2018
1 parent 25a0201 commit bc04304
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
19 changes: 11 additions & 8 deletions hera/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from lark import Token

from .parser import Op
from .utils import copy_token, HERAError, is_symbol, to_u16
from .utils import copy_token, emit_error, HERAError, is_symbol, to_u16


# Arbitrary value copied over from HERA-C.
Expand Down Expand Up @@ -58,21 +58,24 @@ def get_labels(program):
pc = 0
dc = HERA_DATA_START
for op in program:
opname = op.name.lower()
if opname == "label":
odc = dc
if op.name == "LABEL":
labels[op.args[0]] = pc
elif opname == "dlabel":
elif op.name == "DLABEL":
labels[op.args[0]] = dc
elif opname == "constant":
elif op.name == "CONSTANT":
labels[op.args[0]] = op.args[1]
elif opname == "integer":
elif op.name == "INTEGER":
dc += 1
elif opname == "dskip":
elif op.name == "DSKIP":
dc += op.args[0]
elif opname == "lp_string":
elif op.name == "LP_STRING":
dc += len(op.args[0]) + 1
else:
pc += 1

if dc >= 0xFFFF and odc < 0xFFFF:
emit_error("past the end of available memory", line=op.name.line)
return labels


Expand Down
5 changes: 5 additions & 0 deletions test/assets/dskip_overflow.hera
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DSKIP(0xFFFF)
DLABEL(N)
INTEGER(42)

SET(R1, N)
10 changes: 10 additions & 0 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ def test_hera_boilerplate_dot_hera():
assert not vm.flag_carry_block


def test_dskip_overflow_dot_hera(capsys):
vm = VirtualMachine()
with pytest.raises(SystemExit):
main(["test/assets/dskip_overflow.hera"], vm)

captured = capsys.readouterr()
assert "DSKIP(0xFFFF)" in captured.err
assert "line 1" in captured.err


def test_error_message_for_missing_comma(capsys):
line = "SETLO(R1 40)"
with pytest.raises(SystemExit):
Expand Down

0 comments on commit bc04304

Please sign in to comment.