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

Fixes a crash with Module[] #584

Merged
merged 2 commits into from
Sep 27, 2016
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
2 changes: 1 addition & 1 deletion mathics/builtin/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def print_rule(rule, up=False, lhs=lambda l: l, rhs=lambda r: r):
evaluation.check_stopped()
if isinstance(rule, Rule):
r = rhs(rule.replace.replace_vars(
{'System`Definition': Expression('HoldForm', Symbol('Definition'))}))
{'System`Definition': Expression('HoldForm', Symbol('Definition'))}, evaluation))
lines.append(Expression('HoldForm', Expression(
up and 'UpSet' or 'Set', lhs(rule.pattern.expr), r)))

Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def apply_named(self, vars, body, args, evaluation):
else:
vars = dict(list(zip((
var.get_name() for var in vars), args[:len(vars)])))
return body.replace_vars(vars)
return body.replace_vars(vars, evaluation)


class Slot(Builtin):
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ def match(self, yield_func, expression, vars, evaluation, **kwargs):
# for new_vars, rest in self.pattern.match(expression, vars,
# evaluation):
def yield_match(new_vars, rest):
test_expr = self.test.replace_vars(new_vars)
test_expr = self.test.replace_vars(new_vars, evaluation)
test_result = test_expr.evaluate(evaluation)
if test_result.is_true():
yield_func(new_vars, rest)
Expand Down
6 changes: 5 additions & 1 deletion mathics/builtin/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Module(Builtin):
= a
>> Module[{a}, Block[{}, a]]
= a$5

#> Module[{n = 3}, Module[{b = n * 5}, b * 7]]
= 105

"""

attributes = ('HoldAll',)
Expand Down Expand Up @@ -194,7 +198,7 @@ def apply(self, vars, expr, evaluation):
if new_def is not None:
evaluation.definitions.set_ownvalue(new_name, new_def)
replace[name] = Symbol(new_name)
new_expr = expr.replace_vars(replace, in_scoping=False)
new_expr = expr.replace_vars(replace, evaluation, in_scoping=False)
result = new_expr.evaluate(evaluation)
return result

Expand Down
2 changes: 1 addition & 1 deletion mathics/core/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _eval_subs(self, old, new):
old, new = from_sympy(old), from_sympy(new)
old_name = old.get_name()
if old_name:
new_expr = self.expr.replace_vars({old_name: new})
new_expr = self.expr.replace_vars({old_name: new}, None)
return SympyExpression(new_expr)
return self

Expand Down
16 changes: 7 additions & 9 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,16 +1146,14 @@ def descend(expr):
expr = Expression(head, *expr.leaves)
return expr, new_applied[0]


def replace_vars(self, vars, options=None,
in_scoping=True, in_function=True):
def replace_vars(self, vars, evaluation, options=None, in_scoping=True, in_function=True):
from mathics.builtin.scoping import get_scoping_vars

if not in_scoping:
if (self.head.get_name() in ('System`Module', 'System`Block', 'System`With') and
len(self.leaves) > 0): # nopep8

scoping_vars = set(name for name, new_def in get_scoping_vars(self.leaves[0]))
scoping_vars = set(name for name, new_def in get_scoping_vars(self.leaves[0], evaluation=evaluation))
"""for var in new_vars:
if var in scoping_vars:
del new_vars[var]"""
Expand All @@ -1177,7 +1175,7 @@ def replace_vars(self, vars, options=None,
body = self.leaves[1]
replacement = {name: Symbol(name + '$') for name in func_params}
func_params = [Symbol(name + '$') for name in func_params]
body = body.replace_vars(replacement, options, in_scoping)
body = body.replace_vars(replacement, evaluation, options, in_scoping)
leaves = [Expression('List', *func_params), body] + \
self.leaves[2:]

Expand All @@ -1186,8 +1184,8 @@ def replace_vars(self, vars, options=None,

return Expression(
self.head.replace_vars(
vars, options=options, in_scoping=in_scoping),
*[leaf.replace_vars(vars, options=options, in_scoping=in_scoping)
vars, evaluation, options=options, in_scoping=in_scoping),
*[leaf.replace_vars(vars, evaluation, options=options, in_scoping=in_scoping)
for leaf in leaves])

def replace_slots(self, slots, evaluation):
Expand Down Expand Up @@ -1325,7 +1323,7 @@ def get_atom_name(self):
def __repr__(self):
return '<%s: %s>' % (self.get_atom_name(), self)

def replace_vars(self, vars, options=None, in_scoping=True):
def replace_vars(self, vars, evaluation, options=None, in_scoping=True):
return self

def replace_slots(self, slots, evaluation):
Expand Down Expand Up @@ -1424,7 +1422,7 @@ def get_sort_key(self, pattern_sort=False):
def same(self, other):
return isinstance(other, Symbol) and self.name == other.name

def replace_vars(self, vars, options={}, in_scoping=True):
def replace_vars(self, vars, evaluation, options={}, in_scoping=True):
assert all(fully_qualified_symbol_name(v) for v in vars)
var = vars.get(self.name, None)
if var is None:
Expand Down
2 changes: 1 addition & 1 deletion mathics/core/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, pattern, replace, system=False):
self.replace = replace

def do_replace(self, vars, options, evaluation):
new = self.replace.replace_vars(vars)
new = self.replace.replace_vars(vars, evaluation)
new.options = options

# if options is a non-empty dict, we need to ensure reevaluation of the whole expression, since 'new' will
Expand Down