Skip to content

Commit 2ffdf02

Browse files
mmaterarocky
andauthored
Correct LHS evaluation in SetDelayed assignment (#603)
* improving clarity in Builtin.contribute * adding comments. adding tests * fix test for SetDelayed. Move special case for Set with LHS a list away from the conditional. * more on modularize assignment. assign_elementary->assign * fixing set_eval * adding tests for OneIdentity * fix OneIdentity * remove comment * fix pytest * fix a bug that makes that URLSave always fails * fix WriteString standard output * catch not known attributes in ClearAttributes and SetAttributes * Update 'attributes' for current standards `mathics.builtin.attributes`: * apply -> eval * Add WMA links add * Class names ordered alphabetically * Hard breaks in docstring removed * Some incorrect references to "leaves" changed to "attributes" `mathics.core.attributes`: * Add short comments above flag value * adding comments. adding tests * fix test for SetDelayed. Move special case for Set with LHS a list away from the conditional. * more on modularize assignment. assign_elementary->assign * Handle optional with a first element that is not a Pattern[] * Update examples in OneIdentity * More pervasive use of Symbols symbols.py: system_symbols() -> symbol_set(); "systems_symbols" name is too close Symbol( System` to module systemsymbols. Also, we now require symbols as parameters), not strings. systemsymbols.py: more system symbols * Pattern_create -> Pattern.create It appears this was originally Pattern.create. I suspect due to bad modularity and a lack of understandig Python that an import could be added inside the routine, this static method got moved outside of the class. Later on, the modularity was fixed, but the hack persisted. These kinds of code smells side effects of poor communication. * Add function signature; straighten import issue * Put test_rules_patterns tests where they belong * Add note to add skipped example as a doctest ... When it gets fixed. * Changes suggested in PR review Move core-like assignment interals out of builtins and into core. (We may want to split up core more in the future though) Better sort long list of assignment methods alphabetically Some small RstT tagging in a docstring Remove nonexistent word "evaluable" Add yet another type annotation to a signature Make test assert failure messages unique * Move ASSIGNMENT_FUNCTION_MAP into core Co-authored-by: R. Bernstein <[email protected]> Co-authored-by: rocky <[email protected]>
1 parent 069deea commit 2ffdf02

File tree

12 files changed

+403
-306
lines changed

12 files changed

+403
-306
lines changed

mathics/builtin/assignments/assignment.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
"""
55

66

7-
from mathics.builtin.assignments.internals import _SetOperator
87
from mathics.builtin.base import BinaryOperator, Builtin
8+
from mathics.core.assignment import (
9+
ASSIGNMENT_FUNCTION_MAP,
10+
AssignmentException,
11+
assign_store_rules_by_tag,
12+
normalize_lhs,
13+
)
14+
915
from mathics.core.attributes import (
1016
A_HOLD_ALL,
1117
A_HOLD_FIRST,
@@ -18,6 +24,39 @@
1824
from mathics.core.systemsymbols import SymbolFailed
1925

2026

27+
class _SetOperator:
28+
"""
29+
This is the base class for assignment Builtin operators.
30+
31+
Special cases are determined by the head of the expression. Then
32+
they are processed by specific routines, which are poke from
33+
the ``ASSIGNMENT_FUNCTION_MAP`` dict.
34+
"""
35+
36+
# FIXME:
37+
# Assigment is determined by the LHS.
38+
# Are there a larger patterns or natural groupings that we are missing?
39+
# For example, it might be that it
40+
# we can key off of some attributes or other properties of the
41+
# LHS of a builtin, instead of listing all of the builtins in that class
42+
# (which may miss some).
43+
# Below, we key on a string, but Symbol is more correct.
44+
45+
def assign(self, lhs, rhs, evaluation, tags=None, upset=False):
46+
lhs, lookup_name = normalize_lhs(lhs, evaluation)
47+
try:
48+
# Using a builtin name, find which assignment procedure to perform,
49+
# and then call that function.
50+
assignment_func = ASSIGNMENT_FUNCTION_MAP.get(lookup_name, None)
51+
if assignment_func:
52+
return assignment_func(self, lhs, rhs, evaluation, tags, upset)
53+
54+
return assign_store_rules_by_tag(self, lhs, rhs, evaluation, tags, upset)
55+
except AssignmentException:
56+
57+
return False
58+
59+
2160
class Set(BinaryOperator, _SetOperator):
2261
"""
2362
<dl>

mathics/builtin/assignments/clear.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@
3838
SymbolUpValues,
3939
)
4040

41+
from mathics.core.assignment import is_protected
4142
from mathics.core.atoms import String
4243

43-
from mathics.builtin.assignments.internals import is_protected
44-
4544

4645
class Clear(Builtin):
4746
"""

mathics/builtin/assignments/types.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
from mathics.builtin.base import Builtin
1010

11-
from mathics.builtin.assignments.internals import get_symbol_values
12-
11+
from mathics.core.assignment import get_symbol_values
1312
from mathics.core.attributes import A_HOLD_ALL, A_PROTECTED
1413

1514

mathics/builtin/assignments/upvalues.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33

4-
from mathics.builtin.assignments.internals import _SetOperator
4+
from mathics.builtin.assignments.assignment import _SetOperator
55
from mathics.builtin.base import BinaryOperator
66
from mathics.core.attributes import (
77
A_HOLD_ALL,

mathics/builtin/atomic/symbols.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import re
99

10-
from mathics.builtin.assignments.internals import get_symbol_values
1110
from mathics.builtin.base import (
1211
Builtin,
1312
PrefixOperator,
@@ -16,6 +15,7 @@
1615

1716
from mathics.builtin.atomic.strings import to_regex
1817

18+
from mathics.core.assignment import get_symbol_values
1919
from mathics.core.atoms import String
2020

2121
from mathics.core.attributes import (

mathics/builtin/attributes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
from mathics.builtin.base import Predefined, Builtin
18-
from mathics.builtin.assignments.internals import get_symbol_list
18+
from mathics.core.assignment import get_symbol_list
1919
from mathics.core.atoms import String
2020
from mathics.core.attributes import (
2121
attributes_bitset_to_list,

mathics/builtin/scoping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"""
55

66

7-
from mathics.builtin.assignments.internals import get_symbol_list
87
from mathics.core.attributes import (
98
A_HOLD_ALL,
109
A_PROTECTED,
1110
attribute_string_to_number,
1211
)
1312
from mathics.builtin.base import Builtin, Predefined
13+
from mathics.core.assignment import get_symbol_list
1414
from mathics.core.atoms import (
1515
String,
1616
Integer,

0 commit comments

Comments
 (0)