Skip to content

Commit eebf1ca

Browse files
authored
Merge pull request #1242 from mathics/fixReadExpression
Processing comments in Read Expression
2 parents 51ff48b + e68a952 commit eebf1ca

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

mathics/builtin/files.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from mathics.core.expression import (
3535
BoxError,
3636
Complex,
37+
BaseExpression,
3738
Expression,
3839
Integer,
3940
MachineReal,
@@ -624,6 +625,14 @@ class Read(Builtin):
624625
## #> str = Quiet[StringToStream["Sin[1 123"]; Read[str, Expression]]
625626
## = $Failed
626627
628+
## HoldExpression:
629+
>> str = StringToStream["2+2\\n2+3"];
630+
>> Read[str, Hold[Expression]]
631+
= Hold[2 + 2]
632+
>> Read[str, Expression]
633+
= 5
634+
>> Close[str];
635+
627636
## Multiple types
628637
>> str = StringToStream["123 abc"];
629638
>> Read[str, {Number, Word}]
@@ -766,8 +775,15 @@ def apply(self, channel, types, evaluation, options):
766775
else:
767776
types = (types,)
768777

778+
# TODO: look for a better implementation handling "Hold[Expression]".
779+
#
769780
types = (
770-
typ._leaves[0] if typ.get_head_name() == "System`Hold" else typ
781+
Symbol("HoldExpression")
782+
if (
783+
typ.get_head_name() == "System`Hold"
784+
and typ.leaves[0].get_name() == "System`Expression"
785+
)
786+
else typ
771787
for typ in types
772788
)
773789
types = Expression("List", *types)
@@ -778,6 +794,7 @@ def apply(self, channel, types, evaluation, options):
778794
"Byte",
779795
"Character",
780796
"Expression",
797+
"HoldExpression",
781798
"Number",
782799
"Real",
783800
"Record",
@@ -875,7 +892,7 @@ def reader(stream, word_separators, accepted=None):
875892
if tmp == "":
876893
raise EOFError
877894
result.append(tmp)
878-
elif typ == Symbol("Expression"):
895+
elif typ == Symbol("Expression") or typ == Symbol("HoldExpression"):
879896
tmp = next(read_record)
880897
while True:
881898
try:
@@ -889,13 +906,17 @@ def reader(stream, word_separators, accepted=None):
889906
except EOFError:
890907
expr = Symbol("EndOfFile")
891908
break
909+
except Exception as e:
910+
print(e)
892911

893-
if expr is None:
912+
if expr == Symbol("EndOfFile"):
894913
evaluation.message(
895914
"Read", "readt", tmp, Expression("InputSteam", name, n)
896915
)
897916
return SymbolFailed
898-
else:
917+
elif isinstance(expr, BaseExpression):
918+
if typ == Symbol("HoldExpression"):
919+
expr = Expression("Hold", expr)
899920
result.append(expr)
900921

901922
elif typ == Symbol("Number"):
@@ -3815,10 +3836,13 @@ class Compress(Builtin):
38153836

38163837
def apply(self, expr, evaluation, options):
38173838
"Compress[expr_, OptionsPattern[Compress]]"
3818-
string = expr.format(evaluation, "System`FullForm")
3819-
string = string.boxes_to_text(
3820-
evaluation=evaluation, show_string_characters=True
3821-
)
3839+
if isinstance(expr, String):
3840+
string = '"' + expr.value + '"'
3841+
else:
3842+
string = expr.format(evaluation, "System`FullForm")
3843+
string = string.boxes_to_text(
3844+
evaluation=evaluation, show_string_characters=True
3845+
)
38223846
string = string.encode("utf-8")
38233847

38243848
# TODO Implement other Methods

0 commit comments

Comments
 (0)