Exemple #1
0
def repl():
    session = PromptSession(message=">>> ",
                            key_bindings=bindings,
                            auto_suggest=AutoSuggestFromHistory(),
                            complete_while_typing=True,
                            completer=pactor_completer,
                            history=file_history)
    print(f"Pactor REPL v0.5")
    print(f"Type ? for more information")
    vm = VM(Ast())
    while True:
        try:
            line = session.prompt()
            if line == '?':
                print("REPL Commands:")
                print(" .  - remove from stack")
                print(" .. - reset stack")
                print(" :  - prints all symbols")
            elif line == '.':
                vm.stack.pop()
                print_stack(vm)
            elif line == '..':
                vm.stack.clear()
                print_stack(vm)
            elif line == ':':
                for symbol in SYMBOLS.keys():
                    print(':' + symbol)
            else:
                ast = load_script(line)
                vm.run_ast(ast)
                pactor_completer.words = set(
                    list(pactor_completer.words) + list(vm.words.keys()))
                print_stack(vm)
        except InnerPactorRuntimeError as e:
            print("*** " + e.error_arrow)
            print(f"*** Runtime Error: {e.message}")
            print_stack(vm)
        except SyntaxException as e:
            print("*** " + e.error_arrow)
            print(f"*** Syntax Error: {e.message}")
        except Exception as e:
            print(f"Error: {e}")
    def __run_script(self, script):
        ast = load_script(script)
        vm = VM(ast)
        vm.run()

        return vm