From 9be83cceb5006815fd05f9c7ba92320d58d1b2c0 Mon Sep 17 00:00:00 2001 From: Ian Fisher Date: Thu, 13 Dec 2018 22:32:15 -0500 Subject: [PATCH] Do not choke on invalid data statements --- hera/symtab.py | 12 +++++++----- test/test_symtab.py | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/hera/symtab.py b/hera/symtab.py index 1af01f7..1e37406 100644 --- a/hera/symtab.py +++ b/hera/symtab.py @@ -13,17 +13,19 @@ def get_symtab(program): dc = HERA_DATA_START for op in program: odc = dc - if op.name == "LABEL": + if op.name == "LABEL" and len(op.args) == 1: labels[op.args[0]] = pc - elif op.name == "DLABEL": + elif op.name == "DLABEL" and len(op.args) == 1: labels[op.args[0]] = dc - elif op.name == "CONSTANT": + elif op.name == "CONSTANT" and len(op.args) == 2: labels[op.args[0]] = op.args[1] elif op.name == "INTEGER": dc += 1 - elif op.name == "DSKIP": + elif op.name == "DSKIP" and len(op.args) == 1 and isinstance(op.args[0], int): dc += op.args[0] - elif op.name == "LP_STRING": + elif ( + op.name == "LP_STRING" and len(op.args) == 1 and isinstance(op.args[0], str) + ): dc += len(op.args[0]) + 1 else: pc += len(convert(op)) diff --git a/test/test_symtab.py b/test/test_symtab.py index 1ca7db9..766491b 100644 --- a/test/test_symtab.py +++ b/test/test_symtab.py @@ -63,3 +63,9 @@ def test_get_symtab_with_empty_lp_string(): assert len(labels) == 2 assert labels["S"] == HERA_DATA_START assert labels["X"] == HERA_DATA_START + 1 + + +def test_get_symtab_with_invalid_instructions(): + labels = get_symtab([Op("CONSTANT", ["N"]), Op("CONSTANT", ["X", 42])]) + assert len(labels) == 1 + assert labels["X"] == 42