Skip to content

Commit

Permalink
assign_store_rules_by_tag -> eval_assign_store_rules_by_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Jan 6, 2025
1 parent 34966d7 commit 51708b6
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions mathics/eval/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,6 @@ def __init__(self, lhs, rhs) -> None:
self.rhs = rhs


def assign_store_rules_by_tag(self, lhs, rhs, evaluation, tags, upset=False) -> bool:
"""
This is the default assignment. Stores a rule of the form lhs->rhs
as a value associated to each symbol listed in tags.
For special cases, such like conditions or patterns in the lhs,
lhs and rhs are rewritten in a normal form, where
conditions are associated to the lhs.
"""
lhs, condition = unroll_conditions(lhs)
lhs, rhs = unroll_patterns(lhs, rhs, evaluation)
defs = evaluation.definitions
ignore_protection, tags = eval_assign_other(self, lhs, rhs, evaluation, tags, upset)
# In WMA, this does not happens. However, if we remove this,
# some combinatorica tests fail.
# Also, should not be at the beginning?
lhs, rhs = process_rhs_conditions(lhs, rhs, condition, evaluation)
count = 0
rule = Rule(lhs, rhs)
position = "up" if upset else None
for tag in tags:
if rejected_because_protected(self, lhs, tag, evaluation, ignore_protection):
continue
count += 1
defs.add_rule(tag, rule, position=position)
return count > 0


def eval_assign(
self,
lhs: BaseElement,
Expand Down Expand Up @@ -116,7 +89,7 @@ def eval_assign(
if assignment_func:
return assignment_func(self, lhs, rhs, evaluation, tags, upset)

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

Expand Down Expand Up @@ -987,7 +960,7 @@ def eval_assign_n(
defs = evaluation.definitions
# If we try to set `N=4`, (issue #210) just deal with it as with a generic expression:
if lhs is SymbolN:
return assign_store_rules_by_tag(self, lhs, rhs, evaluation, tags, upset)
return eval_assign_store_rules_by_tag(self, lhs, rhs, evaluation, tags, upset)

if len(lhs.elements) not in (1, 2):
evaluation.message_args("N", len(lhs.elements), 1, 2)
Expand Down Expand Up @@ -1197,6 +1170,35 @@ def eval_assign_recursion_limit(lhs, rhs, evaluation):
return False


def eval_assign_store_rules_by_tag(
self, lhs, rhs, evaluation, tags, upset=False
) -> bool:
"""
This is the default assignment. Stores a rule of the form lhs->rhs
as a value associated to each symbol listed in tags.
For special cases, such like conditions or patterns in the lhs,
lhs and rhs are rewritten in a normal form, where
conditions are associated to the lhs.
"""
lhs, condition = unroll_conditions(lhs)
lhs, rhs = unroll_patterns(lhs, rhs, evaluation)
defs = evaluation.definitions
ignore_protection, tags = eval_assign_other(self, lhs, rhs, evaluation, tags, upset)
# In WMA, this does not happens. However, if we remove this,
# some combinatorica tests fail.
# Also, should not be at the beginning?
lhs, rhs = process_rhs_conditions(lhs, rhs, condition, evaluation)
count = 0
rule = Rule(lhs, rhs)
position = "up" if upset else None
for tag in tags:
if rejected_because_protected(self, lhs, tag, evaluation, ignore_protection):
continue
count += 1
defs.add_rule(tag, rule, position=position)
return count > 0


def find_tag_and_check(
lhs: BaseElement, tags: Optional[List[str]], evaluation: Evaluation
) -> str:
Expand Down

0 comments on commit 51708b6

Please sign in to comment.