示例#1
0
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()
示例#2
0
 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)
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
文件: repl.py 项目: kstrempel/pactor
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}")
示例#7
0
    def __run_script(self, script):
        ast = load_script(script)
        vm = VM(ast)
        vm.run()

        return vm
示例#8
0
 def run(self, vm: VM):
     quote = pop(vm)
     is_true = pop_value(vm)
     if is_true:
         vm.run_ast(quote.ast)
示例#9
0
 def run(self, vm: VM):
     quote = pop(vm)
     count = pop_value(vm)
     for _ in range(0, count):
         vm.run_ast(quote.ast)
示例#10
0
 def run(self, vm: VM):
     for name in self.__local_vars[::-1]:
         vm.add_local(name)
示例#11
0
 def run(self, vm: VM):
     quote = vm.stack.pop()
     vm.run_ast(quote.ast)
示例#12
0
 def run(self, vm: VM):
     vm.register_word(self.__name, self)
示例#13
0
 def run(self, vm: VM):
     vm.run_word_or_variable(self.__word, self)