Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplementing eval_makeboxes. step 1 #699

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,7 @@
SymbolSubsuperscriptBox,
SymbolSuperscriptBox,
)
from mathics.eval.makeboxes import eval_makeboxes


def to_boxes(x, evaluation: Evaluation, options={}) -> BoxElementMixin:
"""
This function takes the expression ``x``
and tries to reduce it to a ``BoxElementMixin``
expression unsing an evaluation object.
"""
if isinstance(x, BoxElementMixin):
return x
if isinstance(x, Atom):
x = x.atom_to_boxes(SymbolStandardForm, evaluation)
return to_boxes(x, evaluation, options)
if isinstance(x, Expression):
if x.has_form("MakeBoxes", None):
x_boxed = x.evaluate(evaluation)
else:
x_boxed = eval_makeboxes(x, evaluation)
if isinstance(x_boxed, BoxElementMixin):
return x_boxed
if isinstance(x_boxed, Atom):
return to_boxes(x_boxed, evaluation, options)
raise eval_makeboxes(Expression(SymbolFullForm, x), evaluation)
from mathics.eval.makeboxes import eval_makeboxes, to_boxes


class BoxData(Builtin):
Expand Down
1 change: 1 addition & 0 deletions mathics/builtin/drawing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

import numpy
import PIL
import PIL.Image
import PIL.ImageEnhance
import PIL.ImageFilter
import PIL.ImageOps
Expand Down
3 changes: 2 additions & 1 deletion mathics/builtin/forms/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import re

from mathics.builtin.box.layout import RowBox, to_boxes
from mathics.builtin.box.layout import RowBox
from mathics.builtin.forms.base import FormBaseClass
from mathics.builtin.makeboxes import MakeBoxes
from mathics.core.atoms import String
from mathics.core.element import EvalMixin
from mathics.eval.makeboxes import to_boxes


class StringForm(FormBaseClass):
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Optional

from mathics.builtin.base import Builtin
from mathics.builtin.box.layout import GridBox, RowBox, to_boxes
from mathics.builtin.box.layout import GridBox, RowBox
from mathics.builtin.comparison import expr_min
from mathics.builtin.forms.base import FormBaseClass
from mathics.builtin.makeboxes import MakeBoxes, number_form
Expand Down Expand Up @@ -49,7 +49,7 @@
SymbolSubscriptBox,
SymbolSuperscriptBox,
)
from mathics.eval.makeboxes import format_element
from mathics.eval.makeboxes import format_element, to_boxes

MULTI_NEWLINE_RE = re.compile(r"\n{2,}")

Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


from mathics.builtin.base import BinaryOperator, Builtin, Operator
from mathics.builtin.box.layout import GridBox, RowBox, to_boxes
from mathics.builtin.box.layout import GridBox, RowBox
from mathics.builtin.lists import list_boxes
from mathics.builtin.makeboxes import MakeBoxes
from mathics.builtin.options import options_to_rules
Expand All @@ -20,7 +20,7 @@
from mathics.core.list import ListExpression
from mathics.core.symbols import Symbol
from mathics.core.systemsymbols import SymbolMakeBoxes
from mathics.eval.makeboxes import format_element
from mathics.eval.makeboxes import format_element, to_boxes

SymbolSubscriptBox = Symbol("System`SubscriptBox")

Expand Down
55 changes: 8 additions & 47 deletions mathics/builtin/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import mpmath

from mathics.builtin.base import Builtin, Predefined
from mathics.builtin.box.layout import RowBox, to_boxes
from mathics.builtin.box.layout import RowBox
from mathics.core.atoms import Integer, Integer1, Real, String
from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_READ_PROTECTED
from mathics.core.convert.op import operator_to_ascii, operator_to_unicode
Expand All @@ -20,7 +20,12 @@
from mathics.core.number import dps
from mathics.core.symbols import Atom, Symbol
from mathics.core.systemsymbols import SymbolInputForm, SymbolOutputForm, SymbolRowBox
from mathics.eval.makeboxes import _boxed_string, format_element
from mathics.eval.makeboxes import (
_boxed_string,
eval_makeboxes,
format_element,
to_boxes,
)


def int_to_s_exp(expr, n):
Expand Down Expand Up @@ -374,51 +379,7 @@ class MakeBoxes(Builtin):
def eval_general(self, expr, f, evaluation):
"""MakeBoxes[expr_,
f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm]"""
if isinstance(expr, BoxElementMixin):
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)
return eval_makeboxes(expr, evaluation, f)

def eval_outerprecedenceform(self, expr, prec, evaluation):
"""MakeBoxes[PrecedenceForm[expr_, prec_],
Expand Down
1 change: 1 addition & 0 deletions mathics/eval/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import numpy
import PIL
import PIL.Image

from mathics.builtin.base import String
from mathics.core.atoms import Rational
Expand Down
Loading