Skip to content

Commit

Permalink
tweak some error messages to be more like WMA...
Browse files Browse the repository at this point in the history
in particular Get::noopen, {First,Last,Most}::normal
  • Loading branch information
rocky committed Sep 25, 2024
1 parent ad7c63b commit 58ed3e8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
35 changes: 20 additions & 15 deletions mathics/builtin/list/eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from mathics.core.builtin import BinaryOperator, Builtin
from mathics.core.convert.expression import to_mathics_list
from mathics.core.convert.python import from_python
from mathics.core.evaluation import Evaluation
from mathics.core.exceptions import (
InvalidLevelspecError,
MessageException,
Expand Down Expand Up @@ -589,24 +590,24 @@ class First(Builtin):
>> First[a + b + c]
= a
>> First[x]
: Nonatomic expression expected.
: Nonatomic expression expected at position 1 in First[x].
= First[x]
>> First[{}]
: {} has zero length and no first element.
= First[{}]
"""

messages = {
"normal": "Nonatomic expression expected.",
"normal": "Nonatomic expression expected at position 1 in `1`.",
"nofirst": "`1` has zero length and no first element.",
}
summary_text = "first element of a list or expression"

def eval(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation, expression: Expression):
"First[expr_]"

if isinstance(expr, Atom):
evaluation.message("First", "normal")
evaluation.message("First", "normal", expression)
return
if len(expr.elements) == 0:
evaluation.message("First", "nofirst", expr)
Expand Down Expand Up @@ -820,24 +821,24 @@ class Last(Builtin):
>> Last[{a, b, c}]
= c
>> Last[x]
: Nonatomic expression expected.
: Nonatomic expression expected at position 1 in Last[x].
= Last[x]
>> Last[{}]
: {} has zero length and no last element.
= Last[{}]
"""

messages = {
"normal": "Nonatomic expression expected.",
"normal": "Nonatomic expression expected at position 1 in `1`.",
"nolast": "`1` has zero length and no last element.",
}
summary_text = "last element of a list or expression"

def eval(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation, expression: Expression):
"Last[expr_]"

if isinstance(expr, Atom):
evaluation.message("Last", "normal")
evaluation.message("Last", "normal", expression)
return
if len(expr.elements) == 0:
evaluation.message("Last", "nolast", expr)
Expand Down Expand Up @@ -906,17 +907,21 @@ class Most(Builtin):
>> Most[a + b + c]
= a + b
>> Most[x]
: Nonatomic expression expected.
: Nonatomic expression expected at position 1 in Most[x].
= Most[x]
"""

messages = {
"normal": "Nonatomic expression expected at position 1 in `1`.",
}

summary_text = "remove the last element"

def eval(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation, expression: Expression):
"Most[expr_]"

if isinstance(expr, Atom):
evaluation.message("Most", "normal")
evaluation.message("Most", "normal", expression)
return
return expr.slice(expr.head, slice(0, -1), evaluation)

Expand Down Expand Up @@ -1395,24 +1400,24 @@ class Rest(Builtin):
>> Rest[a + b + c]
= b + c
>> Rest[x]
: Nonatomic expression expected.
: Nonatomic expression expected at position 1 in Rest[x].
= Rest[x]
>> Rest[{}]
: Cannot take Rest of expression {} with length zero.
= Rest[{}]
"""

messages = {
"normal": "Nonatomic expression expected.",
"normal": "Nonatomic expression expected at position 1 in `1`.",
"norest": "Cannot take Rest of expression `1` with length zero.",
}
summary_text = "remove the first element"

def eval(self, expr, evaluation):
def eval(self, expr, evaluation: Evaluation, expression: Expression):
"Rest[expr_]"

if isinstance(expr, Atom):
evaluation.message("Rest", "normal")
evaluation.message("Rest", "normal", expression)
return
if len(expr.elements) == 0:
evaluation.message("Rest", "norest", expr)
Expand Down
2 changes: 1 addition & 1 deletion mathics/eval/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def eval_Get(path: str, evaluation: Evaluation, trace_fn: Optional[Callable]):
continue
result = query.evaluate(evaluation)
except IOError:
evaluation.message("General", "noopen", path)
evaluation.message("Get", "noopen", path)
return SymbolFailed
except MessageException as e:
e.message(evaluation)
Expand Down

0 comments on commit 58ed3e8

Please sign in to comment.