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.
This commit is contained in:
James King 2013-04-09 15:23:50 -04:00
parent 4d90123506
commit 7417789ce0
3 changed files with 65 additions and 13 deletions

View File

@ -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("<STDIN>", sys.argv[1])

View File

@ -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,

View File

@ -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