WIP - Added &key support

This commit is contained in:
James King 2013-04-18 17:47:08 -04:00
parent 18ed72136f
commit 70e0d88149
3 changed files with 21 additions and 10 deletions

View File

@ -189,7 +189,6 @@ class HyASTCompiler(object):
return ret return ret
def _parse_lambda_list(self, exprs): def _parse_lambda_list(self, exprs):
""" Return args, keywords, varargs, kwargs from exprs."""
exprs.reverse() exprs.reverse()
args = [] args = []
keywords = [] keywords = []
@ -206,9 +205,11 @@ class HyASTCompiler(object):
"lambda-keyword.".format(repr(expr))) "lambda-keyword.".format(repr(expr)))
if expr == "&rest" and lambda_keyword is None: if expr == "&rest" and lambda_keyword is None:
lambda_keyword = expr lambda_keyword = expr
elif expr == "&optional" and lambda_keyword == "&rest": elif expr == "&optional":
lambda_keyword = expr lambda_keyword = expr
elif expr == "&aux" and lambda_keyword == "&optional": elif expr == "&key":
lambda_keyword = expr
elif expr == "&kwargs":
lambda_keyword = expr lambda_keyword = expr
else: else:
raise HyCompileError("{0} is in an invalid " raise HyCompileError("{0} is in an invalid "
@ -224,12 +225,18 @@ class HyASTCompiler(object):
raise HyCompileError("There can only be one " raise HyCompileError("There can only be one "
"&rest argument") "&rest argument")
varargs = str(expr) varargs = str(expr)
elif lambda_keyword == "&key":
if type(expr) != HyDict:
raise TypeError("FOOBAR")
else:
keywords = [ast.keyword(arg=ast_str(k),
value=self.compile(v))
for k, v in expr.items()]
elif lambda_keyword == "&optional": elif lambda_keyword == "&optional":
# add key to keywords and kwargs, value to kwargs? Look up AST docs you dummy. # not implemented yet.
pass
elif lambda_keyword == "&aux":
# update kwargs with the rest of the passed in keys/vals
pass pass
elif lambda_keyword == "&kwargs":
kwargs = str(expr)
if not kwargs: if not kwargs:
kwargs = None kwargs = None
@ -1106,9 +1113,9 @@ class HyASTCompiler(object):
col_offset=x.start_column) col_offset=x.start_column)
for x in args], for x in args],
vararg=stararg, vararg=stararg,
kwarg=None, kwarg=kwargs,
kwonlyargs=[], kwonlyargs=[],
kw_defaults=[], kw_defaults=keywords,
defaults=[]), defaults=[]),
body=body, body=body,
decorator_list=[]) decorator_list=[])

View File

@ -33,7 +33,7 @@ class HyLambdaListKeyword(HyString):
pass pass
""" """
_valid_types = ["&rest", "&optional", "&kwargs"] _valid_types = ["&rest", "&optional", "&key", "&kwargs"]
def __init__(self, string): def __init__(self, string):
self += string self += string

View File

@ -339,6 +339,10 @@ def test_lambda_list_keywords():
hy_compile(tokenize("(fn (x &rest xs) (print xs))")) hy_compile(tokenize("(fn (x &rest xs) (print xs))"))
cant_compile("(fn (x &rest xs &rest ys) (print xs))") cant_compile("(fn (x &rest xs &rest ys) (print xs))")
def test_lambda_list_keywords_optional():
""" Ensure we can compile functions with &optional."""
hy_compile(tokenize("(fn (x &optional (foo True)) foo)"))
def test_ast_unicode_strings(): def test_ast_unicode_strings():
"""Ensure we handle unicode strings correctly""" """Ensure we handle unicode strings correctly"""