Skip to content

Commit 1db0228

Browse files
committed
fix Infix grouping
1 parent 4241f5f commit 1db0228

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

mathics/builtin/makeboxes.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@
1818
from mathics.core.list import ListExpression
1919
from mathics.core.number import dps
2020
from mathics.core.symbols import Atom, Symbol
21-
from mathics.core.systemsymbols import SymbolInputForm, SymbolOutputForm, SymbolRowBox
21+
from mathics.core.systemsymbols import (
22+
SymbolInputForm,
23+
SymbolLeft,
24+
SymbolNone,
25+
SymbolOutputForm,
26+
SymbolRight,
27+
SymbolRowBox,
28+
)
2229
from mathics.eval.makeboxes import (
2330
NEVER_ADD_PARENTHESIS,
2431
_boxed_string,
2532
format_element,
2633
parenthesize,
2734
)
2835

36+
SymbolNonAssociative = Symbol("System`NonAssociative")
37+
2938

3039
def int_to_s_exp(expr, n):
3140
n = expr.get_int_value()
@@ -39,18 +48,26 @@ def int_to_s_exp(expr, n):
3948
return s, exp, nonnegative
4049

4150

42-
def make_boxes_infix(elements, op: String, precedence: int, grouping, form: Symbol):
51+
def make_boxes_infix(elements, op: String, precedence: int, form: Symbol):
4352
result = []
53+
last_indx = len(elements) - 1
4454
for index, element in enumerate(elements):
45-
if index > 0:
46-
result.append(op)
55+
grouping = SymbolNone
56+
if element.has_form("Infix", 4):
57+
grouping = element.elements[-1]
58+
59+
# if grouping not in (SymbolNone, SymbolLeft,SymbolRight,SymbolNonAssociative):
60+
# send message
61+
4762
parenthesized = False
48-
if grouping == "System`NonAssociative":
49-
parenthesized = True
50-
elif grouping == "System`Left" and index > 0:
51-
parenthesized = True
52-
elif grouping == "System`Right" and index == 0:
53-
parenthesized = True
63+
if index == 0:
64+
parenthesized = grouping in (SymbolRight, SymbolNonAssociative)
65+
elif index == last_indx:
66+
result.append(op)
67+
parenthesized = grouping in (SymbolLeft, SymbolNonAssociative)
68+
else:
69+
result.append(op)
70+
parenthesized = grouping is (SymbolLeft, SymbolRight, SymbolNonAssociative)
5471

5572
element_boxes = MakeBoxes(element, form)
5673
element = parenthesize(precedence, element, element_boxes, parenthesized)
@@ -462,7 +479,6 @@ def format_operator(operator) -> Union[String, BaseElement]:
462479
py_precedence = (
463480
precedence.value if hasattr(precedence, "value") else NEVER_ADD_PARENTHESIS
464481
)
465-
grouping = grouping.get_name()
466482

467483
if isinstance(expr, Atom):
468484
evaluation.message("Infix", "normal", Integer1)
@@ -484,7 +500,7 @@ def format_operator(operator) -> Union[String, BaseElement]:
484500
String(operator_to_unicode.get(op_str, op_str))
485501
)
486502

487-
return make_boxes_infix(elements, operator, py_precedence, grouping, form)
503+
return make_boxes_infix(elements, operator, py_precedence, form)
488504

489505
elif len(elements) == 1:
490506
return MakeBoxes(elements[0], form)

0 commit comments

Comments
 (0)