Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows users to watch the execution of a program step by step #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions marbelous/marbelous.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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) + \
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand All @@ -595,4 +644,10 @@ def load_mbl_file(filename,ignore_main=True):
exit_code = 0
exit(exit_code)

print
print

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one trailing newline is great. six is overkill