Adding in a Hy REPL

This commit is contained in:
Paul R. Tagliamonte 2013-03-14 17:36:38 -04:00
parent 8a9ffa05ae
commit ff37c47c43
3 changed files with 63 additions and 15 deletions

5
TODO
View File

@ -12,6 +12,8 @@
- New Builtins:
+ While
+ Pass
- New macros:
+ loop
@ -37,3 +39,6 @@
- hyrepl
- pyc compiler
- core tests (odd? even? true? false?) which
build out to a lambda / function / expr

44
bin/hy
View File

@ -1,6 +1,46 @@
#!/usr/bin/env python
from hy.importer import import_file_to_module
import sys
import ast
import code
import readline
from hy.lex.states import Idle, LexException
from hy.lex.machine import Machine
from hy.compiler import hy_compile
from hy.core import process
import_file_to_module("<STDIN>", sys.argv[1])
_machine = Machine(Idle, 1, 0)
class HyREPL(code.InteractiveConsole):
def runsource(self, source, filename='<input>', symbol='single'):
global _machine
try:
_machine.process(source + "\n")
except LexException as e:
_machine = Machine(Idle, 1, 0)
self.showsyntaxerror(filename)
return False
if type(_machine.state) != Idle:
_machine = Machine(Idle, 1, 0)
return True
tokens = process(_machine.nodes)
_machine = Machine(Idle, 1, 0)
_ast = hy_compile(tokens, root=ast.Interactive)
code = compile(_ast, filename, symbol)
self.runcode(code)
return False
sys.ps1 = "=> "
sys.ps2 = "... "
hr = HyREPL()
hr.interact("""
Hi! Welcome to Hy!
""")

View File

@ -507,8 +507,11 @@ class HyASTCompiler(object):
values=vals)
def hy_compile(tree):
def hy_compile(tree, root=None):
" Compile a HyObject tree into a Python AST tree. "
compiler = HyASTCompiler()
ret = ast.Module(body=compiler._mangle_branch(compiler.compile(tree)))
tlo = root
if root is None:
tlo = ast.Module
ret = tlo(body=compiler._mangle_branch(compiler.compile(tree)))
return ret