diff --git a/mathics/builtin/procedural.py b/mathics/builtin/procedural.py index 89ad0d839..9d1eef551 100644 --- a/mathics/builtin/procedural.py +++ b/mathics/builtin/procedural.py @@ -506,6 +506,47 @@ def eval(self, expr, rules, evaluation): # return unevaluated Switch when no pattern matches +class Throw(Builtin): + """ + :WMA link: + https://reference.wolfram.com/language/ref/Throw.html + +
+
'Throw[`value`]' +
stops evaluation and returns `value` as the value of the nearest \ + enclosing 'Catch'. + +
'Catch[`value`, `tag`]' +
is caught only by `Catch[expr,form]`, where tag matches form. +
+ + 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): """ @@ -609,43 +650,3 @@ def eval(self, test, body, evaluation): except ReturnInterrupt as e: return e.expr return SymbolNull - - -class Throw(Builtin): - """ - :WMA link: - https://reference.wolfram.com/language/ref/Throw.html - -
-
'Throw[`value`]' -
stops evaluation and returns `value` as the value of the nearest \ - enclosing 'Catch'. - -
'Catch[`value`, `tag`]' -
is caught only by `Catch[expr,form]`, where tag matches form. -
- - 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) diff --git a/mathics/core/evaluation.py b/mathics/core/evaluation.py index 3d132a5dc..4ef49b534 100644 --- a/mathics/core/evaluation.py +++ b/mathics/core/evaluation.py @@ -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))