Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes suggested in PR review #622

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@
"""


from mathics.builtin.assignments.internals import _SetOperator
from mathics.builtin.base import BinaryOperator, Builtin
from mathics.core.assignment import (
AssignmentException,
assign_store_rules_by_tag,
normalize_lhs,
process_assign_attributes,
process_assign_context,
process_assign_context_path,
process_assign_default,
process_assign_definition_values,
process_assign_format,
process_assign_list,
process_assign_makeboxes,
process_assign_messagename,
process_assign_n,
process_assign_numericq,
process_assign_options,
process_assign_random_state,
process_assign_part,
)

from mathics.core.attributes import (
A_HOLD_ALL,
A_HOLD_FIRST,
Expand All @@ -18,6 +37,62 @@
from mathics.core.systemsymbols import SymbolFailed


class _SetOperator:
"""
This is the base class for assignment Builtin operators.

Special cases are determined by the head of the expression. Then
they are processed by specific routines, which are poke from
the `special_cases` dict.
"""

# FIXME:
# Assigment is determined by the LHS.
# Is there a larger patterns or natural grouping that we are missing?
# For example it might be that
# there are some attributes or other properties of of the
# LHS of a builtin that we should be targetting.
# Below, we key on a string, but Symbol is more correct.
#

special_cases = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this dictionary could be moved also to marhics.core.assignment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Will do next.

"System`$Context": process_assign_context,
"System`$ContextPath": process_assign_context_path,
"System`$RandomState": process_assign_random_state,
"System`Attributes": process_assign_attributes,
"System`Default": process_assign_default,
"System`DefaultValues": process_assign_definition_values,
"System`DownValues": process_assign_definition_values,
"System`Format": process_assign_format,
"System`List": process_assign_list,
"System`MakeBoxes": process_assign_makeboxes,
"System`MessageName": process_assign_messagename,
"System`Messages": process_assign_definition_values,
"System`N": process_assign_n,
"System`NValues": process_assign_definition_values,
"System`NumericQ": process_assign_numericq,
"System`Options": process_assign_options,
"System`OwnValues": process_assign_definition_values,
"System`Part": process_assign_part,
"System`SubValues": process_assign_definition_values,
"System`UpValues": process_assign_definition_values,
}

def assign(self, lhs, rhs, evaluation, tags=None, upset=False):
lhs, lookup_name = normalize_lhs(lhs, evaluation)
try:
# Deal with direct assignation to properties of
# the definition object
func = self.special_cases.get(lookup_name, None)
if func:
return func(self, lhs, rhs, evaluation, tags, upset)

return assign_store_rules_by_tag(self, lhs, rhs, evaluation, tags, upset)
except AssignmentException:

return False


class Set(BinaryOperator, _SetOperator):
"""
<dl>
Expand Down
3 changes: 1 addition & 2 deletions mathics/builtin/assignments/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@
SymbolUpValues,
)

from mathics.core.assignment import is_protected
from mathics.core.atoms import String

from mathics.builtin.assignments.internals import is_protected


class Clear(Builtin):
"""
Expand Down
3 changes: 1 addition & 2 deletions mathics/builtin/assignments/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

from mathics.builtin.base import Builtin

from mathics.builtin.assignments.internals import get_symbol_values

from mathics.core.assignment import get_symbol_values
from mathics.core.attributes import A_HOLD_ALL, A_PROTECTED


Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/assignments/upvalues.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-


from mathics.builtin.assignments.internals import _SetOperator
from mathics.builtin.assignments.assignment import _SetOperator
from mathics.builtin.base import BinaryOperator
from mathics.core.attributes import (
A_HOLD_ALL,
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/atomic/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import re

from mathics.builtin.assignments.internals import get_symbol_values
from mathics.builtin.base import (
Builtin,
PrefixOperator,
Expand All @@ -16,6 +15,7 @@

from mathics.builtin.atomic.strings import to_regex

from mathics.core.assignment import get_symbol_values
from mathics.core.atoms import String

from mathics.core.attributes import (
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


from mathics.builtin.base import Predefined, Builtin
from mathics.builtin.assignments.internals import get_symbol_list
from mathics.core.assignment import get_symbol_list
from mathics.core.atoms import String
from mathics.core.attributes import (
attributes_bitset_to_list,
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"""


from mathics.builtin.assignments.internals import get_symbol_list
from mathics.core.attributes import (
A_HOLD_ALL,
A_PROTECTED,
attribute_string_to_number,
)
from mathics.builtin.base import Builtin, Predefined
from mathics.core.assignment import get_symbol_list
from mathics.core.atoms import (
String,
Integer,
Expand Down
Loading