1212from mathics .core .element import BaseElement , BoxElementMixin
1313from mathics .core .evaluation import Evaluation
1414from mathics .core .expression import Expression
15- from mathics .core .symbols import Atom , Symbol , SymbolFullForm , SymbolMakeBoxes
16- from mathics .core .systemsymbols import SymbolStandardForm
15+ from mathics .core .symbols import (
16+ Atom ,
17+ Symbol ,
18+ SymbolFullForm ,
19+ SymbolList ,
20+ SymbolMakeBoxes ,
21+ )
22+ from mathics .core .systemsymbols import ( # SymbolRule, SymbolRuleDelayed,
23+ SymbolStandardForm ,
24+ )
1725from mathics .eval .makeboxes .formatvalues import do_format
1826from mathics .eval .makeboxes .precedence import parenthesize
1927
@@ -38,7 +46,7 @@ def to_boxes(x, evaluation: Evaluation, options={}) -> BoxElementMixin:
3846 return x_boxed
3947 if isinstance (x_boxed , Atom ):
4048 return to_boxes (x_boxed , evaluation , options )
41- return eval_makeboxes ( Expression ( SymbolFullForm , x ) , evaluation )
49+ return eval_makeboxes_fullform ( x , evaluation )
4250
4351
4452# this temporarily replaces the _BoxedString class
@@ -126,23 +134,53 @@ def int_to_string_shorter_repr(value: int, form: Symbol, max_digits=640):
126134 return String (value_str )
127135
128136
129- def eval_fullform_makeboxes (
130- self , expr , evaluation : Evaluation , form = SymbolStandardForm
131- ) -> Optional [BaseElement ]:
132- """
133- This function takes the definitions provided by the evaluation
134- object, and produces a boxed form for expr.
137+ # TODO: evaluation is needed because `atom_to_boxes` uses it. Can we remove this
138+ # argument?
139+ def eval_makeboxes_fullform (
140+ expr : BaseElement , evaluation : Evaluation
141+ ) -> BoxElementMixin :
142+ """Same as MakeBoxes[FullForm[expr_], f_]"""
143+ from mathics .builtin .box .layout import RowBox
135144
136- Basically: MakeBoxes[expr // FullForm]
137- """
138- # This is going to be reimplemented.
139- expr = Expression (SymbolFullForm , expr )
140- return Expression (SymbolMakeBoxes , expr , form ).evaluate (evaluation )
145+ if isinstance (expr , BoxElementMixin ):
146+ expr = expr .to_expression ()
147+ if isinstance (expr , Atom ):
148+ return expr .atom_to_boxes (SymbolFullForm , evaluation )
149+ head , elements = expr .head , expr .elements
150+ boxed_elements = tuple (
151+ (eval_makeboxes_fullform (element , evaluation ) for element in elements )
152+ )
153+ # In some places it would be less verbose to use special outputs for
154+ # `List`, `Rule` and `RuleDelayed`. WMA does not that, but we do it for
155+ # `List`.
156+ #
157+ # if head is SymbolRule and len(elements) == 2:
158+ # return RowBox(boxed_elements[0], String("->"), boxed_elements[1])
159+ # if head is SymbolRuleDelayed and len(elements) == 2:
160+ # return RowBox(boxed_elements[0], String(":>"), boxed_elements[1])
161+ if head is SymbolList :
162+ left , right , sep = (String (ch ) for ch in ("{" , "}" , "," ))
163+ result_elements = [left ]
164+ else :
165+ left , right , sep = (String (ch ) for ch in ("[" , "]" , ", " ))
166+ result_elements = [eval_makeboxes_fullform (head , evaluation ), left ]
167+
168+ if len (boxed_elements ) > 1 :
169+ arguments = []
170+ for b_elem in boxed_elements :
171+ if len (arguments ) > 0 :
172+ arguments .append (sep )
173+ arguments .append (b_elem )
174+ result_elements .append (RowBox (* arguments ))
175+ elif len (boxed_elements ) == 1 :
176+ result_elements .append (boxed_elements [0 ])
177+ result_elements .append (right )
178+ return RowBox (* result_elements )
141179
142180
143181def eval_generic_makeboxes (self , expr , f , evaluation ):
144182 """MakeBoxes[expr_,
145- f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm ]"""
183+ f:TraditionalForm|StandardForm|OutputForm|InputForm]"""
146184 from mathics .builtin .box .layout import RowBox
147185
148186 if isinstance (expr , BoxElementMixin ):
@@ -174,7 +212,6 @@ def eval_generic_makeboxes(self, expr, f, evaluation):
174212 if f_name in (
175213 "System`InputForm" ,
176214 "System`OutputForm" ,
177- "System`FullForm" ,
178215 ):
179216 sep = ", "
180217 else :
@@ -210,6 +247,8 @@ def eval_makeboxes(
210247 Basically: MakeBoxes[expr // form]
211248 """
212249 # This is going to be reimplemented.
250+ if form is SymbolFullForm :
251+ return eval_makeboxes_fullform (expr , evaluation )
213252 return Expression (SymbolMakeBoxes , expr , form ).evaluate (evaluation )
214253
215254
@@ -220,14 +259,13 @@ def format_element(
220259 Applies formats associated to the expression, and then calls Makeboxes
221260 """
222261 evaluation .is_boxing = True
223- expr = do_format (element , evaluation , form )
224- if expr is None :
262+ formatted_expr = do_format (element , evaluation , form )
263+ if formatted_expr is None :
225264 return None
226- result = Expression (SymbolMakeBoxes , expr , form )
227- result_box = result .evaluate (evaluation )
265+ result_box = eval_makeboxes (formatted_expr , evaluation , form )
228266 if isinstance (result_box , String ):
229267 return result_box
230268 if isinstance (result_box , BoxElementMixin ):
231269 return result_box
232270 else :
233- return format_element (element , evaluation , SymbolFullForm , ** kwargs )
271+ return eval_makeboxes_fullform (element , evaluation )
0 commit comments