55Low level Format definitions
66"""
77
8+ from typing import Union
9+
810import mpmath
911
1012from mathics .builtin .base import Builtin , Predefined
1113from mathics .builtin .box .layout import RowBox , to_boxes
12- from mathics .core .atoms import Integer , Real , String
14+ from mathics .core .atoms import Integer , Integer1 , Real , String
1315from mathics .core .attributes import A_HOLD_ALL_COMPLETE , A_READ_PROTECTED
14- from mathics .core .element import BoxElementMixin
16+ from mathics .core .convert .op import operator_to_ascii , operator_to_unicode
17+ from mathics .core .element import BaseElement , BoxElementMixin
1518from mathics .core .expression import Expression
1619from mathics .core .list import ListExpression
1720from mathics .core .number import dps
18- from mathics .core .symbols import Atom
19- from mathics .core .systemsymbols import SymbolRowBox
21+ from mathics .core .symbols import Atom , Symbol
22+ from mathics .core .systemsymbols import (
23+ SymbolInputForm ,
24+ SymbolNone ,
25+ SymbolOutputForm ,
26+ SymbolRowBox ,
27+ )
2028from mathics .eval .makeboxes import _boxed_string , format_element , parenthesize
2129
2230
@@ -32,6 +40,32 @@ def int_to_s_exp(expr, n):
3240 return s , exp , nonnegative
3341
3442
43+ # FIXME: op should be a string, so remove the Union.
44+ def make_boxes_infix (
45+ elements , op : Union [String , list ], precedence : int , grouping , form : Symbol
46+ ):
47+ result = []
48+ for index , element in enumerate (elements ):
49+ if index > 0 :
50+ if isinstance (op , list ):
51+ result .append (op [index - 1 ])
52+ else :
53+ result .append (op )
54+ parenthesized = False
55+ if grouping == "System`NonAssociative" :
56+ parenthesized = True
57+ elif grouping == "System`Left" and index > 0 :
58+ parenthesized = True
59+ elif grouping == "System`Right" and index == 0 :
60+ parenthesized = True
61+
62+ element_boxes = MakeBoxes (element , form )
63+ element = parenthesize (precedence , element , element_boxes , parenthesized )
64+
65+ result .append (element )
66+ return Expression (SymbolRowBox , ListExpression (* result ))
67+
68+
3569def real_to_s_exp (expr , n ):
3670 if expr .is_zero :
3771 s = "0"
@@ -428,7 +462,7 @@ def eval_infix(
428462 evaluation .message ("Infix" , "intm" , expr )
429463 return self .eval_general (expr , form , evaluation )
430464
431- grouping = grouping . get_name ()
465+ grouping = grouping
432466
433467 ## FIXME: this should go into a some formatter.
434468 def format_operator (operator ) -> Union [String , BaseElement ]:
@@ -458,7 +492,7 @@ def format_operator(operator) -> Union[String, BaseElement]:
458492 return op
459493 return operator
460494
461- precedence = prec .value if hasattr (prec , "value" ) else 0
495+ precedence = precedence .value if hasattr (precedence , "value" ) else 0
462496 grouping = grouping .get_name ()
463497
464498 if isinstance (expr , Atom ):
0 commit comments