-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc.c
More file actions
237 lines (227 loc) · 6.48 KB
/
lc.c
File metadata and controls
237 lines (227 loc) · 6.48 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
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
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
#include "lexer.h"
#include "parser.h"
#include "sema.h"
void print_indent(int depth) {
for (int i = 0; i < depth; i++) printf(" ");
}
const char* get_op_str(binary_op op) {
switch(op) {
case OP_PLUS: return "+";
case OP_MINUS: return "-";
case OP_DIV: return "/";
case OP_MUL: return "*";
case OP_EQ: return "==";
case OP_ASSIGN: return "=";
case OP_AND: return "&&";
case OP_OR: return "||";
case OP_NEQ: return "!=";
case OP_GT: return ">";
case OP_LT: return "<";
case OP_GE: return ">=";
case OP_LE: return "<=";
case OP_BOR: return "|";
case OP_BAND: return "&";
case OP_BXOR: return "^";
case OP_MOD: return "%";
case OP_PLUS_EQ: return "+=";
case OP_MINUS_EQ: return "-=";
case OP_DIV_EQ: return "/=";
case OP_MUL_EQ: return "*=";
default: return "?";
}
}
const char *get_uop_str(unary_op op) {
switch (op) {
case UOP_INCR: return "++";
case UOP_MINUS: return "-";
case UOP_DECR: return "--";
case UOP_DEREF: return "*";
case UOP_REF: return "&";
case UOP_NOT: return "!";
default: return "?";
}
}
void print_ast(ast_node *node, int depth) {
if (!node) return;
print_indent(depth);
switch (node->type) {
case NODE_INTEGER:
printf("Integer: %lu\n", node->expr.integer);
break;
case NODE_FLOAT:
printf("Float: %f\n", node->expr.flt);
break;
case NODE_CHAR:
printf("Char: '%c'\n", node->expr.ch);
break;
case NODE_STRING:
printf("String: \"%.*s\"\n", (int)node->expr.string.len, node->expr.string.start);
break;
case NODE_IDENTIFIER:
printf("Identifier: %.*s\n", (int)node->expr.string.len, node->expr.string.start);
break;
case NODE_CAST:
printf("Cast:\n");
print_ast(node->expr.cast.type, depth);
print_ast(node->expr.cast.value, depth + 1);
break;
case NODE_ACCESS:
printf("Access:\n");
print_ast(node->expr.access.expr, depth + 1);
print_ast(node->expr.access.member, depth + 1);
break;
case NODE_LABEL:
printf("Label: %.*s\n", (int)node->expr.label.name_len, node->expr.label.name);
break;
case NODE_GOTO:
printf("Goto: %.*s\n", (int)node->expr.label.name_len, node->expr.label.name);
break;
case NODE_BINARY:
printf("BinaryOp (%s)\n", get_op_str(node->expr.binary.operator));
print_ast(node->expr.binary.left, depth + 1);
print_ast(node->expr.binary.right, depth + 1);
break;
case NODE_ARRAY_SUBSCRIPT:
printf("Array subscript\n");
print_ast(node->expr.subscript.expr, depth + 1);
print_ast(node->expr.subscript.index, depth + 1);
break;
case NODE_UNARY:
printf("UnaryOp (%s)\n", get_uop_str(node->expr.unary.operator));
print_ast(node->expr.unary.right, depth + 1);
break;
case NODE_POSTFIX:
printf("Postfix (%s)\n", get_uop_str(node->expr.unary.operator));
print_ast(node->expr.unary.right, depth + 1);
break;
case NODE_BREAK:
printf("Break\n");
break;
case NODE_TERNARY:
printf("Ternary (? :)\n");
print_indent(depth + 1); printf("Condition:\n");
print_ast(node->expr.ternary.condition, depth + 2);
print_indent(depth + 1); printf("Then:\n");
print_ast(node->expr.ternary.then, depth + 2);
print_indent(depth + 1); printf("Else:\n");
print_ast(node->expr.ternary.otherwise, depth + 2);
break;
case NODE_UNIT:
printf("Unit\n");
ast_node *current = node;
while (current && current->type == NODE_UNIT) {
print_ast(current->expr.unit_node.expr, depth + 1);
current = current->expr.unit_node.next;
}
break;
case NODE_CALL:
printf("Call: %.*s\n", (int)node->expr.call.name_len, node->expr.call.name);
current = node->expr.call.parameters;
while (current && current->type == NODE_UNIT) {
print_ast(current->expr.unit_node.expr, depth + 1);
current = current->expr.unit_node.next;
}
break;
case NODE_STRUCT_INIT:
printf("Struct init:\n");
current = node->expr.struct_init.members;
while (current && current->type == NODE_UNIT) {
print_ast(current->expr.unit_node.expr, depth + 1);
current = current->expr.unit_node.next;
}
break;
case NODE_STRUCT:
printf("Struct: %.*s\n", (int)node->expr.structure.name_len, node->expr.structure.name);
member *m = node->expr.structure.members;
while (m) {
print_ast(m->type, depth + 1);
m = m->next;
}
break;
case NODE_UNION:
printf("Union: %.*s\n", (int)node->expr.structure.name_len, node->expr.structure.name);
m = node->expr.structure.members;
while (m) {
print_ast(m->type, depth + 1);
m = m->next;
}
break;
case NODE_ENUM:
printf("Enum: %.*s\n", (int)node->expr.enm.name_len, node->expr.enm.name);
variant *v = node->expr.enm.variants;
while (v) {
printf("\t%.*s\n", (int)v->name_len, v->name);
v = v->next;
}
break;
case NODE_IF:
printf("If:\n");
print_ast(node->expr.whle.condition, depth + 1);
print_ast(node->expr.whle.body, depth + 1);
break;
case NODE_VAR_DECL:
printf("VarDecl: ");
print_ast(node->expr.var_decl.type, 0);
print_ast(node->expr.var_decl.value, depth + 1);
break;
case NODE_FUNCTION:
printf("Function: %.*s\n", (int)node->expr.function.name_len, node->expr.function.name);
m = node->expr.function.parameters;
while (m) {
print_ast(m->type, depth + 1);
m = m->next;
}
print_ast(node->expr.function.body, depth + 1);
break;
case NODE_RETURN:
printf("Return:\n");
print_ast(node->expr.ret.value, depth + 1);
break;
case NODE_IMPORT:
printf("Import:\n");
print_ast(node->expr.import.path, depth + 1);
break;
case NODE_WHILE:
printf("While:\n");
print_ast(node->expr.whle.condition, depth + 1);
print_ast(node->expr.whle.body, depth + 1);
break;
case NODE_FOR:
printf("For:\n");
print_ast(node->expr.fr.slices, depth + 1);
print_ast(node->expr.fr.captures, depth + 1);
print_indent(depth + 1);
print_ast(node->expr.fr.body, depth + 1);
break;
case NODE_RANGE:
printf("Range:\n");
print_ast(node->expr.binary.left, depth + 1);
print_ast(node->expr.binary.right, depth + 1);
break;
default:
printf("Unknown Node Type: %d\n", node->type);
break;
}
}
int main(void)
{
FILE *fp = fopen("examples/hello_world.l", "r");
usize size = 0;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *src = malloc(size+1);
fread(src, size, 1, fp);
fclose(fp);
src[size] = '\0';
arena a = arena_init(0x1000 * 0x1000 * 64);
lexer *l = lexer_init(src, size, &a);
parser *p = parser_init(l, &a);
print_ast(p->ast, 0);
sema *s = sema_init(p, &a);
arena_deinit(a);
return 0;
}