Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
16 changes: 12 additions & 4 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mathics.core.atoms import String
from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_PROTECTED, A_READ_PROTECTED
from mathics.core.builtin import Builtin
from mathics.core.element import BaseElement, BoxElementMixin
from mathics.core.element import BaseElement, BoxElementMixin, EvalMixin
from mathics.core.evaluation import Evaluation
from mathics.core.exceptions import BoxConstructError
from mathics.core.expression import Expression
Expand Down Expand Up @@ -208,6 +208,10 @@ class InterpretationBox(BoxExpression):
"""

attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
options = {
"Editable": "Automatic",
"AutoDelete": "Automatic",
}
summary_text = "box associated to an input expression"

def __repr__(self):
Expand All @@ -233,9 +237,13 @@ def elements(self):
)
return self._elements

def eval_create(self, reprs, expr, evaluation):
"""InterpretationBox[reprs_, expr_]"""
return InterpretationBox(reprs, expr)
def eval_create(self, reprs, expr, evaluation, options):
"""InterpretationBox[reprs_, expr_, OptionsPattern[]]"""
if isinstance(reprs, EvalMixin):
reprs = reprs.evaluate(evaluation)
if not isinstance(reprs, BoxElementMixin):
return
return InterpretationBox(reprs, expr, **options)

def eval_to_expression1(self, boxexpr, evaluation):
"""ToExpression[boxexpr_InterpretationBox]"""
Expand Down
8 changes: 4 additions & 4 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
parse_read_options,
read_name_and_stream,
)
from mathics.eval.makeboxes import do_format, format_element
from mathics.eval.makeboxes import do_format, format_element, render_input_form
from mathics.eval.stackframe import get_eval_Expression


Expand Down Expand Up @@ -596,11 +596,11 @@ def eval_input(self, exprs, name, n, evaluation: Evaluation):
# Eventually, we are going to replace this by a `MakeBoxes` call.
def do_format_output(expr, evaluation):
try:
boxed_expr = format_element(expr, evaluation, SymbolInputForm)
# TODO: set character encoding?
return render_input_form(expr, evaluation)
except BoxError:
boxed_expr = format_element(expr, evaluation, SymbolFullForm)

return boxed_expr.boxes_to_text()
return boxed_expr.boxes_to_text()

text = [do_format_output(expr, evaluation) for expr in exprs.get_sequence()]
text = "\n".join(text) + "\n"
Expand Down
24 changes: 23 additions & 1 deletion 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.box.layout import RowBox, StyleBox, TagBox
from mathics.builtin.box.layout import InterpretationBox, RowBox, StyleBox, TagBox
from mathics.builtin.forms.base import FormBaseClass
from mathics.core.atoms import Integer, Real, String, StringFromPython
from mathics.core.builtin import Builtin
Expand All @@ -32,6 +32,7 @@
from mathics.core.systemsymbols import (
SymbolAutomatic,
SymbolInfinity,
SymbolInputForm,
SymbolMakeBoxes,
SymbolNumberForm,
SymbolRowBox,
Expand All @@ -48,6 +49,7 @@
eval_tableform,
eval_texform,
)
from mathics.eval.makeboxes.inputform import render_input_form


class BaseForm(FormBaseClass):
Expand Down Expand Up @@ -207,6 +209,26 @@ class InputForm(FormBaseClass):
in_printforms = True
summary_text = "plain-text input format"

# TODO: eventually, remove OutputForm in the second argument.
def eval_makeboxes(self, expr, evaluation):
"""MakeBoxes[InputForm[expr_], Alternatives[StandardForm,TraditionalForm,OutputForm]]"""

inputform = String(render_input_form(expr, evaluation))
inputform = StyleBox(
inputform,
**{
"System`ShowSpecialCharacters": SymbolFalse,
"System`ShowStringCharacters": SymbolTrue,
"System`NumberMarks": SymbolTrue,
},
)
expr = Expression(SymbolInputForm, expr)
return InterpretationBox(
inputform,
expr,
**{"System`Editable": SymbolTrue, "System`AutoDelete": SymbolTrue},
)


class _NumberForm(Builtin):
"""
Expand Down
11 changes: 5 additions & 6 deletions mathics/builtin/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ class MakeBoxes(Builtin):

