Skip to content

Commit

Permalink
dict instead of kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Oct 1, 2024
1 parent 983c132 commit 81bcde5
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 93 deletions.
2 changes: 1 addition & 1 deletion mathics/builtin/list/constructing.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def eval(self, expr, patterns, f, evaluation: Evaluation):
def listener(e, tag):
result = False
for pattern, items in sown:
if pattern.does_match(tag, evaluation=evaluation):
if pattern.does_match(tag, {"evaluation": evaluation}):
for item in items:
if item[0].sameQ(tag):
item[1].append(e)
Expand Down
98 changes: 53 additions & 45 deletions mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def init(
if match_function:
self.match = match_function

def match_atom(self, expression, **pattern_context):
def match_atom(self, expression: Expression, pattern_context: dict):
"""Match function for AtomQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -619,9 +619,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_string(self, expression, **pattern_context):
def match_string(self, expression: Expression, pattern_context: dict):
"""Match function for StringQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -635,9 +635,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_numberq(self, expression, **pattern_context):
def match_numberq(self, expression: Expression, pattern_context: dict):
"""Match function for NumberQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -651,9 +651,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_numericq(self, expression, **pattern_context):
def match_numericq(self, expression: Expression, pattern_context: dict):
"""Match function for NumericQ"""
yield_func = pattern_context["yield_func"]
evaluation = pattern_context["evaluation"]
Expand All @@ -668,9 +668,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_real_numberq(self, expression, **pattern_context):
def match_real_numberq(self, expression: Expression, pattern_context: dict):
"""Match function for RealValudNumberQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -684,9 +684,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_positive(self, expression, **pattern_context):
def match_positive(self, expression: Expression, pattern_context: dict):
"""Match function for PositiveQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -700,9 +700,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_negative(self, expression, **pattern_context):
def match_negative(self, expression: Expression, pattern_context: dict):
"""Match function for NegativeQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -716,9 +716,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_nonpositive(self, expression, **pattern_context):
def match_nonpositive(self, expression: Expression, pattern_context: dict):
"""Match function for NonPositiveQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -732,9 +732,9 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def match_nonnegative(self, expression, **pattern_context):
def match_nonnegative(self, expression: Expression, pattern_context: dict):
"""Match function for NonNegativeQ"""
yield_func = pattern_context["yield_func"]

Expand All @@ -748,7 +748,7 @@ def yield_match(vars_2, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)

def quick_pattern_test(self, candidate, test, evaluation: Evaluation):
"""Pattern test for some other special cases"""
Expand All @@ -775,7 +775,7 @@ def quick_pattern_test(self, candidate, test, evaluation: Evaluation):
return builtin.test(candidate)
return None

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match expression with PatternTest"""
evaluation = pattern_context["evaluation"]
vars_dict = pattern_context["vars_dict"]
Expand Down Expand Up @@ -805,9 +805,11 @@ def yield_match(vars_2, rest):
# try:
self.pattern.match(
expression,
yield_func=yield_match,
vars_dict=vars_dict,
evaluation=evaluation,
{
"yield_func": yield_match,
"vars_dict": vars_dict,
"evaluation": evaluation,
},
)
# except StopGenerator:
# pass
Expand Down Expand Up @@ -851,13 +853,13 @@ def init(
for element in expr.elements
]

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with Alternatives"""
for alternative in self.alternatives:
# for new_vars_dict, rest in alternative.match(
# expression, vars_dict, evaluation):
# yield_func(new_vars_dict, rest)
alternative.match(expression, **pattern_context)
alternative.match(expression, pattern_context)

def get_match_count(
self, vars_dict: OptionalType[dict] = None
Expand Down Expand Up @@ -918,7 +920,7 @@ def init(
else:
self.p = BasePattern.create(Expression(SymbolBlank), evaluation=evaluation)

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with Exception Pattern"""

def except_yield_func(vars_dict, rest):
Expand All @@ -928,11 +930,11 @@ def except_yield_func(vars_dict, rest):
new_pattern_context["yield_func"] = except_yield_func

try:
self.c.match(expression, **new_pattern_context)
self.c.match(expression, new_pattern_context)
except _StopGeneratorExcept:
pass
else:
self.p.match(expression, **pattern_context)
self.p.match(expression, pattern_context)


class Verbatim(PatternObject):
Expand Down Expand Up @@ -967,7 +969,7 @@ def init(
super().init(expr, evaluation=evaluation)
self.content = expr.elements[0]

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with Verbatim Pattern"""
vars_dict = pattern_context["vars_dict"]
yield_func = pattern_context["yield_func"]
Expand Down Expand Up @@ -1007,11 +1009,11 @@ def init(
super().init(expr, evaluation=evaluation)
self.pattern = BasePattern.create(expr.elements[0], evaluation=evaluation)

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
# for new_vars_dict, rest in self.pattern.match(
# expression, vars_dict, evaluation):
# yield new_vars_dict, rest
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)


class Pattern(PatternObject):
Expand Down Expand Up @@ -1106,7 +1108,7 @@ def get_match_count(
) -> Union[int, tuple]:
return self.pattern.get_match_count(vars_dict)

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with a (named) pattern"""
yield_func = pattern_context["yield_func"]
vars_dict = pattern_context["vars_dict"]
Expand All @@ -1121,9 +1123,13 @@ def match(self, expression, **pattern_context):
# expression, new_vars_dict, evaluation):
# yield vars_2, rest
if isinstance(self.pattern, OptionsPattern):
self.pattern.match(expression=expression, **pattern_context)
self.pattern.match(
expression=expression, pattern_context=pattern_context
)
else:
self.pattern.match(expression=expression, **pattern_context)
self.pattern.match(
expression=expression, pattern_context=pattern_context
)
else:
if existing.sameQ(expression):
yield_func(vars_dict, None)
Expand Down Expand Up @@ -1211,7 +1217,7 @@ def init(
else:
self.default = None

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
head = pattern_context.get("head", None)
evaluation = pattern_context["evaluation"]
element_index = pattern_context.get("element_index", None)
Expand Down Expand Up @@ -1241,7 +1247,7 @@ def match(self, expression, **pattern_context):
# yield vars_2, rest
self.pattern.match(
expression,
**{
{
"yield_func": yield_func,
"vars_dict": vars_dict,
"evaluation": evaluation,
Expand Down Expand Up @@ -1350,7 +1356,7 @@ class Blank(_Blank):
}
summary_text = "match to any single expression"

def match(self, expression: Expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
vars_dict = pattern_context["vars_dict"]
yield_func = pattern_context["yield_func"]

Expand Down Expand Up @@ -1401,7 +1407,7 @@ class BlankSequence(_Blank):
}
summary_text = "match to a non-empty sequence of elements"

def match(self, expression: Expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
vars_dict = pattern_context["vars_dict"]
yield_func = pattern_context["yield_func"]
elements = expression.get_sequence()
Expand Down Expand Up @@ -1445,7 +1451,7 @@ class BlankNullSequence(_Blank):
}
summary_text = "match to a sequence of zero or more elements"

def match(self, expression: Expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with a BlankNullSequence"""
vars_dict = pattern_context["vars_dict"]
yield_func = pattern_context["yield_func"]
Expand Down Expand Up @@ -1517,7 +1523,7 @@ def init(
else:
self.error("range", 2, expr)

def match(self, expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with Repeated[...]"""
yield_func = pattern_context["yield_func"]
vars_dict = pattern_context["vars_dict"]
Expand All @@ -1540,9 +1546,11 @@ def yield_match(new_vars_dict, rest):

self.pattern.match(
rest_elements[0],
yield_func=yield_match,
vars_dict=vars_dict,
evaluation=evaluation,
{
"yield_func": yield_match,
"vars_dict": vars_dict,
"evaluation": evaluation,
},
)
else:
yield_iter(vars_dict, None)
Expand Down Expand Up @@ -1666,7 +1674,7 @@ def init(
# else:
self.pattern = BasePattern.create(expr.elements[0], evaluation=evaluation)

def match(self, expression: Expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with Condition pattern"""
# for new_vars_dict, rest in self.pattern.match(expression, vars_dict,
# evaluation):
Expand All @@ -1681,7 +1689,7 @@ def yield_match(new_vars_dict, rest):

pattern_context = pattern_context.copy()
pattern_context["yield_func"] = yield_match
self.pattern.match(expression, **pattern_context)
self.pattern.match(expression, pattern_context)


class OptionsPattern(PatternObject):
Expand Down Expand Up @@ -1738,7 +1746,7 @@ def init(
# function. Set to not None in self.match
self.defaults = None

def match(self, expression: Expression, **pattern_context):
def match(self, expression: Expression, pattern_context: dict):
"""Match with an OptionsPattern"""
head = pattern_context.get("head", None)
evaluation = pattern_context["evaluation"]
Expand Down
Loading

0 comments on commit 81bcde5

Please sign in to comment.