Skip to content

Commit

Permalink
Match WMA nocatch msg better and check...
Browse files Browse the repository at this point in the history
mathics/builtin/procedural.py: check error message
mathics/core/evaluation.py; wrap result in Hold, but not the message
  • Loading branch information
rocky committed Sep 23, 2024
1 parent c9b4b7d commit 744a59f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
81 changes: 41 additions & 40 deletions mathics/builtin/procedural.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,47 @@ def eval(self, expr, rules, evaluation):
# return unevaluated Switch when no pattern matches


class Throw(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Throw.html</url>
<dl>
<dt>'Throw[`value`]'
<dd> stops evaluation and returns `value` as the value of the nearest \
enclosing 'Catch'.
<dt>'Catch[`value`, `tag`]'
<dd> is caught only by `Catch[expr,form]`, where tag matches form.
</dl>
Using Throw can affect the structure of what is returned by a function:
>> NestList[#^2 + 1 &, 1, 7]
= ...
>> Catch[NestList[If[# > 1000, Throw[#], #^2 + 1] &, 1, 7]]
= 458330
>> Throw[1]
: Uncaught Throw[1] returned to top level.
= Hold[Throw[1]]
"""

messages = {
"nocatch": "Uncaught `1` returned to top level.",
}

summary_text = "throw an expression to be caught by a surrounding 'Catch'"

def eval(self, value, evaluation: Evaluation):
"Throw[value_]"
raise WLThrowInterrupt(value)

def eval_with_tag(self, value, tag, evaluation: Evaluation):
"Throw[value_, tag_]"
raise WLThrowInterrupt(value, tag)


class Which(Builtin):
"""
<url>
Expand Down Expand Up @@ -609,43 +650,3 @@ def eval(self, test, body, evaluation):
except ReturnInterrupt as e:
return e.expr
return SymbolNull


class Throw(Builtin):
"""
<url>:WMA link:
https://reference.wolfram.com/language/ref/Throw.html</url>
<dl>
<dt>'Throw[`value`]'
<dd> stops evaluation and returns `value` as the value of the nearest \
enclosing 'Catch'.
<dt>'Catch[`value`, `tag`]'
<dd> is caught only by `Catch[expr,form]`, where tag matches form.
</dl>
Using Throw can affect the structure of what is returned by a function:
>> NestList[#^2 + 1 &, 1, 7]
= ...
>> Catch[NestList[If[# > 1000, Throw[#], #^2 + 1] &, 1, 7]]
= 458330
X> Throw[1]
= Null
"""

messages = {
"nocatch": "Uncaught `1` returned to top level.",
}

summary_text = "throw an expression to be caught by a surrounding 'Catch'"

def eval(self, value, evaluation: Evaluation):
"Throw[value_]"
raise WLThrowInterrupt(value)

def eval_with_tag(self, value, tag, evaluation: Evaluation):
"Throw[value_, tag_]"
raise WLThrowInterrupt(value, tag)
21 changes: 8 additions & 13 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,14 @@ def evaluate():
else:
raise
except WLThrowInterrupt as ti:
if ti.tag:
self.exc_result = Expression(
SymbolHold, Expression(SymbolThrow, ti.value, ti.tag)
)
else:
self.exc_result = Expression(
SymbolHold, Expression(SymbolThrow, ti.value)
)
self.message("Throw", "nocatch", self.exc_result)
# except OverflowError:
# print("Catch the overflow")
# self.message("General", "ovfl")
# self.exc_result = Expression(SymbolOverflow)
msg_expr = (
Expression(SymbolThrow, ti.value, ti.tag)
if ti.tag
else Expression(SymbolThrow, ti.value)
)
self.message("Throw", "nocatch", msg_expr)
self.exc_result = Expression(SymbolHold, msg_expr)

except BreakInterrupt:
self.message("Break", "nofdw")
self.exc_result = Expression(SymbolHold, Expression(SymbolBreak))
Expand Down

0 comments on commit 744a59f

Please sign in to comment.