From 9e16fb4ca0df3fc01a1b92a5f99f59e8bcf9e78d Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Sun, 3 Mar 2013 19:40:46 -0500 Subject: [PATCH] Very broken. --- hy/compiler.py | 10 +--------- hy/compilers/pyast.py | 18 ++++++++++++++++-- hy/importer.py | 13 +++++++++++++ hy/lex/states.py | 20 ++++++++++++++++++++ tests/compilers/test_ast.py | 6 +++--- tests/importer/__init__.py | 0 tests/importer/test_importer.py | 9 +++++++++ tests/resources/importer/basic.hy | 4 ++++ 8 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 hy/importer.py create mode 100644 tests/importer/__init__.py create mode 100644 tests/importer/test_importer.py create mode 100644 tests/resources/importer/basic.hy diff --git a/hy/compiler.py b/hy/compiler.py index 4052435..7c54e23 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -18,12 +18,4 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from hy.compilers.pyast import HyASTCompiler - - -compiler = HyASTCompiler() - - -def compile(tree): - " Compile a HyObject tree into a Python AST tree. " - return compiler.compile(tree) +from hy.compilers.pyast import hy_compile diff --git a/hy/compilers/pyast.py b/hy/compilers/pyast.py index 1f0ad6e..1ca3e28 100644 --- a/hy/compilers/pyast.py +++ b/hy/compilers/pyast.py @@ -23,6 +23,7 @@ from hy.errors import HyError from hy.models.expression import HyExpression from hy.models.symbol import HySymbol +from hy.models.string import HyString import ast @@ -46,11 +47,11 @@ def builds(_type): class HyASTCompiler(HyCompiler): 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.") + raise HyCompileError("Unknown type - `%s'" % (str(type(tree)))) @builds(list) def compile_raw_list(self, entries): @@ -71,3 +72,16 @@ class HyASTCompiler(HyCompiler): return ast.Name(id=str(symbol), ctx=ast.Load(), lineno=symbol.start_line, col_offset=symbol.start_column) + + @builds(HyString) + def compile_string(self, string): + return ast.Str(s=string) + + +compiler = HyASTCompiler() + + +def hy_compile(tree): + " Compile a HyObject tree into a Python AST tree. " + ret = ast.Module(body=compiler.compile(tree)) + return ret diff --git a/hy/importer.py b/hy/importer.py new file mode 100644 index 0000000..e8de9ab --- /dev/null +++ b/hy/importer.py @@ -0,0 +1,13 @@ +# + +from hy.lex import tokenize +from hy.compiler import hy_compile +import imp + + +def import_file_to_module(name, fpath): + ast = hy_compile(tokenize(open(fpath, 'r').read())) + mod = imp.new_module(name) + mod.__file__ = fpath + eval(compile(ast, fpath, "exec"), mod.__dict__) + return mod diff --git a/hy/lex/states.py b/hy/lex/states.py index 48e917e..aff80bd 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -192,7 +192,27 @@ class Idle(State): if char == "(": return Expression + if char == ";": + return Comment + if char in WHITESPACE: return raise LexException("Unknown char (Idle state): `%s`" % (char)) + + +class Comment(State): + """ + Comment state. + """ + + def process(self, char): + """ + State transitions: + + - \n - Idle + - (default) - disregard. + """ + + if char == "\n": + return Idle diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 7c26106..e84a6e6 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -19,7 +19,7 @@ # DEALINGS IN THE SOFTWARE. from hy.compilers.pyast import HyCompileError -from hy.compiler import compile +from hy.compiler import hy_compile from hy.lex import tokenize import ast @@ -35,7 +35,7 @@ def _ast_spotcheck(arg, root, secondary): def test_ast_bad_type(): try: - compile("foo") + hy_compile("foo") assert True == False except HyCompileError: pass @@ -43,7 +43,7 @@ def test_ast_bad_type(): def test_ast_expression_basics(): """ Ensure basic AST expression conversion works. """ - code = compile(tokenize("(foo bar)"))[0] + code = hy_compile(tokenize("(foo bar)")).body[0] tree = ast.Call( func=ast.Name( id="foo", diff --git a/tests/importer/__init__.py b/tests/importer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/importer/test_importer.py b/tests/importer/test_importer.py new file mode 100644 index 0000000..8bc325b --- /dev/null +++ b/tests/importer/test_importer.py @@ -0,0 +1,9 @@ +from hy.importer import import_file_to_module + + +# import_file_to_module + + +def test_basics(): + module = import_file_to_module("basic", + "tests/resources/importer/basic.hy") diff --git a/tests/resources/importer/basic.hy b/tests/resources/importer/basic.hy new file mode 100644 index 0000000..425099b --- /dev/null +++ b/tests/resources/importer/basic.hy @@ -0,0 +1,4 @@ +; This is a comment. It shall be ignored by the parser. + + +(print "Foo")