Skip to content

Commit 2b2667d

Browse files
committed
Add more typing
Little improvement (measured) in var unification
1 parent e612e38 commit 2b2667d

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

src/arch/zx48k/peephole/pattern.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,11 @@ def match(self, line: str, vars_: Dict[str, str]) -> bool:
8080
if match is None:
8181
return False
8282

83-
vars__ = {'_' + k[1:]: v for k, v in (vars_ or {}).items()}
8483
mdict = match.groupdict()
85-
if any(mdict.get(k, v) != v for k, v in vars__.items()):
84+
if any(mdict.get(k, v) != v for k, v in vars_.items()):
8685
return False
8786

88-
vars_.update({'$' + k[1:]: v for k, v in match.groupdict().items()})
87+
vars_.update(mdict)
8988
return True
9089

9190
def __repr__(self):
@@ -128,7 +127,7 @@ def match(self, instructions: List[str], start: int = 0) -> Optional[Dict[str, s
128127
if not patt.match(line, vars_=univars):
129128
return None
130129

131-
return univars
130+
return {'$' + k[1:]: v for k, v in univars.items()}
132131

133132
def __repr__(self):
134133
return str([repr(x) for x in self.patterns])

src/arch/zx48k/peephole/template.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from typing import List
4+
35
from .pattern import BasicLinePattern
46

57

@@ -13,7 +15,7 @@ class LineTemplate(BasicLinePattern):
1315
with their values. '$$' is replaced by '$'. If any variable is unbound,
1416
an assertion is raised.
1517
"""
16-
def filter(self, vars_=None):
18+
def filter(self, vars_=None) -> str:
1719
""" Applies a list of vars to the given pattern and returns the line
1820
"""
1921
vars_ = vars_ or {}
@@ -33,14 +35,14 @@ def __repr__(self):
3335
return self.line
3436

3537

36-
class BlockTemplate(object):
38+
class BlockTemplate:
3739
""" Extends a Line template to a block of them
3840
"""
3941
def __init__(self, lines):
4042
lines = [x.strip() for x in lines]
4143
self.templates = [LineTemplate(x) for x in lines if x]
4244

43-
def filter(self, vars_=None):
45+
def filter(self, vars_=None) -> List[str]:
4446
return [y for y in [x.filter(vars_) for x in self.templates] if y]
4547

4648
def __repr__(self):

tests/arch/zx48k/peephole/test_pattern.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,27 @@ def setUp(self) -> None:
4646
def test_matches_parsed(self):
4747
patt = pattern.LinePattern('push $1')
4848
self.assertTrue(patt.match('push af', self.vars))
49-
self.assertEqual({'$1': 'af'}, self.vars)
49+
self.assertEqual({'_1': 'af'}, self.vars)
5050

5151
def test_no_match(self):
52-
patt = pattern.LinePattern('push $1')
52+
patt = pattern.LinePattern('push _1')
5353
self.assertFalse(patt.match('pop af', self.vars))
5454
self.assertEqual({}, self.vars)
5555

5656
def test_double_match(self):
5757
patt = pattern.LinePattern('push $1 $1') # three spaces
5858
self.assertTrue(patt.match('push af af', self.vars)) # only two spaces
59-
self.assertEqual({'$1': 'af'}, self.vars)
59+
self.assertEqual({'_1': 'af'}, self.vars)
6060

6161
def test_match_two_patterns(self):
6262
patt = pattern.LinePattern('$2 $1')
6363
self.assertTrue(patt.match('push af', self.vars))
64-
self.assertEqual({'$1': 'af', '$2': 'push'}, self.vars)
64+
self.assertEqual({'_1': 'af', '_2': 'push'}, self.vars)
6565

6666
def test_match_two_patterns_twice(self):
6767
patt = pattern.LinePattern('$2 $1 $2 $1')
6868
self.assertTrue(patt.match('push af push af', self.vars))
69-
self.assertEqual({'$1': 'af', '$2': 'push'}, self.vars)
69+
self.assertEqual({'_1': 'af', '_2': 'push'}, self.vars)
7070

7171
def test_matches_empty_novars(self):
7272
patt = pattern.LinePattern('push af')

0 commit comments

Comments
 (0)