Very broken.

This commit is contained in:
Paul R. Tagliamonte 2013-03-03 19:40:46 -05:00
parent ef9007c229
commit 9e16fb4ca0
8 changed files with 66 additions and 14 deletions

View File

@ -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

View File

@ -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

13
hy/importer.py Normal file
View File

@ -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

View File

@ -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

View File

@ -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",

View File

View File

@ -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")

View File

@ -0,0 +1,4 @@
; This is a comment. It shall be ignored by the parser.
(print "Foo")