From c4c1e6dcfa0a9e95203c28c80972205f230983e7 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 7 Jan 2023 09:03:32 -0500 Subject: [PATCH] Residual mege changes to get this working again --- mathics/builtin/layout.py | 2 +- mathics/builtin/makeboxes.py | 46 +++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/mathics/builtin/layout.py b/mathics/builtin/layout.py index a6588ef94..8812a3026 100644 --- a/mathics/builtin/layout.py +++ b/mathics/builtin/layout.py @@ -16,9 +16,9 @@ from mathics.builtin.makeboxes import MakeBoxes from mathics.builtin.options import options_to_rules from mathics.core.atoms import Integer, Integer1, Real, String -from mathics.core.expression import Evaluation, Expression from mathics.core.convert.op import operator_to_ascii, operator_to_unicode from mathics.core.element import BaseElement +from mathics.core.expression import Evaluation, Expression from mathics.core.list import ListExpression from mathics.core.symbols import Atom, Symbol from mathics.core.systemsymbols import ( diff --git a/mathics/builtin/makeboxes.py b/mathics/builtin/makeboxes.py index faf50a523..e58005889 100644 --- a/mathics/builtin/makeboxes.py +++ b/mathics/builtin/makeboxes.py @@ -5,18 +5,26 @@ Low level Format definitions """ +from typing import Union + import mpmath from mathics.builtin.base import Builtin, Predefined from mathics.builtin.box.layout import RowBox, to_boxes -from mathics.core.atoms import Integer, Real, String +from mathics.core.atoms import Integer, Integer1, Real, String from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_READ_PROTECTED -from mathics.core.element import BoxElementMixin +from mathics.core.convert.op import operator_to_ascii, operator_to_unicode +from mathics.core.element import BaseElement, BoxElementMixin from mathics.core.expression import Expression from mathics.core.list import ListExpression from mathics.core.number import dps -from mathics.core.symbols import Atom -from mathics.core.systemsymbols import SymbolRowBox +from mathics.core.symbols import Atom, Symbol +from mathics.core.systemsymbols import ( + SymbolInputForm, + SymbolNone, + SymbolOutputForm, + SymbolRowBox, +) from mathics.eval.makeboxes import _boxed_string, format_element, parenthesize @@ -32,6 +40,32 @@ def int_to_s_exp(expr, n): return s, exp, nonnegative +# FIXME: op should be a string, so remove the Union. +def make_boxes_infix( + elements, op: Union[String, list], precedence: int, grouping, form: Symbol +): + result = [] + for index, element in enumerate(elements): + if index > 0: + if isinstance(op, list): + result.append(op[index - 1]) + else: + result.append(op) + parenthesized = False + if grouping == "System`NonAssociative": + parenthesized = True + elif grouping == "System`Left" and index > 0: + parenthesized = True + elif grouping == "System`Right" and index == 0: + parenthesized = True + + element_boxes = MakeBoxes(element, form) + element = parenthesize(precedence, element, element_boxes, parenthesized) + + result.append(element) + return Expression(SymbolRowBox, ListExpression(*result)) + + def real_to_s_exp(expr, n): if expr.is_zero: s = "0" @@ -428,7 +462,7 @@ def eval_infix( evaluation.message("Infix", "intm", expr) return self.eval_general(expr, form, evaluation) - grouping = grouping.get_name() + grouping = grouping ## FIXME: this should go into a some formatter. def format_operator(operator) -> Union[String, BaseElement]: @@ -458,7 +492,7 @@ def format_operator(operator) -> Union[String, BaseElement]: return op return operator - precedence = prec.value if hasattr(prec, "value") else 0 + precedence = precedence.value if hasattr(precedence, "value") else 0 grouping = grouping.get_name() if isinstance(expr, Atom):