[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:
James King 2013-04-03 11:39:31 -04:00
parent c200b4e3d1
commit dceaad716a
2 changed files with 16 additions and 4 deletions

View File

@ -22,6 +22,7 @@ from hy.errors import HyError
from hy.models.expression import HyExpression from hy.models.expression import HyExpression
from hy.models.integer import HyInteger from hy.models.integer import HyInteger
from hy.models.lambdalist import HyLambdaListKeyword
from hy.models.string import HyString from hy.models.string import HyString
from hy.models.symbol import HySymbol from hy.models.symbol import HySymbol
from hy.models.list import HyList from hy.models.list import HyList
@ -80,6 +81,10 @@ class HyASTCompiler(object):
ret.reverse() ret.reverse()
return ret 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) @builds(list)
def compile_raw_list(self, entries): def compile_raw_list(self, entries):
return [self.compile(x) for x in entries] return [self.compile(x) for x in entries]
@ -481,11 +486,13 @@ class HyASTCompiler(object):
if expression[0].startswith("."): if expression[0].startswith("."):
return self.compile_dotted_expression(expression) return self.compile_dotted_expression(expression)
args, keywords, starargs, kwargs = self._parse_lambda_list(expression[1:])
return ast.Call(func=self.compile(fn), return ast.Call(func=self.compile(fn),
args=[self.compile(x) for x in expression[1:]], args=args,
keywords=[], keywords=keywords,
starargs=None, starargs=starargs,
kwargs=None, kwargs=kwargs,
lineno=expression.start_line, lineno=expression.start_line,
col_offset=expression.start_column) col_offset=expression.start_column)

View File

@ -101,3 +101,8 @@ def test_ast_tuple():
""" Ensure tuples work. """ """ Ensure tuples work. """
code = hy_compile(tokenize("(, 1 2 3)")).body[0].value code = hy_compile(tokenize("(, 1 2 3)")).body[0].value
assert type(code) == ast.Tuple assert type(code) == ast.Tuple
def test_lambda_list_keywords_rest():
code = hy_compile(tokenize("(defun foo (x &rest xs))"))
assert False == True