Skip to content

Commit 3fd98c1

Browse files
committed
fixing all derivate classes of BoxConstruct
1 parent 946b31c commit 3fd98c1

5 files changed

Lines changed: 39 additions & 26 deletions

File tree

mathics/builtin/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,10 @@ def __new__(cls, *leaves, **kwargs):
603603
instance._leaves = leaves
604604
return instance
605605

606+
def evaluate(self, evaluation):
607+
# Shall here evaluate the leaves?
608+
return
609+
606610
def get_lookup_name(self):
607611
return self.get_name()
608612

mathics/builtin/compilation.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,26 @@ def __hash__(self):
149149
return hash(("CompiledCode", ctypes.addressof(self.cfunc))) # XXX hack
150150

151151
def atom_to_boxes(self, f, evaluation):
152-
return Expression('CompiledCodeBox')
152+
return CompiledCodeBox(String("Nocode"))
153153

154154

155155
class CompiledCodeBox(BoxConstruct):
156156
"""
157157
Used internally by <i>CompileCode[]</i>.
158158
"""
159-
def boxes_to_text(self, leaves, **options):
159+
def boxes_to_text(self, leaves=None, **options):
160+
if not leaves:
161+
leaves = self._leaves
160162
return '-CompiledCode-'
161163

162-
def boxes_to_xml(self, leaves, **options):
164+
def boxes_to_xml(self, leaves=None, **options):
165+
if not leaves:
166+
leaves = self._leaves
163167
return '-CompiledCode-'
164168

165-
def boxes_to_tex(self, leaves, **options):
169+
def boxes_to_tex(self, leaves=None, **options):
170+
if not leaves:
171+
leaves = self._leaves
166172
return '-CompiledCode-'
167173

168174

mathics/builtin/image.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,18 +2124,20 @@ def test(self, expr):
21242124

21252125

21262126
class ImageBox(BoxConstruct):
2127-
def boxes_to_text(self, leaves, **options):
2127+
def boxes_to_text(self, leaves=None, **options):
21282128
return "-Image-"
21292129

2130-
def boxes_to_xml(self, leaves, **options):
2130+
def boxes_to_xml(self, leaves=None, **options):
2131+
if leaves is None:
2132+
leaves = self._leaves
21312133
# see https://tools.ietf.org/html/rfc2397
21322134
return '<mglyph src="%s" width="%dpx" height="%dpx" />' % (
21332135
leaves[0].get_string_value(),
21342136
leaves[1].get_int_value(),
21352137
leaves[2].get_int_value(),
21362138
)
21372139

2138-
def boxes_to_tex(self, leaves, **options):
2140+
def boxes_to_tex(self, leaves=None, **options):
21392141
return "-Image-"
21402142

21412143

@@ -2259,8 +2261,7 @@ def atom_to_boxes(self, f, evaluation):
22592261
encoded = base64.b64encode(contents)
22602262
encoded = b"data:image/png;base64," + encoded
22612263

