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 run(self, vm: VM): quote_false = pop(vm) quote_true = pop(vm) is_true = pop_value(vm) if is_true: vm.run_ast(quote_true.ast) else: vm.run_ast(quote_false.ast)
def run(self, vm: VM): quote = pop(vm) sequence = pop(vm) result = ArrayNode() for entry in sequence: run_node(vm, entry) vm.run_ast(quote.ast) node = pop(vm) result.append(node) push(vm, result)
def run(self, vm: VM): quote = pop(vm) acc = pop(vm) sequence = pop(vm) for entry in sequence: run_node(vm, entry) run_node(vm, acc) vm.run_ast(quote.ast) acc = pop(vm) push(vm, acc)
def run(self, vm: VM): quote = pop(vm) sequence = pop(vm) filtered = ArrayNode() for entry in sequence: run_node(vm, entry) vm.run_ast(quote.ast) result = pop_value(vm) if result: filtered.append(entry) push(vm, filtered)
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
def run(self, vm: VM): quote = pop(vm) is_true = pop_value(vm) if is_true: vm.run_ast(quote.ast)
def run(self, vm: VM): quote = pop(vm) count = pop_value(vm) for _ in range(0, count): vm.run_ast(quote.ast)
def run(self, vm: VM): for name in self.__local_vars[::-1]: vm.add_local(name)
def run(self, vm: VM): quote = vm.stack.pop() vm.run_ast(quote.ast)
def run(self, vm: VM): vm.register_word(self.__name, self)
def run(self, vm: VM): vm.run_word_or_variable(self.__word, self)