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

Milestone 3: Allow verbose printing of structures, lists, and tuples #312

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions asteroid/mad.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
###########################################################################################

from os.path import exists, split, basename
from asteroid.support import term2string, get_tail_term
from asteroid.support import term2string, get_tail_term, term2verbose
from asteroid.version import MAD_VERSION
import copy

Expand Down Expand Up @@ -281,7 +281,7 @@ def _handle_help(self,_):
print("help\t\t\t- display help")
print("list [<num>|*]\t\t\t- display <num> (default 4) lines of source code, * displays all lines in file")
print("next\t\t\t- step execution across a nested scope")
print("print <name>[@<num>|<name>]+|*\t\t- print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @")
print("print <name>[@<num>|<name>]+|* [-v]\t\t- print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @, '-v' enables verbose printing of nested data")
print("quit\t\t\t- quit debugger")
print("set [<func>|<line#> [<file>]]\n\t\t\t- set a breakpoint")
print("stack\t\t\t- display runtime stack")
Expand Down Expand Up @@ -338,13 +338,21 @@ def _handle_next(self,_):
return RETURN_TO_INTERP

def _handle_print(self,args):
if len(args) > 1:
if len(args) > 2:
print("error: too many arguments")
return False
elif len(args) == 0:
print("error: no argument given")
return False

if len(args) == 2 and args[1] == '-v':
verbose = True
elif len(args) == 1:
verbose = False
else:
print("error: unknown option '{}'".format(args[1]))
return False

# Split any arguments by the '@' character when necessary
syms = args[0].split('@')
# '@' occurs at beginning or end of argument, or multiple `@`s occur next to each other is rejected with an error message
Expand All @@ -356,7 +364,7 @@ def _handle_print(self,args):
# If '*' is the only argument, handle output as normal
if syms[0] == '*' and len(syms) == 1:
for (name,val) in var_list:
print("{}: {}".format(name,term2string(val)))
print("{}: {}".format(name, term2verbose(val) if verbose else term2string(val)))
else:
# Loop through scope and check if any symbols in the scope are the first symbol in the list
term = None
Expand All @@ -368,7 +376,7 @@ def _handle_print(self,args):
val = get_tail_term(syms[0], term, syms[1:])
# Print the entire argument along with its current symbol if it is found
if val:
print("{}: {}".format(args[0], term2string(val)))
print("{}: {}".format(args[0], term2verbose(val) if verbose else term2string(val)))
return START_DEBUGGER

def _handle_quit(self,_):
Expand Down
43 changes: 43 additions & 0 deletions asteroid/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,46 @@ def get_tail_term(sym, term, attrs):
# Used for error handling: attach the next attribute to the existing symbol
sym += "@{}".format(a)
return term

def term2verbose(term, indent_size=0, initial_indent=True):
TYPE = term[0]
# Type does not contain nested data: call term2string and return the value
if TYPE not in ['list', 'tuple', 'object']:
return term2string(term)

curr_indent, next_indent = ' '*4*indent_size, ' '*4*(indent_size+1)
term_string = curr_indent if initial_indent else ''

# Type is an object: build string with struct name, and data members and corresponding types on separate lines
# Make sure indent is not applied twice
if TYPE == 'object':
(OBJECT,
(STRUCT_ID, (ID, struct_id)),
(MEMBER_NAMES, (LIST, member_names)),
(OBJECT_MEMORY, (LIST, object_memory))) = term

