diff --git a/pano/function.py b/pano/function.py index 608a21ae..5960c969 100644 --- a/pano/function.py +++ b/pano/function.py @@ -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) diff --git a/pano/matcher.py b/pano/matcher.py index 97896c0f..5fbe0535 100644 --- a/pano/matcher.py +++ b/pano/matcher.py @@ -1,4 +1,5 @@ import copy +import builtins import logging logger = logging.getLogger(__name__) @@ -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 @@ -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: