- 重命名和重构了多个文件,包括 lexer、parser 和 AST 相关代码 - 添加了日志功能,使用 LOG_* 宏替代原有的 error 和 warn 函数 - 优化了错误处理和内存分配方式 - 调整了代码结构,提高了模块化和可读性
52 lines
1.5 KiB
C
52 lines
1.5 KiB
C
#include "../ast.h"
|
|
#include "../parser.h"
|
|
#include "../type.h"
|
|
|
|
ast_node_t* new_ast_ident_node(tok_t* tok) {
|
|
if (tok->type != TOKEN_IDENT) {
|
|
LOG_ERROR("syntax error: want identifier but got %d", tok->type);
|
|
}
|
|
ast_node_t* node = new_ast_node();
|
|
node->type = NT_TERM_IDENT;
|
|
node->syms.tok = *tok;
|
|
node->syms.decl_node = NULL;
|
|
return node;
|
|
}
|
|
|
|
ast_node_t* expect_pop_ident(tok_stream_t* tokbuf) {
|
|
flush_peek_tok(tokbuf);
|
|
tok_t* tok = peek_tok(tokbuf);
|
|
ast_node_t* node = new_ast_ident_node(tok);
|
|
pop_tok(tokbuf);
|
|
return node;
|
|
}
|
|
|
|
ast_node_t* parse_type(parser_t* parser) {
|
|
tok_stream_t* tokbuf = &parser->tokbuf;
|
|
flush_peek_tok(tokbuf);
|
|
tok_type_t ttype = peek_tok_type(tokbuf);
|
|
data_type_t dtype;
|
|
switch(ttype) {
|
|
case TOKEN_VOID: dtype = TYPE_VOID; break;
|
|
case TOKEN_CHAR: dtype = TYPE_CHAR; break;
|
|
case TOKEN_SHORT: dtype = TYPE_SHORT; break;
|
|
case TOKEN_INT: dtype = TYPE_INT; break;
|
|
case TOKEN_LONG: dtype = TYPE_LONG; break;
|
|
case TOKEN_FLOAT: dtype = TYPE_FLOAT; break;
|
|
case TOKEN_DOUBLE: dtype = TYPE_DOUBLE; break;
|
|
default:
|
|
LOG_ERROR("无效的类型说明符");
|
|
}
|
|
|
|
ast_node_t* node = new_ast_node();
|
|
node->type = NT_TERM_TYPE;
|
|
// TODO added by disable warning, will add typing system
|
|
dtype += 1;
|
|
pop_tok(tokbuf);
|
|
|
|
if (peek_tok_type(tokbuf) == TOKEN_MUL) {
|
|
pop_tok(tokbuf);
|
|
}
|
|
return node;
|
|
}
|