-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.ts
More file actions
116 lines (100 loc) · 2.4 KB
/
debug.ts
File metadata and controls
116 lines (100 loc) · 2.4 KB
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
import { Span, startSpan, addEvent } from '@matanuska/debug';
export { startSpan, addEvent };
export type { Span };
import type { Chunk } from './bytecode/chunk';
import type { Runtime } from './runtime';
//#if _MATBAS_BUILD == 'debug'
import { parseBoolEnv } from './env';
//#else
//#unset _DEBUG_SHOW_TREE
//#unset _DEBUG_SHOW_CHUNK
//#unset _DEBUG_TRACE_RUNTIME
//#endif
//#if _DEBUG_SHOW_TREE
import type { Tree } from './ast';
//#set _IMPORT_FORMATTER = 1
//#endif
//#if _DEBUG_SHOW_CHUNK
import { disassemble } from './bytecode/disassembler';
//#endif
//#if _DEBUG_TRACE_RUNTIME
import { disassembleInstruction } from './bytecode/disassembler';
//#set _IMPORT_FORMATTER = 1
//#endif
//#if _IMPORT_FORMATTER
import { formatter } from './format';
//#endif
let NO_TRACE = true;
let SHOW_UNDEF = false;
//#if _MATBAS_BUILD == 'debug'
NO_TRACE = parseBoolEnv(process.env.NO_TRACE);
SHOW_UNDEF = parseBoolEnv(process.env.DEBUG_SHOW_UNDEF);
//#endif
export { NO_TRACE, SHOW_UNDEF }
/**
* Show a parse tree.
*
* @param tree The tree to show.
*/
export function showTree(tree: Tree): void {
//#if _DEBUG_SHOW_TREE
if (!NO_TRACE) {
console.log('=== Parse Tree: ===');
console.log(formatter.format(tree));
}
//#endif
};
/**
* Show a compiled chunk.
*
* @param chunk The chunk to show.
*/
export function showChunk(chunk: Chunk): void {
//#if _DEBUG_SHOW_CHUNK
if (!NO_TRACE) {
console.log(disassemble(chunk));
}
//#endif
}
/**
* Log the start of execution tracing.
*/
export function startTraceExec(): void {
//#if _DEBUG_TRACE_RUNTIME
if (!NO_TRACE) {
console.log('=== Execution Trace: ===');
}
//#endif
};
/**
* Show the runtime's state. This is the stack, plus the locations of
* relevant pointers.
*/
export function showStack(rt: Runtime): void {
const { sp } = rt.registers;
let stack = formatter.format(rt.stack).split('\n');
const top = stack.shift();
const bottom = stack.pop();
stack = stack.map((line, i) => {
if (i === sp) {
return `->${line}`;
}
return ` ${line}`;
});
stack.unshift(top);
stack.push(bottom);
return stack.join('\n');
}
/**
* Trace a step in execution.
* @param rt The runtime.
*/
export function traceExec(rt: Runtime): void {
//#if _DEBUG_TRACE_RUNTIME
if (!NO_TRACE) {
console.log('>', disassembleInstruction(rt.chunk, rt.frame.pc));
console.log('>', showStack(rt));
console.log('---');
}
//#endif
}