Beispiel #1
0
def run_interactive():
    _, interpreter = virtual_machine("()")

    while True:
        line = stdin_readline(":> ")

        if len(line) == 0 or line.strip() == "exit":
            writeln()
            return 0

        try:
            for expr in lex_and_parse(line):
                code = expr.compile(CodeContext())
                process = interpreter.add_process(code)
                interpreter.interpret()

                if process.finished_with_error:
                    print "Error:", process.result.__str__()
                    print
                    print "Code object:"
                    print
                    print code.debug_repr()
                else:
                    if process.result != NIL:
                        print process.result.__str__()

        except ParsingError as e:
            ewriteln("Parse error.")
            if e.message:
                ewriteln(e.message)
            continue

    return 0
Beispiel #2
0
def show_ast(path):
    with open(path) as f:
        try:
            for expr in lex_and_parse(f.read()):
                print expr.__str__()
        except ParsingError as e:
            ewriteln("Parse error.")
            if e.message:
                ewriteln(e.message)
            return 1

    return 0
Beispiel #3
0
def compile_file(path):
    with open(path) as f:
        try:
            ast = lex_and_parse_as_root(f.read())
            code = ast.compile(CodeContext())
            code.finalize()
        except ParsingError as e:
            ewriteln("Parse error.")
            if e.message:
                ewriteln(e.message)
            return 1

    print code.debug_repr()

    return 0
Beispiel #4
0
def parse_arg_with_file_param(command, path):
    if not os.path.exists(path):
        ewriteln("`%s` not found!\n" % path)
        return 1

    if command in ["-a", "--ast"]:
        return show_ast(path)
    elif command in ["-f", "--filename"]:
        return run_script(path)
    elif command in ["-c", "--compile"]:
        return compile_file(path)
    # elif command in ["-s", "--snapshot"]:
    #     return run_script(path)

    ewriteln("Unknown command `%s`!" % command)
    return 1
Beispiel #5
0
def run_script(path):
    with open(path) as f:
        process, interpreter = virtual_machine(f.read())

    if process.finished_with_error:
        ewrite("Error: ")
        ewriteln(process.result.__str__())
        ewriteln("\n")
        ewriteln("CodeContext debug:")
        ewriteln(process.frame.code_context.debug_repr())

        return 1

    return 0
Beispiel #6
0
def parse_args(argv):
    if len(argv) == 1:
        return run_interactive()

    elif len(argv) == 2:
        if argv[1] in ["-h", "--help"]:
            print_help(argv[0])
            return 0

        if argv[1] in ["-v", "--version"]:
            print "tSelf", VERSION
            print get_compiler_info()
            return 0

        elif argv[1].startswith("-"):
            ewriteln(
                "%s probably requires a parameter. See --help for details!" %
                argv[1])
            return 1

        elif not os.path.exists(argv[1]):
            ewriteln("Unrecognized option `%s`!" % argv[1])
            return 1

        else:
            return run_script(argv[1])

    elif len(argv) == 3:
        return parse_arg_with_file_param(argv[1], argv[2])

    else:
        ewriteln("Unknown arguments `%s`!" % str(argv[1:]))
        return 1

    return 0
Beispiel #7
0
def run_stdlib(interpreter, stdlib_source):
    stdlib_ast = lex_and_parse_as_root(stdlib_source)
    code = stdlib_ast.compile(CodeContext())

    stdlib_process = interpreter.add_process(code.finalize())
    interpreter.interpret()

    if stdlib_process.finished_with_error:
        ewriteln("Couldn't initialize stdlib:")
        ewriteln(stdlib_process.result.__str__())
        ewriteln("\n")

        return False

    return True
Beispiel #8
0
def print_help(fn):
    ewriteln("Usage:")
    ewriteln("\t%s [-h, -v] [-f FN] [-c FN] [-a FN]" % fn)
    ewriteln("")
    ewriteln("\t-f FN, --filename FN")
    ewriteln("\t\tRun `FN` as a tinySelf script.")
    ewriteln("")
    # ewriteln("\t-s FN, --snapshot FN")
    # ewriteln("\t\tRun memory snapshot `FN`.")
    # ewriteln("")
    ewriteln("\t-h, --help")
    ewriteln("\t\tShow this help.")
    ewriteln("")
    ewriteln("\t-v, --version")
    ewriteln("\t\tShow version and compiler information.")
    ewriteln("")
    ewriteln("\tSCRIPT_PATH")
    ewriteln("\t\tRun script `SCRIPT_PATH`.")
    ewriteln("")
    ewriteln("Debug options:")
    ewriteln("")
    ewriteln("\t-c FN, --compile FN")
    ewriteln("\t\tCompile FN, output bytecode to the stdout.")
    ewriteln("")
    ewriteln("\t-a FN, --ast FN")
    ewriteln("\t\tShow AST of the `FN`.")
    ewriteln("")