Skip to content

Commit

Permalink
Merge pull request #768 from Mathics3/message-and-svg-tweaking
Browse files Browse the repository at this point in the history
evaluation.message now returns a value - adjust
  • Loading branch information
rocky committed Jan 29, 2023
2 parents 8a0e3e3 + 6b77948 commit de71edc
Show file tree
Hide file tree
Showing 49 changed files with 597 additions and 329 deletions.
2 changes: 1 addition & 1 deletion mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ def eval(self, x, evaluation):

def eval_error(self, x, seqs, evaluation):
"Sign[x_, seqs__]"
return evaluation.message("Sign", "argx", Integer(len(seqs.get_sequence()) + 1))
evaluation.message("Sign", "argx", Integer(len(seqs.get_sequence()) + 1))


class Sum(IterationFunction, SympyFunction):
Expand Down
20 changes: 13 additions & 7 deletions mathics/builtin/atomic/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class RealDigits(Builtin):

def eval_complex(self, n, var, evaluation):
"%(name)s[n_Complex, var___]"
return evaluation.message("RealDigits", "realx", n)
evaluation.message("RealDigits", "realx", n)

def eval_rational_with_base(self, n, b, evaluation):
"%(name)s[n_Rational, b_Integer]"
Expand Down Expand Up @@ -574,7 +574,8 @@ def eval(self, n, evaluation):
# Handling the testcases that throw the error message and return the
# output that doesn't include `base` argument
if isinstance(n, Symbol) and n.name.startswith("System`"):
return evaluation.message("RealDigits", "ndig", n)
evaluation.message("RealDigits", "ndig", n)
return

if n.is_numeric(evaluation):
return self.eval_with_base(n, from_python(10), evaluation)
Expand All @@ -600,14 +601,17 @@ def eval_with_base(self, n, b, evaluation, nr_elements=None, pos=None):
if rational_no:
n = eval_N(n, evaluation)
else:
return evaluation.message("RealDigits", "ndig", expr)
evaluation.message("RealDigits", "ndig", expr)
return
py_n = abs(n.value)

if not py_b > 1:
return evaluation.message("RealDigits", "rbase", py_b)
evaluation.message("RealDigits", "rbase", py_b)
return

if isinstance(py_n, complex):
return evaluation.message("RealDigits", "realx", expr)
evaluation.message("RealDigits", "realx", expr)
return

