forked from clark800/lambda-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
55 lines (50 loc) · 1.52 KB
/
debug.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
#include <stdio.h>
#include "lib/tree.h"
#include "lib/stack.h"
#include "ast.h"
#include "print.h"
#include "debug.h"
void debug(const char* message) {
fputs(message, stderr);
}
void debugLine(void) {
debug("======================================");
debug("======================================\n");
}
void serializeAST(Node* node, FILE* stream) {
if (node == NULL) {
fputs("NULL", stream); // for debugging
} else if (node == VOID) {
fputs("VOID", stream);
} else if (isBranch(node)) {
fputs("(", stream);
serializeAST(getLeft(node), stream);
fputs(isDefinition(node) ? " = " : " ", stream);
serializeAST(getRight(node), stream);
fputs(")", stream);
} else if (isParameter(node)) {
printToken(node, stream);
fputs(" ->", stream);
} else if (isInteger(node)) {
// builtins create integers, so not all integers will exist in input
fputll(getInteger(node), stream);
} else if (isReference(node)) {
printToken(node, stream);
fputs("#", stream);
fputll((long long)getDebruijnIndex(node), stream);
} else {
printToken(node, stream);
}
}
void debugAST(Node* node) {
serializeAST(node, stderr);
}
void debugStack(Stack* stack, Node* (*select)(Node*)) {
debug("[");
for (Iterator* it = iterate(stack); !end(it); it = next(it)) {
debugAST(select == NULL ? cursor(it) : select(cursor(it)));
if (!end(next(it)))
debug(", ");
}
debug("]");
}