#!/usr/bin/env python

# Twisted, the Framework of Your Internet
# Copyright (C) 2001 Matthew W. Lefkowitz
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of version 2.1 of the GNU Lesser General Public
# License as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

usage_message = """
Usage: eco

Run a command-line interpreter for ECO lisp.
"""

### Twisted Preamble
# This makes sure that users don't have to set up their environment
# specially in order to run these programs from bin/.
import sys,os,string

if string.find(os.path.abspath(sys.argv[0]),'Twisted') != -1:
    sys.path.append(os.path.dirname(
        os.path.dirname(os.path.abspath(sys.argv[0]))))
### end of preamble

from twisted.eco import eco, sexpy
import traceback

def interpret():
    """Run a command-line intepreter for ECO."""
    continuation = 0
    parser = sexpy.SymbolicExpressionReceiver()
    parser.connectionMade()
    l = []
    parser.symbolicExpressionReceived = l.append
    env = (eco.ValueEnvironment(), eco.FunctionEnvironment())
    
    while 1:
        if continuation:
            sys.stdout.write("... ")
        else:
            sys.stdout.write(">>> ")
        line = sys.stdin.readline()
        if not line:
            sys.stdout.write("\n")
            return
        try:
            parser.dataReceived(line)
            if l:
                value = l.pop()
                sys.stdout.write("%s\n" % eco.evalExp(eco.consify(value), env))
                continuation = 0
            else:
                continuation = 1
        except:
            traceback.print_exc()


if __name__ == '__main__':
    interpret()
