-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugger.c
373 lines (285 loc) · 10.2 KB
/
debugger.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <stdio.h>
#include <ctype.h>
#include <ncurses.h>
#include "debugger.h"
#include "interpreter.h"
#include "interface.h"
#include "color.h"
#define KEY_SENTER 0x157
#define INPUT_BUFFER_SIZE 1024
enum State {
STATE_IDLE, // Does not run. The initial and default state.
STATE_RUN, // Runs while updating the display with the given delay (not implemented yet).
STATE_BIG_STEP, // Skips all characters of the same type.
STATE_EXIT_LOOP, // Run up to the end of the current brackets or a breakpoint.
};
// I really like this C feature.
const char* stateNames[] = {
[STATE_IDLE] = "idle",
[STATE_RUN] = "run",
[STATE_BIG_STEP] = "step",
[STATE_EXIT_LOOP] = "exit loop",
};
// The mode determines where the user input goes.
enum Mode {
MODE_DEBUG, // Default.
MODE_INPUT,
MODE_MENU
};
static const char *modeNames[] = {
[MODE_DEBUG] = "debug",
[MODE_INPUT] = "input",
[MODE_MENU] = "menu",
};
static void HandleDebugKeyPress(int key);
static int16_t InputRequested(void);
static void InputPutChar(char put);
static void HandleTypedInputChar(int typedChar);
static enum State state = STATE_IDLE;
static enum Mode mode = MODE_DEBUG;
static uint8_t stateDebugElement = 0;
static uint8_t modeDebugElement = 0;
static char inputBuffer[INPUT_BUFFER_SIZE]; // I really do not feel like making this dynamic.
static uint16_t inputBufferSize = 0;
static uint8_t printOutput = 1; // Replace these with bools in C23 (not using stdbool lmao).
static uint8_t breakOnBreakpoint = 1;
static uint8_t printOutputDebugElement = 0;
static uint8_t breakOnBreakpointDebugElement = 0;
// waitForInput gets set whenever a ',' is encountered, but there is no input.
// The program will halt, but remember it's current state,
// so that it can continue when the user has provided their input.
static uint8_t waitForInput = 0;
static uint8_t interpretError = 0;
// Keeps track of the initial loop depth when entering an 'exit loop' state.
static uint16_t initialLoopDepth = 0;
uint8_t InitDebug(const char* brainfuckFile, uint16_t outputHeight) {
// Initialize the interpreter.
char* brainfuckCode = InitInterpreter(brainfuckFile, OutputChar, InputRequested);
if(brainfuckCode == NULL) {
fprintf(stderr, "\nCould not open/read file.\n\n");
return -1;
}
// Setup ncurses
initscr();
noecho(); // Don't echo input.
curs_set(0); // Hide cursor.
timeout(0); // Non-blocking inputs please.
cbreak();
keypad(stdscr, 1);
refresh(); // Refresh the screen once before doing anything.
getch(); // Get rid of the initial \n.
// Enable mouse things (useful for setting breakpoints).
mousemask(BUTTON1_CLICKED, NULL);
InitInterface(outputHeight, brainfuckCode);
// Init the debug window.
printOutputDebugElement = NewDebugElement("Visual", 5);
SetDebugElementBool(printOutputDebugElement, printOutput);
breakOnBreakpointDebugElement = NewDebugElement("Breakpoints", 5);
SetDebugElementBool(breakOnBreakpointDebugElement, breakOnBreakpoint);
stateDebugElement = NewDebugElement("State", 9);
modeDebugElement = NewDebugElement("Mode", 5);
return 0;
}
void EndDebug(void) {
EndInterface();
}
void RunDebug(void) {
MEVENT mouseEvent;
// Toggling breakpoints with the mouse.
if(getmouse(&mouseEvent) == OK) {
int16_t clickedIndex = InterfaceGetCodeIndex(mouseEvent.y, mouseEvent.x);
if(clickedIndex != ERR) {
ToggleBreakPointAtCodeIndex(clickedIndex);
// This actually scrolls the window if you click too low, which is actually kinda nice, so I'll call it a feature.
// For some reason it also doesn't scroll away from the cursor, which is doubly nice.
UpdateCode(clickedIndex, 0);
}
}
// TODO: Check if end of code has been reached.
// TODO: Make input mode only available if input is in a seperate window.
char nextChar = '\0';
char prevChar;
if(state != STATE_IDLE && !waitForInput) prevChar = InterpretNextChar(&nextChar);
// Handle errors.
if(INTERPRETER_IS_ERROR(prevChar)) {
interpretError = prevChar;
switch(prevChar) {
case INTERPRETER_EOF:
PrintInfoMessage("Finished running!", COLOR_PAIR(SUCCESS_PAIR));
break;
case INTERPRETER_MEMORY_OUT_OF_BOUNDS:
PrintInfoMessage("Error: Tried to go out of bounds.", COLOR_PAIR(ERROR_PAIR));
break;
case INTERPRETER_OUT_OF_MEMORY:
PrintInfoMessage("Error: Out of memory! Buy more RAM!", COLOR_PAIR(ERROR_PAIR));
break;
case INTERPRETER_LEAK:
PrintInfoMessage("Bro I don't even know when this error happens.", COLOR_PAIR(ERROR_PAIR));
break;
}
}
// Handle special states.
if(!waitForInput && !interpretError) {
switch(state) {
case STATE_BIG_STEP:
// Run until the character changes.
const char prevChar = NO_BREAKPOINT_bm & InterpretNextChar(&nextChar);
if(prevChar != nextChar) state = STATE_IDLE;
break;
case STATE_EXIT_LOOP:
// Run until the loop depth is lower than the loop depth when entering this state.
if(GetLoopDepth() < initialLoopDepth) state = STATE_IDLE;
break;
default: break;
}
}
// Keep track of state changes.
static enum State prevState = STATE_RUN;
// Break on breakpoints if it's enabled.
if(breakOnBreakpoint && (nextChar & BREAKPOINT_bm))
state = STATE_IDLE;
// Print the output if it's enabled and we just did something.
if(printOutput && prevState != STATE_IDLE) {
UpdateCode(GetCodeIndex(), 1);
UpdateMemory(GetMemory(), GetMemIndex());
}
// Handle key presses.
int inKey = getch();
if(inKey != ERR) {
switch(mode) {
case MODE_DEBUG:
HandleDebugKeyPress(inKey);
break;
case MODE_INPUT:
HandleTypedInputChar(inKey);
break;
case MODE_MENU:
break;
}
}
if(state != prevState) {
// Gotta update the interface when coming out of a state that doesn't.
if(state == STATE_IDLE) {
// Set timeout to block to spare resources.
timeout(-1);
UpdateCode(GetCodeIndex(), 1);
UpdateMemory(GetMemory(), GetMemIndex());
}
else timeout(0);
SetDebugElementString(stateDebugElement, stateNames[state]);
prevState = state;
}
// Keep track of mode changes.
static enum Mode prevMode = MODE_MENU;
if(mode != prevMode) {
SetDebugElementString(modeDebugElement, modeNames[mode]);
prevMode = mode;
}
}
void HandleDebugKeyPress(int key) {
switch(key) {
case 'i':
mode = MODE_INPUT;
curs_set(1);
break;
case 'b':
ToggleBreakPoint();
UpdateCode(GetCodeIndex(), 0);
break;
case ' ':
if(state == STATE_IDLE) state = STATE_RUN;
else state = STATE_IDLE;
break;
case KEY_RIGHT:
// Step to the next character.
InterpretNextChar(NULL);
UpdateCode(GetCodeIndex(), 1);
UpdateMemory(GetMemory(), GetMemIndex());
break;
case KEY_SRIGHT:
state = STATE_BIG_STEP;
break;
case ']':
state = STATE_EXIT_LOOP;
initialLoopDepth = GetLoopDepth();
break;
case 's':
printOutput = !printOutput;
SetDebugElementBool(printOutputDebugElement, printOutput);
break;
case 'B':
breakOnBreakpoint = !breakOnBreakpoint;
SetDebugElementBool(breakOnBreakpointDebugElement, breakOnBreakpoint);
break;
}
}
void HandleTypedInputChar(int typedChar) {
if(typedChar != ERR) fprintf(stderr, "key %x\n", typedChar);
switch(typedChar) {
case ERR: break;
// Switch to idle if either Enter or Escape is pressed.
case '\n':
InputPutChar('\n');
mode = MODE_DEBUG;
waitForInput = 0;
curs_set(0);
// Set getch() back to non-blocking so we can continue to runwithout needing an input at every command.
if(state != STATE_IDLE) timeout(0);
break;
case '\e':
case '\t':
// Tab is also used to exit input mode
// because apparently the escape key blocks the program for an entire second.
state = STATE_IDLE;
mode = MODE_DEBUG;
waitForInput = 0;
curs_set(0);
break;
// Do not switch to idle if Shift + Enter is pressed.
case KEY_SENTER:
InputPutChar('\n');
break;
// For some fucking reason backspaces are given as 0x07 (which is BELL, btw)
// so I'll just list all the possibilities, just in case.
case 0x7:
case '\b':
case KEY_BACKSPACE:
InputPutChar('\b');
break;
default:
InputPutChar(typedChar);
break;
}
}
void InputPutChar(char put) {
if(inputBufferSize >= INPUT_BUFFER_SIZE) return;
if(put == '\b') {
// TODO: Check if it's a return in the buffer and handle that so that it doesn't teleport you to the end of a line that never was at the end of a line.
if(!inputBufferSize) return;
for(uint16_t i = 0; i < inputBufferSize - 1; i++) {
inputBuffer[i] = inputBuffer[i + 1];
}
inputBufferSize--;
OutputBackspaceChar();
return;
}
fprintf(stderr, "in is %02x which is %x but does it run?\n", put, isprint(put));
if(!isprint(put) && put != '\n') return;
fprintf(stderr, "it does :)\n");
// Move the entire buffer to the right.
// (I couldn't be bothered with a circular buffer.)
for(uint16_t i = inputBufferSize; i > 0; i--) {
inputBuffer[i] = inputBuffer[i - 1];
}
inputBuffer[0] = put;
inputBufferSize++;
OutputChar(put);
}
int16_t InputRequested(void) {
if(inputBufferSize) return inputBuffer[--inputBufferSize];
waitForInput = 1;
mode = MODE_INPUT;
// Make getch blocking to spare resources.
timeout(-1);
return -1;
}