hy/bin/hy

61 lines
1.2 KiB
Plaintext
Raw Normal View History

2013-03-03 16:26:17 -05:00
#!/usr/bin/env python
2013-03-14 21:03:33 -04:00
import hy
2013-03-06 22:09:13 -05:00
import sys
2013-03-14 21:03:33 -04:00
2013-03-15 12:38:44 -04:00
2013-03-14 21:03:33 -04:00
if len(sys.argv) > 1:
from hy.importer import import_file_to_module
2013-03-15 13:00:28 -04:00
sys.argv.pop(0)
import_file_to_module("__main__", sys.argv[0])
2013-03-14 21:03:33 -04:00
sys.exit(0) # right?
2013-03-14 17:36:38 -04:00
import ast
import code
import readline
2013-03-14 19:44:33 -04:00
2013-03-14 17:36:38 -04:00
from hy.lex.states import Idle, LexException
from hy.lex.machine import Machine
from hy.compiler import hy_compile
from hy.core import process
2013-03-06 22:09:13 -05:00
2013-03-15 12:38:44 -04:00
2013-03-14 17:36:38 -04:00
_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
2013-03-15 12:38:44 -04:00
2013-03-14 17:36:38 -04:00
sys.ps1 = "=> "
sys.ps2 = "... "
2013-03-15 12:38:44 -04:00
2013-03-14 17:36:38 -04:00
hr = HyREPL()
2013-03-14 19:44:33 -04:00
hr.interact("{appname} {version}".format(
appname=hy.__appname__,
version=hy.__version__
))