Skip to content

Commit 8e42c96

Browse files
committed
not ready
1 parent 2d9b1a9 commit 8e42c96

File tree

11 files changed

+991
-31
lines changed

11 files changed

+991
-31
lines changed

mathics/builtin/arithfns/basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class Divide(BinaryOperator):
191191
}
192192

193193
formats = {
194-
(("InputForm", "OutputForm"), "Divide[x_, y_]"): (
194+
(("InputForm",), "Divide[x_, y_]"): (
195195
'Infix[{HoldForm[x], HoldForm[y]}, "/", 400, Left]'
196196
),
197197
}
@@ -573,7 +573,7 @@ class Power(BinaryOperator, _MPMathFunction):
573573
Expression(SymbolPattern, Symbol("x"), Expression(SymbolBlank)),
574574
RationalOneHalf,
575575
): "HoldForm[Sqrt[x]]",
576-
(("InputForm", "OutputForm"), "x_ ^ y_"): (
576+
(("InputForm",), "x_ ^ y_"): (
577577
'Infix[{HoldForm[x], HoldForm[y]}, "^", 590, Right]'
578578
),
579579
("", "x_ ^ y_"): (

mathics/builtin/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import sympy
1111

12-
from mathics.core.atoms import Integer, MachineReal, PrecisionReal, String
12+
from mathics.core.atoms import Integer, MachineReal, PrecisionReal, Real, String
1313
from mathics.core.attributes import A_NO_ATTRIBUTES, A_PROTECTED
1414
from mathics.core.convert.expression import to_expression, to_numeric_sympy_args
1515
from mathics.core.convert.op import ascii_operator_to_symbol

mathics/builtin/box/layout.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,33 @@ def apply_display(boxexpr, evaluation):
192192
return boxexpr.elements[0]
193193

194194

195+
class PaneBox(BoxExpression):
196+
"""
197+
<url>
198+
:WMA link:
199+
https://reference.wolfram.com/language/ref/InterpretationBox.html</url>
200+
201+
<dl>
202+
<dt>'PaneBox[expr]'
203+
<dd> is a low-level box construct, used in OutputForm.
204+
</dl>
205+
206+
"""
207+
208+
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
209+
summary_text = "box associated to panel"
210+
211+
def apply_display(boxexpr, evaluation, expression):
212+
"""ToExpression[boxexpr_PaneBox, form_]"""
213+
return Expression(expression.head, boxexpr.elements[0], form).evaluate(
214+
evaluation
215+
)
216+
217+
def apply_display(boxexpr, evaluation):
218+
"""DisplayForm[boxexpr_PaneBox]"""
219+
return boxexpr.elements[0]
220+
221+
195222
class RowBox(BoxExpression):
196223
"""
197224
<url>

mathics/builtin/forms/output.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
from typing import Optional
1515

1616
from mathics.builtin.base import Builtin
17-
from mathics.builtin.box.layout import GridBox, RowBox, to_boxes
17+
from mathics.builtin.box.layout import (
18+
GridBox,
19+
InterpretationBox,
20+
PaneBox,
21+
RowBox,
22+
to_boxes,
23+
)
1824
from mathics.builtin.comparison import expr_min
1925
from mathics.builtin.forms.base import FormBaseClass
2026
from mathics.builtin.makeboxes import MakeBoxes, number_form
@@ -27,6 +33,7 @@
2733
String,
2834
StringFromPython,
2935
)
36+
from mathics.core.convert.prettyprint import expression_to_2d_text
3037
from mathics.core.expression import BoxError, Expression
3138
from mathics.core.list import ListExpression
3239
from mathics.core.number import convert_base, dps, machine_precision, reconstruct_digits
@@ -751,6 +758,14 @@ class OutputForm(FormBaseClass):
751758

752759
summary_text = "plain-text output format"
753760

761+
def apply_makeboxes(self, expr, form, evaluation):
762+
"""MakeBoxes[OutputForm[expr_], form_]"""
763+
text2d = expression_to_2d_text(expr, evaluation, form).text
764+
elem1 = PaneBox(String(text2d))
765+
elem2 = Expression(SymbolOutputForm, expr)
766+
result = InterpretationBox(elem1, elem2)
767+
return result
768+
754769

755770
class PythonForm(FormBaseClass):
756771
"""

mathics/builtin/makeboxes.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from mathics.core.number import dps
2121
from mathics.core.symbols import Atom, Symbol
2222
from mathics.core.systemsymbols import SymbolInputForm, SymbolOutputForm, SymbolRowBox
23-
from mathics.eval.makeboxes import _boxed_string, format_element
23+
from mathics.eval.makeboxes import _boxed_string, compare_precedence, format_element
2424

2525

2626
def int_to_s_exp(expr, n):
@@ -36,26 +36,12 @@ def int_to_s_exp(expr, n):
3636

3737

3838
def parenthesize(precedence, element, element_boxes, when_equal):
39-
from mathics.builtin import builtins_precedence
40-
41-
while element.has_form("HoldForm", 1):
42-
element = element.elements[0]
43-
44-
if element.has_form(("Infix", "Prefix", "Postfix"), 3, None):
45-
element_prec = element.elements[2].value
46-
elif element.has_form("PrecedenceForm", 2):
47-
element_prec = element.elements[1].value
48-
# For negative values, ensure that the element_precedence is at least the precedence. (Fixes #332)
49-
elif isinstance(element, (Integer, Real)) and element.value < 0:
50-
element_prec = precedence
51-
else:
52-
element_prec = builtins_precedence.get(element.get_head_name())
53-
if precedence is not None and element_prec is not None:
54-
if precedence > element_prec or (precedence == element_prec and when_equal):
55-
return Expression(
56-
SymbolRowBox,
57-
ListExpression(String("("), element_boxes, String(")")),
58-
)
39+
cmp = compare_precedence(element, precedence)
40+
if cmp is not None and (cmp == -1 or cmp == 0 and when_equal):
41+
return Expression(
42+
SymbolRowBox,
43+
ListExpression(String("("), element_boxes, String(")")),
44+
)
5945
return element_boxes
6046

6147

@@ -359,9 +345,9 @@ class MakeBoxes(Builtin):
359345
'MakeBoxes[Infix[head[elements], StringForm["~`1`~", head]], f]'
360346
),
361347
"MakeBoxes[expr_]": "MakeBoxes[expr, StandardForm]",
362-
"MakeBoxes[(form:StandardForm|TraditionalForm|OutputForm|TeXForm|"
348+
"MakeBoxes[(form:StandardForm|TraditionalForm|TeXForm|"
363349
"MathMLForm)[expr_], StandardForm|TraditionalForm]": ("MakeBoxes[expr, form]"),
364-
"MakeBoxes[(form:StandardForm|OutputForm|MathMLForm|TeXForm)[expr_], OutputForm]": "MakeBoxes[expr, form]",
350+
"MakeBoxes[(form:StandardForm|MathMLForm|TeXForm)[expr_], OutputForm]": "MakeBoxes[expr, form]",
365351
"MakeBoxes[(form:FullForm|InputForm)[expr_], StandardForm|TraditionalForm|OutputForm]": "StyleBox[MakeBoxes[expr, form], ShowStringCharacters->True]",
366352
"MakeBoxes[PrecedenceForm[expr_, prec_], f_]": "MakeBoxes[expr, f]",
367353
"MakeBoxes[Style[expr_, OptionsPattern[Style]], f_]": (

0 commit comments

Comments
 (0)