# Path: util.py
# STDIN = 0
# 
# STDOUT = 1
# 
# STDERR = 2
# 
# def read_file(path):
#     assert isinstance(path, str)
#     fd = os.open(path, os.O_RDONLY, 0777)
#     try:
#         data = ""
#         frame = os.read(fd, frame_size)
#         while frame != "":
#             data += frame
#             frame = os.read(fd, frame_size)
#     finally:
#         os.close(fd)
#     return data.decode('utf-8')
# 
# def write(fd, message):
#     assert isinstance(message, unicode)
#     os.write(fd, message.encode('utf-8'))


from rpython.config.translationoption import get_combined_translation_config
from util import STDIN, STDOUT, STDERR, read_file, write
    from rpython.jit.codewriter.policy import JitPolicy
import api
import base
import evaluator.loader
import space
import sys, os
    import green
#                write(STDOUT, exp.repr() + u"\n")
#        else:
#            status = batch(arg)
#            if status != 0:
#                return status
#    return 0

def entry_point(argv):
    if config.translation.continuation:
        green.process.init(config)
    api.init(argv)
    try:
        for arg in argv[1:]:
            module = space.Module(u'main', {}, extends=base.module)
            program = evaluator.loader.from_file(arg)
            result = program.call([module])
            os.write(1, (result.repr() + u'\n').encode('utf-8'))
    except space.Error as error:
        print_traceback(error)
        return 1
    return 0

def print_traceback(error):
    out = u""
    if len(error.stacktrace) > 0:
        out = u"Traceback:\n"
    for frame, start, stop in reversed(error.stacktrace):
        out += u"    %s: %s %s\n" % (
            frame.module.name, start.repr(), stop.repr())
    out += error.__class__.__name__.decode('utf-8')

# Path: util.py
# STDIN = 0
# 
# STDOUT = 1
# 
# STDERR = 2
# 
# def read_file(path):
#     assert isinstance(path, str)
#     fd = os.open(path, os.O_RDONLY, 0777)
#     try:
#         data = ""
#         frame = os.read(fd, frame_size)
#         while frame != "":
#             data += frame
#             frame = os.read(fd, frame_size)
#     finally:
#         os.close(fd)
#     return data.decode('utf-8')
# 
# def write(fd, message):
#     assert isinstance(message, unicode)
#     os.write(fd, message.encode('utf-8'))


# Path: main.py
from rpython.config.translationoption import get_combined_translation_config
from util import STDIN, STDOUT, STDERR, read_file, write
    from rpython.jit.codewriter.policy import JitPolicy
import api
import base
import evaluator.loader
import space
import sys, os
    import green

#                write(STDOUT, exp.repr() + u"\n")
#        else:
#            status = batch(arg)
#            if status != 0:
#                return status
#    return 0

def entry_point(argv):
    if config.translation.continuation:
        green.process.init(config)
    api.init(argv)
    try:
        for arg in argv[1:]:
            module = space.Module(u'main', {}, extends=base.module)
            program = evaluator.loader.from_file(arg)
            result = program.call([module])
            os.write(1, (result.repr() + u'\n').encode('utf-8'))
    except space.Error as error:
        print_traceback(error)
        return 1
    return 0

def print_traceback(error):
    out = u""
    if len(error.stacktrace) > 0:
        out = u"Traceback:\n"
    for frame, start, stop in reversed(error.stacktrace):
        out += u"    %s: %s %s\n" % (
            frame.module.name, start.repr(), stop.repr())
    out += error.__class__.__name__.decode('utf-8')

    write(STDERR, out + u": " + error.message + u"\n")