Unmangle in compile_expression before build_method

This ensures that e.g. the symbols "~" and "hyx_XtildeX" in the root position will both appear as "~" to the build method.
This commit is contained in:
Kodi Arfer 2018-06-21 10:56:19 -07:00
parent fca2eb93b0
commit bd675a5db6

View File

@ -954,7 +954,6 @@ class HyASTCompiler(object):
@special(["lfor", "sfor", "gfor"], [_loopers, FORM]) @special(["lfor", "sfor", "gfor"], [_loopers, FORM])
@special(["dfor"], [_loopers, brackets(FORM, FORM)]) @special(["dfor"], [_loopers, brackets(FORM, FORM)])
def compile_comprehension(self, expr, root, parts, final): def compile_comprehension(self, expr, root, parts, final):
root = unmangle(ast_str(root))
node_class = { node_class = {
"for": asty.For, "for": asty.For,
"lfor": asty.ListComp, "lfor": asty.ListComp,
@ -1136,7 +1135,7 @@ class HyASTCompiler(object):
else: else:
assignments = [(k, v or k) for k, v in kids] assignments = [(k, v or k) for k, v in kids]
if root == HySymbol("import"): if root == "import":
ast_module = ast_str(module, piecewise=True) ast_module = ast_str(module, piecewise=True)
module = ast_module.lstrip(".") module = ast_module.lstrip(".")
level = len(ast_module) - len(module) level = len(ast_module) - len(module)
@ -1158,7 +1157,7 @@ class HyASTCompiler(object):
for k, v in assignments] for k, v in assignments]
ret += node( ret += node(
expr, module=module or None, names=names, level=level) expr, module=module or None, names=names, level=level)
else: # root == HySymbol("require") else: # root == "require"
__import__(module) __import__(module)
require(module, self.module_name, require(module, self.module_name,
assignments=assignments, prefix=prefix) assignments=assignments, prefix=prefix)
@ -1170,8 +1169,9 @@ class HyASTCompiler(object):
ops = {"and": (ast.And, "True"), ops = {"and": (ast.And, "True"),
"or": (ast.Or, "None")} "or": (ast.Or, "None")}
opnode, default = ops[operator] opnode, default = ops[operator]
osym = expr[0]
if len(args) == 0: if len(args) == 0:
return asty.Name(operator, id=default, ctx=ast.Load()) return asty.Name(osym, id=default, ctx=ast.Load())
elif len(args) == 1: elif len(args) == 1:
return self.compile(args[0]) return self.compile(args[0])
ret = Result() ret = Result()
@ -1179,16 +1179,16 @@ class HyASTCompiler(object):
if any(value.stmts for value in values): if any(value.stmts for value in values):
# Compile it to an if...else sequence # Compile it to an if...else sequence
var = self.get_anon_var() var = self.get_anon_var()
name = asty.Name(operator, id=var, ctx=ast.Store()) name = asty.Name(osym, id=var, ctx=ast.Store())
expr_name = asty.Name(operator, id=var, ctx=ast.Load()) expr_name = asty.Name(osym, id=var, ctx=ast.Load())
temp_variables = [name, expr_name] temp_variables = [name, expr_name]
def make_assign(value, node=None): def make_assign(value, node=None):
positioned_name = asty.Name( positioned_name = asty.Name(
node or operator, id=var, ctx=ast.Store()) node or osym, id=var, ctx=ast.Store())
temp_variables.append(positioned_name) temp_variables.append(positioned_name)
return asty.Assign( return asty.Assign(
node or operator, targets=[positioned_name], value=value) node or osym, targets=[positioned_name], value=value)
current = root = [] current = root = []
for i, value in enumerate(values): for i, value in enumerate(values):
@ -1210,7 +1210,7 @@ class HyASTCompiler(object):
ret = sum(root, ret) ret = sum(root, ret)
ret += Result(expr=expr_name, temp_variables=temp_variables) ret += Result(expr=expr_name, temp_variables=temp_variables)
else: else:
ret += asty.BoolOp(operator, ret += asty.BoolOp(osym,
op=opnode(), op=opnode(),
values=[value.force_expr for value in values]) values=[value.force_expr for value in values])
return ret return ret
@ -1255,8 +1255,6 @@ class HyASTCompiler(object):
@special(["**", "//", "<<", ">>"], [times(2, Inf, FORM)]) @special(["**", "//", "<<", ">>"], [times(2, Inf, FORM)])
@special(["%", "^"], [times(2, 2, FORM)]) @special(["%", "^"], [times(2, 2, FORM)])
def compile_maths_expression(self, expr, root, args): def compile_maths_expression(self, expr, root, args):
root = unmangle(ast_str(root))
if len(args) == 0: if len(args) == 0:
# Return the identity element for this operator. # Return the identity element for this operator.
return asty.Num(expr, n=long_type( return asty.Num(expr, n=long_type(
@ -1293,7 +1291,7 @@ class HyASTCompiler(object):
@special(list(a_ops.keys()), [FORM, FORM]) @special(list(a_ops.keys()), [FORM, FORM])
def compile_augassign_expression(self, expr, root, target, value): def compile_augassign_expression(self, expr, root, target, value):
op = self.a_ops[unmangle(ast_str(root))] op = self.a_ops[root]
target = self._storeize(target, self.compile(target)) target = self._storeize(target, self.compile(target))
ret = self.compile(value) ret = self.compile(value)
return ret + asty.AugAssign( return ret + asty.AugAssign(
@ -1301,8 +1299,8 @@ class HyASTCompiler(object):
@special("setv", [many(FORM + FORM)]) @special("setv", [many(FORM + FORM)])
def compile_def_expression(self, expr, root, pairs): def compile_def_expression(self, expr, root, pairs):
if len(pairs) == 0: if not pairs:
return asty.Name(root, id='None', ctx=ast.Load()) return asty.Name(expr, id='None', ctx=ast.Load())
result = Result() result = Result()
for pair in pairs: for pair in pairs:
result += self._compile_assign(*pair) result += self._compile_assign(*pair)
@ -1535,7 +1533,7 @@ class HyASTCompiler(object):
@special(["eval-and-compile", "eval-when-compile"], [many(FORM)]) @special(["eval-and-compile", "eval-when-compile"], [many(FORM)])
def compile_eval_and_compile(self, expr, root, body): def compile_eval_and_compile(self, expr, root, body):
new_expr = HyExpression([HySymbol("do").replace(root)]).replace(expr) new_expr = HyExpression([HySymbol("do").replace(expr[0])]).replace(expr)
if self.module_name not in self._namespaces: if self.module_name not in self._namespaces:
# Initialize a compile-time namespace for this module. # Initialize a compile-time namespace for this module.
self._namespaces[self.module_name] = { self._namespaces[self.module_name] = {
@ -1594,7 +1592,7 @@ class HyASTCompiler(object):
expression[0], expression[0],
e.msg.replace("<EOF>", "end of form"))) e.msg.replace("<EOF>", "end of form")))
return Result() + build_method( return Result() + build_method(
self, expression, expression[0], *parse_tree) self, expression, unmangle(sfn), *parse_tree)
if fn.startswith("."): if fn.startswith("."):
# (.split "test test") -> "test test".split() # (.split "test test") -> "test test".split()