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

Match WMA nocatch msg better and check... #1092

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
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