Remove the implementation of apply
This commit is contained in:
parent
97ecb0b553
commit
784a44601b
2
NEWS
2
NEWS
@ -2,6 +2,8 @@ Changes from 0.13.0
|
|||||||
|
|
||||||
[ Language Changes ]
|
[ Language Changes ]
|
||||||
* `yield-from` is no longer supported under Python 2
|
* `yield-from` is no longer supported under Python 2
|
||||||
|
* `apply` has been replaced with Python-style unpacking operators `#*` and
|
||||||
|
`#**` (e.g., `(f #* args #** kwargs)`)
|
||||||
* Single-character "sharp macros" changed to "tag macros", which can have
|
* Single-character "sharp macros" changed to "tag macros", which can have
|
||||||
longer names
|
longer names
|
||||||
* Periods are no longer allowed in keywords
|
* Periods are no longer allowed in keywords
|
||||||
|
109
hy/compiler.py
109
hy/compiler.py
@ -1587,115 +1587,6 @@ class HyASTCompiler(object):
|
|||||||
generators=expr.generators)
|
generators=expr.generators)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@builds("apply")
|
|
||||||
@checkargs(min=1, max=3)
|
|
||||||
def compile_apply_expression(self, expr):
|
|
||||||
expr.pop(0) # apply
|
|
||||||
|
|
||||||
ret = Result()
|
|
||||||
|
|
||||||
fun = expr.pop(0)
|
|
||||||
|
|
||||||
# We actually defer the compilation of the function call to
|
|
||||||
# @builds(HyExpression), allowing us to work on method calls
|
|
||||||
call = HyExpression([fun]).replace(fun)
|
|
||||||
|
|
||||||
if isinstance(fun, HySymbol) and fun.startswith("."):
|
|
||||||
# (apply .foo lst) needs to work as lst[0].foo(*lst[1:])
|
|
||||||
if not expr:
|
|
||||||
raise HyTypeError(
|
|
||||||
expr, "apply of a method needs to have an argument"
|
|
||||||
)
|
|
||||||
|
|
||||||
# We need to grab the arguments, and split them.
|
|
||||||
|
|
||||||
# Assign them to a variable if they're not one already
|
|
||||||
if type(expr[0]) == HyList:
|
|
||||||
if len(expr[0]) == 0:
|
|
||||||
raise HyTypeError(
|
|
||||||
expr, "apply of a method needs to have an argument"
|
|
||||||
)
|
|
||||||
call.append(expr[0].pop(0))
|
|
||||||
else:
|
|
||||||
if isinstance(expr[0], HySymbol):
|
|
||||||
tempvar = expr[0]
|
|
||||||
else:
|
|
||||||
tempvar = HySymbol(self.get_anon_var()).replace(expr[0])
|
|
||||||
assignment = HyExpression(
|
|
||||||
[HySymbol("setv"), tempvar, expr[0]]
|
|
||||||
).replace(expr[0])
|
|
||||||
|
|
||||||
# and add the assignment to our result
|
|
||||||
ret += self.compile(assignment)
|
|
||||||
|
|
||||||
# The first argument is the object on which to call the method
|
|
||||||
# So we translate (apply .foo args) to (.foo (get args 0))
|
|
||||||
call.append(HyExpression(
|
|
||||||
[HySymbol("get"), tempvar, HyInteger(0)]
|
|
||||||
).replace(tempvar))
|
|
||||||
|
|
||||||
# We then pass the other arguments to the function
|
|
||||||
expr[0] = HyExpression(
|
|
||||||
[HySymbol("cut"), tempvar, HyInteger(1)]
|
|
||||||
).replace(expr[0])
|
|
||||||
|
|
||||||
ret += self.compile(call)
|
|
||||||
|
|
||||||
if not isinstance(ret.expr, ast.Call):
|
|
||||||
raise HyTypeError(
|
|
||||||
fun, "compiling the application of `{}' didn't return a "
|
|
||||||
"function call, but `{}'".format(fun, type(ret.expr).__name__)
|
|
||||||
)
|
|
||||||
if ret.expr.starargs or ret.expr.kwargs:
|
|
||||||
raise HyTypeError(
|
|
||||||
expr, "compiling the function application returned a function "
|
|
||||||
"call with arguments"
|
|
||||||
)
|
|
||||||
|
|
||||||
if expr:
|
|
||||||
stargs = expr.pop(0)
|
|
||||||
if stargs is not None:
|
|
||||||
stargs = self.compile(stargs)
|
|
||||||
if PY35:
|
|
||||||
stargs_expr = stargs.force_expr
|
|
||||||
ret.expr.args.append(
|
|
||||||
ast.Starred(stargs_expr, ast.Load(),
|
|
||||||
lineno=stargs_expr.lineno,
|
|
||||||
col_offset=stargs_expr.col_offset)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
ret.expr.starargs = stargs.force_expr
|
|
||||||
ret = stargs + ret
|
|
||||||
|
|
||||||
if expr:
|
|
||||||
kwargs = expr.pop(0)
|
|
||||||
if isinstance(kwargs, HyDict):
|
|
||||||
new_kwargs = []
|
|
||||||
for k, v in kwargs.items():
|
|
||||||
if isinstance(k, HySymbol):
|
|
||||||
pass
|
|
||||||
elif isinstance(k, HyString):
|
|
||||||
k = HyString(hy_symbol_mangle(str_type(k))).replace(k)
|
|
||||||
elif isinstance(k, HyKeyword):
|
|
||||||
sym = hy_symbol_mangle(str_type(k)[2:])
|
|
||||||
k = HyString(sym).replace(k)
|
|
||||||
new_kwargs += [k, v]
|
|
||||||
kwargs = HyDict(new_kwargs).replace(kwargs)
|
|
||||||
|
|
||||||
kwargs = self.compile(kwargs)
|
|
||||||
if PY35:
|
|
||||||
kwargs_expr = kwargs.force_expr
|
|
||||||
ret.expr.keywords.append(
|
|
||||||
ast.keyword(None, kwargs_expr,
|
|
||||||
lineno=kwargs_expr.lineno,
|
|
||||||
col_offset=kwargs_expr.col_offset)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
ret.expr.kwargs = kwargs.force_expr
|
|
||||||
ret = kwargs + ret
|
|
||||||
|
|
||||||
return ret
|
|
||||||
|
|
||||||
@builds("not")
|
@builds("not")
|
||||||
@builds("~")
|
@builds("~")
|
||||||
@checkargs(1)
|
@checkargs(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user