Skip to content

Commit 73b7e81

Browse files
committed
outputform and fullform following the WMA formatting steps
1 parent c5a87df commit 73b7e81

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

mathics/builtin/forms/output.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@
6464
SymbolSubscriptBox,
6565
SymbolSuperscriptBox,
6666
)
67-
from mathics.eval.makeboxes import StringLParen, StringRParen, format_element
67+
from mathics.eval.makeboxes import (
68+
StringLParen,
69+
StringRParen,
70+
format_element,
71+
makeboxes_outputform,
72+
)
6873
from mathics.eval.testing_expressions import expr_min
6974
from mathics.format.outputform import expression_to_outputform_text
7075

@@ -574,12 +579,7 @@ class OutputForm(FormBaseClass):
574579

575580
def eval_makeboxes(self, expr, form, evaluation):
576581
"""MakeBoxes[OutputForm[expr_], form_]"""
577-
print(" eval Makeboxes outputform")
578-
text_outputform = str(expression_to_outputform_text(expr, evaluation, form))
579-
elem1 = PaneBox(String(text_outputform))
580-
elem2 = Expression(SymbolOutputForm, expr)
581-
result = InterpretationBox(elem1, elem2)
582-
return result
582+
return makeboxes_outputform(expr, evaluation, form)
583583

584584

585585
class PythonForm(FormBaseClass):

mathics/core/atoms.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,10 +1015,13 @@ def __str__(self) -> str:
10151015
return '"%s"' % self.value
10161016

10171017
def atom_to_boxes(self, f, evaluation):
1018+
return self.make_boxes(f.get_name())
1019+
1020+
def make_boxes(self, f):
10181021
from mathics.eval.makeboxes import _boxed_string
10191022

10201023
inner = str(self.value)
1021-
if f in SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM:
1024+
if f in ("System`InputForm", "System`FullForm"):
10221025
inner = '"' + inner.replace("\\", "\\\\") + '"'
10231026
return _boxed_string(inner, **{"System`ShowStringCharacters": SymbolTrue})
10241027
return String('"' + inner + '"')

mathics/eval/makeboxes.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,50 @@ def int_to_string_shorter_repr(value: int, form: Symbol, max_digits=640):
179179
return String(value_str)
180180

181181

182-
def eval_fullform_makeboxes(
183-
self, expr, evaluation: Evaluation, form=SymbolStandardForm
184-
) -> Optional[BaseElement]:
182+
def eval_fullform_makeboxes(expr, evaluation) -> Optional[BaseElement]:
185183
"""
186184
This function takes the definitions provided by the evaluation
187185
object, and produces a boxed form for expr.
188186
189187
Basically: MakeBoxes[expr // FullForm]
190188
"""
189+
from mathics.builtin.box.layout import RowBox
190+
191+
comma = String(", ")
191192
# This is going to be reimplemented.
192-
expr = Expression(SymbolFullForm, expr)
193-
return Expression(SymbolMakeBoxes, expr, form).evaluate(evaluation)
193+
if isinstance(expr, Symbol):
194+
return String(evaluation.definitions.shorten_name(expr.name))
195+
if isinstance(expr, (Integer, Real, String)):
196+
return expr.make_boxes("System`FullForm")
197+
if isinstance(expr, Rational):
198+
return eval_fullform_makeboxes(
199+
Expression(SymbolRational, expr.numerator(), expr.denominator()), evaluation
200+
)
201+
if isinstance(expr, Complex):
202+
return eval_fullform_makeboxes(
203+
Expression(SymbolComplex, expr.real, expr.imag), evaluation
204+
)
205+
if isinstance(expr, Atom):
206+
return String(str(expr))
207+
if isinstance(expr, Expression):
208+
head = expr.get_head()
209+
if head is SymbolList:
210+
start_str, end_str, comma = String("{"), String("}"), String(",")
211+
elems_list = [start_str]
212+
else:
213+
start_str, end_str, comma = String("["), String("]"), String(", ")
214+
elems_list = [eval_fullform_makeboxes(head, evaluation), start_str]
215+
elements = expr.elements
216+
if elements:
217+
first_elem, *rest_elems = elements
218+
elems_list.append(eval_fullform_makeboxes(first_elem, evaluation))
219+
for elem in rest_elems:
220+
elems_list.append(comma)
221+
elems_list.append(eval_fullform_makeboxes(elem, evaluation))
222+
elems_list.append(end_str)
223+
return RowBox(*elems_list)
224+
if hasattr(expr, "to_expression"):
225+
return eval_fullform_makeboxes(expr.to_expression(), evaluation)
194226

195227

196228
def eval_makeboxes(
@@ -206,7 +238,7 @@ def eval_makeboxes(
206238
return Expression(SymbolMakeBoxes, expr, form).evaluate(evaluation)
207239

208240

209-
def make_output_form(expr, evaluation, form):
241+
def makeboxes_outputform(expr, evaluation, form):
210242
"""
211243
Build a 2D text representation of the expression.
212244
"""
@@ -225,11 +257,23 @@ def format_element(
225257
"""
226258
Applies formats associated to the expression, and then calls Makeboxes
227259
"""
260+
# Strip repeated form
228261
while element.get_head() is form:
229262
element = element.elements[0]
230263

264+
if element.has_form("FullForm", 1):
265+
return eval_fullform_makeboxes(element.elements[0], evaluation)
266+
267+
# In order to work like in WMA, `format_element`
268+
# should evaluate `MakeBoxes[element//form, StandardForm]`
269+
# Then, MakeBoxes[expr_, StandardForm], for any expr,
270+
# should apply Format[...] rules, and then
271+
# MakeBoxes[...] rules. These rules should be stored
272+
# as FormatValues[...]
273+
# As a first step in that direction, let's mimic this behaviour
274+
# just for the case of OutputForm:
231275
if element.has_form("OutputForm", 1):
232-
return make_output_form(element.elements[0], evaluation, form)
276+
return makeboxes_output_form(element.elements[0], evaluation, form)
233277

234278
expr = do_format(element, evaluation, form)
235279
if expr is None:

0 commit comments

Comments
 (0)