-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
105 lines (93 loc) · 2.27 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
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
#include "./georgecc.h"
#include <stdio.h>
#include <string.h>
char* substring(char *result, char *c, int len) {
for (int i = 0; i < len; i++) {
result[i] = c[i];
}
result[len] = '\0';
return result;
}
void printToken(Token token) {
char *result;
switch (token.kind) {
case TK_RESERVED:
substring(result, token.str, token.len);;
if (result[0] == ';') {
printf("%s", result);
printf("\n ");
} else {
printf("%s", result);
printf(" -> ");
}
break;
case TK_NUM:
printf("%d", token.val);
printf(" -> ");
break;
case TK_IDENT:
case TK_RETURN:
case TK_IF:
case TK_ELSE:
case TK_WHILE:
substring(result, token.str, token.len);;
printf("%s", result);
printf(" -> ");
break;
case TK_EOF:
printf("EOF");
break;
}
}
// Debug用関数
void printTokens(Token t) {
printf("; ");
do {
printToken(t);
t = *t.next;
} while (t.next != NULL);
printf("\n");
}
// ASTを出力する関数
void print_ast(Node *node, int depth) {
if (node == NULL) return;
// 右の子ノードを先に出力
print_ast(node->rhs, depth + 1);
// 現在のノードを出力
for (int i = 0; i < depth; i++) {
printf(" ");
}
switch (node->kind) {
case ND_LVAR:
printf("%.*s\n", node->len, node->name);
break;
case ND_ASSIGN:
printf("=(%d)\n", node->type->ty);
break;
case ND_DEREF:
printf("*(%d)\n", node->type->ty);
break;
case ND_ADD:
printf("+(%d)\n", node->type->ty);
break;
case ND_SUB:
printf("-\n");
break;
case ND_MUL:
printf("*(%d)\n", node->type->ty);
break;
case ND_NUM:
printf("%d\n", node->val);
break;
case ND_STRING:
printf("%.*s\n", node->len, node->name);
break;
case ND_RETURN:
printf("戻\n");
break;
default:
printf("Unknown NodeKind: %d\n", node->kind);
}
// 左の子ノードを出力
print_ast(node->lhs, depth + 1);
}