Skip to content

Commit

Permalink
formatting OutputForm
Browse files Browse the repository at this point in the history
outputform and fullform following the WMA formatting steps
  • Loading branch information
mmatera committed Jan 21, 2025
1 parent b6eac71 commit a35983e
Show file tree
Hide file tree
Showing 9 changed files with 900 additions and 11 deletions.
27 changes: 27 additions & 0 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ def eval_display(boxexpr, evaluation):
return boxexpr.elements[0]


class PaneBox(BoxExpression):
"""
<url>
:WMA link:
https://reference.wolfram.com/language/ref/InterpretationBox.html</url>
<dl>
<dt>'PaneBox[expr]'
<dd> is a low-level box construct, used in OutputForm.
</dl>
"""

attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
summary_text = "box associated to panel"

def apply_display_form(boxexpr, form, evaluation, expression):
"""ToExpression[boxexpr_PaneBox, form_]"""
return Expression(expression.head, boxexpr.elements[0], form).evaluate(
evaluation
)

def apply_display(boxexpr, evaluation):
"""DisplayForm[boxexpr_PaneBox]"""
return boxexpr.elements[0]


class RowBox(BoxExpression):
"""
<url>
Expand Down
6 changes: 6 additions & 0 deletions mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
StringLParen,
StringRParen,
eval_baseform,
eval_makeboxes_outputform,
eval_mathmlform,
eval_tableform,
eval_texform,
Expand Down Expand Up @@ -490,8 +491,13 @@ class OutputForm(FormBaseClass):
= -Graphics-
"""

formats = {"OutputForm[s_String]": "s"}
summary_text = "plain-text output format"

def eval_makeboxes(self, expr, form, evaluation):
"""MakeBoxes[OutputForm[expr_], form_]"""
return eval_makeboxes_outputform(expr, evaluation, form)


class PythonForm(FormBaseClass):
"""
Expand Down
5 changes: 4 additions & 1 deletion mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,10 +1016,13 @@ def __str__(self) -> str:
return '"%s"' % self.value

def atom_to_boxes(self, f, evaluation):
return self.make_boxes(f.get_name())

def make_boxes(self, f):
from mathics.eval.makeboxes import _boxed_string

inner = str(self.value)
if f in SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM:
if f in ("System`InputForm", "System`FullForm"):
inner = '"' + inner.replace("\\", "\\\\") + '"'
return _boxed_string(inner, **{"System`ShowStringCharacters": SymbolTrue})
return String('"' + inner + '"')
Expand Down
9 changes: 8 additions & 1 deletion mathics/eval/makeboxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_boxed_string,
eval_generic_makeboxes,
eval_makeboxes,
eval_makeboxes_outputform,
format_element,
int_to_string_shorter_repr,
to_boxes,
Expand All @@ -18,19 +19,25 @@
eval_tableform,
eval_texform,
)
from mathics.eval.makeboxes.precedence import builtins_precedence, parenthesize
from mathics.eval.makeboxes.precedence import (
builtins_precedence,
compare_precedence,
parenthesize,
)

__all__ = [
"NumberForm_to_String",
"StringLParen",
"StringRParen",
"_boxed_string",
"builtins_precedence",
"compare_precedence",
"do_format",
"eval_baseform",
"eval_generic_makeboxes",
"eval_infix",
"eval_makeboxes",
"eval_makeboxes_outputform",
"eval_mathmlform",
"eval_postprefix",
"eval_tableform",
Expand Down
35 changes: 33 additions & 2 deletions mathics/eval/makeboxes/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.symbols import Atom, Symbol, SymbolFullForm, SymbolMakeBoxes
from mathics.core.systemsymbols import SymbolStandardForm
from mathics.core.systemsymbols import SymbolOutputForm, SymbolStandardForm
from mathics.eval.makeboxes.formatvalues import do_format
from mathics.eval.makeboxes.precedence import parenthesize

Expand Down Expand Up @@ -126,8 +126,21 @@ def int_to_string_shorter_repr(value: int, form: Symbol, max_digits=640):
return String(value_str)


def eval_makeboxes_outputform(expr, evaluation, form):
"""
Build a 2D text representation of the expression.
"""
from mathics.builtin.box.layout import InterpretationBox, PaneBox
from mathics.format.outputform import expression_to_outputform_text

text_outputform = str(expression_to_outputform_text(expr, evaluation, form))
elem1 = PaneBox(String(text_outputform))
elem2 = Expression(SymbolOutputForm, expr)
return InterpretationBox(elem1, elem2)


def eval_fullform_makeboxes(
self, expr, evaluation: Evaluation, form=SymbolStandardForm
expr, evaluation: Evaluation, form=SymbolStandardForm
) -> Optional[BaseElement]:
"""
This function takes the definitions provided by the evaluation
Expand Down Expand Up @@ -220,9 +233,27 @@ def format_element(
Applies formats associated to the expression, and then calls Makeboxes
"""
evaluation.is_boxing = True
while element.get_head() is form:
element = element.elements[0]

if element.has_form("FullForm", 1):
return eval_fullform_makeboxes(element.elements[0], evaluation)

# In order to work like in WMA, `format_element`
# should evaluate `MakeBoxes[element//form, StandardForm]`
# Then, MakeBoxes[expr_, StandardForm], for any expr,
# should apply Format[...] rules, and then
# MakeBoxes[...] rules. These rules should be stored
# as FormatValues[...]
# As a first step in that direction, let's mimic this behaviour
# just for the case of OutputForm:
if element.has_form("OutputForm", 1):
return eval_makeboxes_outputform(element.elements[0], evaluation, form)

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

result = Expression(SymbolMakeBoxes, expr, form)
result_box = result.evaluate(evaluation)
if isinstance(result_box, String):
Expand Down
12 changes: 12 additions & 0 deletions mathics/format/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from mathics.builtin.box.layout import (
FractionBox,
GridBox,
InterpretationBox,
PaneBox,
RowBox,
SqrtBox,
StyleBox,
Expand Down Expand Up @@ -133,6 +135,16 @@ def render(format, string, in_text=False):
add_conversion_fn(String, string)


def interpretation_panebox(self, **options):
return lookup_conversion_method(self.elements[0], "latex")(
self.elements[0], **options
)


add_conversion_fn(InterpretationBox, interpretation_panebox)
add_conversion_fn(PaneBox, interpretation_panebox)


def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
Expand Down
12 changes: 12 additions & 0 deletions mathics/format/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from mathics.builtin.box.layout import (
FractionBox,
GridBox,
InterpretationBox,
PaneBox,
RowBox,
SqrtBox,
StyleBox,
Expand Down Expand Up @@ -110,6 +112,16 @@ def render(format, string):
add_conversion_fn(String, string)


def interpretation_panebox(self, **options):
return lookup_conversion_method(self.elements[0], "latex")(
self.elements[0], **options
)


add_conversion_fn(InterpretationBox, interpretation_panebox)
add_conversion_fn(PaneBox, interpretation_panebox)


def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
Expand Down
Loading

0 comments on commit a35983e

Please sign in to comment.