Rename variables in @builds_model(HyExpression)

This commit is contained in:
Kodi Arfer 2018-07-24 09:41:57 -07:00
parent 271f2846dc
commit 0f85331c81

View File

@ -1539,98 +1539,98 @@ class HyASTCompiler(object):
else Result()) else Result())
@builds_model(HyExpression) @builds_model(HyExpression)
def compile_expression(self, expression): def compile_expression(self, expr):
# Perform macro expansions # Perform macro expansions
expression = macroexpand(expression, self) expr = macroexpand(expr, self)
if not isinstance(expression, HyExpression): if not isinstance(expr, HyExpression):
# Go through compile again if the type changed. # Go through compile again if the type changed.
return self.compile(expression) return self.compile(expr)
if not expression: if not expr:
raise HyTypeError( raise HyTypeError(
expression, "empty expressions are not allowed at top level") expr, "empty expressions are not allowed at top level")
fn = expression[0] root = expr[0]
func = None func = None
if isinstance(fn, HySymbol): if isinstance(root, HySymbol):
# First check if `fn` is a special operator, unless it has an # First check if `root` is a special operator, unless it has an
# `unpack-iterable` in it, since Python's operators (`+`, # `unpack-iterable` in it, since Python's operators (`+`,
# etc.) can't unpack. An exception to this exception is that # etc.) can't unpack. An exception to this exception is that
# tuple literals (`,`) can unpack. # tuple literals (`,`) can unpack.
sfn = ast_str(fn) sroot = ast_str(root)
if (sfn in _special_form_compilers or sfn in _bad_roots) and ( if (sroot in _special_form_compilers or sroot in _bad_roots) and (
sfn == mangle(",") or sroot == mangle(",") or
not any(is_unpack("iterable", x) for x in expression[1:])): not any(is_unpack("iterable", x) for x in expr[1:])):
if sfn in _bad_roots: if sroot in _bad_roots:
raise HyTypeError( raise HyTypeError(
expression, expr,
"The special form '{}' is not allowed here".format(fn)) "The special form '{}' is not allowed here".format(root))
# `sfn` is a special operator. Get the build method and # `sroot` is a special operator. Get the build method and
# pattern-match the arguments. # pattern-match the arguments.
build_method, pattern = _special_form_compilers[sfn] build_method, pattern = _special_form_compilers[sroot]
try: try:
parse_tree = pattern.parse(expression[1:]) parse_tree = pattern.parse(expr[1:])
except NoParseError as e: except NoParseError as e:
raise HyTypeError( raise HyTypeError(
expression[min(e.state.pos + 1, len(expression) - 1)], expr[min(e.state.pos + 1, len(expr) - 1)],
"parse error for special form '{}': {}".format( "parse error for special form '{}': {}".format(
expression[0], expr[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, unmangle(sfn), *parse_tree) self, expr, unmangle(sroot), *parse_tree)
if fn.startswith("."): if root.startswith("."):
# (.split "test test") -> "test test".split() # (.split "test test") -> "test test".split()
# (.a.b.c x) -> (.c (. x a b)) -> x.a.b.c() # (.a.b.c x) -> (.c (. x a b)) -> x.a.b.c()
# Get the method name (the last named attribute # Get the method name (the last named attribute
# in the chain of attributes) # in the chain of attributes)
attrs = [HySymbol(a).replace(fn) for a in fn.split(".")[1:]] attrs = [HySymbol(a).replace(root) for a in root.split(".")[1:]]
fn = attrs.pop() root = attrs.pop()
# Get the object we're calling the method on # Get the object we're calling the method on
# (extracted with the attribute access DSL) # (extracted with the attribute access DSL)
i = 1 i = 1
if len(expression) != 2: if len(expr) != 2:
# If the expression has only one object, # If the expression has only one object,
# always use that as the callee. # always use that as the callee.
# Otherwise, hunt for the first thing that # Otherwise, hunt for the first thing that
# isn't a keyword argument or its value. # isn't a keyword argument or its value.
while i < len(expression): while i < len(expr):
if isinstance(expression[i], HyKeyword): if isinstance(expr[i], HyKeyword):
# Skip the keyword argument and its value. # Skip the keyword argument and its value.
i += 1 i += 1
else: else:
# Use expression[i]. # Use expr[i].
break break
i += 1 i += 1
else: else:
raise HyTypeError(expression, raise HyTypeError(expr,
"attribute access requires object") "attribute access requires object")
func = self.compile(HyExpression( func = self.compile(HyExpression(
[HySymbol(".").replace(fn), expression.pop(i)] + [HySymbol(".").replace(root), expr.pop(i)] +
attrs)) attrs))
# And get the method # And get the method
func += asty.Attribute(fn, func += asty.Attribute(root,
value=func.force_expr, value=func.force_expr,
attr=ast_str(fn), attr=ast_str(root),
ctx=ast.Load()) ctx=ast.Load())
if not func: if not func:
func = self.compile(fn) func = self.compile(root)
# An exception for pulling together keyword args is if we're doing # An exception for pulling together keyword args is if we're doing
# a typecheck, eg (type :foo) # a typecheck, eg (type :foo)
with_kwargs = fn not in ( with_kwargs = root not in (
"type", "HyKeyword", "keyword", "name", "keyword?", "identity") "type", "HyKeyword", "keyword", "name", "keyword?", "identity")
args, ret, keywords, oldpy_star, oldpy_kw = self._compile_collect( args, ret, keywords, oldpy_star, oldpy_kw = self._compile_collect(
expression[1:], with_kwargs, oldpy_unpack=True) expr[1:], with_kwargs, oldpy_unpack=True)
return func + ret + asty.Call( return func + ret + asty.Call(
expression, func=func.expr, args=args, keywords=keywords, expr, func=func.expr, args=args, keywords=keywords,
starargs=oldpy_star, kwargs=oldpy_kw) starargs=oldpy_star, kwargs=oldpy_kw)
@builds_model(HyInteger, HyFloat, HyComplex) @builds_model(HyInteger, HyFloat, HyComplex)