diff --git a/bin/hy2py b/bin/hy2py index d746d5a..03073fb 100755 --- a/bin/hy2py +++ b/bin/hy2py @@ -4,7 +4,10 @@ from hy.compiler import hy_compile from hy.lex import tokenize import codegen import sys +import ast - -ast = hy_compile(tokenize(open(sys.argv[1], 'r').read())) -print codegen.to_source(ast) +tokens = tokenize(open(sys.argv[1], 'r').read()) +print tokens +_ast = hy_compile(tokens) +print ast.dump(_ast) +print codegen.to_source(_ast) diff --git a/hy/compiler.py b/hy/compiler.py index f212e8b..8882be3 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -63,8 +63,9 @@ class HyASTCompiler(object): tree.reverse() if self.returnable and len(tree) > 0: - el = tree.pop() + el = tree[0] if not isinstance(el, ast.stmt): + el = tree.pop() ret.append(ast.Return(value=el, lineno=el.lineno, col_offset=el.col_offset)) @@ -80,6 +81,30 @@ class HyASTCompiler(object): def compile_raw_list(self, entries): return [self.compile(x) for x in entries] + @builds("assert") + def compile_assert_expression(self, expr): + expr.pop(0) # assert + e = expr.pop(0) + return ast.Assert(test=self.compile(e), + msg=None, + lineno=e.start_line, + col_offset=e.start_column) + + @builds("=") + def compile_compare_op_expression(self, expression): + ops = {"=": ast.Eq} + + inv = expression.pop(0) + op = ops[inv] + ops = [op() for x in range(1, len(expression))] + e = expression.pop(0) + + return ast.Compare(left=self.compile(e), + ops=ops, + comparators=[self.compile(x) for x in expression], + lineno=e.start_line, + col_offset=e.start_column) + @builds("+") @builds("-") @builds("/") diff --git a/hy/models/expression.py b/hy/models/expression.py index 47722b4..7ccac91 100644 --- a/hy/models/expression.py +++ b/hy/models/expression.py @@ -18,7 +18,6 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from hy.models import HyObject from hy.models.list import HyList diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index 7a56553..9c463be 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -2,4 +2,4 @@ (def test_basic_math (fn [] - (assert (+ 2 2) 4))) + (assert (= (+ 2 2) 4))))