Skip to content

Commit 03dc038

Browse files
committed
partial
1 parent 14889d7 commit 03dc038

File tree

7 files changed

+66
-142
lines changed

7 files changed

+66
-142
lines changed

mathics/builtin/forms/output.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,14 @@ class OutputForm(FormBaseClass):
571571
formats = {"OutputForm[s_String]": "s"}
572572
summary_text = "plain-text output format"
573573

574-
# def apply_makeboxes(self, expr, form, evaluation):
575-
# """MakeBoxes[OutputForm[expr_], form_]"""
576-
# text2d = expression_to_2d_text(expr, evaluation, form).text
577-
# elem1 = PaneBox(String(text2d))
578-
# elem2 = Expression(SymbolOutputForm, expr)
579-
# result = InterpretationBox(elem1, elem2)
580-
# return result
574+
def eval_makeboxes(self, expr, form, evaluation):
575+
"""MakeBoxes[OutputForm[expr_], form_]"""
576+
print(" eval Makeboxes outputform")
577+
text2d = expression_to_2d_text(expr, evaluation, form).text
578+
elem1 = PaneBox(String(text2d))
579+
elem2 = Expression(SymbolOutputForm, expr)
580+
result = InterpretationBox(elem1, elem2)
581+
return result
581582

582583

583584
class PythonForm(FormBaseClass):

mathics/builtin/list/associations.py

Lines changed: 30 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,15 @@
1010

1111

1212
from mathics.builtin.box.layout import RowBox
13-
from mathics.builtin.layout import Row
14-
from mathics.core.atoms import Integer, String
13+
from mathics.core.atoms import Integer
1514
from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_PROTECTED
1615
from mathics.core.builtin import Builtin, Test
1716
from mathics.core.convert.expression import to_mathics_list
1817
from mathics.core.evaluation import Evaluation
1918
from mathics.core.expression import Expression
2019
from mathics.core.symbols import Symbol, SymbolTrue
21-
from mathics.core.systemsymbols import (
22-
SymbolAssociation,
23-
SymbolHoldForm,
24-
SymbolInputForm,
25-
SymbolMakeBoxes,
26-
SymbolMathMLForm,
27-
SymbolMissing,
28-
SymbolOutputForm,
29-
SymbolStandardForm,
30-
SymbolTeXForm,
31-
SymbolTraditionalForm,
32-
)
33-
from mathics.eval.lists import list_boxes, riffle
34-
from mathics.eval.makeboxes import do_format
35-
from mathics.eval.strings import eval_ToString
36-
37-
38-
class NotAnAssociationItem(Exception):
39-
pass
40-
41-
42-
SymbolInterpretation = Symbol("System`Interpretation")
43-
44-
ASSOCIATION_DELIMITER_FORMATS = {
45-
SymbolInputForm: {"start": String("<|"), "sep": String(", "), "end": String("|>")},
46-
SymbolOutputForm: {"start": String("<|"), "sep": String(","), "end": String("|>")},
47-
SymbolStandardForm: {
48-
"start": String("<|"),
49-
"sep": String(","),
50-
"end": String("|>"),
51-
},
52-
SymbolTraditionalForm: {
53-
"start": String("<|"),
54-
"sep": String(","),
55-
"end": String("|>"),
56-
},
57-
SymbolTeXForm: {"start": String("<|"), "sep": String(", "), "end": String("|>")},
58-
SymbolMathMLForm: {"start": String("<|"), "sep": String(","), "end": String("|>")},
59-
}
60-
61-
62-
def format_association(rules: tuple, evaluation: Evaluation, form: Symbol):
63-
"""Association[rules___]"""
64-
delimiters = ASSOCIATION_DELIMITER_FORMATS[form]
65-
66-
def yield_rules(rule_tuple):
67-
for rule in rule_tuple:
68-
if rule.has_form(("Rule", "RuleDelayed"), 2):
69-
yield rule
70-
elif rule.has_form(
71-
(
72-
"List",
73-
"Association",
74-
),
75-
None,
76-
):
77-
for subrule in yield_rules(rule.elements):
78-
yield subrule
79-
else:
80-
raise NotAnAssociationItem
81-
82-
try:
83-
items = riffle(
84-
[do_format(rule, evaluation, form) for rule in yield_rules(rules)],
85-
delimiters["sep"],
86-
)
87-
return Row(to_mathics_list(delimiters["start"], *items, delimiters["end"]))
88-
except NotAnAssociationItem:
89-
return None
20+
from mathics.core.systemsymbols import SymbolAssociation, SymbolMakeBoxes, SymbolMissing
21+
from mathics.eval.lists import list_boxes
9022

