From 7417789ce0f06b4f67512d47a9816182189e8f65 Mon Sep 17 00:00:00 2001 From: James King Date: Tue, 9 Apr 2013 15:23:50 -0400 Subject: [PATCH] WIP - Adding Call support and FuncionDef The test is still broken, there are print statements... this is an ongoing WIP and will get squashed before submitted the PR. --- bin/hy2py | 21 ++++++++-------- hy/compiler.py | 50 ++++++++++++++++++++++++++++++++++++- tests/compilers/test_ast.py | 7 ++++-- 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/bin/hy2py b/bin/hy2py index 9894ec5..253e796 100755 --- a/bin/hy2py +++ b/bin/hy2py @@ -1,22 +1,23 @@ #!/usr/bin/env python +from __future__ import print_function from hy.importer import (import_file_to_ast, import_file_to_module, import_file_to_hst) -import astor.codegen +#import astor.codegen import sys import ast hst = import_file_to_hst(sys.argv[1]) -print hst -print "" -print "" +print(hst) +print("") +print("") _ast = import_file_to_ast(sys.argv[1]) -print "" -print "" -print ast.dump(_ast) -print "" -print "" -print astor.codegen.to_source(_ast) +print("") +print("") +print(ast.dump(_ast)) +print("") +print("") +#print(astor.codegen.to_source(_ast)) import_file_to_module("", sys.argv[1]) diff --git a/hy/compiler.py b/hy/compiler.py index 7161f85..2260b20 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -176,7 +176,52 @@ class HyASTCompiler(object): def _parse_lambda_list(self, exprs): """ Return args, keywords, starargs, kwargs from exprs.""" - return [self.compile(expr) for expr in exprs], [], None, None + exprs.reverse() + args = [] + keywords = [] + starargs = None + kwargs = {} + lambda_keyword = None + + while exprs: + expr = exprs.pop() + + if isinstance(expr, HyLambdaListKeyword): + if expr not in expr._valid_types: + 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 + elif expr == "&aux" and lambda_keyword == "&optional": + lambda_keyword = expr + else: + raise HyCompileError("{0} is in an invalid " + "position.".format(repr(expr))) + # we don't actually care about this token, so we set + # our state and continue to the next token... + continue + + if lambda_keyword is None: + args.append(self.compile(expr)) + elif lambda_keyword == "&rest": + print("The keyword is &rest, the expr is {0}".format(expr)) + if starargs: + raise HyCompileError("There can only be one " + "&rest argument") + starargs = self.compile(expr) + elif lambda_keyword == "&optional": + # add key to keywords and kwargs, value to kwargs? Look up AST docs you dummy. + pass + elif lambda_keyword == "&aux": + # update kwargs with the rest of the passed in keys/vals + pass + + if not kwargs: + kwargs = None + return args, keywords, starargs, kwargs @builds(list) def compile_raw_list(self, entries): @@ -842,6 +887,9 @@ class HyASTCompiler(object): expression.start_line, expression.start_column) + print("HELLO", sig) + # TODO: Parse those args here + ret = ast.FunctionDef( name=name, lineno=expression.start_line, diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index d239a7f..246ee9a 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -328,5 +328,8 @@ def test_ast_tuple(): def test_lambda_list_keywords_rest(): - code = hy_compile(tokenize("(defun foo (x &rest xs))")) - assert False == True + src = ("(defun foo (x &rest xs) (print xs))\n" + "(foo 1 2 3 4 5)") + code = hy_compile(tokenize(src)) + print(ast.dump(code)) + assert False