From 7ed68a50b1379a8d8265ff76292ed0620d2a45e5 Mon Sep 17 00:00:00 2001 From: Alexis Roda Date: Wed, 7 Aug 2019 02:05:14 +0200 Subject: Fix issue 11. --- tests/test_parser.py | 23 +++++++++++++++++++++++ tests/test_z80count.py | 21 +++++++++++++++++++++ z80count/z80count.py | 11 ++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/test_z80count.py diff --git a/tests/test_parser.py b/tests/test_parser.py index 3153415..b6ab468 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -918,6 +918,10 @@ def test_lookup(instruction, cycles, parser_table): assert entry["cycles"] == cycles, "Failed: {} expected '{}' != found '{}'".format(instruction, cycles, entry["cycles"]) +########################################################################## +# _extract_mnemonic # +########################################################################## + @pytest.mark.parametrize("line,operator", ( ("foo: LD A, 1 ; load accumulator", "LD"), ("foo: CALL 0xABCD", "CALL"), @@ -935,3 +939,22 @@ def test_extract_mnemonic(line, operator): def test_extract_mnemonic_normalizes_operator(): assert Parser._extract_mnemonic("call 0xabcd") == "CALL" + + +########################################################################## +# _remove_label # +########################################################################## + +@pytest.mark.parametrize("line,expected", ( + ("foo: ld A, 1 ; load accumulator", "ld A, 1 ; load accumulator"), + ("foo: CALL 0xABCD", "CALL 0xABCD"), + ("foo: EI", "EI"), + ("LD A, 1 ; load accumulator", "LD A, 1 ; load accumulator"), + ("call 0xABCE", "call 0xABCE"), + ("EI", "EI"), + ("foo: ; some label", None), + ("foo:", None), + ("; some comment", None), +)) +def test_remove_label(line, expected): + assert Parser._remove_label(line) == expected diff --git a/tests/test_z80count.py b/tests/test_z80count.py new file mode 100644 index 0000000..c816359 --- /dev/null +++ b/tests/test_z80count.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +import pytest + +from z80count.z80count import Parser +from z80count.z80count import z80count + + +@pytest.mark.parametrize("line,expected", ( + ("PLY_InterruptionOn: call PLY_Init", + "PLY_InterruptionOn: call PLY_Init ; [17]\n"), + ("PLY_ReplayFrequency:\tld de,0", + "PLY_ReplayFrequency:\tld de,0 ; [10]\n"), + +)) +def test_issue_11(line, expected): + parser = Parser() + output, _ = z80count(line, parser, total=0, subt=False, + no_update=True, column=1, use_tabs=False, + tab_width=4, debug=False) + assert output == expected diff --git a/z80count/z80count.py b/z80count/z80count.py index a5b1fce..8356622 100644 --- a/z80count/z80count.py +++ b/z80count/z80count.py @@ -186,13 +186,14 @@ class Parser(object): """Simple parser based on a table of regexes.""" # [label:] OPERATOR [OPERANDS] [; comment] - _LINE_RE = re.compile(r"^([\w]+:)?\s*(?P\w+)(\s+.*)?$") + _LINE_RE = re.compile(r"^([\w]+:)?\s*(?P\w+)(?P\s+.*)?$") def __init__(self): self._table = self._load_table() def lookup(self, line): mnemo = self._extract_mnemonic(line) + line = self._remove_label(line) if mnemo is None or mnemo not in self._table: return None for entry in self._table[mnemo]: @@ -226,6 +227,14 @@ class Parser(object): return match.group("operator").upper() return None + @classmethod + def _remove_label(cls, line): + match = cls._LINE_RE.match(line) + if match: + rest = match.group("rest") or "" + return match.group("operator") + rest + return None + @staticmethod def _init_entry(entry): entry["cregex"] = re.compile(r"^\s*" + entry["regex"] + r"\s*(;.*)?$", re.I) -- cgit v1.2.3 From fd9f4e10bf775303d19273feaf327b4f5c52a74a Mon Sep 17 00:00:00 2001 From: Alexis Roda Date: Wed, 7 Aug 2019 02:08:27 +0200 Subject: Fix regex for labels. SDCC also allows $ and dots in the labels. --- tests/test_z80count.py | 2 ++ z80count/z80count.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_z80count.py b/tests/test_z80count.py index c816359..85a9084 100644 --- a/tests/test_z80count.py +++ b/tests/test_z80count.py @@ -9,6 +9,8 @@ from z80count.z80count import z80count @pytest.mark.parametrize("line,expected", ( ("PLY_InterruptionOn: call PLY_Init", "PLY_InterruptionOn: call PLY_Init ; [17]\n"), + ("$PLY_Interruption.On: call PLY_Init", + "$PLY_Interruption.On: call PLY_Init ; [17]\n"), ("PLY_ReplayFrequency:\tld de,0", "PLY_ReplayFrequency:\tld de,0 ; [10]\n"), diff --git a/z80count/z80count.py b/z80count/z80count.py index 8356622..abfa48f 100644 --- a/z80count/z80count.py +++ b/z80count/z80count.py @@ -186,7 +186,7 @@ class Parser(object): """Simple parser based on a table of regexes.""" # [label:] OPERATOR [OPERANDS] [; comment] - _LINE_RE = re.compile(r"^([\w]+:)?\s*(?P\w+)(?P\s+.*)?$") + _LINE_RE = re.compile(r"^([$.\w]+:)?\s*(?P\w+)(?P\s+.*)?$") def __init__(self): self._table = self._load_table() -- cgit v1.2.3