Skip to content

Commit b81ccb8

Browse files
committed
handle create pattern when expr is not an Expression/Atom
1 parent c8cbe50 commit b81ccb8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mathics/core/pattern.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,26 @@ class Pattern:
8080
def create(expr: BaseElement) -> "Pattern":
8181
"""
8282
If ``expr`` is listed in ``pattern_object`` return the pattern found there.
83-
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``.
84-
Otherwise, create and return and ``ExpressionPattern`` for ``expr``.
83+
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``;
84+
if it is an ``Expression`` create and return and ``ExpressionPattern`` for ``expr``.
85+
Finally, it tries to get one object of one of these classes
86+
by calling the method ``to_expression``, and then call the function
87+
again with this new object as input.
8588
"""
89+
if not isinstance(expr, (Atom, Expression)):
90+
expr = expr.to_expression()
8691

8792
name = expr.get_head_name()
8893
pattern_object = pattern_objects.get(name)
8994
if pattern_object is not None:
9095
return pattern_object(expr)
9196
if isinstance(expr, Atom):
9297
return AtomPattern(expr)
93-
else:
98+
elif isinstance(expr, Expression):
9499
return ExpressionPattern(expr)
100+
# Otherwise, try to get an expression and
101+
# call again.
102+
return Pattern.create(expr.to_expression())
95103

96104
def match(
97105
self,

0 commit comments

Comments
 (0)