diff --git a/bin/hy2py b/bin/hy2py index 03073fb..e2db066 100755 --- a/bin/hy2py +++ b/bin/hy2py @@ -1,13 +1,20 @@ #!/usr/bin/env python -from hy.compiler import hy_compile -from hy.lex import tokenize +from hy.importer import (import_file_to_ast, import_file_to_module, + import_file_to_hst) import codegen import sys import ast -tokens = tokenize(open(sys.argv[1], 'r').read()) -print tokens -_ast = hy_compile(tokens) + +hst = import_file_to_hst(sys.argv[1]) +print hst +print "" +print "" +_ast = import_file_to_ast(sys.argv[1]) +print "" +print "" print ast.dump(_ast) +print "" +print "" print codegen.to_source(_ast) diff --git a/hy/compiler.py b/hy/compiler.py index 6d29418..fbdf630 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -204,6 +204,8 @@ class HyASTCompiler(object): @builds("fn") def compile_fn_expression(self, expression): + print expression, type(expression), expression.start_line + expression.pop(0) # fn ret_status = self.returnable diff --git a/hy/core/bootstrap.py b/hy/core/bootstrap.py index 383d082..dc04919 100644 --- a/hy/core/bootstrap.py +++ b/hy/core/bootstrap.py @@ -20,8 +20,13 @@ from hy.macros import macro +from hy.models.expression import HyExpression +from hy.models.symbol import HySymbol -@macro("noop") -def noop_macro(tree): - return tree +@macro("defn") +def defn_macro(tree): + # (defn foo [x] ...) + # (def foo (fn [x] ...)) + return HyExpression([HySymbol("def"), + tree[1], HyExpression([HySymbol("fn")] + tree[2:])]) diff --git a/hy/importer.py b/hy/importer.py index 413d302..43dcdd3 100644 --- a/hy/importer.py +++ b/hy/importer.py @@ -1,15 +1,31 @@ # from hy.lex import tokenize +from hy.macros import process from hy.compiler import hy_compile +import hy.core.bootstrap # language bits. + + import imp import sys import os +def import_file_to_hst(fpath): + tree = tokenize(open(fpath, 'r').read()) + tree = process(tree) + return tree + + +def import_file_to_ast(fpath): + tree = import_file_to_hst(fpath) + ast = hy_compile(tree) + return ast + + def import_file_to_module(name, fpath): - ast = hy_compile(tokenize(open(fpath, 'r').read())) + ast = import_file_to_ast(fpath) mod = imp.new_module(name) mod.__file__ = fpath eval(compile(ast, fpath, "exec"), mod.__dict__) diff --git a/hy/macros.py b/hy/macros.py index 7ea5f66..da1e225 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -20,6 +20,7 @@ from hy.models.expression import HyExpression from hy.models.list import HyList +from hy.models import HyObject _hy_macros = {} @@ -31,20 +32,6 @@ def macro(name): return _ + def process(tree): - if isinstance(tree, HyExpression): - it = iter(tree) - fn = next(it) - - body = [process(x) for x in it] - - if fn in _hy_macros: - m = _hy_macros[fn] - return m(HyExpression([fn] + body)) - - return [fn] + body - - if isinstance(tree, HyList): - return HyList([process(x) for x in tree]) - return tree diff --git a/hy/models/__init__.py b/hy/models/__init__.py index e462db4..bf03b76 100644 --- a/hy/models/__init__.py +++ b/hy/models/__init__.py @@ -25,3 +25,12 @@ class HyObject(object): Hy lexing Objects at once. """ pass + + def replace(self, other): + if isinstance(other, HyObject): + self.start_line = other.start_line + self.end_line = other.end_line + self.start_column = other.start_column + self.end_column = other.end_column + else: + raise TypeError("Can't replace a non Hy object with a Hy object") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index bab2624..c4b808d 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1,14 +1,14 @@ ; -(def test_lists (fn [] +(defn test_lists [] "NATIVE: test lists work right" - (assert (= [1 2 3 4] (+ [1 2] [3 4]))))) + (assert (= [1 2 3 4] (+ [1 2] [3 4])))) -(def test_for_loop (fn [] +(defn test_for_loop [] "NATIVE: test for loops?" (def count 0) (for [x [1 2 3 4 5]] (def count (+ count x))) - (assert (= count 15)))) + (assert (= count 15)))