From 9ff3b128b49e168d7d831af8eda54918501bed82 Mon Sep 17 00:00:00 2001 From: James King Date: Thu, 11 Apr 2013 12:00:27 -0400 Subject: [PATCH] Got &rest working, tests pass Did a little house cleaning in lex states.py too and started removing stupid print() statements. --- hy/compiler.py | 12 +++++------- hy/lex/states.py | 8 ++++---- tests/compilers/test_ast.py | 10 ++++------ 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 9e4ee66..c77750b 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -175,11 +175,11 @@ class HyASTCompiler(object): return ret def _parse_lambda_list(self, exprs): - """ Return args, keywords, starargs, kwargs from exprs.""" + """ Return args, keywords, varargs, kwargs from exprs.""" exprs.reverse() args = [] keywords = [] - starargs = None + varargs = None kwargs = {} lambda_keyword = None @@ -191,7 +191,6 @@ class HyASTCompiler(object): raise HyCompileError("{0} is not a valid " "lambda-keyword.".format(repr(expr))) if expr == "&rest" and lambda_keyword is None: - print("Found &rest") lambda_keyword = expr elif expr == "&optional" and lambda_keyword == "&rest": lambda_keyword = expr @@ -207,11 +206,10 @@ class HyASTCompiler(object): if lambda_keyword is None: args.append(expr) elif lambda_keyword == "&rest": - print("The keyword is &rest, the expr is {0}".format(expr)) - if starargs: + if varargs: raise HyCompileError("There can only be one " "&rest argument") - starargs = str(expr) + varargs = str(expr) elif lambda_keyword == "&optional": # add key to keywords and kwargs, value to kwargs? Look up AST docs you dummy. pass @@ -221,7 +219,7 @@ class HyASTCompiler(object): if not kwargs: kwargs = None - return args, keywords, starargs, kwargs + return args, keywords, varargs, kwargs @builds(list) def compile_raw_list(self, entries): diff --git a/hy/lex/states.py b/hy/lex/states.py index 721c6c0..4c7781a 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -167,7 +167,7 @@ class ListeyThing(State): return Idle if char in ")]}": - raise LexException("Unexpected closing character: `%s'" % (char)) + raise LexException("Unexpected closing character: `{0}'".format(char)) if char in WHITESPACE: self.commit() @@ -248,7 +248,7 @@ class String(State): self.nodes.append("\"") return - raise LexException("Unknown modifier: `%s'" % (char)) + raise LexException("Unknown modifier: `{0}'".format(char)) if char == "\"": return Idle @@ -292,7 +292,7 @@ class Idle(State): if char in WHITESPACE: return - raise LexException("Unknown char (Idle state): `%s`" % (char)) + raise LexException("Unknown char (Idle state): `{0}`".format(char)) class Comment(State): @@ -327,4 +327,4 @@ class Hash(State): if char == "!": return Comment - raise LexException("Unknown char (Hash state): `%s`" % (char)) + raise LexException("Unknown char (Hash state): `{0}'".format(char)) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 51b7e35..2e76da4 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -328,9 +328,7 @@ def test_ast_tuple(): assert type(code) == ast.Tuple -def test_lambda_list_keywords_rest(): - src = ("(defun foo (x &rest xs) (print xs))\n" - "(foo 1 2 3 4 5)") - code = hy_compile(tokenize(src)) - print(dump(code)) - assert False +def test_lambda_list_keywords(): + """ Ensure we can compile functions with lambda list keywords.""" + hy_compile(tokenize("(fn (x &rest xs) (print xs))")) + cant_compile("(fn (x &rest xs &rest ys) (print xs))")