Skip to content

Commit

Permalink
handle create pattern when expr is not an Expression/Atom
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Nov 16, 2022
1 parent c8cbe50 commit b81ccb8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions mathics/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,26 @@ class Pattern:
def create(expr: BaseElement) -> "Pattern":
"""
If ``expr`` is listed in ``pattern_object`` return the pattern found there.
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``.
Otherwise, create and return and ``ExpressionPattern`` for ``expr``.
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``;
if it is an ``Expression`` create and return and ``ExpressionPattern`` for ``expr``.
Finally, it tries to get one object of one of these classes
by calling the method ``to_expression``, and then call the function
again with this new object as input.
"""
if not isinstance(expr, (Atom, Expression)):
expr = expr.to_expression()

name = expr.get_head_name()
pattern_object = pattern_objects.get(name)
if pattern_object is not None:
return pattern_object(expr)
if isinstance(expr, Atom):
return AtomPattern(expr)
else:
elif isinstance(expr, Expression):
return ExpressionPattern(expr)
# Otherwise, try to get an expression and
# call again.
return Pattern.create(expr.to_expression())

def match(
self,
Expand Down

0 comments on commit b81ccb8

Please sign in to comment.