diff --git a/mathics/builtin/__init__.py b/mathics/builtin/__init__.py index 8a601e25e..25be2adff 100755 --- a/mathics/builtin/__init__.py +++ b/mathics/builtin/__init__.py @@ -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. @@ -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)) @@ -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.. diff --git a/mathics/eval/makeboxes.py b/mathics/eval/makeboxes.py index ae12aa8f8..9ad27229d 100644 --- a/mathics/eval/makeboxes.py +++ b/mathics/eval/makeboxes.py @@ -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 @@ -42,6 +42,8 @@ SymbolStandardForm, ) +builtins_precedence: Dict[Symbol, int] = {} + element_formatters = {} @@ -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] @@ -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(