Skip to content

Commit

Permalink
Print error message for undefined labels
Browse files Browse the repository at this point in the history
Resolves #17
  • Loading branch information
iafisher committed Dec 13, 2018
1 parent b0b2331 commit e126ac2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
11 changes: 9 additions & 2 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, is_symbol, to_u16
from .utils import copy_token, HERAError, is_symbol, to_u16


# Arbitrary value copied over from HERA-C.
Expand Down Expand Up @@ -36,7 +36,14 @@ def substitute_label(op, labels):
if op.name == "SETLO" and is_symbol(op.args[1]):
d, v = op.args
name = copy_token("SETLO", op.name)
return Op(name, [d, labels[v] & 0xFF])
try:
label = labels[v]
except KeyError:
raise HERAError(
"undefined symbol `{}`".format(v), line=op.name.line, column=v.column
) from None
else:
return Op(name, [d, label & 0xFF])
elif op.name == "SETHI" and is_symbol(op.args[1]):
d, v = op.args
name = copy_token("SETHI", op.name)
Expand Down
38 changes: 36 additions & 2 deletions test/test_preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
import pytest

from lark import Token

from hera.parser import Op
from hera.preprocessor import (
convert,
convert_set,
get_labels,
preprocess,
HERA_DATA_START,
preprocess,
substitute_label,
)
from hera.utils import IntToken
from hera.utils import HERAError, IntToken


def R(s):
return Token("REGISTER", s)


def SYM(s):
return Token("SYMBOL", s)


def test_substitute_label_with_SETLO():
labels = {"N": 10}
assert substitute_label(Op(SYM("SETLO"), [R("R1"), SYM("N")]), labels) == Op(
"SETLO", ["R1", 10]
)


def test_substitute_label_with_SETHI():
labels = {"N": 10}
# 0 is substituted and not 10 because only the high bits are taken.
assert substitute_label(Op(SYM("SETHI"), [R("R1"), SYM("N")]), labels) == Op(
"SETHI", ["R1", 0]
)


def test_substitute_label_with_other_op():
labels = {"N": 10}
assert substitute_label(Op(SYM("INC"), [R("R1"), SYM("N")]), labels) == Op(
"INC", ["R1", "N"]
)


def test_substitute_label_with_undefined_label():
with pytest.raises(HERAError):
substitute_label(Op(SYM("SETLO"), [R("R1"), SYM("N")]), {})


def test_convert_set_with_small_positive():
assert convert_set("R5", 18) == [Op("SETLO", ["R5", 18])]

Expand Down

0 comments on commit e126ac2

Please sign in to comment.