hy/bin/hy
2013-03-15 13:00:28 -04:00

61 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python
import hy
import sys
if len(sys.argv) > 1:
from hy.importer import import_file_to_module
sys.argv.pop(0)
import_file_to_module("__main__", sys.argv[0])
sys.exit(0) # right?
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
_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("{appname} {version}".format(
appname=hy.__appname__,
version=hy.__version__
))