-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmakeboxes.py
578 lines (485 loc) · 18.2 KB
/
makeboxes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# -*- coding: utf-8 -*-
"""
Low level Format definitions
"""
from typing import Union
import mpmath
from mathics.builtin.base import Builtin, Predefined
from mathics.builtin.box.layout import (
_boxed_string,
RowBox,
to_boxes,
)
from mathics.core.convert.op import operator_to_unicode, operator_to_ascii
from mathics.core.atoms import (
Integer,
Integer1,
Real,
String,
)
from mathics.core.attributes import (
A_HOLD_ALL_COMPLETE,
A_READ_PROTECTED,
)
from mathics.core.element import BaseElement, BoxElementMixin
from mathics.core.expression import Expression
from mathics.core.formatter import format_element
from mathics.core.list import ListExpression
from mathics.core.number import dps
from mathics.core.symbols import (
Atom,
Symbol,
)
from mathics.core.systemsymbols import (
SymbolInputForm,
SymbolOutputForm,
SymbolRowBox,
)
def int_to_s_exp(expr, n):
n = expr.get_int_value()
if n < 0:
nonnegative = 0
s = str(-n)
else:
nonnegative = 1
s = str(n)
exp = len(s) - 1
return s, exp, nonnegative
def parenthesize(precedence, element, element_boxes, when_equal):
from mathics.builtin import builtins_precedence
while element.has_form("HoldForm", 1):
element = element.elements[0]
if element.has_form(("Infix", "Prefix", "Postfix"), 3, None):
element_prec = element.elements[2].value
elif element.has_form("PrecedenceForm", 2):
element_prec = element.elements[1].value
# For negative values, ensure that the element_precedence is at least the precedence. (Fixes #332)
elif isinstance(element, (Integer, Real)) and element.value < 0:
element_prec = precedence
else:
element_prec = builtins_precedence.get(element.get_head_name())
if precedence is not None and element_prec is not None:
if precedence > element_prec or (precedence == element_prec and when_equal):
return Expression(
SymbolRowBox,
ListExpression(String("("), element_boxes, String(")")),
)
return element_boxes
# FIXME: op should be a string, so remove the Union.
def make_boxes_infix(
elements, op: Union[String, list], precedence: int, grouping, form: Symbol
):
result = []
for index, element in enumerate(elements):
if index > 0:
if isinstance(op, list):
result.append(op[index - 1])
else:
result.append(op)
parenthesized = False
if grouping == "System`NonAssociative":
parenthesized = True
elif grouping == "System`Left" and index > 0:
parenthesized = True
elif grouping == "System`Right" and index == 0:
parenthesized = True
element_boxes = MakeBoxes(element, form)
element = parenthesize(precedence, element, element_boxes, parenthesized)
result.append(element)
return Expression(SymbolRowBox, ListExpression(*result))
def real_to_s_exp(expr, n):
if expr.is_zero:
s = "0"
if expr.is_machine_precision():
exp = 0
else:
p = expr.get_precision()
exp = -dps(p)
nonnegative = 1
else:
if n is None:
if expr.is_machine_precision():
value = expr.get_float_value()
s = repr(value)
else:
with mpmath.workprec(expr.get_precision()):
value = expr.to_mpmath()
s = mpmath.nstr(value, dps(expr.get_precision()) + 1)
else:
with mpmath.workprec(expr.get_precision()):
value = expr.to_mpmath()
s = mpmath.nstr(value, n)
# sign prefix
if s[0] == "-":
assert value < 0
nonnegative = 0
s = s[1:]
else:
assert value >= 0
nonnegative = 1
# exponent (exp is actual, pexp is printed)
if "e" in s:
s, exp = s.split("e")
exp = int(exp)
if len(s) > 1 and s[1] == ".":
# str(float) doesn't always include '.' if 'e' is present.
s = s[0] + s[2:].rstrip("0")
else:
exp = s.index(".") - 1
s = s[: exp + 1] + s[exp + 2 :].rstrip("0")
# consume leading '0's.
i = 0
while s[i] == "0":
i += 1
exp -= 1
s = s[i:]
# add trailing zeros for precision reals
if n is not None and not expr.is_machine_precision() and len(s) < n:
s = s + "0" * (n - len(s))
return s, exp, nonnegative
def number_form(expr, n, f, evaluation, options):
"""
Converts a Real or Integer instance to Boxes.
n digits of precision with f (can be None) digits after the decimal point.
evaluation (can be None) is used for messages.
The allowed options are python versions of the options permitted to
NumberForm and must be supplied. See NumberForm or Real.make_boxes
for correct option examples.
"""
assert isinstance(n, int) and n > 0 or n is None
assert f is None or (isinstance(f, int) and f >= 0)
is_int = False
if isinstance(expr, Integer):
assert n is not None
s, exp, nonnegative = int_to_s_exp(expr, n)
if f is None:
is_int = True
elif isinstance(expr, Real):
if n is not None:
n = min(n, dps(expr.get_precision()) + 1)
s, exp, nonnegative = real_to_s_exp(expr, n)
if n is None:
n = len(s)
else:
raise ValueError("Expected Real or Integer.")
assert isinstance(n, int) and n > 0
sign_prefix = options["NumberSigns"][nonnegative]
# round exponent to ExponentStep
rexp = (exp // options["ExponentStep"]) * options["ExponentStep"]
if is_int:
# integer never uses scientific notation
pexp = ""
else:
method = options["ExponentFunction"]
pexp = method(Integer(rexp)).get_int_value()
if pexp is not None:
exp -= pexp
pexp = str(pexp)
else:
pexp = ""
# pad right with '0'.
if len(s) < exp + 1:
if evaluation is not None:
evaluation.message("NumberForm", "sigz")
# TODO NumberPadding?
s = s + "0" * (1 + exp - len(s))
# pad left with '0'.
if exp < 0:
s = "0" * (-exp) + s
exp = 0
# left and right of NumberPoint
left, right = s[: exp + 1], s[exp + 1 :]
def _round(number, ndigits):
"""
python round() for integers but with correct rounding.
e.g. `_round(14225, -1)` is `14230` not `14220`.
"""
assert isinstance(ndigits, int)
assert ndigits < 0
assert isinstance(number, int)
assert number >= 0
number += 5 * int(10 ** -(1 + ndigits))
number //= int(10**-ndigits)
return number
# pad with NumberPadding
if f is not None:
if len(right) < f:
# pad right
right = right + (f - len(right)) * options["NumberPadding"][1]
elif len(right) > f:
# round right
tmp = int(left + right)
tmp = _round(tmp, f - len(right))
tmp = str(tmp)
left, right = tmp[: exp + 1], tmp[exp + 1 :]
def split_string(s, start, step):
if start > 0:
yield s[:start]
for i in range(start, len(s), step):
yield s[i : i + step]
# insert NumberSeparator
digit_block = options["DigitBlock"]
if digit_block[0] != 0:
left = split_string(left, len(left) % digit_block[0], digit_block[0])
left = options["NumberSeparator"][0].join(left)
if digit_block[1] != 0:
right = split_string(right, 0, digit_block[1])
right = options["NumberSeparator"][1].join(right)
left_padding = 0
max_sign_len = max(len(options["NumberSigns"][0]), len(options["NumberSigns"][1]))
i = len(sign_prefix) + len(left) + len(right) - max_sign_len
if i < n:
left_padding = n - i
elif len(sign_prefix) < max_sign_len:
left_padding = max_sign_len - len(sign_prefix)
left_padding = left_padding * options["NumberPadding"][0]
# insert NumberPoint
if options["SignPadding"]:
prefix = sign_prefix + left_padding
else:
prefix = left_padding + sign_prefix
if is_int:
s = prefix + left
else:
s = prefix + left + options["NumberPoint"] + right
# base
base = "10"
# build number
method = options["NumberFormat"]
if options["_Form"] in ("System`InputForm", "System`FullForm"):
return method(
_boxed_string(s, number_as_text=True),
_boxed_string(base, number_as_text=True),
_boxed_string(pexp, number_as_text=True),
options,
)
else:
return method(String(s), String(base), String(pexp), options)
# TODO: Differently from the current implementation, MakeBoxes should only
# accept as its format field the symbols in `$BoxForms`. This is something to
# fix in a following step, changing the way in which Format and MakeBoxes work.
class BoxForms_(Predefined):
"""
<dl>
<dt>
<dd>$BoxForms is the list of box formats.
</dl>
>> $BoxForms
= ...
"""
attributes = A_READ_PROTECTED
name = "$BoxForms"
rules = {"$BoxForms": "{StandardForm, TraditionalForm}"}
summary_text = "the list of box formats"
class MakeBoxes(Builtin):
"""
<dl>
<dt>'MakeBoxes[$expr$]'
<dd>is a low-level formatting primitive that converts $expr$
to box form, without evaluating it.
<dt>'\\( ... \\)'
<dd>directly inputs box objects.
</dl>
String representation of boxes
>> \\(x \\^ 2\\)
= SuperscriptBox[x, 2]
>> \\(x \\_ 2\\)
= SubscriptBox[x, 2]
>> \\( a \\+ b \\% c\\)
= UnderoverscriptBox[a, b, c]
>> \\( a \\& b \\% c\\)
= UnderoverscriptBox[a, c, b]
#> \\( \\@ 5 \\)
= SqrtBox[5]
>> \\(x \\& y \\)
= OverscriptBox[x, y]
>> \\(x \\+ y \\)
= UnderscriptBox[x, y]
#> \\( x \\^ 2 \\_ 4 \\)
= SuperscriptBox[x, SubscriptBox[2, 4]]
## Tests for issue 151 (infix operators in heads)
#> (a + b)[x]
= (a + b)[x]
#> (a b)[x]
= (a b)[x]
#> (a <> b)[x]
: String expected.
= (a <> b)[x]
"""
attributes = A_HOLD_ALL_COMPLETE
rules = {
"MakeBoxes[Infix[head_[elements___]], "
" f:StandardForm|TraditionalForm|OutputForm|InputForm]": (
'MakeBoxes[Infix[head[elements], StringForm["~`1`~", head]], f]'
),
"MakeBoxes[expr_]": "MakeBoxes[expr, StandardForm]",
"MakeBoxes[(form:StandardForm|TraditionalForm|OutputForm|TeXForm|"
"MathMLForm)[expr_], StandardForm|TraditionalForm]": ("MakeBoxes[expr, form]"),
"MakeBoxes[(form:StandardForm|OutputForm|MathMLForm|TeXForm)[expr_], OutputForm]": "MakeBoxes[expr, form]",
"MakeBoxes[(form:FullForm|InputForm)[expr_], StandardForm|TraditionalForm|OutputForm]": "StyleBox[MakeBoxes[expr, form], ShowStringCharacters->True]",
"MakeBoxes[PrecedenceForm[expr_, prec_], f_]": "MakeBoxes[expr, f]",
"MakeBoxes[Style[expr_, OptionsPattern[Style]], f_]": (
"StyleBox[MakeBoxes[expr, f], "
"ImageSizeMultipliers -> OptionValue[ImageSizeMultipliers]]"
),
}
summary_text = "settable low-level translator from expression to display boxes"
def apply_general(self, expr, f, evaluation):
"""MakeBoxes[expr_,
f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm]"""
if isinstance(expr, BoxElementMixin):
# If we are inside a DisplayForm block,
# BoxElementMixin are not processed.
if evaluation.in_display_form:
return expr
expr = expr.to_expression()
if isinstance(expr, Atom):
return expr.atom_to_boxes(f, evaluation)
else:
head = expr.head
elements = expr.elements
f_name = f.get_name()
if f_name == "System`TraditionalForm":
left, right = "(", ")"
else:
left, right = "[", "]"
# Parenthesize infix operators at the head of expressions,
# like (a + b)[x], but not f[a] in f[a][b].
#
head_boxes = parenthesize(670, head, MakeBoxes(head, f), False)
head_boxes = head_boxes.evaluate(evaluation)
head_boxes = to_boxes(head_boxes, evaluation)
result = [head_boxes, to_boxes(String(left), evaluation)]
if len(elements) > 1:
row = []
if f_name in (
"System`InputForm",
"System`OutputForm",
"System`FullForm",
):
sep = ", "
else:
sep = ","
for index, element in enumerate(elements):
if index > 0:
row.append(to_boxes(String(sep), evaluation))
row.append(
to_boxes(MakeBoxes(element, f).evaluate(evaluation), evaluation)
)
result.append(RowBox(*row))
elif len(elements) == 1:
result.append(
to_boxes(MakeBoxes(elements[0], f).evaluate(evaluation), evaluation)
)
result.append(to_boxes(String(right), evaluation))
return RowBox(*result)
def apply_outerprecedenceform(self, expr, prec, evaluation):
"""MakeBoxes[OuterPrecedenceForm[expr_, prec_],
StandardForm|TraditionalForm|OutputForm|InputForm]"""
precedence = prec.get_int_value()
boxes = MakeBoxes(expr)
return parenthesize(precedence, expr, boxes, True)
def apply_postprefix(self, p, expr, h, prec, f, evaluation):
"""MakeBoxes[(p:Prefix|Postfix)[expr_, h_, prec_:None],
f:StandardForm|TraditionalForm|OutputForm|InputForm]"""
if not isinstance(h, String):
h = MakeBoxes(h, f)
precedence = prec.get_int_value()
elements = expr.elements
if len(elements) == 1:
element = elements[0]
element_boxes = MakeBoxes(element, f)
element = parenthesize(precedence, element, element_boxes, True)
if p.get_name() == "System`Postfix":
args = (element, h)
else:
args = (h, element)
return Expression(SymbolRowBox, ListExpression(*args).evaluate(evaluation))
else:
return MakeBoxes(expr, f).evaluate(evaluation)
def apply_infix(
self, expr, operator, prec: Integer, grouping, form: Symbol, evaluation
):
"""MakeBoxes[Infix[expr_, operator_, prec_:None, grouping_:None],
form:StandardForm|TraditionalForm|OutputForm|InputForm]"""
## FIXME: this should go into a some formatter.
def format_operator(operator) -> Union[String, BaseElement]:
"""
Format infix operator `operator`. To do this outside parameter form is used.
Sometimes no changes are made and operator is returned unchanged.
This function probably should be rewritten be more scalable across other forms
and moved to a module that contiaing similar formatting routines.
"""
if not isinstance(operator, String):
return MakeBoxes(operator, form)
op_str = operator.value
# FIXME: performing a check using the operator symbol representation feels a bit
# fragile. The operator name seems more straightforward and more robust.
if form == SymbolInputForm and op_str in ["*", "^", " "]:
return operator
elif (
form in (SymbolInputForm, SymbolOutputForm)
and not op_str.startswith(" ")
and not op_str.endswith(" ")
):
# FIXME: Again, testing on specific forms is fragile and not scalable.
op = String(" " + op_str + " ")
return op
return operator
precedence = prec.value
grouping = grouping.get_name()
if isinstance(expr, Atom):
evaluation.message("Infix", "normal", Integer1)
return None
elements = expr.elements
if len(elements) > 1:
if operator.has_form("List", len(elements) - 1):
operator = [format_operator(op) for op in operator.elements]
return make_boxes_infix(elements, operator, precedence, grouping, form)
else:
encoding_rule = evaluation.definitions.get_ownvalue(
"$CharacterEncoding"
)
encoding = (
"UTF8" if encoding_rule is None else encoding_rule.replace.value
)
op_str = (
operator.value
if isinstance(operator, String)
else operator.short_name
)
if encoding == "ASCII":
operator = format_operator(
String(operator_to_ascii.get(op_str, op_str))
)
else:
operator = format_operator(
String(operator_to_unicode.get(op_str, op_str))
)
return make_boxes_infix(elements, operator, precedence, grouping, form)
elif len(elements) == 1:
return MakeBoxes(elements[0], form)
else:
return MakeBoxes(expr, form)
class ToBoxes(Builtin):
"""
<dl>
<dt>'ToBoxes[$expr$]'
<dd>evaluates $expr$ and converts the result to box form.
</dl>
Unlike 'MakeBoxes', 'ToBoxes' evaluates its argument:
>> ToBoxes[a + a]
= RowBox[{2, , a}]
>> ToBoxes[a + b]
= RowBox[{a, +, b}]
>> ToBoxes[a ^ b] // FullForm
= SuperscriptBox["a", "b"]
"""
summary_text = "produce the display boxes of an evaluated expression"
def apply(self, expr, form, evaluation):
"ToBoxes[expr_, form_:StandardForm]"
form_name = form.get_name()
if form_name is None:
evaluation.message("ToBoxes", "boxfmt", form)
boxes = format_element(expr, evaluation, form)
return boxes