Skip to content

Commit 7196a95

Browse files
authored
Merge pull request #1306 from mathics/toexpression-unaryop
fix support for \!
2 parents 2a72cd5 + 87bc59d commit 7196a95

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

mathics/builtin/strings.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
from typing import Any, Callable, List
1515

1616
from mathics.version import __version__ # noqa used in loading to check consistency.
17-
from mathics.builtin.base import BinaryOperator, Builtin, Test, Predefined
17+
from mathics.builtin.base import (
18+
BinaryOperator,
19+
Builtin,
20+
Test,
21+
Predefined,
22+
PrefixOperator,
23+
)
1824
from mathics.core.expression import (
1925
Expression,
2026
Symbol,
@@ -1878,6 +1884,31 @@ def apply_form(self, value, form, evaluation, options):
18781884
return String(text)
18791885

18801886

1887+
class InterpretedBox(PrefixOperator):
1888+
"""
1889+
<dl>
1890+
<dt>'InterpretedBox[$box$]'
1891+
<dd>is the ad hoc fullform for \! $box$. just
1892+
for internal use...
1893+
1894+
>> \! \(2+2\)
1895+
= 4
1896+
</dl>
1897+
"""
1898+
1899+
operator = "\\!"
1900+
precedence = 670
1901+
1902+
def apply_dummy(self, boxes, evaluation):
1903+
"""InterpretedBox[boxes_]"""
1904+
# TODO: the following is a very raw and dummy way to
1905+
# handle these expressions.
1906+
# In the first place, this should handle different kind
1907+
# of boxes in different ways.
1908+
reinput = boxes.boxes_to_text()
1909+
return Expression("ToExpression", reinput).evaluate(evaluation)
1910+
1911+
18811912
class ToExpression(Builtin):
18821913
"""
18831914
<dl>
@@ -1926,7 +1957,6 @@ class ToExpression(Builtin):
19261957
#> ToExpression["log(x)", StandardForm]
19271958
= log x
19281959
"""
1929-
19301960
attributes = ("Listable", "Protected")
19311961

19321962
messages = {

mathics/core/expression.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,8 @@ def boxes_to_text(self, **options) -> str:
15131513
)
15141514
elif self.has_form("SuperscriptBox", 2):
15151515
return "^".join([leaf.boxes_to_text(**options) for leaf in self._leaves])
1516+
elif self.has_form("FractionBox", 2):
1517+
return "/".join([" ( " + leaf.boxes_to_text(**options)+ " ) " for leaf in self._leaves])
15161518
else:
15171519
raise BoxError(self, "text")
15181520

mathics/core/parser/operators.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"Not": 230,
1919
"Information": 5001,
2020
"Definition": 5000,
21+
"InterpretedBox": 670,
2122
}
2223

2324
postfix_ops = {

mathics/core/parser/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def parse_box(self, p):
170170
elif result is None and tag != "END":
171171
self.consume()
172172
new_result = String(token.text)
173+
if new_result.value == r"\(":
174+
new_result = self.p_LeftRowBox(token)
173175
else:
174176
new_result = None
175177
if new_result is None:
@@ -311,7 +313,8 @@ def p_LeftRowBox(self, token):
311313
self.bracket_depth += 1
312314
token = self.next()
313315
while token.tag not in ("RightRowBox", "OtherscriptBox"):
314-
children.append(self.parse_box(0))
316+
newnode = self.parse_box(0)
317+
children.append(newnode)
315318
token = self.next()
316319
if len(children) == 0:
317320
result = String("")

0 commit comments

Comments
 (0)