Skip to content

Commit 889e9fc

Browse files
mmaterarocky
authored andcommitted
moving do_eval_infix to be a non-member function in the same module.
1 parent a417f00 commit 889e9fc

File tree

1 file changed

+77
-85
lines changed

1 file changed

+77
-85
lines changed

mathics/builtin/layout.py

Lines changed: 77 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,78 @@
3939
SymbolSubscriptBox = Symbol("System`SubscriptBox")
4040

4141

42+
def eval_makeboxes_infix(
43+
expr: Expression,
44+
operator: BaseElement,
45+
form: Symbol,
46+
evaluation,
47+
precedence_value: Optional[int] = None,
48+
grouping: Optional[str] = None,
49+
):
50+
"""Implements MakeBoxes[Infix[...]]"""
51+
52+
## FIXME: this should go into a some formatter.
53+
def format_operator(operator) -> Union[String, BaseElement]:
54+
"""
55+
Format infix operator `operator`. To do this outside parameter form is used.
56+
Sometimes no changes are made and operator is returned unchanged.
57+
58+
This function probably should be rewritten be more scalable across other forms
59+
and moved to a module that contiaing similar formatting routines.
60+
"""
61+
if not isinstance(operator, String):
62+
return MakeBoxes(operator, form)
63+
64+
op_str = operator.value
65+
66+
# FIXME: performing a check using the operator symbol representation feels a bit
67+
# fragile. The operator name seems more straightforward and more robust.
68+
if form == SymbolInputForm and op_str in ["*", "^", " "]:
69+
return operator
70+
elif (
71+
form in (SymbolInputForm, SymbolOutputForm)
72+
and not op_str.startswith(" ")
73+
and not op_str.endswith(" ")
74+
):
75+
# FIXME: Again, testing on specific forms is fragile and not scalable.
76+
op = String(" " + op_str + " ")
77+
return op
78+
return operator
79+
80+
if isinstance(expr, Atom):
81+
evaluation.message("Infix", "normal", Integer1)
82+
return None
83+
84+
elements = expr.elements
85+
if len(elements) > 1:
86+
if operator.has_form("List", len(elements) - 1):
87+
operator = [format_operator(op) for op in operator.elements]
88+
return make_boxes_infix(
89+
elements, operator, precedence_value, grouping, form
90+
)
91+
else:
92+
encoding_rule = evaluation.definitions.get_ownvalue("$CharacterEncoding")
93+
encoding = "UTF8" if encoding_rule is None else encoding_rule.replace.value
94+
op_str = (
95+
operator.value if isinstance(operator, String) else operator.short_name
96+
)
97+
if encoding == "ASCII":
98+
operator = format_operator(
99+
String(operator_to_ascii.get(op_str, op_str))
100+
)
101+
else:
102+
operator = format_operator(
103+
String(operator_to_unicode.get(op_str, op_str))
104+
)
105+
106+
return make_boxes_infix(elements, operator, precedence_value, grouping, form)
107+
108+
elif len(elements) == 1:
109+
return MakeBoxes(elements[0], form)
110+
else:
111+
return MakeBoxes(expr, form)
112+
113+
42114
class Center(Builtin):
43115
"""
44116
<url>:WMA link:https://reference.wolfram.com/language/ref/Center.html</url>
@@ -168,7 +240,7 @@ class Infix(Builtin):
168240

169241
# the right rule should be
170242
# mbexpression:MakeBoxes[Infix[___], form]
171-
def eval_all(self, expression, form, evaluation):
243+
def eval_makeboxes(self, expression, form, evaluation):
172244
"""MakeBoxes[Infix[___],
173245
form:StandardForm|TraditionalForm|OutputForm|InputForm]"""
174246
infix_expr = expression.elements[0]
@@ -181,10 +253,10 @@ def eval_all(self, expression, form, evaluation):
181253

182254
if num_parms == 1:
183255
expr = elements[0]
184-
return self.do_eval_infix(expr, String("~"), form, evaluation)
256+
return eval_makeboxes_infix(expr, String("~"), form, evaluation)
185257
if num_parms == 2:
186258
expr, operator = elements
187-
return self.do_eval_infix(expr, operator, form, evaluation)
259+
return eval_makeboxes_infix(expr, operator, form, evaluation)
188260

189261
expr, operator, precedence = elements[:3]
190262
if not isinstance(precedence, Integer):
@@ -197,97 +269,17 @@ def eval_all(self, expression, form, evaluation):
197269

198270
grouping = SymbolNone if num_parms < 4 else elements[3]
199271
if grouping is SymbolNone:
200-
return self.do_eval_infix(
272+
return eval_makeboxes_infix(
201273
expr, operator, form, evaluation, precedence.value
202274
)
203275
if grouping in (SymbolNonAssociative, SymbolLeft, SymbolRight):
204-
return self.do_eval_infix(
276+
return eval_makeboxes_infix(
205277
expr, operator, form, evaluation, precedence.value, grouping.get_name()
206278
)
207279

208280
evaluation.message("Infix", "argb", grouping)
209281
return eval_fullform_makeboxes(infix_expr, evaluation, form)
210282

211-
def do_eval_infix(
212-
self,
213-
expr: Expression,
214-
operator: BaseElement,
215-
form: Symbol,
216-
evaluation,
217-
precedence_value: Optional[int] = None,
218-
grouping: Optional[str] = None,
219-
):
220-
"""Implements MakeBoxes[Infix[...]]"""
221-
222-
## FIXME: this should go into a some formatter.
223-
def format_operator(operator) -> Union[String, BaseElement]:
224-
"""
225-
Format infix operator `operator`. To do this outside parameter form is used.
226-
Sometimes no changes are made and operator is returned unchanged.
227-
228-
This function probably should be rewritten be more scalable across other forms
229-
and moved to a module that contiaing similar formatting routines.
230-
"""
231-
if not isinstance(operator, String):
232-
return MakeBoxes(operator, form)
233-
234-
op_str = operator.value
235-
236-
# FIXME: performing a check using the operator symbol representation feels a bit
237-
# fragile. The operator name seems more straightforward and more robust.
238-
if form == SymbolInputForm and op_str in ["*", "^", " "]:
239-
return operator
240-
elif (
241-
form in (SymbolInputForm, SymbolOutputForm)
242-
and not op_str.startswith(" ")
243-
and not op_str.endswith(" ")
244-
):
245-
# FIXME: Again, testing on specific forms is fragile and not scalable.
246-
op = String(" " + op_str + " ")
247-
return op
248-
return operator
249-
250-
if isinstance(expr, Atom):
251-
evaluation.message("Infix", "normal", Integer1)
252-
return None
253-
254-
elements = expr.elements
255-
if len(elements) > 1:
256-
if operator.has_form("List", len(elements) - 1):
257-
operator = [format_operator(op) for op in operator.elements]
258-
return make_boxes_infix(
259-
elements, operator, precedence_value, grouping, form
260-
)
261-
else:
262-
encoding_rule = evaluation.definitions.get_ownvalue(
263-
"$CharacterEncoding"
264-
)
265-
encoding = (
266-
"UTF8" if encoding_rule is None else encoding_rule.replace.value
267-
)
268-
op_str = (
269-
operator.value
270-
if isinstance(operator, String)
271-
else operator.short_name
272-
)
273-
if encoding == "ASCII":
274-
operator = format_operator(
275-
String(operator_to_ascii.get(op_str, op_str))
276-
)
277-
else:
278-
operator = format_operator(
279-
String(operator_to_unicode.get(op_str, op_str))
280-
)
281-
282-
return make_boxes_infix(
283-
elements, operator, precedence_value, grouping, form
284-
)
285-
286-
elif len(elements) == 1:
287-
return MakeBoxes(elements[0], form)
288-
else:
289-
return MakeBoxes(expr, form)
290-
291283

292284
class Left(Builtin):
293285
"""

0 commit comments

Comments
 (0)