Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 40 additions & 1 deletion mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
"""


from mathics.builtin.assignments.internals import _SetOperator
from mathics.builtin.base import BinaryOperator, Builtin
from mathics.core.assignment import (
ASSIGNMENT_FUNCTION_MAP,
AssignmentException,
assign_store_rules_by_tag,
normalize_lhs,
)

from mathics.core.attributes import (
A_HOLD_ALL,
A_HOLD_FIRST,
Expand All @@ -18,6 +24,39 @@
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 ``ASSIGNMENT_FUNCTION_MAP`` dict.
"""

# FIXME:
# Assigment is determined by the LHS.
# Are there a larger patterns or natural groupings that we are missing?
# For example, it might be that it
# we can key off of some attributes or other properties of the
# LHS of a builtin, instead of listing all of the builtins in that class
# (which may miss some).
# Below, we key on a string, but Symbol is more correct.

def assign(self, lhs, rhs, evaluation, tags=None, upset=False):
lhs, lookup_name = normalize_lhs(lhs, evaluation)
try:
# Using a builtin name, find which assignment procedure to perform,
# and then call that function.
assignment_func = ASSIGNMENT_FUNCTION_MAP.get(lookup_name, None)
if assignment_func:
return assignment_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
9 changes: 8 additions & 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 Expand Up @@ -745,6 +745,13 @@ def eval(self, symbols, attributes, evaluation):
evaluation.message("Attributes", "attnf", Symbol(value))
return SymbolNull

def eval_arg_error(self, args, evaluation):
"SetAttributes[args___]"
# We should only come here when we don't have 2 args, because
# eval() should be called otherwise.
nargs = len(args.elements) if isinstance(args, Expression) else 1
evaluation.message("SetAttributes", "argrx", "SetAttributes", nargs, 2)


class Unprotect(Builtin):
"""
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,15 @@ def get_head_name(self):
def get_lookup_name(self):
return self.get_name()

def get_sort_key(self) -> tuple:
def get_sort_key(self, pattern_sort=False) -> tuple:
return self.to_expression().get_sort_key()

def get_string_value(self):
return "-@" + self.get_head_name() + "@-"

def sameQ(self, expr) -> bool:
"""Mathics SameQ"""
return expr.sameQ(self)
return self.to_expression().sameQ(expr)

def do_format(self, evaluation, format):
return self
Expand Down
Loading