diff --git a/hera/parser.py b/hera/parser.py index a7c9caa..96af9b2 100644 --- a/hera/parser.py +++ b/hera/parser.py @@ -44,6 +44,10 @@ def value(self, matches): ntkn.end_line = otkn.end_line ntkn.end_column = otkn.end_column return ntkn + elif matches[0].type == "SYMBOL": + if matches[0][0] in "rR" and matches[0][1:].isdigit(): + matches[0].type = "REGISTER" + return matches[0] else: return matches[0] @@ -65,10 +69,9 @@ def value(self, matches): _arglist: ( value "," )* value - value: DECIMAL | HEX | OCTAL | BINARY | REGISTER | SYMBOL | STRING + value: DECIMAL | HEX | OCTAL | BINARY | SYMBOL | STRING - REGISTER.3: /[rR][0-9]+/ - SYMBOL.2: /[A-Za-z_][A-Za-z0-9_]*/ + SYMBOL: /[A-Za-z_][A-Za-z0-9_]*/ DECIMAL: /-?[0-9]+/ HEX: /-?0x[0-9a-fA-F]+/ // TODO: How should I handle zero-prefixed numbers, which the HERA-C diff --git a/test/test_parser.py b/test/test_parser.py index 5fce607..3bc4d45 100644 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -71,6 +71,10 @@ def test_parse_negative_octal_number(): assert parse("SETLO(R3, -0o173)") == [Op("SETLO", ["R3", -123])] +def test_parse_label_starting_with_register_name(): + assert parse("LABEL(R1_INIT)") == [Op("LABEL", ["R1_INIT"])] + + def test_parse_single_line_comment(): assert parse("SETLO(R1, 0) // R1 <- 0") == [Op("SETLO", ["R1", 0])]