-
Notifications
You must be signed in to change notification settings - Fork 8
/
commands.py
214 lines (158 loc) · 6.37 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import re
import sys
import vt100
import inspect
import functools
import textwrap
class Command:
pass
class Shortcut(Command):
def run(self, execute, prompt, ctx):
return execute(self.COMMAND)
class Run(Shortcut):
KEYS = ['>', 'r']
HELP = 'Run the program forward'
COMMAND = 'run'
class Reverse(Shortcut):
KEYS = ['<', 'R']
HELP = 'Run the program backward'
COMMAND = 'reverse'
class Next(Shortcut):
KEYS = ['j']
HELP = 'Next line'
COMMAND = 'next'
class Prev(Shortcut):
KEYS = ['k']
HELP = 'Previous line'
COMMAND = 'prev'
class Step(Shortcut):
KEYS = [']', 's']
HELP = 'Step forward'
COMMAND = 'step'
class Backstep(Shortcut):
KEYS = ['[', 'S']
HELP = 'Step backward'
COMMAND = 'backstep'
class Yes(Shortcut):
KEYS = ['y']
HELP = 'Answer "y" to the question'
COMMAND = 'y'
HIDDEN = True
class No(Shortcut):
KEYS = ['n']
HELP = 'Answer "n" to the question'
COMMAND = 'n'
HIDDEN = True
class Timetravel(Shortcut):
KEYS = ['t', 'g']
HELP = 'Travel to specified time'
def run(self, execute, prompt, ctx):
banner = vt100.from_tags_unsafe(textwrap.dedent("""
<bold><blue>TIME TRAVEL</blue></bold>
<bold>1337 </bold> - jump to specified time
<bold>+100 </bold> - jump 100 time units forward
<bold>-100 </bold> - jump 100 time units backwards
<dim>(time travel)</dim> """))
time = prompt(banner)
if not len(time):
return
now = int(ctx['loc'].get('time') or 0)
if time.startswith('-'):
location = now - int(time[1:])
elif time.startswith('+'):
location = now + int(time[1:])
elif time.isdigit():
location = int(time)
else:
return
return execute('goto ' + str(location))
class Modules(Command):
KEYS = ['m']
HELP = 'Show available modules'
std_modules = re.compile('(' + '\\b|\\b'.join([
"Arg", "Arg_helper", "Arith_status", "Array", "ArrayLabels", "Ast_helper", "Ast_invariants",
"Ast_iterator", "Ast_mapper", "Asttypes", "Attr_helper", "Big_int", "Bigarray", "Buffer",
"Builtin_attributes", "Bytes", "BytesLabels", "Callback", "CamlinternalFormat",
"CamlinternalFormatBasics", "CamlinternalLazy", "CamlinternalMod", "CamlinternalOO", "Ccomp",
"Char", "Clflags", "Complex", "Condition", "Config", "Consistbl", "Depend", "Digest",
"Docstrings", "Dynlink", "Ephemeron", "Event", "Filename", "Format", "Gc", "Genlex", "Graphics",
"GraphicsX11", "Hashtbl", "Identifiable", "Int32", "Int64", "Lazy", "Lexer", "Lexing", "List",
"ListLabels", "Location", "Longident", "Map", "Marshal", "Misc", "MoreLabels", "Mutex",
"Nativeint", "Num", "Numbers", "Obj", "Oo", "Parse", "Parser", "Parsetree", "Parsing",
"Pervasives", "Pprintast", "Printast", "Printexc", "Printf", "Queue", "Random", "Ratio",
"Scanf", "Set", "Sort", "Spacetime", "Stack", "Std_exit", "StdLabels", "Str", "Stream", "String",
"StringLabels", "Strongly_connected_components", "Syntaxerr", "Sys", "Tbl", "Terminfo", "Thread",
"ThreadUnix", "Timings", "Uchar", "Unix", "UnixLabels", "Warnings", "Weak"
]) + ')')
def run(self, execute, prompt, ctx):
return re.sub(Modules.std_modules, vt100.dim('\\1'), execute('info modules'))
class Print(Command):
KEYS = ['p']
HELP = 'Print variable value'
def run(self, execute, prompt, ctx):
var = prompt(vt100.magenta(vt100.dim('(print) ')))
if not len(var):
return
output = execute('print ' + var)
return vt100.magenta(output)
class Quit(Shortcut):
KEYS = ['q']
HELP = 'Quit'
COMMAND = 'quit'
class Breakpoint(Command):
KEYS = ['b']
HELP = 'Add, remove and list breakpoints'
def format_breakpoints(self, breakpoints):
if len(breakpoints) == 0:
return vt100.dim(' No breakpoins added yet')
lines = []
for b in breakpoints:
lines.append(vt100.red(('#' + str(b.get('num'))).rjust(5)) + ' ' + (b.get('file') + ':' + str(b.get('line'))).ljust(30)
+ vt100.dim(' pc = ' + str(b.get('pc'))))
return '\n'.join(lines)
def command(self, prompt, ctx):
loc = ctx['loc']
breakpoints = ctx['breakpoints']
banner = vt100.from_tags_unsafe(textwrap.dedent("""
<bold><blue>BREAKPOINTS</blue></bold>
{0}
<bold>42 </bold> - add breakpoint for current module ({1}) at line 42
<bold>Module:42 </bold> - add breakpoint for specified module Module at line 42
<bold>Module.foo</bold> - add breakpoint for Module.foo function
<bold>-#2 </bold> - remove breakoint #2
<bold><enter> </bold> - do nothing
<dim>(break)</dim> """)).format(self.format_breakpoints(breakpoints), loc.get('module'))
command = prompt(banner)
if command:
if command.startswith('-#'):
return 'delete ' + command[2:]
elif command.isdigit():
if loc.get('module'):
return 'break @ ' + loc.get('module') + ' ' + command
else:
if ':' in command:
return 'break @ ' + command.replace(':', ' ')
else:
return 'break ' + command
def run(self, execute, prompt, ctx):
command = self.command(prompt, ctx)
# TODO: check if the breakpoint was really added
# TODO: breakpoints don't work at the beginning/end of the program
if command:
return execute(command)
class Custom(Command):
KEYS = [':', ';']
HELP = 'Custom OcamlDebug command'
def run(self, execute, prompt, ctx):
command = prompt(vt100.dim('(odb) '))
if not len(command):
return
output = execute(command)
return vt100.blue('>> {0}\n'.format(command)) + output
def all_command_classes():
# Suck hack, wow
with open(__file__.replace('.pyc', '.py'), 'r') as f:
content = f.read()
class_pos = lambda pair: content.find('class ' + pair[0])
cmds = sorted(inspect.getmembers(sys.modules[__name__], inspect.isclass), key=class_pos)
return [cls for (_, cls) in cmds if hasattr(cls, 'HELP')]