|
10 | 10 |
|
11 | 11 |
|
12 | 12 | 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 |
15 | 14 | from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_PROTECTED |
16 | 15 | from mathics.core.builtin import Builtin, Test |
17 | 16 | from mathics.core.convert.expression import to_mathics_list |
18 | 17 | from mathics.core.evaluation import Evaluation |
19 | 18 | from mathics.core.expression import Expression |
20 | 19 | 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 |
90 | 22 |
|
91 | 23 |
|
92 | 24 | class Association(Builtin): |
@@ -122,52 +54,34 @@ class Association(Builtin): |
122 | 54 |
|
123 | 55 | summary_text = "an association between keys and values" |
124 | 56 |
|
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, "[", "]") |
141 | 80 | ) |
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 |
171 | 85 |
|
172 | 86 | def eval(self, rules, evaluation: Evaluation): |
173 | 87 | "Association[rules__]" |
|
0 commit comments