Skip to content

Commit

Permalink
Improve matcher and fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
palkeo committed Apr 23, 2020
1 parent 032d64b commit 30ee281
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pano/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ def __init__(self, hash, trace):

def cleanup_masks(self, trace):
def rem_masks(exp):
if m := match(exp, ("bool", ("cd", ":idx"))):
if m := match(exp, ("bool", ("cd", ":int:idx"))):
idx = m.idx
assert type(idx) == int
if idx in self.params and self.params[idx][0] == "bool":
return ("cd", idx)

elif m := match(exp, ("mask_shl", ":size", 0, 0, ("cd", ":idx"))):
elif m := match(exp, ("mask_shl", ":size", 0, 0, ("cd", ":int:idx"))):
size, idx = m.size, m.idx
assert type(idx) == int
if idx in self.params:
kind = self.params[idx][0]
def_size = type_to_mask(kind)
Expand Down
11 changes: 11 additions & 0 deletions pano/matcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import builtins
import logging

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,6 +48,10 @@ def _match_helper(expression, pattern, match):

if isinstance(pattern, str) and pattern.startswith(":"):
attr = pattern[1:]
if ":" in attr:
type_name, attr = attr.split(":")
if not isinstance(expression, getattr(builtins, type_name)):
raise NoMatch(expression, pattern)
if hasattr(match, attr):
if getattr(match, attr) == expression:
return
Expand Down Expand Up @@ -89,6 +94,12 @@ def match(expression, pattern):
2
>>> bool(match((1, 2, 3, 4), (tuple, ':two', ...)))
False
>>> bool(match((1, 2, 3, 4), (int, ':int:two', ...)))
True
>>> bool(match((1, "two", 3, 4), (int, ':int:two', ...)))
False
>>> match(("one", "two", 3, 4), (str, ':str:two', ...)).two
'two'
"""
m = Match()
try:
Expand Down

0 comments on commit 30ee281

Please sign in to comment.