#!/usr/bin/env python
# -*- coding: utf-8 -*-

# === haiku.shell ---------------------------------------------------------===
# Copyright © 2011-2012, RokuSigma Inc. and contributors. See AUTHORS for more
# details.
#
# Some rights reserved.
#
# Redistribution and use in source and binary forms of the software as well as
# documentation, with or without modification, are permitted provided that the
# following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#  * The names of the copyright holders or contributors may not be used to
#    endorse or promote products derived from this software without specific
#    prior written permission.
#
# THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND
# DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ===----------------------------------------------------------------------===

# Haiku language, environment mapping
from haiku.environment import Environment
# Haiku language, interpreter
from haiku.interpreter import BaseInterpreter
# Haiku language, type definitions
from haiku.types import *

from haiku.builtin import builtinEnvironment
globalEnvironment = Environment(parent=builtinEnvironment)

import sys
import traceback
from haiku.pickle import CanonicalExpressionPickler, SimpleExpressionPickler
def repl(prompt=u'haiku> '):
  "A read-eval-print loop for haiku."
  canonical   = CanonicalExpressionPickler()
  simple      = SimpleExpressionPickler()
  interpreter = BaseInterpreter(pickler=simple, environment=globalEnvironment)

  while True:
    try:
      expression = interpreter.read(raw_input(prompt))
    except EOFError:
      sys.stdout.write('\n'); return 0
    except KeyboardInterrupt:
      sys.stdout.write('\n'); continue
    except Exception, e:
      traceback.print_exc();  continue

    try:
      result = interpreter.evaluate(expression)
      print u"INPUT:  %s" % canonical.dumps(*expression).encode('string_escape')
      print u"DEBUG:  %s" % Unicode(expression)
      print u"RESULT: %s" % Unicode(result)
      print u"OUTPUT: %s" % simple.dumps(result)[1:-1]
    except Exception, e:
      traceback.print_exc(); continue

if __name__ == "__main__":
  sys.exit(repl())

# ===----------------------------------------------------------------------===
# End of File
# ===----------------------------------------------------------------------===