if isinstance(n, Integer):
display_len = (
Expand Down Expand Up @@ -697,7 +701,8 @@ def eval_with_base_and_length(self, n, b, length, evaluation, pos=None):
elements.append(from_python(pos))
expr = Expression(SymbolRealDigits, n, b, length, *elements)
if not (isinstance(length, Integer) and length.get_int_value() >= 0):
return evaluation.message("RealDigits", "intnm", expr)
evaluation.message("RealDigits", "intnm", expr)
return

return self.eval_with_base(
n, b, evaluation, nr_elements=length.get_int_value(), pos=pos
Expand All @@ -706,9 +711,10 @@ def eval_with_base_and_length(self, n, b, length, evaluation, pos=None):
def eval_with_base_length_and_precision(self, n, b, length, p, evaluation):
"%(name)s[n_?NumericQ, b_Integer, length_, p_]"
if not isinstance(p, Integer):
return evaluation.message(
evaluation.message(
"RealDigits", "intm", Expression(SymbolRealDigits, n, b, length, p)
)
return

return self.eval_with_base_and_length(
n, b, length, evaluation, pos=p.get_int_value()
Expand Down
32 changes: 21 additions & 11 deletions mathics/builtin/atomic/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def _pattern_search(name, string, patt, evaluation, options, matched):
for p in patts:
py_p = to_regex(p, evaluation)
if py_p is None:
return evaluation.message("StringExpression", "invld", p, patt)
evaluation.message("StringExpression", "invld", p, patt)
return
re_patts.append(py_p)

flags = re.MULTILINE
Expand All @@ -171,16 +172,18 @@ def _search(patts, str, flags, matched):
if string.has_form("List", None):
py_s = [s.get_string_value() for s in string.elements]
if any(s is None for s in py_s):
return evaluation.message(
evaluation.message(
name, "strse", Integer1, Expression(Symbol(name), string, patt)
)
return
return to_mathics_list(*[_search(re_patts, s, flags, matched) for s in py_s])
else:
py_s = string.get_string_value()
if py_s is None:
return evaluation.message(
evaluation.message(
name, "strse", Integer1, Expression(Symbol(name), string, patt)
)
return
return _search(re_patts, py_s, flags, matched)


Expand Down Expand Up @@ -649,7 +652,8 @@ def eval_alpha_str(self, chars: List[Any], alpha: String, evaluation):
result.append(self.eval_alpha_str(element, alpha, evaluation))
return ListExpression(*result)
else:
return evaluation.message(self.__class__.__name__, "nas", chars)
evaluation.message(self.__class__.__name__, "nas", chars)
return
return None

def eval(self, chars: List[Any], evaluation):
Expand All @@ -673,7 +677,7 @@ def eval(self, chars: List[Any], evaluation):
result.append(self.eval(element, evaluation))
return ListExpression(*result)
else:
return evaluation.message(self.__class__.__name__, "nas", chars)
evaluation.message(self.__class__.__name__, "nas", chars)
return None


Expand Down Expand Up @@ -757,29 +761,34 @@ def _apply(self, string, rule, n, evaluation, options, cases):
if string.has_form("List", None):
py_strings = [stri.get_string_value() for stri in string.elements]
if None in py_strings:
return evaluation.message(self.get_name(), "strse", Integer1, expr)
evaluation.message(self.get_name(), "strse", Integer1, expr)
return
else:
py_strings = string.get_string_value()
if py_strings is None:
return evaluation.message(self.get_name(), "strse", Integer1, expr)
evaluation.message(self.get_name(), "strse", Integer1, expr)
return

# convert rule
def convert_rule(r):
if r.has_form("Rule", None) and len(r.elements) == 2:
py_s = to_regex(r.elements[0], evaluation)
if py_s is None:
return evaluation.message(
evaluation.message(
"StringExpression", "invld", r.elements[0], r.elements[0]
)
return
py_sp = r.elements[1]
return py_s, py_sp
elif cases:
py_s = to_regex(r, evaluation)
if py_s is None:
return evaluation.message("StringExpression", "invld", r, r)
evaluation.message("StringExpression", "invld", r, r)
return
return py_s, None

return evaluation.message(self.get_name(), "srep", r)
evaluation.message(self.get_name(), "srep", r)
return

if rule.has_form("List", None):
py_rules = [convert_rule(r) for r in rule.elements]
Expand All @@ -796,7 +805,8 @@ def convert_rule(r):
else:
py_n = n.get_int_value()
if py_n is None or py_n < 0:
return evaluation.message(self.get_name(), "innf", Integer(3), expr)
evaluation.message(self.get_name(), "innf", Integer(3), expr)
return

# flags
flags = re.MULTILINE
Expand Down
6 changes: 4 additions & 2 deletions mathics/builtin/binary/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,12 +1016,14 @@ def eval(self, name, n, b, typ, evaluation):
x_py = x.get_int_value()

if x_py is None:
return evaluation.message(SymbolBinaryWrite, "nocoerce", b)
evaluation.message(SymbolBinaryWrite, "nocoerce", b)
return

try:
self.writers[t](stream.io, x_py)
except struct.error:
return evaluation.message(SymbolBinaryWrite, "nocoerce", b)
evaluation.message(SymbolBinaryWrite, "nocoerce", b)
return
i += 1

try:
Expand Down
3 changes: 2 additions & 1 deletion mathics/builtin/colors/color_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def eval(self, colors, u, evaluation: Evaluation):
values = 0.0
use_list = False
if values is None:
return evaluation.message("Blend", "argl", u, ListExpression(colors_orig))
evaluation.message("Blend", "argl", u, ListExpression(colors_orig))
return

if use_list:
return self.do_blend(colors, values).to_expr()
Expand Down
6 changes: 4 additions & 2 deletions mathics/builtin/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def eval(self, vars, expr, evaluation: Evaluation):
"Compile[vars_, expr_]"

if not vars.has_form("List", None):
return evaluation.message("Compile", "invars")
evaluation.message("Compile", "invars")
return

try:
cfunc, args = expression_to_callable_and_args(
Expand Down Expand Up @@ -222,5 +223,6 @@ def eval(self, argnames, expr, code, args, evaluation: Evaluation):
try:
result = code.cfunc(*py_args)
except (TypeError, ctypes.ArgumentError):
return evaluation.message("CompiledFunction", "argerr", args)
evaluation.message("CompiledFunction", "argerr", args)
return
return from_python(result)
15 changes: 10 additions & 5 deletions mathics/builtin/drawing/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def eval(self, functions, x, start, stop, evaluation: Evaluation, options: dict)
if plotpoints == "System`None":
plotpoints = 57
if not (isinstance(plotpoints, int) and plotpoints >= 2):
return evaluation.message(self.get_name(), "ppts", plotpoints)
evaluation.message(self.get_name(), "ppts", plotpoints)
return

# MaxRecursion Option
max_recursion_limit = 15
Expand Down Expand Up @@ -591,9 +592,11 @@ def process_function_and_options(
py_start = start.round_to_float(evaluation)
py_stop = stop.round_to_float(evaluation)
if py_start is None or py_stop is None:
return evaluation.message(self.get_name(), "plln", stop, expr)
evaluation.message(self.get_name(), "plln", stop, expr)
return
if py_start >= py_stop:
return evaluation.message(self.get_name(), "plld", expr_limits)
evaluation.message(self.get_name(), "plld", expr_limits)
return

plotrange_option = self.get_option(options, "PlotRange", evaluation)
plot_range = eval_N(plotrange_option, evaluation).to_python()
Expand Down Expand Up @@ -1595,9 +1598,11 @@ def eval(
py_nmax = nmax.value
py_step = step.value
if py_start is None or py_nmax is None:
return evaluation.message(self.get_name(), "plln", nmax, expr)
evaluation.message(self.get_name(), "plln", nmax, expr)
return
if py_start >= py_nmax:
return evaluation.message(self.get_name(), "plld", expr_limits)
evaluation.message(self.get_name(), "plld", expr_limits)
return

plotrange_option = self.get_option(options, "PlotRange", evaluation)
plot_range = eval_N(plotrange_option, evaluation).to_python()
Expand Down
12 changes: 8 additions & 4 deletions mathics/builtin/exp_structure/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,10 @@ def eval(self, p, expr, n, evaluation: Evaluation):

head_depth = n.get_int_value()
if head_depth is None or head_depth < 0:
return evaluation.message(
evaluation.message(
"Operate", "intnn", Expression(SymbolOperate, p, expr, n), 3
)
return

if head_depth == 0:
# Act like Apply
Expand Down Expand Up @@ -530,10 +531,12 @@ def eval(self, li, f, evaluation: Evaluation):
"SortBy[li_, f_]"

if isinstance(li, Atom):
return evaluation.message("Sort", "normal")
evaluation.message("Sort", "normal")
return
elif li.get_head_name() != "System`List":
expr = Expression(SymbolSortBy, li, f)
return evaluation.message(self.get_name(), "list", expr, 1)
evaluation.message(self.get_name(), "list", expr, 1)
return
else:
keys_expr = Expression(SymbolMap, f, li).evaluate(evaluation) # precompute:
# even though our sort function has only (n log n) comparisons, we should
Expand All @@ -545,7 +548,8 @@ def eval(self, li, f, evaluation: Evaluation):
or len(keys_expr.elements) != len(li.elements)
):
expr = Expression(SymbolSortBy, li, f)
return evaluation.message("SortBy", "func", expr, 2)
evaluation.message("SortBy", "func", expr, 2)
return

keys = keys_expr.elements
raw_keys = li.elements
Expand Down
5 changes: 3 additions & 2 deletions mathics/builtin/exp_structure/size_and_sig.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ByteCount(Builtin):
def eval(self, expression, evaluation: Evaluation):
"ByteCount[expression_]"
if not bytecount_support:
return evaluation.message("ByteCount", "pypy")
evaluation.message("ByteCount", "pypy")
else:
return Integer(count_bytes(expression))

Expand Down Expand Up @@ -202,7 +202,8 @@ def callback(level):

expr = expr.get_sequence()
if len(expr) != 1:
return evaluation.message("LeafCount", "argx", Integer(len(expr)))
evaluation.message("LeafCount", "argx", Integer(len(expr)))
return

walk_levels(expr[0], start=-1, stop=-1, heads=True, callback=callback)
return Integer(len(elements))
3 changes: 2 additions & 1 deletion mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,11 +1880,12 @@ def eval(self, channel, expr, evaluation):
try:
result = result.boxes_to_text(evaluation=evaluation)
except BoxError:
return evaluation.message(
evaluation.message(
"General",
"notboxes",
to_expression("FullForm", result).evaluate(evaluation),
)
return
exprs.append(result)
line = "".join(exprs)
if type(stream) is BytesIO:
Expand Down
12 changes: 7 additions & 5 deletions mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ def eval_makeboxes(self, expr, n, f, evaluation: Evaluation):
try:
val = convert_base(x, base, p)
except ValueError:
return evaluation.message("BaseForm", "basf", n)
evaluation.message("BaseForm", "basf", n)
return

if f is SymbolOutputForm:
return to_boxes(String("%s_%d" % (val, base)), evaluation)
Expand Down Expand Up @@ -313,7 +314,7 @@ def check_DigitBlock(self, value, evaluation: Evaluation):
result = [nleft, nright]
if None not in result:
return result
return evaluation.message(self.get_name(), "dblk", value)
evaluation.message(self.get_name(), "dblk", value)

def check_ExponentFunction(self, value, evaluation: Evaluation):
if value.sameQ(SymbolAutomatic):
Expand Down Expand Up @@ -348,22 +349,23 @@ def check_NumberPoint(self, value, evaluation: Evaluation):
def check_ExponentStep(self, value, evaluation: Evaluation):
result = value.get_int_value()
if result is None or result <= 0:
return evaluation.message(self.get_name(), "estep", "ExponentStep", value)
evaluation.message(self.get_name(), "estep", "ExponentStep", value)
return
return result

def check_SignPadding(self, value, evaluation: Evaluation):
if value.sameQ(SymbolTrue):
return True
elif value.sameQ(SymbolFalse):
return False
return evaluation.message(self.get_name(), "opttf", value)
evaluation.message(self.get_name(), "opttf", value)

def _check_List2str(self, value, msg, evaluation: Evaluation):
if value.has_form("List", 2):
result = [element.get_string_value() for element in value.elements]
if None not in result:
return result
return evaluation.message(self.get_name(), msg, value)
evaluation.message(self.get_name(), msg, value)

def check_NumberSigns(self, value, evaluation: Evaluation):
return self._check_List2str(value, "nsgn", evaluation)
Expand Down
8 changes: 5 additions & 3 deletions mathics/builtin/functional/apply_fns_to_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,14 @@ def eval_n(self, f, expr, n, evaluation):
n = n.get_int_value()

if n is None or n < 0:
return evaluation.message("MapThread", "intnm", full_expr, 3)
evaluation.message("MapThread", "intnm", full_expr, 3)
return

if expr.has_form("List", 0):
return ListExpression()
if not expr.has_form("List", None):
return evaluation.message("MapThread", "list", 2, full_expr)
evaluation.message("MapThread", "list", 2, full_expr)
return

heads = expr.elements

Expand Down Expand Up @@ -473,7 +475,7 @@ class Scan(Builtin):
def eval_invalidlevel(self, f, expr, ls, evaluation, options={}):
"Scan[f_, expr_, ls_, OptionsPattern[Map]]"

return evaluation.message("Map", "level", ls)
evaluation.message("Map", "level", ls)

def eval_level(self, f, expr, ls, evaluation, options={}):
"""Scan[f_, expr_, Optional[Pattern[ls, _?LevelQ], {1}],
Expand Down
Loading

0 comments on commit de71edc

Please sign in to comment.