term_string += (struct_id + '(\n')
l = len(member_names)
for ix in range(l):
term_string += "{}{}: {}".format(next_indent,
member_names[ix],
term2verbose(object_memory[ix], indent_size=indent_size+1, initial_indent=False))
term_string += ',\n' if ix < l-1 else '\n'
term_string += curr_indent + ')'
# Type is list or tuple: build string with brackets, and stored data on each new line, making sure to indent new data when not nested.
else:
val = term[1]
term_string += '[\n' if TYPE == 'list' else '(\n'
l = len(val)
for i in range(l):
if val[i][0] not in ['list', 'tuple', 'object']:
term_string += next_indent
term_string += term2verbose(val[i], indent_size=indent_size+1)
if i != l-1:
term_string += ','
if l > 1 or TYPE == 'list':
term_string += '\n'
if l == 1 and TYPE == 'tuple':
term_string += ',\n'
term_string += curr_indent + (']' if TYPE == 'list' else ')')
return term_string
32 changes: 16 additions & 16 deletions docs/MAD.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,22 @@ Commands
========
Here is a table of the available commands in the the debugger,
::
breakpoints .................... show all breakpoints
clear .......................... clear all breakpoints
continue ....................... continue execution to next breakpoint
down ........................... move down one stack frame
frame .......................... display current stack frame number
help ........................... display help
list [<num>|*].................. display <num> (default 4) lines of source code, * displays all lines in file
next ........................... step execution across a nested scope
print <name>[@<num>|<name>]+|* . print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @
quit ........................... quit debugger
stack .......................... display runtime stack
set [<func>|<line#> [<file>]] .. set a breakpoint
step ........................... step to next executable statement
trace .......................... display runtime stack
up ............................. move up one stack frame
where .......................... print current program line
breakpoints ........................ show all breakpoints
clear .............................. clear all breakpoints
continue ........................... continue execution to next breakpoint
down ............................... move down one stack frame
frame .............................. display current stack frame number
help ............................... display help
list [<num>|*]...................... display <num> (default 4) lines of source code, * displays all lines in file
next ............................... step execution across a nested scope
print <name>[@<num>|<name>]+|* [-v]. print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @, '-v' enables verbose printing of nested data
quit ............................... quit debugger
stack .............................. display runtime stack
set [<func>|<line#> [<file>]] ...... set a breakpoint
step ............................... step to next executable statement
trace .............................. display runtime stack
up ................................. move up one stack frame
where .............................. print current program line

The or bar ``|`` means different options as arguments to the commands. Anything between
square brackets is optional. The plus symbol ``+`` means at least one of the preceding symbols must be present.
Expand Down
32 changes: 16 additions & 16 deletions docs/MAD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ Commands
========
Here is a table of the available commands in the the debugger,
::
breakpoints .................... show all breakpoints
clear .......................... clear all breakpoints
continue ....................... continue execution to next breakpoint
down ........................... move down one stack frame
frame .......................... display current stack frame number
help ........................... display help
list [<num>|*].................. display <num> (default 4) lines of source code, * displays all lines in file
next ........................... step execution across a nested scope
print <name>[@<num>|<name>]+|* . print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @
quit ........................... quit debugger
stack .......................... display runtime stack
set [<func>|<line#> [<file>]] .. set a breakpoint
step ........................... step to next executable statement
trace .......................... display runtime stack
up ............................. move up one stack frame
where .......................... print current program line
breakpoints ........................ show all breakpoints
clear .............................. clear all breakpoints
continue ........................... continue execution to next breakpoint
down ............................... move down one stack frame
frame .............................. display current stack frame number
help ............................... display help
list [<num>|*]...................... display <num> (default 4) lines of source code, * displays all lines in file
next ............................... step execution across a nested scope
print <name>[@<num>|<name>]+|* [-v]. print contents of <name>, * lists all vars in scope, recursively access (nested) objects with @, '-v' enables verbose printing of nested data
quit ............................... quit debugger
stack .............................. display runtime stack
set [<func>|<line#> [<file>]] ...... set a breakpoint
step ............................... step to next executable statement
trace .............................. display runtime stack
up ................................. move up one stack frame
where .............................. print current program line

The or bar ``|`` means different options as arguments to the commands. Anything between
square brackets is optional. The plus symbol ``+`` means at least one of the preceding symbols must be present.
Expand Down
Loading