2262-
return Expression(
2263-
"ImageBox", String(encoded), Integer(scaled_width), Integer(scaled_height)
2264+
return ImageBox(String(encoded), Integer(scaled_width), Integer(scaled_height)
22642265
)
22652266

22662267
def __str__(self):

mathics/builtin/inout.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ def get_array(self, leaves, evaluation):
791791
raise BoxConstructError
792792
return items, options
793793

794-
def boxes_to_tex(self, leaves, **box_options) -> str:
794+
def boxes_to_tex(self, leaves=None, **box_options) -> str:
795+
if not leaves:
796+
leaves = self._leaves
795797
evaluation = box_options.get('evaluation')
796798
items, options = self.get_array(leaves, evaluation)
797799
new_box_options = box_options.copy()
@@ -811,14 +813,16 @@ def boxes_to_tex(self, leaves, **box_options) -> str:
811813
column_count = max(column_count, len(row))
812814
result = r'\begin{array}{%s} ' % (column_alignments * column_count)
813815
for index, row in enumerate(items):
814-
result += ' & '.join(item.boxes_to_tex(**new_box_options)
816+
result += ' & '.join(item.evaluate(evaluation).boxes_to_tex(**new_box_options)
815817
for item in row)
816818
if index != len(items) - 1:
817819
result += '\\\\ '
818820
result += r'\end{array}'
819821
return result
820822

821-
def boxes_to_xml(self, leaves, **box_options) -> str:
823+
def boxes_to_xml(self, leaves=None, **box_options) -> str:
824+
if not leaves:
825+
leaves = self._leaves
822826
evaluation = box_options.get('evaluation')
823827
items, options = self.get_array(leaves, evaluation)
824828
attrs = {}
@@ -855,8 +859,7 @@ def boxes_to_text(self, leaves=None, **box_options) -> str:
855859
if not items:
856860
return ''
857861
widths = [0] * len(items[0])
858-
cells = [[item.boxes_to_text(**box_options).splitlines()
859-
for item in row] for row in items]
862+
cells = [[item.evaluate(evaluation).boxes_to_text(**box_options).splitlines() for item in row] for row in items]
860863
for row in cells:
861864
for index, cell in enumerate(row):
862865
if index >= len(widths):
@@ -907,14 +910,12 @@ class Grid(Builtin):
907910
def apply_makeboxes(self, array, f, evaluation, options) -> Expression:
908911
'''MakeBoxes[Grid[array_?MatrixQ, OptionsPattern[Grid]],
909912
f:StandardForm|TraditionalForm|OutputForm]'''
910-
911-
return Expression(
912-
'GridBox',
913-
Expression('List', *(
913+
return GridBox(Expression('List', *(
914914
Expression('List', *(
915915
Expression('MakeBoxes', item, f) for item in row.leaves))
916916
for row in array.leaves)),
917917
*options_to_rules(options))
918+
# return Expression('GridBox',Expression('List', *(Expression('List', *(Expression('MakeBoxes', item, f) for item in row.leaves)) for row in array.leaves)), *options_to_rules(options))
918919

919920

920921
class TableForm(Builtin):
@@ -974,10 +975,13 @@ def apply_makeboxes(self, table, f, evaluation, options):
974975
if depth <= 0:
975976
return Expression('MakeBoxes', table, f)
976977
elif depth == 1:
977-
return Expression(
978-
'GridBox', Expression('List', *(
978+
return GridBox( Expression('List', *(
979979
Expression('List', Expression('MakeBoxes', item, f))
980980
for item in table.leaves)))
981+
#return Expression(
982+
# 'GridBox', Expression('List', *(
983+
# Expression('List', Expression('MakeBoxes', item, f))
984+
# for item in table.leaves)))
981985
else:
982986
new_depth = Expression('Rule', Symbol('TableDepth'), depth - 2)
983987

@@ -987,8 +991,7 @@ def transform_item(item):
987991
else:
988992
return item
989993

990-
return Expression(
991-
'GridBox', Expression('List', *(
994+
return GridBox(Expression('List', *(
992995
Expression('List', *(
993996
Expression('MakeBoxes', transform_item(item), f)
994997
for item in row.leaves)) for row in table.leaves)))
@@ -2011,11 +2014,9 @@ def apply_mathml(self, expr, evaluation) -> Expression:
20112014
'MakeBoxes[expr_, MathMLForm]'
20122015

20132016
boxes = MakeBoxes(expr).evaluate(evaluation)
2014-
print("=====MakeBoxes, MathMLForm boxes=",boxes)
20152017
try:
20162018
xml = boxes.boxes_to_xml(evaluation=evaluation)
20172019
except BoxError:
2018-
print("======error")
20192020
evaluation.message(
20202021
'General', 'notboxes',
20212022
Expression('FullForm', boxes).evaluate(evaluation))

mathics/core/expression.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def format_expr(expr):
454454
head != 'System`Graphics'):
455455
new_leaves = [leaf.do_format(evaluation, form)
456456
for leaf in expr.leaves]
457+
formathead = expr.head.do_format(evaluation, form)
457458
expr = Expression(
458459
expr.head.do_format(evaluation, form), *new_leaves)
459460

@@ -660,8 +661,8 @@ class Expression(BaseExpression):
660661
leaves: typing.List[Any]
661662
_sequences: Any
662663

663-
def __new__(cls, head, *leaves) -> 'Expression':
664-
self = super(Expression, cls).__new__(cls)
664+
def __new__(cls, head, *leaves, **kwargs) -> 'Expression':
665+
self = super(Expression, cls).__new__(cls)
665666
if isinstance(head, str):
666667
head = Symbol(head)
667668
self._head = head

0 commit comments

Comments
 (0)