Skip to content

Commit

Permalink
Print error for data statement after non-data statement
Browse files Browse the repository at this point in the history
Resolves #10
  • Loading branch information
iafisher committed Dec 13, 2018
1 parent 21acf72 commit 2690bbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions hera/typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@
from .utils import emit_error, is_symbol, register_to_index


DATA_STATEMENTS = set(["CONSTANT", "DLABEL", "INTEGER", "LP_STRING", "DSKIP"])


def typecheck(program):
"""Type-check the program and emit errors as appropriate."""
end_of_data = False
for op in program:
if not end_of_data:
if op.name not in DATA_STATEMENTS:
end_of_data = True
else:
if op.name in DATA_STATEMENTS:
emit_error("data statement after instruction", line=op.name.line)
typecheck_one(op)


Expand Down
9 changes: 9 additions & 0 deletions test/test_typechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,12 @@ def test_typecheck_multiple_errors():
call_args = mock_emit_error.call_args_list[2][0]
assert "INC" in call_args[0]
assert "too few" in call_args[0]


def test_typecheck_data_statement_after_instruction():
program = [Op("SET", [R("R1"), 42]), Op(SYM("DLABEL"), [SYM("N")])]

with patch("hera.utils._emit_msg") as mock_emit_error:
typecheck(program)
assert mock_emit_error.call_count == 1
assert "data statement after instruction" in mock_emit_error.call_args[0][0]

0 comments on commit 2690bbd

Please sign in to comment.