def main(args): """Main entry point allowing external calls Args: args ([str]): command line parameter list """ args = parse_args(args) setup_logging(args.loglevel) if args.file: try: ast = load_file(args.file) vm = VM(ast) vm.run() except InnerPactorRuntimeError as e: print(f"Runtime error in {args.file} at [{e.line}:{e.column}]") with open(args.file) as f: line = f.readline() for _ in range(1, e.line): line = f.readline() print("> " + line[:-1]) print("> " + e.error_arrow) print("> " + e.message) except SyntaxException as e: print(f"Syntax Error: {e.message}") print(f"{e.error_arrow}") except Exception as e: print(f"Error: {e}") if (args.stack): print(vm.stack) else: repl()
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