-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from .utils import emit_error | ||
|
||
|
||
# Arbitrary value copied over from HERA-C. | ||
HERA_DATA_START = 16743 | ||
|
||
|
||
def get_symtab(program): | ||
"""Return a dictionary mapping all labels and constants to their values.""" | ||
labels = {} | ||
pc = 0 | ||
dc = HERA_DATA_START | ||
for op in program: | ||
odc = dc | ||
if op.name == "LABEL": | ||
labels[op.args[0]] = pc | ||
elif op.name == "DLABEL": | ||
labels[op.args[0]] = dc | ||
elif op.name == "CONSTANT": | ||
labels[op.args[0]] = op.args[1] | ||
elif op.name == "INTEGER": | ||
dc += 1 | ||
elif op.name == "DSKIP": | ||
dc += op.args[0] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from hera.parser import Op | ||
from hera.symtab import get_symtab, HERA_DATA_START | ||
|
||
|
||
def test_get_symtab_with_example(): | ||
labels = get_symtab( | ||
[ | ||
Op("DLABEL", ["data"]), | ||
Op("INTEGER", [42]), | ||
Op("INTEGER", [43]), | ||
Op("DLABEL", ["data2"]), | ||
Op("INTEGER", [100]), | ||
Op("LABEL", ["top"]), | ||
Op("ADD", ["R0", "R0", "R0"]), | ||
Op("LABEL", ["bottom"]), | ||
] | ||
) | ||
assert len(labels) == 4 | ||
assert labels["data"] == HERA_DATA_START | ||
assert labels["data2"] == HERA_DATA_START + 2 | ||
assert labels["top"] == 0 | ||
assert labels["bottom"] == 1 | ||
|
||
|
||
def test_get_symtab_with_dskip(): | ||
labels = get_symtab( | ||
[ | ||
Op("DLABEL", ["data"]), | ||
Op("INTEGER", [42]), | ||
Op("DSKIP", [10]), | ||
Op("DLABEL", ["data2"]), | ||
Op("INTEGER", [84]), | ||
] | ||
) | ||
assert len(labels) == 2 | ||
assert labels["data"] == HERA_DATA_START | ||
assert labels["data2"] == HERA_DATA_START + 11 | ||
|
||
|
||
def test_get_symtab_with_lp_string(): | ||
labels = get_symtab( | ||
[ | ||
Op("DLABEL", ["S"]), | ||
Op("LP_STRING", ["hello"]), | ||
Op("DLABEL", ["X"]), | ||
Op("INTEGER", [42]), | ||
] | ||
) | ||
assert len(labels) == 2 | ||
assert labels["S"] == HERA_DATA_START | ||
assert labels["X"] == HERA_DATA_START + 6 | ||
|
||
|
||
def test_get_symtab_with_empty_lp_string(): | ||
labels = get_symtab( | ||
[ | ||
Op("DLABEL", ["S"]), | ||
Op("LP_STRING", [""]), | ||
Op("DLABEL", ["X"]), | ||
Op("INTEGER", [42]), | ||
] | ||
) | ||
assert len(labels) == 2 | ||
assert labels["S"] == HERA_DATA_START | ||
assert labels["X"] == HERA_DATA_START + 1 |