def __init__(self, greet, load): """ Initialise the interpreter session. """ # true if a prompt is needed on next cycle self.prompt = True # input mode is AUTO (used by AUTO) state.basic_state.auto_mode = False # interpreter is executing a command state.basic_state.parse_mode = False # interpreter is waiting for INPUT or LINE INPUT state.basic_state.input_mode = False # previous interpreter mode self.last_mode = False, False # syntax error prompt and EDIT state.basic_state.edit_prompt = False # initialise the display display.init() # initialise the console console.init_mode() # set up event handlers state.basic_state.events = events.Events() # load initial program if load: # on load, accept capitalised versions and default extension with disk.open_native_or_dos_filename(load) as progfile: program.load(progfile) # set up interpreter and memory model state reset.clear() # greeting and keys if greet: console.clear() console.write_line(greeting.format(version=plat.version, free=var.fre())) console.show_keys(True)
def auto_step(self): """ Generate an AUTO line number and wait for input. """ numstr = str(state.basic_state.auto_linenum) console.write(numstr) if state.basic_state.auto_linenum in state.basic_state.line_numbers: console.write('*') line = bytearray(console.wait_screenline(from_start=True)) if line[:len(numstr)+1] == numstr+'*': line[len(numstr)] = ' ' else: console.write(' ') line = bytearray(console.wait_screenline(from_start=True)) # run or store it; don't clear lines or raise undefined line number state.basic_state.direct_line = tokenise.tokenise_line(line) c = util.peek(state.basic_state.direct_line) if c == '\0': # check for lines starting with numbers (6553 6) and empty lines empty, scanline = program.check_number_start(state.basic_state.direct_line) if not empty: program.store_line(state.basic_state.direct_line) reset.clear() state.basic_state.auto_linenum = scanline + state.basic_state.auto_increment elif c != '': # it is a command, go and execute state.basic_state.parse_mode = True
def store_line(self, line): """ Store a program line or schedule a command line for execution. """ if not line: return True state.basic_state.direct_line = tokenise.tokenise_line(line) c = util.peek(state.basic_state.direct_line) if c == '\0': # check for lines starting with numbers (6553 6) and empty lines program.check_number_start(state.basic_state.direct_line) program.store_line(state.basic_state.direct_line) reset.clear() elif c != '': # it is a command, go and execute state.basic_state.parse_mode = True return not state.basic_state.parse_mode
def store_line(line): """ Store a program line or schedule a command line for execution. """ if not line: return True state.basic_state.direct_line = tokenise.tokenise_line(line) c = util.peek(state.basic_state.direct_line) if c == '\0': # check for lines starting with numbers (6553 6) and empty lines program.check_number_start(state.basic_state.direct_line) program.store_line(state.basic_state.direct_line) reset.clear() elif c != '': # it is a command, go and execute state.basic_state.execute_mode = True return not state.basic_state.execute_mode
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound do_reset = False backend, console = None, None try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(0) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: msg = error.get_message(e.err) if console and config.get('wait'): console.write_error_message(msg, None) if backend and backend.video: # close terminal to avoid garbled error message backend.video.close() backend.video = None logging.error(msg) except error.Exit: pass except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: logging.error("Unhandled exception\n%s", traceback.format_exc()) finally: try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) try: sound.audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e)
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound import audio do_reset = False backend, console = None, None exit_error = '' try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(1) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: exit_error = error.get_message(e.err) except error.Exit: # pause before exit if requested if config.get('wait'): backend.video_queue.put(backend.Event(backend.VIDEO_SET_CAPTION, 'Press a key to close window')) backend.video_queue.put(backend.Event(backend.VIDEO_SHOW_CURSOR, False)) state.console_state.keyb.pause = True # this performs a blocking keystroke read if in pause state backend.check_events() except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: print traceback.print_exc() exit_error = "Unhandled exception\n%s" % traceback.format_exc() finally: try: audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e) try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) if exit_error: logging.error(exit_error)
def start_basic(): """ Load & run programs and commands and hand over to interactive mode. """ import program import run import error import state import devices import disk import cassette import reset import sound import audio do_reset = False backend, console = None, None exit_error = '' try: # resume from saved emulator state if requested and available resume = config.get('resume') and state.load() # choose the video and sound backends backend, console = prepare_console() # greet, load and run only if not resuming if resume: # override selected settings from command line cassette.override() disk.override() # suppress double prompt if not state.basic_state.execute_mode: state.basic_state.prompt = False run.start('', False, config.get('quit')) else: # load/run program config.options['run'] = config.get(0) or config.get('run') prog = config.get('run') or config.get('load') if prog: # on load, accept capitalised versions and default extension with open_native_or_dos_filename(prog) as progfile: program.load(progfile) reset.clear() print_greeting(console) # start the interpreter (and get out if we ran with -q) run.start(config.get('exec'), config.get('run'), config.get('quit')) except error.RunError as e: exit_error = e.message except error.Exit: # pause before exit if requested if config.get('wait'): backend.video_queue.put( backend.Event(backend.VIDEO_SET_CAPTION, 'Press a key to close window')) backend.video_queue.put( backend.Event(backend.VIDEO_SHOW_CURSOR, False)) state.console_state.keyb.pause = True # this performs a blocking keystroke read if in pause state backend.check_events() except error.Reset: do_reset = True except KeyboardInterrupt: if config.get('debug'): raise except Exception as e: exit_error = "Unhandled exception\n%s" % traceback.format_exc() finally: try: audio.close() except (NameError, AttributeError) as e: logging.debug('Error on closing audio: %s', e) try: # fix the terminal on exit (important for ANSI terminals) # and save display interface state into screen state state.console_state.screen.close() except (NameError, AttributeError) as e: logging.debug('Error on closing screen: %s', e) # delete state if resetting if do_reset: state.delete() if plat.system == 'Android': shutil.rmtree(plat.temp_dir) else: state.save() try: # close files if we opened any devices.close_files() except (NameError, AttributeError) as e: logging.debug('Error on closing files: %s', e) try: devices.close_devices() except (NameError, AttributeError) as e: logging.debug('Error on closing devices: %s', e) if exit_error: logging.error(exit_error)