From ff37c47c4391129c28b274aa8dd0a3e344704ddd Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Thu, 14 Mar 2013 17:36:38 -0400 Subject: [PATCH] Adding in a Hy REPL --- TODO | 27 ++++++++++++++++----------- bin/hy | 44 ++++++++++++++++++++++++++++++++++++++++++-- hy/compiler.py | 7 +++++-- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 3950767..0914a33 100644 --- a/TODO +++ b/TODO @@ -9,31 +9,36 @@ + Use (def) to imply global foo - - New Builtins: + - New Builtins: + While - - New macros: + + Pass + + - New macros: + loop - - nREPL type thing: + - nREPL type thing: + SocketIO endpoint + Socket endpoint + hyde (hy-dee-e - thanks arno) - - Builtin type checking (@params(HyExpression, HyList, HyExpression)) + - Builtin type checking (@params(HyExpression, HyList, HyExpression)) - - Better Exception handling + - Better Exception handling + linenos in the dump + except IOError as E: # <-- implement - - Tuples + - Tuples - - List / Dict comprehensions + - List / Dict comprehensions - - var-arity [x y & z] + - var-arity [x y & z] - - car / cdr / first / rest + - car / cdr / first / rest - - hyrepl + - hyrepl - - pyc compiler + - pyc compiler + + - core tests (odd? even? true? false?) which + build out to a lambda / function / expr diff --git a/bin/hy b/bin/hy index 0a9af67..24496c3 100755 --- a/bin/hy +++ b/bin/hy @@ -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("", sys.argv[1]) +_machine = Machine(Idle, 1, 0) + + +class HyREPL(code.InteractiveConsole): + def runsource(self, source, filename='', 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! + +""") diff --git a/hy/compiler.py b/hy/compiler.py index 9b7941f..15b5832 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -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