From a9b5d851b285f86dd45f4754178d271012762b83 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Sun, 3 Mar 2013 13:10:50 -0500 Subject: [PATCH] Fiddling with more AST testing --- hy/compilers/{ast.py => pyast.py} | 11 +++++++++-- tests/compilers/test_ast.py | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) rename hy/compilers/{ast.py => pyast.py} (86%) diff --git a/hy/compilers/ast.py b/hy/compilers/pyast.py similarity index 86% rename from hy/compilers/ast.py rename to hy/compilers/pyast.py index 0e600a2..4c6635d 100644 --- a/hy/compilers/ast.py +++ b/hy/compilers/pyast.py @@ -25,6 +25,8 @@ from hy.models.symbol import HySymbol from hy.errors import HyError +import ast + class HyCompileError(HyError): pass @@ -56,8 +58,13 @@ class HyASTCompiler(HyCompiler): @builds(HyExpression) def compile_expression(self, expression): - pass + return ast.Call(func=self.compile_symbol(expression[0]), + args=[self.compile(x) for x in expression[1:]], + keywords=[], + starargs=None, + kwargs=None) + @builds(HySymbol) def compile_symbol(self, symbol): - pass + return ast.Name(id=str(symbol), ctx=ast.Load()) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 323c543..163de1b 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -18,11 +18,20 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from hy.compilers.ast import HyASTCompiler +from hy.compilers.pyast import HyASTCompiler from hy.lex import tokenize import ast +def _ast_spotcheck(arg, root, secondary): + if "." in arg: + local, full = arg.split(".", 1) + return _ast_spotcheck(full, + getattr(root, local), + getattr(secondary, local)) + assert getattr(root, arg) == getattr(secondary, arg) + + def test_ast_expression_basics(): """ Ensure basic AST expression conversion works. """ compiler = HyASTCompiler() @@ -39,4 +48,5 @@ def test_ast_expression_basics(): starargs=None, kwargs=None, ) - assert code == tree + _ast_spotcheck("func.id", code, tree) + _ast_spotcheck("id", code.args[0], tree.args[0])