Adding in a Hy REPL
This commit is contained in:
parent
8a9ffa05ae
commit
ff37c47c43
5
TODO
5
TODO
@ -12,6 +12,8 @@
|
|||||||
- New Builtins:
|
- New Builtins:
|
||||||
+ While
|
+ While
|
||||||
|
|
||||||
|
+ Pass
|
||||||
|
|
||||||
- New macros:
|
- New macros:
|
||||||
+ loop
|
+ loop
|
||||||
|
|
||||||
@ -37,3 +39,6 @@
|
|||||||
- hyrepl
|
- hyrepl
|
||||||
|
|
||||||
- pyc compiler
|
- pyc compiler
|
||||||
|
|
||||||
|
- core tests (odd? even? true? false?) which
|
||||||
|
build out to a lambda / function / expr
|
||||||
|
44
bin/hy
44
bin/hy
@ -1,6 +1,46 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from hy.importer import import_file_to_module
|
|
||||||
import sys
|
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!
|
||||||
|
|
||||||
|
""")
|
||||||
|
@ -507,8 +507,11 @@ class HyASTCompiler(object):
|
|||||||
values=vals)
|
values=vals)
|
||||||
|
|
||||||
|
|
||||||
def hy_compile(tree):
|
def hy_compile(tree, root=None):
|
||||||
" Compile a HyObject tree into a Python AST tree. "
|
" Compile a HyObject tree into a Python AST tree. "
|
||||||
compiler = HyASTCompiler()
|
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
|
return ret
|
||||||
|
Loading…
x
Reference in New Issue
Block a user