diff --git a/mathics/builtin/list/constructing.py b/mathics/builtin/list/constructing.py index a142df164..72c673b40 100644 --- a/mathics/builtin/list/constructing.py +++ b/mathics/builtin/list/constructing.py @@ -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) diff --git a/mathics/builtin/patterns.py b/mathics/builtin/patterns.py index 44e783813..82f681837 100644 --- a/mathics/builtin/patterns.py +++ b/mathics/builtin/patterns.py @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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"] @@ -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""" @@ -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"] @@ -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 @@ -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 @@ -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): @@ -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): @@ -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"] @@ -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): @@ -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"] @@ -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) @@ -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) @@ -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, @@ -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"] @@ -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() @@ -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"] @@ -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"] @@ -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) @@ -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): @@ -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): @@ -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"] diff --git a/mathics/core/pattern.py b/mathics/core/pattern.py index 9c81a078f..7afadb705 100644 --- a/mathics/core/pattern.py +++ b/mathics/core/pattern.py @@ -239,7 +239,7 @@ def has_form(self, *args): """Compare the expression against a form""" return self.expr.has_form(*args) - def match(self, expression: BaseElement, **pattern_context): + def match(self, expression: BaseElement, pattern_context: dict): """ Check if the expression matches the pattern (self). If it does, calls `yield_func`. @@ -256,7 +256,7 @@ def match(self, expression: BaseElement, **pattern_context): """ raise NotImplementedError - def does_match(self, expression: BaseElement, **pattern_context) -> bool: + def does_match(self, expression: BaseElement, pattern_context: dict) -> bool: """returns True if `expression` matches self or we have reached the end of the matches, and False if it does not. """ @@ -274,10 +274,12 @@ def yield_match(sub_vars, rest): try: self.match( expression=expression, - yield_func=yield_match, - vars_dict=vars_dict, - evaluation=evaluation, - fully=fully, + pattern_context={ + "yield_func": yield_match, + "vars_dict": vars_dict, + "evaluation": evaluation, + "fully": fully, + }, ) except StopGenerator_Pattern as exc: return exc.value @@ -328,18 +330,20 @@ def __repr__(self): def match_symbol( self, expression: BaseElement, - **pattern_context, + pattern_context, ): """Match against a symbol""" assert isinstance(expression, BaseElement) if expression is self.atom: pattern_context["yield_func"](pattern_context["vars_dict"], None) - def get_match_symbol_candidates(self, elements: tuple, pattern_context) -> tuple: + def get_match_symbol_candidates( + self, elements: tuple, pattern_context: dict + ) -> tuple: """Find the sub-tuple of elements that matches with the pattern""" return tuple((element for element in elements if element is self.atom)) - def match(self, expression: BaseElement, **pattern_context): + def match(self, expression: BaseElement, pattern_context: dict): """Try to match the patterh with the expression.""" if isinstance(expression, Atom) and expression.sameQ(self.atom): @@ -412,7 +416,7 @@ def __set_pattern_attributes__(self, attributes): element.isliteral for element in self.elements ) - def match(self, expression: BaseElement, **pattern_context): + def match(self, expression: BaseElement, pattern_context: dict): """Try to match the pattern against an Expression""" evaluation = pattern_context["evaluation"] yield_func = pattern_context["yield_func"] @@ -518,7 +522,7 @@ def get_wrappings(self, yield_func: Callable, items: Tuple, pattern_context: dic def match_element( self, element: BaseElement, - **pattern_context, + pattern_context, ): """Try to match an element.""" attributes: int = pattern_context["attributes"] @@ -622,7 +626,9 @@ def get_match_candidates( ( element for element in elements - if self.does_match(element, evaluation=evaluation, vars_dict=vars_dict) + if self.does_match( + element, {"evaluation": evaluation, "vars_dict": vars_dict} + ) ) ) @@ -640,7 +646,9 @@ def get_match_candidates_count( count = 0 for element in elements: - if self.does_match(element, evaluation=evaluation, vars_dict=vars_dict): + if self.does_match( + element, {"evaluation": evaluation, "vars_dict": vars_dict} + ): count += 1 return count @@ -734,7 +742,7 @@ def match_expression_with_one_identity( # TODO: remove me eventually del parms["attributes"] - new_pattern.match(expression=expression, **parms) + new_pattern.match(expression=expression, pattern_context=parms) def basic_match_expression( @@ -787,7 +795,8 @@ def yield_choice(pre_vars): if not unmatched_elements: raise StopGenerator_ExpressionPattern_match() if not element.does_match( - unmatched_elements[0], evaluation=evaluation, vars_dict=pre_vars + unmatched_elements[0], + {"evaluation": evaluation, "vars_dict": pre_vars}, ): raise StopGenerator_ExpressionPattern_match() unmatched_elements = unmatched_elements[1:] @@ -814,17 +823,19 @@ def yield_choice(pre_vars): # def yield_element(new_vars, rest): # yield_func(new_vars, rest) self.match_element( - yield_func=yield_func, element=next_element, - rest_elements=tuple(next_elements), - rest_expression=([], expression.elements), - vars_dict=pre_vars, - expression=expression, - attributes=attributes, - evaluation=evaluation, - first=True, - fully=fully, - element_count=len(self.elements), + pattern_context={ + "yield_func": yield_func, + "rest_elements": tuple(next_elements), + "rest_expression": ([], expression.elements), + "vars_dict": pre_vars, + "expression": expression, + "attributes": attributes, + "evaluation": evaluation, + "first": True, + "fully": fully, + "element_count": len(self.elements), + }, ) # for head_vars, _ in self.head.match(expression.get_head(), vars, @@ -852,9 +863,11 @@ def yield_head(head_vars, _): self.head.match( expression.get_head(), - yield_func=yield_head, - vars_dict=vars_dict, - evaluation=evaluation, + { + "yield_func": yield_head, + "vars_dict": vars_dict, + "evaluation": evaluation, + }, ) @@ -974,7 +987,9 @@ def match_yield(new_vars, _): new_parms["element_index"] = parms["next_index"] new_parms["yield_func"] = element_yield del new_parms["element"] - pattern.match_element(element=parms["next_element"], **new_parms) + pattern.match_element( + element=parms["next_element"], pattern_context=new_parms + ) else: if not fully or (not items_rest[0] and not items_rest[1]): yield_func(new_vars, items_rest) @@ -982,13 +997,15 @@ def match_yield(new_vars, _): def yield_wrapping(item): parms["element"].match( item, - yield_func=match_yield, - vars_dict=parms["vars_dict"], - evaluation=evaluation, - fully=True, - head=expression.head, - element_index=parms["element_index"], - element_count=element_count, + { + "yield_func": match_yield, + "vars_dict": parms["vars_dict"], + "evaluation": evaluation, + "fully": True, + "head": expression.head, + "element_index": parms["element_index"], + "element_count": element_count, + }, ) # parms = parms.copy() diff --git a/mathics/core/rules.py b/mathics/core/rules.py index 4abb22e40..83feb70c3 100644 --- a/mathics/core/rules.py +++ b/mathics/core/rules.py @@ -164,10 +164,12 @@ def yield_match(vars, rest): try: self.pattern.match( expression, - yield_func=yield_match, - vars_dict={}, - evaluation=evaluation, - fully=fully, + pattern_context={ + "yield_func": yield_match, + "vars_dict": {}, + "evaluation": evaluation, + "fully": fully, + }, ) except StopGenerator_BaseRule as exc: # FIXME: figure where these values are not getting set or updated properly. diff --git a/mathics/doc/latex/mathics-title.pdf b/mathics/doc/latex/mathics-title.pdf deleted file mode 100644 index 996fb5c97..000000000 Binary files a/mathics/doc/latex/mathics-title.pdf and /dev/null differ diff --git a/mathics/eval/patterns.py b/mathics/eval/patterns.py index 66ccc8533..dfecc670d 100644 --- a/mathics/eval/patterns.py +++ b/mathics/eval/patterns.py @@ -19,7 +19,8 @@ def yield_func(vars, rest): try: self.form.match( - expr, yield_func=yield_func, vars_dict={}, evaluation=evaluation + expr, + {"yield_func": yield_func, "vars_dict": {}, "evaluation": evaluation}, ) except _StopGeneratorMatchQ: return True diff --git a/mathics/eval/test.py b/mathics/eval/test.py index 52943e33c..0f380219b 100644 --- a/mathics/eval/test.py +++ b/mathics/eval/test.py @@ -22,10 +22,12 @@ def yield_match(vars, rest): try: form.match( item, - yield_func=yield_match, - vars_dict={}, - evaluation=evaluation, - fully=False, + pattern_context={ + "yield_func": yield_match, + "vars_dict": {}, + "evaluation": evaluation, + "fully": False, + }, ) except _StopGeneratorBaseElementIsFree as exc: return exc.value diff --git a/mathics/eval/testing_expressions.py b/mathics/eval/testing_expressions.py index ac3e40d4f..e0b8a6f28 100644 --- a/mathics/eval/testing_expressions.py +++ b/mathics/eval/testing_expressions.py @@ -140,7 +140,7 @@ def check(level, expr): return SymbolFalse depth = len(dims) - 1 # None doesn't count - if not pattern.does_match(Integer(depth), evaluation=evaluation): + if not pattern.does_match(Integer(depth), {"evaluation": evaluation}): return SymbolFalse return SymbolTrue @@ -154,7 +154,7 @@ def check_SparseArrayQ(expr, pattern, test, evaluation: Evaluation): pattern = BasePattern.create(pattern, evaluation=evaluation) dims, default_value, rules = expr.elements[1:] - if not pattern.does_match(Integer(len(dims.elements)), evaluation): + if not pattern.does_match(Integer(len(dims.elements)), {"evaluation": evaluation}): return SymbolFalse array_size = Expression(SymbolTimes, *dims.elements).evaluate(evaluation)