rules = {
"MakeBoxes[Infix[head_[elements___]], "
" f:StandardForm|TraditionalForm|OutputForm|InputForm]": (
" f:StandardForm|TraditionalForm|OutputForm]": (
'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[InputForm[expr_], StandardForm|TraditionalForm|OutputForm]": "StyleBox[MakeBoxes[expr, InputForm], ShowStringCharacters->True]",
"MakeBoxes[PrecedenceForm[expr_, prec_], f_]": "MakeBoxes[expr, f]",
"MakeBoxes[Style[expr_, OptionsPattern[Style]], f_]": (
"StyleBox[MakeBoxes[expr, f], "
Expand All @@ -114,26 +113,26 @@ def eval_fullform(self, expr, evaluation):

def eval_general(self, expr, f, evaluation):
"""MakeBoxes[expr_,
f:TraditionalForm|StandardForm|OutputForm|InputForm]"""
f:TraditionalForm|StandardForm|OutputForm]"""
return eval_generic_makeboxes(self, expr, f, evaluation)

def eval_outerprecedenceform(self, expr, precedence, form, evaluation):
"""MakeBoxes[PrecedenceForm[expr_, precedence_],
form:StandardForm|TraditionalForm|OutputForm|InputForm]"""
form:StandardForm|TraditionalForm|OutputForm]"""

py_precedence = precedence.get_int_value()
boxes = MakeBoxes(expr, form)
return parenthesize(py_precedence, expr, boxes, True)

def eval_postprefix(self, p, expr, h, precedence, form, evaluation):
"""MakeBoxes[(p:Prefix|Postfix)[expr_, h_, precedence_:None],
form:StandardForm|TraditionalForm|OutputForm|InputForm]"""
form:StandardForm|TraditionalForm|OutputForm]"""
return eval_postprefix(self, p, expr, h, precedence, form, evaluation)

def eval_infix(
self, expr, operator, precedence: Integer, grouping, form: Symbol, evaluation
):
"""MakeBoxes[Infix[expr_, operator_, precedence_:None, grouping_:None], form:StandardForm|TraditionalForm|OutputForm|InputForm]"""
"""MakeBoxes[Infix[expr_, operator_, precedence_:None, grouping_:None], form:StandardForm|TraditionalForm|OutputForm]"""
return eval_infix(self, expr, operator, precedence, grouping, form, evaluation)


Expand Down
20 changes: 10 additions & 10 deletions mathics/doc/documentation/1-Manual.mdoc
Original file line number Diff line number Diff line change
Expand Up @@ -1224,16 +1224,16 @@ A dice object shall be displayed as a rectangle with the given number of points

#> Definition[Dice]
= Attributes[Dice] = {Orderless}
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], MathMLForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{Plus[1, Times[-1, p]], 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], OutputForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{Plus[1, Times[-1, p]], 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], StandardForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{Plus[1, Times[-1, p]], 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], TeXForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{Plus[1, Times[-1, p]], 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], TraditionalForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, Plus[1, Times[-1, p]]}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{Plus[1, Times[-1, p]], p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{Plus[1, Times[-1, p]], 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], MathMLForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{1 - p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{1 - p, p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{1 - p, 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], OutputForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{1 - p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{1 - p, p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{1 - p, 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], StandardForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{1 - p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{1 - p, p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{1 - p, 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], TeXForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{1 - p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{1 - p, p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{1 - p, 0.5}, r]}]}, ImageSize -> Tiny]]
.
. Format[Dice[n_Integer ? (1 <= #1 <= 6&)], TraditionalForm] = Block[{p = 0.2, r = 0.05}, Graphics[{EdgeForm[Black], White, Rectangle[], Black, EdgeForm[], If[OddQ[n], Disk[{0.5, 0.5}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{p, p}, r]], If[MemberQ[{2, 3, 4, 5, 6}, n], Disk[{1 - p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{p, 1 - p}, r]], If[MemberQ[{4, 5, 6}, n], Disk[{1 - p, p}, r]], If[n === 6, {Disk[{p, 0.5}, r], Disk[{1 - p, 0.5}, r]}]}, ImageSize -> Tiny]]

The empty series of dice shall be displayed as an empty dice:
>> Format[Dice[]] := Graphics[{EdgeForm[Black], White, Rectangle[]}, ImageSize -> Tiny]
Expand Down
9 changes: 8 additions & 1 deletion mathics/eval/makeboxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from mathics.eval.makeboxes.formatvalues import StringLParen, StringRParen, do_format
from mathics.eval.makeboxes.inputform import render_input_form
from mathics.eval.makeboxes.makeboxes import (
_boxed_string,
eval_generic_makeboxes,
Expand All @@ -19,14 +20,19 @@
eval_tableform,
eval_texform,
)
from mathics.eval.makeboxes.precedence import builtins_precedence, parenthesize
from mathics.eval.makeboxes.precedence import (
builtins_precedence,
compare_precedence,
parenthesize,
)

__all__ = [
"NumberForm_to_String",
"StringLParen",
"StringRParen",
"_boxed_string",
"builtins_precedence",
"compare_precedence",
"do_format",
"eval_baseform",
"eval_generic_makeboxes",
Expand All @@ -40,5 +46,6 @@
"format_element",
"int_to_string_shorter_repr",
"parenthesize",
"render_input_form",
"to_boxes",
]
Loading
Loading