diff --git a/marbelous/marbelous.py b/marbelous/marbelous.py index 7aec95c..647f96f 100755 --- a/marbelous/marbelous.py +++ b/marbelous/marbelous.py @@ -12,6 +12,7 @@ except ImportError: from queue import Queue, Empty # python 3.x +import time # for pausing momentarily between frames with watch mode oct_digits = '01234567' @@ -33,6 +34,11 @@ parser.add_argument('-m', metavar='W', dest='memoize_width', action='store', type=int, default=2, help='maximum function width to memoize') +#TODO add argument specifying speed +parser.add_argument('-w', '-watch', dest='watch', action='store_true', + help='watch the execution of the program' \ + ' (requires that there is no verbosity)') + options = vars(parser.parse_args()) verbose_stream = sys.stderr if options['stderr'] else sys.stdout @@ -127,7 +133,7 @@ def printr(self, s): def write_stdout(self,stdout_str): # print "write_stdout", self if stdout_str: - if options['verbose'] > 0: + if options['verbose'] > 0 or options['watch']: self.print_out += stdout_str if options['verbose'] > 1: self.printr("write_stdout STDOUT: " + ' '.join(["0x" + hex(ord(char))[2:].upper().zfill(2) + \ @@ -502,6 +508,35 @@ def put(y, x, m): self.marbles = nmb return True + + def to_string(self): + out = "" + + if self.function_queue: + board, coordinates = self.function_queue[0] + out += board.to_string() + + out += ':' + self.name + " tick " + str(self.tick_count) + '\n' + for y in range(self.board_h): + line = '' + for x in range(self.board_w): + line += (format_cell(self.marbles[y][x]) if self.marbles[y][x] is not None else format_cell(self.devices[y][x])) + ' ' + out += line + '\n' + return out +'\n' + + + def display_frame(self): + time.sleep(.5) + self.printr( '\n'*70 ) + self.printr( self.to_string() ) + + + + + + + + # the boards array contains pristine instances of boards from the source boards = {} files_included = set() @@ -564,10 +599,22 @@ def load_mbl_file(filename,ignore_main=True): board.display_tick() total_ticks = 1 + + +# display the initial state +if options['watch']: + board.display_frame() + +previous_iteration = "" + while board.tick():# and board.tick_count < 10000: + processed_boards = [] total_ticks += 1 - if options['verbose'] > 2: + if options['verbose'] > 2: board.display_tick() + if options['watch']: + board.display_frame() + if options['verbose'] > 1: board.printr("Total ticks across all boards: " + str(total_ticks)) @@ -576,6 +623,8 @@ def load_mbl_file(filename,ignore_main=True): board.printr("Combined STDOUT: " + ' '.join(["0x" + hex(ord(v))[2:].upper().zfill(2) + \ '/"' + (v if ord(v) > 31 else '?') + '"' \ for v in board.print_out])) +if options['watch']: + board.printr("Final STDOUT:\n\n"+board.print_out) outputs = board.get_output_values() if options['verbose'] > 0: @@ -595,4 +644,10 @@ def load_mbl_file(filename,ignore_main=True): exit_code = 0 exit(exit_code) -print \ No newline at end of file +print + + + + + +