[WIP] Added lambda list keyword parsing step
This allows us to translate lisp argument lists to Python ones. (defun foo (x y &rest z &optional {foo 3} &aux kwargs)) translates roughly to: def foo(x, y, *z, foo=3, **kwargs): pass
This commit is contained in:
parent
c200b4e3d1
commit
dceaad716a
@ -22,6 +22,7 @@ from hy.errors import HyError
|
||||
|
||||
from hy.models.expression import HyExpression
|
||||
from hy.models.integer import HyInteger
|
||||
from hy.models.lambdalist import HyLambdaListKeyword
|
||||
from hy.models.string import HyString
|
||||
from hy.models.symbol import HySymbol
|
||||
from hy.models.list import HyList
|
||||
@ -80,6 +81,10 @@ class HyASTCompiler(object):
|
||||
ret.reverse()
|
||||
return ret
|
||||
|
||||
def _parse_lambda_list(self, exprs):
|
||||
""" Return args, keywords, starargs, kwargs from exprs."""
|
||||
return [self.compile(expr) for expr in exprs], [], None, None
|
||||
|
||||
@builds(list)
|
||||
def compile_raw_list(self, entries):
|
||||
return [self.compile(x) for x in entries]
|
||||
@ -481,11 +486,13 @@ class HyASTCompiler(object):
|
||||
if expression[0].startswith("."):
|
||||
return self.compile_dotted_expression(expression)
|
||||
|
||||
args, keywords, starargs, kwargs = self._parse_lambda_list(expression[1:])
|
||||
|
||||
return ast.Call(func=self.compile(fn),
|
||||
args=[self.compile(x) for x in expression[1:]],
|
||||
keywords=[],
|
||||
starargs=None,
|
||||
kwargs=None,
|
||||
args=args,
|
||||
keywords=keywords,
|
||||
starargs=starargs,
|
||||
kwargs=kwargs,
|
||||
lineno=expression.start_line,
|
||||
col_offset=expression.start_column)
|
||||
|
||||
|
@ -101,3 +101,8 @@ def test_ast_tuple():
|
||||
""" Ensure tuples work. """
|
||||
code = hy_compile(tokenize("(, 1 2 3)")).body[0].value
|
||||
assert type(code) == ast.Tuple
|
||||
|
||||
|
||||
def test_lambda_list_keywords_rest():
|
||||
code = hy_compile(tokenize("(defun foo (x &rest xs))"))
|
||||
assert False == True
|
||||
|
Loading…
x
Reference in New Issue
Block a user