-
-
Notifications
You must be signed in to change notification settings - Fork 60
MakeBoxes OutputForm / FullForm #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1081,10 +1081,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"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason, this method receives strings instead symbols as input. We can change this, too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I would not be surprised if there is a conversion or access function to go from an Expression symbol into a string. If so, this was one of the many ways that the original code was hideously slow.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW I noticed recently that Expression.replace_vars is another place where strings are used instead of Symbol. |
||
inner = '"' + inner.replace("\\", "\\\\") + '"' | ||
return _boxed_string(inner, **{"System`ShowStringCharacters": SymbolTrue}) | ||
return String('"' + inner + '"') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If 2D is synonymous with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this more. For high-level form handling and boxing, which largely involve Mathics3 (simple and compound) expressions, these should be done in separate directories, e.g., |
||
""" | ||
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 | ||
|
@@ -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): | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the class called PaneBox when the WMA link goes to InterpretationBox?
I can't find PaneBox mentioned in WMA docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A PaneBox is a block of multiline text, which by default is "centered".
It can be produced by applying MakeBoxes to a
Pane
expression:Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok - thanks for the information. Given this, it seems this is more like a Pane than it is an InterpretationBox. I see that
Information[PaneBox]
gives information.Are there any other Boxes in this category? (Like ParentBox, it exists but is not documented). I can't find any, but I feel we've encountered this kind of situation where there is something that exists but is not documented. If so, how did we handle things there?
But as for changing the link say from InterpretationBox to Pane (if that turns out to be the better thing to do), that is not urgent right now.
This PR is large and I think this PR should come after we work out some simple basics of MakeBoxes.
Also, the PR title is misleading because a lot of this is about character-based formatting on top of stuff related to MakeBoxes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably this can be reformulated in small pieces. One of them is #1316