Skip to content

Commit

Permalink
Adjust builtins_precedence and doc parenthesize
Browse files Browse the repository at this point in the history
We should be using Symbols insead of string where possible.
builtins_precedence is really used in eval/makeboxes so it is defined
there.

It is just initialized in `mathics.buitins`.
  • Loading branch information
rocky committed Dec 27, 2022
1 parent 385c5fb commit 827b8b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
mathics_to_python,
)
from mathics.core.pattern import pattern_objects
from mathics.core.symbols import Symbol
from mathics.eval.makeboxes import builtins_precedence
from mathics.settings import ENABLE_FILES_MODULE
from mathics.version import __version__ # noqa used in loading to check consistency.

Expand All @@ -60,7 +62,7 @@ def add_builtins(new_builtins):
# print("XXX1", sympy_name)
sympy_to_mathics[sympy_name] = builtin
if isinstance(builtin, Operator):
builtins_precedence[name] = builtin.precedence
builtins_precedence[Symbol(name)] = builtin.precedence
if isinstance(builtin, PatternObject):
pattern_objects[name] = builtin.__class__
_builtins.update(dict(new_builtins))
Expand Down Expand Up @@ -236,8 +238,6 @@ def name_is_builtin_symbol(module, name: str) -> Optional[type]:
mathics_to_sympy = {} # here we have: name -> sympy object
sympy_to_mathics = {}

builtins_precedence = {}

new_builtins = _builtins_list

# FIXME: some magic is going on here..
Expand Down
18 changes: 14 additions & 4 deletions mathics/eval/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


import typing
from typing import Any
from typing import Any, Dict, Type

from mathics.core.atoms import Complex, Integer, Rational, Real, String, SymbolI
from mathics.core.convert.expression import to_expression_with_specialization
Expand Down Expand Up @@ -42,6 +42,8 @@
SymbolStandardForm,
)

builtins_precedence: Dict[Symbol, int] = {}

element_formatters = {}


Expand Down Expand Up @@ -290,9 +292,17 @@ def do_format_expression(
return expr


def parenthesize(precedence, element, element_boxes, when_equal):
from mathics.builtin import builtins_precedence
def parenthesize(
precedence: int, element: Type[BaseElement], element_boxes, when_equal: bool
) -> Type[Expression]:
"""
"Determines if ``element_boxes`` needs to be surrounded with parenthesis.
This is done based on ``precedence`` and the computed preceence of
``element``. The adjusted ListExpression is returned.
If when_equal is True, parentheses will be added if the two
precedence values are equal.
"""
while element.has_form("HoldForm", 1):
element = element.elements[0]

Expand All @@ -304,7 +314,7 @@ def parenthesize(precedence, element, element_boxes, when_equal):
elif isinstance(element, (Integer, Real)) and element.value < 0:
element_prec = precedence
else:
element_prec = builtins_precedence.get(element.get_head_name())
element_prec = builtins_precedence.get(element.get_head())
if precedence is not None and element_prec is not None:
if precedence > element_prec or (precedence == element_prec and when_equal):
return Expression(
Expand Down

0 comments on commit 827b8b8

Please sign in to comment.