Skip to content

Commit e9f4eaa

Browse files
committed
fix assignment
1 parent 4622f55 commit e9f4eaa

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

mathics/builtin/assignment.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@
1717
from mathics.core.definitions import PyMathicsLoadException
1818

1919

20+
def repl_pattern_by_symbol(expr):
21+
leaves = expr.get_leaves()
22+
if len(leaves) == 0:
23+
return expr
24+
25+
headname = expr.get_head_name()
26+
if headname == "System`Pattern":
27+
return leaves[0]
28+
29+
changed = False
30+
newleaves = []
31+
for leave in leaves:
32+
l = repl_pattern_by_symbol(leave)
33+
if not(l is leave):
34+
changed = True
35+
newleaves.append(l)
36+
if changed:
37+
return Expression(headname,*newleaves)
38+
else:
39+
return expr
40+
41+
2042
def get_symbol_list(list, error_callback):
2143
if list.has_form('List', None):
2244
list = list.leaves
@@ -38,6 +60,13 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
3860
name = lhs.get_head_name()
3961
lhs._format_cache = None
4062

63+
if name == "System`Pattern":
64+
lhsleaves= lhs.get_leaves()
65+
lhs = lhsleaves[1]
66+
rulerepl = (lhsleaves[0], repl_pattern_by_symbol(lhs))
67+
rhs, status = rhs.apply_rules([Rule(*rulerepl)], evaluation)
68+
name = lhs.get_head_name()
69+
4170
if name in system_symbols('OwnValues', 'DownValues', 'SubValues',
4271
'UpValues', 'NValues', 'Options',
4372
'DefaultValues', 'Attributes', 'Messages'):
@@ -220,7 +249,7 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
220249
elif lhs_name == 'System`$ContextPath':
221250
currContext = evaluation.definitions.get_current_context()
222251
context_path = [s.get_string_value() for s in rhs.get_leaves()]
223-
context_path = [s if (s is None or s[0]!="`") else currContext +s for s in context_path]
252+
context_path = [s if (s is None or s[0]!="`") else currContext[:-1] +s for s in context_path]
224253
if rhs.has_form('List', None) and all(valid_context_name(s) for s in context_path):
225254
evaluation.definitions.set_context_path(context_path)
226255
ignore_protection = True

0 commit comments

Comments
 (0)