Skip to content

Commit c5834d0

Browse files
mmaterarocky
authored andcommitted
Handle optional with a first element that is not a Pattern[]
1 parent f66757a commit c5834d0

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

mathics/core/pattern.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,20 @@ def yield_head(head_vars, _):
393393
elif pat_elem.get_head_name() == "System`Optional":
394394
if len(pat_elem.elements) == 2:
395395
pat, value = pat_elem.elements
396-
assert pat.get_head_name() == "System`Pattern"
397-
key = pat.elements[0].atom.name
396+
if pat.get_head_name() == "System`Pattern":
397+
key = pat.elements[0].atom.name
398+
else:
399+
# if the first element of the Optional
400+
# is not a `Pattern`, then we need to
401+
# store an empty element.
402+
key = ""
398403
optionals[key] = value
399404
elif len(pat_elem.elements) == 1:
400405
pat = pat_elem.elements[0]
401-
assert pat.get_head_name() == "System`Pattern"
402-
key = pat.elements[0].atom.name
406+
if pat.get_head_name() == "System`Pattern":
407+
key = pat.elements[0].atom.name
408+
else:
409+
key = ""
403410
# Now, determine the default value
404411
defaultvalue_expr = Expression(
405412
SymbolDefault, pattern_head, Integer(default_indx)
@@ -420,7 +427,9 @@ def yield_head(head_vars, _):
420427
if len(optionals) == 0:
421428
return
422429

423-
# Load the default values in vars
430+
# Remove the empty key and load the default values in vars
431+
if "" in optionals:
432+
del optionals[""]
424433
vars.update(optionals)
425434
# Try to match the non-optional element with the expression
426435
new_pattern.match(

0 commit comments

Comments
 (0)