Exemplo n.º 1
0
 def test_run_status(self):
     program = tokenize(['1000 A=3:ERROR'])
     executor = Executor(program)
     try:
         executor.run_program()
     except:
         pass
     self.assertEqual(RunStatus.END_ERROR_INTERNAL, executor._run)
Exemplo n.º 2
0
 def test_trace(self):
     # This doesn't actually test the output, but it's handy to have for debugging trace.
     listing = [
         '100 PRINT:PRINT:PRINT:PRINT',
         '110 J=3+2',
         '120 PRINTJ',
     ]
     program = tokenize(listing)
     self.assertEqual(len(listing), len(program))
     with open("tracefile.txt", "w") as f:
         executor = Executor(program, trace_file=f)
         executor.run_program()
Exemplo n.º 3
0
 def test_load_program(self):
     # TODO This no longer works. format_program assumes everything is in stmt.args,
     # which is no longer true after implementing PreparedStatements for all keywords.
     input_file = "../programs/simple_test.bas"
     with open(input_file) as f:
         source = f.read()
     program = load_program(input_file)
     self.assertEqual(5, len(program))
     executor = Executor(program)
     lines = executor.get_program_lines()
     output = "\n".join(lines) + "\n"
     self.assertEqual(source, output)
Exemplo n.º 4
0
 def test_get_next_stmt(self):
     listing = [
         '100 J=0:FOR I=1TO10',
         '110 J=J+2',
         '120 NEXTI',
     ]
     program = tokenize(listing)
     self.assertEqual(len(listing), len(program))
     executor = Executor(program)
     ct = executor.get_next_stmt()
     self.assertEqual(0, ct.index)
     self.assertEqual(1, ct.offset)
Exemplo n.º 5
0
 def load_program(self, program):
     """
     Used by renum code to replace source program with renumbered one
     :return:
     """
     executor = Executor(program)
     self.executor = executor
Exemplo n.º 6
0
 def load_from_string(self, listing):
     """
     Used by test code
     :return:
     """
     program = tokenize(listing)
     executor = Executor(program)
     self.executor = executor
Exemplo n.º 7
0
    def assert_value(self, executor: Executor, symbol: str, expected_value):
        """
        Asserts the that symbol has the expected value

        ONLY CHECKS THE SYMBOL TABLE SymbolTable.VARIABLE, not ARRAY, or FUNCTION
        :param executor:
        :param symbol:
        :param expected_value:
        :return:
        """
        value = executor.get_symbol(symbol)
        self.assertEqual(expected_value, value)
Exemplo n.º 8
0
 def load_from_file(self, coverage=False):
     if not os.path.exists(self._program_file):
         if os.path.exists(self._program_file+".bas"):
             self._program_file += ".bas"
     print("Loading ", self._program_file)
     try:
         program = load_program(self._program_file)
     except FileNotFoundError as e:
         print(F"File {self._program_file} not found.")
         return
     except BasicSyntaxError:
         return
     except Exception as e:
         print(e)
         traceback.print_exc()
         return
     executor = Executor(program, coverage=coverage)
     self.executor = executor
Exemplo n.º 9
0
 def runit(self, listing, trace=False):
     program = tokenize(listing)
     self.assertEqual(len(listing), len(program))
     executor = Executor(program, trace=trace, stack_trace=True)
     executor.run_program()
     return executor
Exemplo n.º 10
0
 def assert_value(self, executor:Executor, symbol:str, expected_value):
     value = executor.get_symbol(symbol)
     self.assertEqual(expected_value, value)
Exemplo n.º 11
0
    parser = argparse.ArgumentParser(description='Run BASIC programs.')
    parser.add_argument(
        'program',
        help="The name of the basic file to run. Will add '.bas' of not found."
    )
    parser.add_argument('--trace',
                        '-t',
                        action='store_true',
                        help="Write  a trace of execution to 'tracefile.txt'.")
    parser.add_argument('--symbols',
                        '-s',
                        action='store_true',
                        help="Dump the symbol table on exit.")
    args = parser.parse_args()
    # Makes it to 1320 of superstartrek, and line 20 of startrek.bas
    if len(sys.argv) < 2:
        print("Usage: python3 basic_intrpreter.py name_of_program.bas")
        sys.exit(1)
    program = load_program(args.program)
    if args.trace:
        with open("tracefile.txt", "w") as f:
            executor = Executor(program, trace_file=f)
            rc = executor.run_program()
    else:
        executor = Executor(program)
        rc = executor.run_program()
    print(F"Program completed with a status of {rc}")

    if args.symbols:
        pprint.pprint(executor._symbols.dump())