9123

9224
class Association(Builtin):
@@ -122,52 +54,34 @@ class Association(Builtin):
12254

12355
summary_text = "an association between keys and values"
12456

125-
def format_association_input(self, rules, evaluation: Evaluation, expression):
126-
"""InputForm: Association[rules___]"""
127-
print("format association input", rules)
128-
formatted = format_association(
129-
rules.get_sequence(), evaluation, SymbolInputForm
130-
)
131-
if formatted is None:
132-
return None
133-
print(" formatted elements:")
134-
elements = formatted.elements[0].elements
135-
for elem in elements:
136-
print(" ", elem)
137-
elems = tuple(
138-
(
139-
eval_ToString(elem, SymbolOutputForm, "unicode", evaluation).value
140-
for elem in elements
57+
def eval_makeboxes(self, rules, f, evaluation: Evaluation):
58+
"""MakeBoxes[<|rules___|>,
59+
f:StandardForm|TraditionalForm|OutputForm|InputForm]"""
60+
61+
def validate(exprs):
62+
for expr in exprs:
63+
if expr.has_form(("Rule", "RuleDelayed"), 2):
64+
pass
65+
elif expr.has_form(("List", "Association"), None):
66+
if not validate(expr.elements):
67+
return False
68+
else:
69+
return False
70+
return True
71+
72+
rules = rules.get_sequence()
73+
if self.error_idx == 0 and validate(rules) is True:
74+
expr = RowBox(*list_boxes(rules, f, evaluation, "<|", "|>"))
75+
else:
76+
self.error_idx += 1
77+
symbol = Expression(SymbolMakeBoxes, SymbolAssociation, f)
78+
expr = RowBox(
79+
symbol.evaluate(evaluation), *list_boxes(rules, f, evaluation, "[", "]")
14180
)
142-
)
143-
elems = tuple((elem[1:-1] if elem[0] == '"' else elem for elem in elems))
144-
print(" elems", elems)
145-
result_str = "".join(elems)
146-
result = Expression(SymbolOutputForm, String(result_str))
147-
print(" result->", result)
148-
return result
149-
150-
def format_association_output(self, rules, evaluation: Evaluation):
151-
"""OutputForm: Association[rules___]"""
152-
return format_association(rules.get_sequence(), evaluation, SymbolOutputForm)
153-
154-
def format_association_standard(self, rules, evaluation: Evaluation):
155-
"""StandardForm: Association[rules___]"""
156-
return format_association(rules.get_sequence(), evaluation, SymbolStandardForm)
157-
158-
def format_association_traditional(self, rules, evaluation: Evaluation):
159-
"""TraditionalForm: Association[rules___]"""
160-
return format_association(
161-
rules.get_sequence(), evaluation, SymbolTraditionalForm
162-
)
163-
164-
def format_association_tex(self, rules, evaluation: Evaluation):
165-
"""TeXForm: Association[rules___]"""
166-
return format_association(rules.get_sequence(), evaluation, SymbolTeXForm)
167-
168-
def format_association_mathml(self, rules, evaluation: Evaluation):
169-
"""MathMLForm: Association[rules___]"""
170-
return format_association(rules.get_sequence(), evaluation, SymbolMathMLForm)
81+
82+
if self.error_idx > 0:
83+
self.error_idx -= 1
84+
return expr
17185

17286
def eval(self, rules, evaluation: Evaluation):
17387
"Association[rules__]"

mathics/builtin/patterns/rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,11 @@ class Rule_(BinaryOperator):
503503
= a
504504
"""
505505

506-
name = "Rule"
507-
operator = "->"
508506
attributes = A_SEQUENCE_HOLD | A_PROTECTED
509507
grouping = "Right"
508+
name = "Rule"
510509
needs_verbatim = True
510+
operator = "->"
511511
summary_text = "a replacement rule"
512512

513513
def eval_rule(self, elems, evaluation):

mathics/core/builtin.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,28 +1151,29 @@ def __init__(self, *args, **kwargs):
11511151

11521152
# Prevent pattern matching symbols from gaining meaning here using
11531153
# Verbatim
1154-
name = f"Verbatim[{name}]"
1154+
verbatim_name = f"Verbatim[{name}]"
11551155

11561156
# For compatibility, allow grouping symbols in builtins to be
11571157
# specified without System`.
11581158
self.grouping = ensure_context(self.grouping)
11591159

11601160
if self.grouping in ("System`None", "System`NonAssociative"):
1161-
op_pattern = f"{name}[items__]"
1161+
op_pattern = f"{verbatim_name}[items__]"
11621162
replace_items = "items"
11631163
else:
1164-
op_pattern = f"{name}[x_, y_]"
1164+
op_pattern = f"{verbatim_name}[x_, y_]"
11651165
replace_items = "x, y"
11661166

11671167
operator = ascii_operator_to_symbol.get(self.operator, self.__class__.__name__)
11681168

11691169
if self.default_formats:
1170-
formats = {
1171-
op_pattern: "HoldForm[Infix[{%s}, %s, %d, %s]]"
1172-
% (replace_items, operator, self.precedence, self.grouping)
1173-
}
1174-
formats.update(self.formats)
1175-
self.formats = formats
1170+
if name not in ("Rule", "RuleDelayed"):
1171+
formats = {
1172+
op_pattern: "HoldForm[Infix[{%s}, %s, %d, %s]]"
1173+
% (replace_items, operator, self.precedence, self.grouping)
1174+
}
1175+
formats.update(self.formats)
1176+
self.formats = formats
11761177
formatted = "MakeBoxes[Infix[{%s}, %s, %d,%s], form]" % (
11771178
replace_items,
11781179
operator,

mathics/core/convert/prettyprint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def integrate_expression_to_2d_text(expr, evaluation, form, **kwargs):
181181
if len(elems) > 2 or not kwargs.get("2d", False):
182182
raise _WrongFormattedExpression
183183

184-
result = expression_to_2d_text(elems.pop(0), evaluation, form, **kwargs)
184+
integrand = elems.pop(0)
185+
result = expression_to_2d_text(integrand, evaluation, form, **kwargs)
185186
while elems:
186187
var = elems.pop(0)
187188
if var.has_form("List", 3):

mathics/eval/makeboxes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,26 @@ def eval_makeboxes(
206206
return Expression(SymbolMakeBoxes, expr, form).evaluate(evaluation)
207207

208208

209+
def make_output_form(expr, evaluation, form):
210+
""" """
211+
from mathics.builtin.box.layout import InterpretationBox, PaneBox
212+
from mathics.core.convert.prettyprint import expression_to_2d_text
213+
214+
text2d = expression_to_2d_text(expr, evaluation, form, **{"2d": True}).text
215+
elem1 = PaneBox(String(8 * " " + text2d))
216+
elem2 = Expression(SymbolOutputForm, expr)
217+
return InterpretationBox(elem1, elem2)
218+
219+
209220
def format_element(
210221
element: BaseElement, evaluation: Evaluation, form: Symbol, **kwargs
211222
) -> Optional[BaseElement]:
212223
"""
213224
Applies formats associated to the expression, and then calls Makeboxes
214225
"""
226+
if element.has_form("OutputForm", 1):
227+
return make_output_form(element.elements[0], evaluation, form)
228+
215229
expr = do_format(element, evaluation, form)
216230
if expr is None:
217231
return None

test/format/test_format.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,6 @@
774774
},
775775
}
776776

777-
# TODO: REMOVE ME ONCE THE ISSUE WITH FORMATTING ASSOCIATIONS GET SOLVED.
778-
omit = tuple(
779-
(key for key, val in all_test.items() if "Association" in val.get("msg", ""))
780-
)
781-
for key in omit:
782-
del all_test[key]
783-
784777

785778
def load_tests(key):
786779
"""

0 commit comments

Comments
 (0)