From 20805fc7b29749282d9d05240bbf39e9225f54af Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Mon, 4 Mar 2013 19:12:57 -0500 Subject: [PATCH] tweaking the ast. --- Makefile | 5 +++++ hy/compiler.py | 3 ++- hy/compilers/__init__.py | 3 --- hy/compilers/pyast.py | 35 ++++++++++++++++++++++++++++------- tests/compilers/test_ast.py | 8 ++++---- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index a34f10e..4e3c552 100644 --- a/Makefile +++ b/Makefile @@ -16,3 +16,8 @@ test: flake: flake8 hy + +clear: + clear + +d: clear dev diff --git a/hy/compiler.py b/hy/compiler.py index 7c54e23..db8c2cd 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -18,4 +18,5 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from hy.compilers.pyast import hy_compile + +from hy.compilers.pyast import hy_compile # NOQA diff --git a/hy/compilers/__init__.py b/hy/compilers/__init__.py index e8b1e34..1102ece 100644 --- a/hy/compilers/__init__.py +++ b/hy/compilers/__init__.py @@ -24,9 +24,6 @@ from abc import ABCMeta, abstractmethod class HyCompiler(object): __metaclass__ = ABCMeta - def __init__(self): - pass - @abstractmethod def compile(self, tree): pass diff --git a/hy/compilers/pyast.py b/hy/compilers/pyast.py index 1ca3e28..77fe77b 100644 --- a/hy/compilers/pyast.py +++ b/hy/compilers/pyast.py @@ -46,12 +46,34 @@ def builds(_type): class HyASTCompiler(HyCompiler): + + def __init__(self): + self.returnable = False + def compile(self, tree): - for _type in _compile_table: + for _type in _compile_table: if type(tree) == _type: return _compile_table[_type](self, tree) - raise HyCompileError("Unknown type - `%s'" % (str(type(tree)))) + raise HyCompileError("Unknown type - `%s'" % (str(type(tree)))) + + def _mangle_branch(self, tree): + ret = [] + tree.reverse() + + if self.returnable: + el = tree.pop() + if not isinstance(el, ast.stmt): + ret.append(ast.Return(value=el, + lineno=el.lineno, + col_offset=el.col_offset)) + ret += [ast.Expr(value=el, + lineno=el.lineno, + col_offset=el.col_offset + ) if not isinstance(el, ast.stmt) else el for el in tree] + + ret.reverse() + return ret @builds(list) def compile_raw_list(self, entries): @@ -75,13 +97,12 @@ class HyASTCompiler(HyCompiler): @builds(HyString) def compile_string(self, string): - return ast.Str(s=string) - - -compiler = HyASTCompiler() + return ast.Str(s=str(string), lineno=string.start_line, + col_offset=string.start_column) def hy_compile(tree): " Compile a HyObject tree into a Python AST tree. " - ret = ast.Module(body=compiler.compile(tree)) + compiler = HyASTCompiler() + ret = ast.Module(body=compiler._mangle_branch(compiler.compile(tree))) return ret diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index e84a6e6..aa03325 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -44,7 +44,7 @@ def test_ast_bad_type(): def test_ast_expression_basics(): """ Ensure basic AST expression conversion works. """ code = hy_compile(tokenize("(foo bar)")).body[0] - tree = ast.Call( + tree = ast.Expr(value=ast.Call( func=ast.Name( id="foo", ctx=ast.Load(), @@ -55,6 +55,6 @@ def test_ast_expression_basics(): keywords=[], starargs=None, kwargs=None, - ) - _ast_spotcheck("func.id", code, tree) - _ast_spotcheck("id", code.args[0], tree.args[0]) + )) + + _ast_spotcheck("value.func.id", code, tree)