-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.c
More file actions
489 lines (429 loc) · 13.5 KB
/
Copy pathparser.c
File metadata and controls
489 lines (429 loc) · 13.5 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "parser.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const char *token_type_to_string(TokenType type)
{
switch (type)
{
case TOKEN_EOF:
return "EOF";
case TOKEN_UNKNOWN:
return "UNKNOWN";
case TOKEN_SAY:
return "SAY";
case TOKEN_STRING:
return "STRING";
case TOKEN_SEMI:
return "SEMI";
case TOKEN_END:
return "END";
case TOKEN_NEWLINE:
return "NEWLINE";
case TOKEN_INDENT:
return "INDENT";
case TOKEN_DEDENT:
return "DEDENT";
case TOKEN_FUNCTION:
return "FUNCTION";
case TOKEN_IDENTIFIER:
return "IDENTIFIER";
case TOKEN_COLON:
return "COLON";
default:
return "UNRECOGNIZED";
}
}
Parser *new_parser(Lexer *lexer)
{
Parser *parser = malloc(sizeof(Parser));
parser->lexer = lexer;
parser->current_token = next_token(lexer);
parser->current_indent = 0; // 初始缩进深度为0
return parser;
}
void free_parser(Parser *parser)
{
free_token(parser->current_token);
free(parser->lexer);
free(parser);
}
void free_token(Token *token)
{
if (token)
{
free(token->value);
free(token);
}
}
void eat(Parser *parser, TokenType type)
{
if (parser->current_token->type == type)
{
free_token(parser->current_token);
parser->current_token = next_token(parser->lexer);
}
else
{
printf("Syntax error: Expected token type %d (%s), but got token type %d (%s)\n",
type, token_type_to_string(type), parser->current_token->type, token_type_to_string(parser->current_token->type));
exit(1);
}
}
ASTNode *parse_statement(Parser *parser)
{
// 跳过无关token
while (parser->current_token->type == TOKEN_DEDENT ||
parser->current_token->type == TOKEN_NEWLINE ||
parser->current_token->type == TOKEN_INDENT)
{
// 更新缩进状态
if (parser->current_token->type == TOKEN_INDENT)
{
parser->current_indent++;
}
else if (parser->current_token->type == TOKEN_DEDENT)
{
parser->current_indent--;
}
eat(parser, parser->current_token->type);
}
// 打印调试信息
printf("[PARSER] parse_statement token: %s (%d)\n",
token_type_to_string(parser->current_token->type),
parser->current_token->type);
// 识别不同语句类型
switch (parser->current_token->type)
{
case TOKEN_SAY:
return parse_say_statement(parser);
case TOKEN_FUNCTION:
return parse_function_definition(parser);
case TOKEN_IDENTIFIER:
return parse_function_call(parser);
}
// 未知语句类型
fprintf(stderr, "Syntax error: Unknown statement. Got token %d (%s)\n",
parser->current_token->type,
token_type_to_string(parser->current_token->type));
exit(1);
}
ASTNode *parse_say_statement(Parser *parser)
{
eat(parser, TOKEN_SAY); // 消耗'say' token
// 确保下一个token是字符串
if (parser->current_token->type != TOKEN_STRING)
{
fprintf(stderr, "Syntax error: Expected string after 'say'\n");
exit(1);
}
char *str_value = parser->current_token->value ? strdup(parser->current_token->value) : strdup("");
eat(parser, TOKEN_STRING); // 消耗字符串token
return create_say_node(str_value);
}
ASTNode *parse_function_definition(Parser *parser)
{
printf("[PARSER] Parsing function definition\n");
// 消耗 function 关键字
eat(parser, TOKEN_FUNCTION);
// 检查函数名
if (parser->current_token->type != TOKEN_IDENTIFIER)
{
fprintf(stderr, "Syntax error: Expected function name after 'function'. Got token %d (%s)\n",
parser->current_token->type,
token_type_to_string(parser->current_token->type));
exit(1);
}
char *func_name = strdup(parser->current_token->value);
eat(parser, TOKEN_IDENTIFIER);
printf(" Function name: '%s'\n", func_name);
// 检查冒号
if (parser->current_token->type != TOKEN_COLON)
{
fprintf(stderr, "Syntax error: Expected colon after function name. Got token %d (%s)\n",
parser->current_token->type,
token_type_to_string(parser->current_token->type));
exit(1);
}
eat(parser, TOKEN_COLON);
// 处理函数定义后的可能空白
int first_token = 1;
// 解析函数体
ASTNode **body = NULL;
int body_capacity = 8; // 初始容量
int body_count = 0;
parser->current_indent = -1; // 标记函数体缩进级别未设置
// 初始化body数组
body = malloc(body_capacity * sizeof(ASTNode *));
if (!body)
{
fprintf(stderr, "Memory allocation failed for function body\n");
exit(1);
}
// 直到遇到end或DEDENT
while (1)
{
// 处理空白token
while (parser->current_token->type == TOKEN_NEWLINE ||
parser->current_token->type == TOKEN_INDENT ||
parser->current_token->type == TOKEN_DEDENT)
{
// 第一次遇到缩进,设置当前缩进级别
if (parser->current_token->type == TOKEN_INDENT &&
parser->current_indent == -1)
{
parser->current_indent = parser->lexer->indent_stack[parser->lexer->indent_top];
printf(" Function body indent set to: %d\n", parser->current_indent);
}
eat(parser, parser->current_token->type);
}
// 检查结束条件
if (parser->current_token->type == TOKEN_END)
{
break;
}
// 如果遇到DEDENT,检查是否已经返回到函数定义层级
if (parser->current_token->type == TOKEN_DEDENT &&
parser->current_indent != -1 &&
parser->lexer->indent_stack[parser->lexer->indent_top] < parser->current_indent)
{
printf(" Exiting function body at indent: %d (current: %d)\n",
parser->current_indent, parser->lexer->indent_stack[parser->lexer->indent_top]);
break;
}
// 遇到函数体中的语句
printf(" Parsing function body statement (%s)\n", token_type_to_string(parser->current_token->type));
// 检查并扩展数组容量
if (body_count >= body_capacity)
{
body_capacity *= 2;
ASTNode **new_body = realloc(body, body_capacity * sizeof(ASTNode *));
if (!new_body)
{
fprintf(stderr, "Memory reallocation failed for function body\n");
exit(1);
}
body = new_body;
}
// 解析语句并存储
ASTNode *stmt = parse_statement(parser);
body[body_count++] = stmt;
}
// 消耗end关键字
if (parser->current_token->type == TOKEN_END)
{
eat(parser, TOKEN_END);
}
else
{
fprintf(stderr, "Syntax error: Expected 'end' to close function definition. Got %d (%s)\n",
parser->current_token->type,
token_type_to_string(parser->current_token->type));
exit(1);
}
// 重置缩进级别
parser->current_indent = 0;
printf("Successfully parsed function '%s' with %d statements\n", func_name, body_count);
ASTNode *result = create_function_def_node(func_name, body, body_count);
if (!result)
{
free(body);
return NULL;
}
return result;
}
ASTNode *parse_function_call(Parser *parser)
{
if (parser->current_token->type != TOKEN_IDENTIFIER)
{
fprintf(stderr, "Syntax error: Expected function name\n");
exit(1);
}
char *func_name = strdup(parser->current_token->value);
eat(parser, TOKEN_IDENTIFIER);
return create_function_call_node(func_name);
}
ASTNode *parse_block(Parser *parser, int *count)
{
*count = 0;
ASTNode **nodes = NULL;
int nodes_capacity = 0;
while (1)
{
// 处理行内Token
while (parser->current_token->type == TOKEN_NEWLINE ||
parser->current_token->type == TOKEN_INDENT)
{ // 添加对缩进Token的处理
eat(parser, parser->current_token->type);
}
// 块结束检查
if (parser->current_token->type == TOKEN_DEDENT ||
parser->current_token->type == TOKEN_END)
{
break;
}
if (*count >= nodes_capacity)
{
nodes_capacity = nodes_capacity ? nodes_capacity * 2 : 8;
ASTNode **new_nodes = realloc(nodes, nodes_capacity * sizeof(ASTNode *));
if (!new_nodes)
{
fprintf(stderr, "Memory reallocation failed for block nodes\n");
exit(1);
}
nodes = new_nodes;
}
nodes[*count] = parse_statement(parser);
(*count)++;
}
ASTNode *block = create_block_node(nodes, *count);
if (!block)
{
for (int i = 0; i < *count; i++)
{
free_node(nodes[i]);
}
free(nodes);
return NULL;
}
free(nodes);
return block;
}
ASTNode **parse_program(Parser *parser, int *count)
{
*count = 0;
ASTNode **nodes = NULL;
int nodes_capacity = 0;
// 允许函数定义出现在程序开头
while (parser->current_token->type != TOKEN_EOF)
{
// 跳过缩进和换行符
while (parser->current_token->type == TOKEN_NEWLINE ||
parser->current_token->type == TOKEN_INDENT ||
parser->current_token->type == TOKEN_DEDENT)
{
eat(parser, parser->current_token->type);
}
// 检查是否达到文件末尾
if (parser->current_token->type == TOKEN_EOF)
{
break;
}
// 检查是否遇到start关键字
if (parser->current_token->type == TOKEN_START)
{
break;
}
// 解析函数定义
if (*count >= nodes_capacity)
{
nodes_capacity = nodes_capacity ? nodes_capacity * 2 : 8;
ASTNode **new_nodes = realloc(nodes, nodes_capacity * sizeof(ASTNode *));
if (!new_nodes)
{
fprintf(stderr, "Memory reallocation failed for program nodes\n");
exit(1);
}
nodes = new_nodes;
}
// 解析其他语句(包括函数定义)
ASTNode *node = parse_statement(parser);
if (node)
{
nodes[(*count)++] = node;
}
}
// 程序必须以start开始
if (parser->current_token->type != TOKEN_START)
{
fprintf(stderr, "Syntax error: Program must contain 'start:' block\n");
exit(1);
}
eat(parser, TOKEN_START); // 消耗start token
// 处理可选的换行符
while (parser->current_token->type == TOKEN_NEWLINE)
{
eat(parser, TOKEN_NEWLINE);
}
// 必须有缩进
if (parser->current_token->type != TOKEN_INDENT)
{
fprintf(stderr, "Syntax error: Expected indentation after 'start:'\n");
exit(1);
}
eat(parser, TOKEN_INDENT);
parser->current_indent++;
// 解析程序主体
while (parser->current_token->type != TOKEN_EOF)
{
// 处理缩出(从当前缩进级别退出)
if (parser->current_token->type == TOKEN_DEDENT)
{
eat(parser, TOKEN_DEDENT);
parser->current_indent--;
// 当缩进级别回到0时,准备退出程序块
if (parser->current_indent == 0)
{
break;
}
continue;
}
// 跳过换行符
if (parser->current_token->type == TOKEN_NEWLINE)
{
eat(parser, TOKEN_NEWLINE);
continue;
}
// 处理end关键字(提前退出)
if (parser->current_token->type == TOKEN_END)
{
break;
}
// 确保不超过最大语句数
if (*count >= nodes_capacity)
{
nodes_capacity = nodes_capacity ? nodes_capacity * 2 : 8;
nodes = realloc(nodes, nodes_capacity * sizeof(ASTNode *));
}
// 解析语句
nodes[(*count)++] = parse_statement(parser);
}
// 在缩出循环后,跳过所有换行符和DEDENT
while (parser->current_token->type == TOKEN_NEWLINE ||
parser->current_token->type == TOKEN_DEDENT)
{
eat(parser, parser->current_token->type);
}
// 处理end关键字
if (parser->current_token->type == TOKEN_EOF)
{
fprintf(stderr, "Syntax error: Program must end with 'end'\n");
exit(1);
}
if (parser->current_token->type != TOKEN_END)
{
fprintf(stderr, "Syntax error: Expected 'end' at end of program. Got token type %d (%s)\n",
parser->current_token->type,
token_type_to_string(parser->current_token->type));
exit(1);
}
eat(parser, TOKEN_END);
// 确保缩进级别回到0
if (parser->current_indent != 0)
{
// 处理剩余的缩出标记
while (parser->current_token->type == TOKEN_DEDENT)
{
eat(parser, TOKEN_DEDENT);
parser->current_indent--;
}
// 如果还有剩余的缩进级别
if (parser->current_indent != 0)
{
fprintf(stderr, "Syntax error: Missing dedent at end of program (indent level=%d)\n",
parser->current_indent);
exit(1);
}
}
return nodes;
}