refactor: 重构前端代码并添加日志功能
- 重命名和重构了多个文件,包括 lexer、parser 和 AST 相关代码 - 添加了日志功能,使用 LOG_* 宏替代原有的 error 和 warn 函数 - 优化了错误处理和内存分配方式 - 调整了代码结构,提高了模块化和可读性
This commit is contained in:
		
							
								
								
									
										0
									
								
								ccompiler/frontend/parser/ast-gdb.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								ccompiler/frontend/parser/ast-gdb.py
									
									
									
									
									
										Normal file
									
								
							| @ -1,12 +1,11 @@ | ||||
| #include "ast.h" | ||||
| #include "../parser.h" | ||||
| struct ASTNode* new_ast_node(void) { | ||||
|     struct ASTNode* node = xmalloc(sizeof(struct ASTNode)); | ||||
| ast_node_t* new_ast_node(void) { | ||||
|     ast_node_t* node = rt._malloc(sizeof(ast_node_t)); | ||||
|     init_ast_node(node); | ||||
|     return node; | ||||
| } | ||||
| 
 | ||||
| void init_ast_node(struct ASTNode* node) { | ||||
| void init_ast_node(ast_node_t* node) { | ||||
|     node->type = NT_INIT; | ||||
|      | ||||
|     for (int i = 0; i < sizeof(node->children) / sizeof(node->children[0]); i++) { | ||||
| @ -14,7 +13,7 @@ void init_ast_node(struct ASTNode* node) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // struct ASTNode* find_ast_node(struct ASTNode* node, ast_type_t type) {
 | ||||
| // ast_node_t* find_ast_node(ast_node_t* node, ast_type_t type) {
 | ||||
|      | ||||
| // }
 | ||||
| 
 | ||||
| @ -25,7 +24,7 @@ static void pnt_depth(int depth) { | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // void pnt_ast(struct ASTNode* node, int depth) {
 | ||||
| // void pnt_ast(ast_node_t* node, int depth) {
 | ||||
| //     if (!node) return;
 | ||||
| //     pnt_depth(depth);
 | ||||
| //     switch (node->type) {
 | ||||
| @ -1,10 +1,9 @@ | ||||
| #ifndef __AST_H__ | ||||
| #define __AST_H__ | ||||
| 
 | ||||
| #include "../../frontend.h" | ||||
| #include "../../lexer/lexer.h" | ||||
| #include "../../../../libcore/vector.h" | ||||
| #include "../type.h" | ||||
| #include <lib/utils/ds/vector.h> | ||||
| #include "../lexer/lexer.h" | ||||
| #include "type.h" | ||||
| 
 | ||||
| typedef enum { | ||||
|     NT_INIT, | ||||
| @ -78,98 +77,98 @@ typedef enum { | ||||
|     NT_TERM_TYPE, | ||||
| } ast_type_t; | ||||
| 
 | ||||
| typedef struct ASTNode { | ||||
| typedef struct ast_node { | ||||
|     ast_type_t type; | ||||
| 
 | ||||
|     union { | ||||
|         void *children[6]; | ||||
|         struct { | ||||
|             vector_header(children, struct ASTNode*); | ||||
|             vector_header(children, struct ast_node *); | ||||
|         } root; | ||||
|         struct { | ||||
|             vector_header(children, struct ASTNode*); | ||||
|             vector_header(children, struct ast_node *); | ||||
|         } block; | ||||
|         struct { | ||||
|             struct ASTNode* decl_node; | ||||
|             struct ast_node * decl_node; | ||||
|             tok_t tok; | ||||
|         } syms; | ||||
|         struct { | ||||
|             vector_header(params, struct ASTNode*); | ||||
|             vector_header(params, struct ast_node *); | ||||
|         } params; | ||||
|         struct { | ||||
|             struct ASTNode* name; | ||||
|             struct ASTNode* params; | ||||
|             struct ASTNode* func_decl; | ||||
|             struct ast_node * name; | ||||
|             struct ast_node * params; | ||||
|             struct ast_node * func_decl; | ||||
|         } call; | ||||
|         struct { | ||||
|             struct ASTNode *type; | ||||
|             struct ASTNode *name; | ||||
|             struct ASTNode *expr_stmt; // optional
 | ||||
|             struct ast_node *type; | ||||
|             struct ast_node *name; | ||||
|             struct ast_node *expr_stmt; // optional
 | ||||
|             void* data; | ||||
|         } decl_val; | ||||
|         struct { | ||||
|             struct ASTNode *ret; | ||||
|             struct ASTNode *name; | ||||
|             struct ASTNode *params; // array of params
 | ||||
|             struct ASTNode *def; | ||||
|             struct ast_node *ret; | ||||
|             struct ast_node *name; | ||||
|             struct ast_node *params; // array of params
 | ||||
|             struct ast_node *def; | ||||
|         } decl_func; | ||||
|         struct { | ||||
|             struct ASTNode *decl; | ||||
|             struct ASTNode *body; // optional
 | ||||
|             struct ast_node *decl; | ||||
|             struct ast_node *body; // optional
 | ||||
|             void* data; | ||||
|         } func; | ||||
|         struct { | ||||
|             struct ASTNode *left; | ||||
|             struct ASTNode *right; | ||||
|             struct ASTNode *optional; // optional
 | ||||
|             struct ast_node *left; | ||||
|             struct ast_node *right; | ||||
|             struct ast_node *optional; // optional
 | ||||
|         } expr; | ||||
|         struct { | ||||
|             struct ASTNode *cond; | ||||
|             struct ASTNode *if_stmt;  | ||||
|             struct ASTNode *else_stmt; // optional
 | ||||
|             struct ast_node *cond; | ||||
|             struct ast_node *if_stmt;  | ||||
|             struct ast_node *else_stmt; // optional
 | ||||
|         } if_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *cond; | ||||
|             struct ASTNode *body; | ||||
|             struct ast_node *cond; | ||||
|             struct ast_node *body; | ||||
|         } switch_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *cond; | ||||
|             struct ASTNode *body; | ||||
|             struct ast_node *cond; | ||||
|             struct ast_node *body; | ||||
|         } while_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *body; | ||||
|             struct ASTNode *cond; | ||||
|             struct ast_node *body; | ||||
|             struct ast_node *cond; | ||||
|         } do_while_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *init; | ||||
|             struct ASTNode *cond; // optional
 | ||||
|             struct ASTNode *iter; // optional
 | ||||
|             struct ASTNode *body; | ||||
|             struct ast_node *init; | ||||
|             struct ast_node *cond; // optional
 | ||||
|             struct ast_node *iter; // optional
 | ||||
|             struct ast_node *body; | ||||
|         } for_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *expr_stmt; // optional
 | ||||
|             struct ast_node *expr_stmt; // optional
 | ||||
|         } return_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *label; | ||||
|             struct ast_node *label; | ||||
|         } goto_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *label; | ||||
|             struct ast_node *label; | ||||
|         } label_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *block; | ||||
|             struct ast_node *block; | ||||
|         } block_stmt; | ||||
|         struct { | ||||
|             struct ASTNode *expr_stmt; | ||||
|             struct ast_node *expr_stmt; | ||||
|         } expr_stmt; | ||||
|     }; | ||||
| } ast_node_t; | ||||
| 
 | ||||
| struct ASTNode* new_ast_node(void); | ||||
| void init_ast_node(struct ASTNode* node); | ||||
| void pnt_ast(struct ASTNode* node, int depth); | ||||
| ast_node_t* new_ast_node(void); | ||||
| void init_ast_node(ast_node_t* node); | ||||
| void pnt_ast(ast_node_t* node, int depth); | ||||
| 
 | ||||
| typedef struct parser parser_t; | ||||
| typedef struct ASTNode* (*parse_func_t) (parser_t*); | ||||
| typedef ast_node_t* (*parse_func_t) (parser_t*); | ||||
| 
 | ||||
| void parse_prog(parser_t* parser); | ||||
| ast_node_t* parse_decl(parser_t* parser); | ||||
| @ -182,8 +181,8 @@ ast_node_t* parse_expr(parser_t* parser); | ||||
| ast_node_t* parse_type(parser_t* parser); | ||||
| 
 | ||||
| ast_node_t* new_ast_ident_node(tok_t* tok); | ||||
| ast_node_t* expect_pop_ident(tok_buf_t* tokbuf); | ||||
| ast_node_t* expect_pop_ident(tok_stream_t* tokbuf); | ||||
| 
 | ||||
| int peek_decl(tok_buf_t* tokbuf); | ||||
| int peek_decl(tok_stream_t* tokbuf); | ||||
| 
 | ||||
| #endif | ||||
| @ -1,5 +1,5 @@ | ||||
|  | ||||
| #include "ast.h" | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "../symtab/symtab.h" | ||||
|  | ||||
| @ -17,7 +17,7 @@ ast_node_t* new_ast_node_block() { | ||||
|  | ||||
| ast_node_t* parse_block(parser_t* parser) { | ||||
|     symtab_enter_scope(parser->symtab); | ||||
|     tok_buf_t *tokbuf = &parser->tokbuf; | ||||
|     tok_stream_t *tokbuf = &parser->tokbuf; | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype; | ||||
|     ast_node_t* node = new_ast_node_block(); | ||||
|  | ||||
| @ -1,19 +1,19 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "ast.h" | ||||
| #include "../symtab/symtab.h" | ||||
|  | ||||
| /** | ||||
|  * 0 false | ||||
|  * 1 true | ||||
|  */ | ||||
| int peek_decl(tok_buf_t* tokbuf) { | ||||
| int peek_decl(tok_stream_t* tokbuf) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|     switch (peek_tok_type(tokbuf)) { | ||||
|         case TOKEN_STATIC: | ||||
|         case TOKEN_EXTERN: | ||||
|         case TOKEN_REGISTER: | ||||
|         case TOKEN_TYPEDEF: | ||||
|             error("not impliment"); | ||||
|             LOG_ERROR("not impliment"); | ||||
|             break; | ||||
|         default: | ||||
|             flush_peek_tok(tokbuf); | ||||
| @ -36,7 +36,7 @@ int peek_decl(tok_buf_t* tokbuf) { | ||||
| } | ||||
|  | ||||
| ast_node_t* parse_decl_val(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &parser->tokbuf; | ||||
|     tok_stream_t* tokbuf = &parser->tokbuf; | ||||
|     tok_type_t ttype; | ||||
|     flush_peek_tok(tokbuf); | ||||
|  | ||||
| @ -55,28 +55,28 @@ ast_node_t* parse_decl_val(parser_t* parser) { | ||||
|     if (ttype == TOKEN_ASSIGN) { | ||||
|         node->decl_val.expr_stmt = parse_stmt(parser); | ||||
|         if (node->decl_val.expr_stmt->type != NT_STMT_EXPR) { | ||||
|             error("parser_decl_val want stmt_expr"); | ||||
|             LOG_ERROR("parser_decl_val want stmt_expr"); | ||||
|         } | ||||
|     } else if (ttype == TOKEN_SEMICOLON) { | ||||
|         pop_tok(tokbuf); | ||||
|         expect_pop_tok(tokbuf, TOKEN_SEMICOLON); | ||||
|     } else { | ||||
|         error("parser_decl_val syntax error"); | ||||
|         LOG_ERROR("parser_decl_val syntax error"); | ||||
|     } | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| ast_node_t* parse_decl(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &parser->tokbuf; | ||||
|     tok_stream_t* tokbuf = &parser->tokbuf; | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype; | ||||
|     ast_node_t* node; | ||||
|      | ||||
|     if (peek_decl(tokbuf) == 0) { | ||||
|         error("syntax error expect decl_val TYPE"); | ||||
|         LOG_ERROR("syntax error expect decl_val TYPE"); | ||||
|     } | ||||
|     if (peek_tok_type(tokbuf) != TOKEN_IDENT) { | ||||
|         error("syntax error expect decl_val IDENT"); | ||||
|         LOG_ERROR("syntax error expect decl_val IDENT"); | ||||
|     } | ||||
|  | ||||
|     ttype = peek_tok_type(tokbuf); | ||||
| @ -89,7 +89,7 @@ ast_node_t* parse_decl(parser_t* parser) { | ||||
|             node = parse_decl_val(parser); | ||||
|             break; | ||||
|         default: | ||||
|             error("syntax error expect decl_val ASSIGN or SEMICOLON"); | ||||
|             LOG_ERROR("syntax error expect decl_val ASSIGN or SEMICOLON"); | ||||
|             return NULL; | ||||
|     } | ||||
|     return node; | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "ast.h" | ||||
| #include "../symtab/symtab.h" | ||||
|  | ||||
| // Copy from `CParse` | ||||
| @ -33,7 +33,7 @@ enum ParseType { | ||||
|     PREFIX_PARSER, | ||||
| }; | ||||
|  | ||||
| static ast_node_t *parse_subexpression(tok_buf_t* tokbuf, symtab_t *symtab, enum Precedence prec); | ||||
| static ast_node_t *parse_subexpression(tok_stream_t* tokbuf, symtab_t *symtab, enum Precedence prec); | ||||
| #define NEXT(prec) parse_subexpression(tokbuf, symtab, prec) | ||||
|  | ||||
| static ast_node_t* gen_node2(ast_node_t* left, ast_node_t* right, | ||||
| @ -72,7 +72,7 @@ static ast_node_t* gen_node2(ast_node_t* left, ast_node_t* right, | ||||
|     // } | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_comma(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
| static ast_node_t* parse_comma(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
|     ast_node_t* node = new_ast_node(); | ||||
|     node->type = NT_COMMA; | ||||
|     node->expr.left = left; | ||||
| @ -80,7 +80,7 @@ static ast_node_t* parse_comma(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_assign(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
| static ast_node_t* parse_assign(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
|     pop_tok(tokbuf); | ||||
| @ -124,14 +124,14 @@ static ast_node_t* parse_assign(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* | ||||
|             left = gen_node2(left, NEXT(next), NT_XOR); | ||||
|             break; | ||||
|         default: | ||||
|             error("unsupported operator"); | ||||
|             LOG_ERROR("unsupported operator"); | ||||
|             break; | ||||
|     } | ||||
|     node->expr.right = left; | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_cmp(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
| static ast_node_t* parse_cmp(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
|     pop_tok(tokbuf); | ||||
| @ -164,12 +164,12 @@ static ast_node_t* parse_cmp(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* le | ||||
|             node->expr.right = NEXT(PREC_RELATIONAL); | ||||
|             break; | ||||
|         default: | ||||
|             error("invalid operator"); | ||||
|             LOG_ERROR("invalid operator"); | ||||
|     } | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_cal(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
| static ast_node_t* parse_cal(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
|     pop_tok(tokbuf); | ||||
| @ -230,7 +230,7 @@ static ast_node_t* parse_cal(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* le | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_call(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* ident) { | ||||
| static ast_node_t* parse_call(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* ident) { | ||||
|     ast_node_t* node = new_ast_node(); | ||||
|     node->type = NT_TERM_CALL; | ||||
|     node->call.name = ident; | ||||
| @ -257,14 +257,14 @@ static ast_node_t* parse_call(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* i | ||||
|     ast_node_t* sym = symtab_lookup_symbol(symtab, name); | ||||
|     // TODO check func is match | ||||
|     if (sym == NULL || sym->type != NT_DECL_FUNC) { | ||||
|         error("function not decl %s", name); | ||||
|         LOG_ERROR("function not decl %s", name); | ||||
|     } | ||||
|     node->call.name = ident; | ||||
|     node->call.func_decl = sym; | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t* parse_paren(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
| static ast_node_t* parse_paren(tok_stream_t* tokbuf, symtab_t *symtab, ast_node_t* left) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|     expect_pop_tok(tokbuf, TOKEN_L_PAREN); | ||||
|     left = NEXT(PREC_EXPRESSION); | ||||
| @ -273,7 +273,7 @@ static ast_node_t* parse_paren(tok_buf_t* tokbuf, symtab_t *symtab, ast_node_t* | ||||
|     return left; | ||||
| } | ||||
|  | ||||
| typedef ast_node_t* (*parse_expr_fun_t)(tok_buf_t*, symtab_t* , ast_node_t*); | ||||
| typedef ast_node_t* (*parse_expr_fun_t)(tok_stream_t*, symtab_t* , ast_node_t*); | ||||
| static struct expr_prec_table_t { | ||||
|     parse_expr_fun_t parser; | ||||
|     enum Precedence prec; | ||||
| @ -322,7 +322,7 @@ static struct expr_prec_table_t { | ||||
|     [TOKEN_L_PAREN] = {parse_paren, PREC_POSTFIX, INFIX_PARSER}, | ||||
| }; | ||||
|  | ||||
| static ast_node_t *parse_primary_expression(tok_buf_t* tokbuf, symtab_t *symtab) { | ||||
| static ast_node_t *parse_primary_expression(tok_stream_t* tokbuf, symtab_t *symtab) { | ||||
|     flush_peek_tok(tokbuf); | ||||
|  | ||||
|     tok_t* tok = peek_tok(tokbuf); | ||||
| @ -335,7 +335,7 @@ static ast_node_t *parse_primary_expression(tok_buf_t* tokbuf, symtab_t *symtab) | ||||
|         // node->data.data_type = TYPE_INT; | ||||
|         break; | ||||
|     case TOKEN_FLOAT_LITERAL: | ||||
|         warn("float not supported"); | ||||
|         LOG_WARN("float not supported"); | ||||
|         break; | ||||
|     case TOKEN_CHAR_LITERAL: | ||||
|         // node->data.data_type = TYPE_CHAR; | ||||
| @ -350,7 +350,7 @@ static ast_node_t *parse_primary_expression(tok_buf_t* tokbuf, symtab_t *symtab) | ||||
|         } else { | ||||
|             void *sym = symtab_lookup_symbol(symtab, tok->val.str); | ||||
|             if (sym == NULL) { | ||||
|                 error("undefined symbol but use %s", tok->val.str); | ||||
|                 LOG_ERROR("undefined symbol but use %s", tok->val.str); | ||||
|             } | ||||
|             node->type = NT_TERM_IDENT; | ||||
|             node->syms.decl_node = sym; | ||||
| @ -364,7 +364,7 @@ END: | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| static ast_node_t *parse_subexpression(tok_buf_t* tokbuf, symtab_t *symtab, enum Precedence prec) { | ||||
| static ast_node_t *parse_subexpression(tok_stream_t* tokbuf, symtab_t *symtab, enum Precedence prec) { | ||||
|     tok_type_t                  ttype; | ||||
|     struct expr_prec_table_t*   work; | ||||
|     ast_node_t*                 left; | ||||
| @ -397,7 +397,7 @@ static ast_node_t *parse_subexpression(tok_buf_t* tokbuf, symtab_t *symtab, enum | ||||
| } | ||||
|  | ||||
| ast_node_t* parse_expr(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &(parser->tokbuf); | ||||
|     tok_stream_t* tokbuf = &(parser->tokbuf); | ||||
|     symtab_t *symtab = parser->symtab; | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
| @ -419,7 +419,7 @@ ast_node_t* parse_expr(parser_t* parser) { | ||||
|     case TOKEN_IDENT: | ||||
|         return NEXT(PREC_EXPRESSION); | ||||
|     default: | ||||
|         error("Want expr but not got %s", get_tok_name(ttype)); | ||||
|         LOG_ERROR("Want expr but not got %s", get_tok_name(ttype)); | ||||
|         break; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,13 +1,13 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "../symtab/symtab.h" | ||||
| #include "ast.h" | ||||
|  | ||||
| #ifndef FUNC_PARAM_CACHE_SIZE | ||||
| #define FUNC_PARAM_CACHE_SIZE 32  // 合理初始值,可覆盖99%常见情况 | ||||
| #endif | ||||
|  | ||||
| // TODO 语义分析压入符号表 | ||||
| static void parse_params(parser_t* parser, tok_buf_t* cache, ast_node_t* node) { | ||||
| static void parse_params(parser_t* parser, tok_stream_t* cache, ast_node_t* node) { | ||||
|     flush_peek_tok(cache); | ||||
|     tok_type_t ttype; | ||||
|     ast_node_t *params = new_ast_node(); | ||||
| @ -23,10 +23,10 @@ static void parse_params(parser_t* parser, tok_buf_t* cache, ast_node_t* node) { | ||||
|         case TOKEN_ELLIPSIS: | ||||
|             ttype = peek_tok_type(cache); | ||||
|             if (ttype != TOKEN_R_PAREN) { | ||||
|                 error("... must be a last parameter list (expect ')')"); | ||||
|                 LOG_ERROR("... must be a last parameter list (expect ')')"); | ||||
|             } | ||||
|             // TODO | ||||
|             error("not implement"); | ||||
|             LOG_ERROR("not implement"); | ||||
|             break; | ||||
|         case TOKEN_IDENT: | ||||
|             // TODO 静态数组 | ||||
| @ -62,13 +62,13 @@ static void parse_params(parser_t* parser, tok_buf_t* cache, ast_node_t* node) { | ||||
|             //     flush_peek_tok(tokbuf); | ||||
|             //     continue; | ||||
|             // } | ||||
|             // error("function expected ')' or ','\n"); | ||||
|             // LOG_ERROR("function expected ')' or ','\n"); | ||||
|         } | ||||
|         pop_tok(cache); | ||||
|     } | ||||
| } | ||||
|  | ||||
| ast_type_t check_is_func_decl(tok_buf_t* tokbuf, tok_buf_t* cache) { | ||||
| ast_type_t check_is_func_decl(tok_stream_t* tokbuf, tok_stream_t* cache) { | ||||
|     expect_pop_tok(tokbuf, TOKEN_L_PAREN); | ||||
|     int depth = 1; | ||||
|      | ||||
| @ -76,7 +76,7 @@ ast_type_t check_is_func_decl(tok_buf_t* tokbuf, tok_buf_t* cache) { | ||||
|         tok_t* tok = peek_tok(tokbuf); | ||||
|         pop_tok(tokbuf); | ||||
|         if (cache->size >= cache->cap - 1) { | ||||
|             error("function parameter list too long"); | ||||
|             LOG_ERROR("function parameter list too long"); | ||||
|         } | ||||
|         cache->buf[cache->size++] = *tok; | ||||
|         switch (tok->type) { | ||||
| @ -100,7 +100,7 @@ ast_type_t check_is_func_decl(tok_buf_t* tokbuf, tok_buf_t* cache) { | ||||
|             return NT_FUNC; | ||||
|             break; | ||||
|         default: | ||||
|             error("function define or decl need '{' or ';' but you don't got"); | ||||
|             LOG_ERROR("function define or decl need '{' or ';' but you don't got"); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -114,14 +114,14 @@ static ast_node_t* new_ast_node_funcdecl(ast_node_t* ret, ast_node_t* name) { | ||||
| } | ||||
|  | ||||
| void parse_func(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &(parser->tokbuf); | ||||
|     tok_stream_t* tokbuf = &(parser->tokbuf); | ||||
|     flush_peek_tok(tokbuf); | ||||
|     ast_node_t* ret_node = parse_type(parser); | ||||
|     ast_node_t* name_node = expect_pop_ident(tokbuf); | ||||
|     const char* func_name = name_node->syms.tok.val.str; | ||||
|     ast_node_t* decl = new_ast_node_funcdecl(ret_node, name_node); | ||||
|  | ||||
|     tok_buf_t cache; | ||||
|     tok_stream_t cache; | ||||
|     init_tokbuf(&cache, NULL, NULL); | ||||
|     cache.cap = FUNC_PARAM_CACHE_SIZE; | ||||
|     tok_t buf[FUNC_PARAM_CACHE_SIZE]; | ||||
| @ -132,12 +132,12 @@ void parse_func(parser_t* parser) { | ||||
|     ast_node_t* prev = symtab_add_symbol(parser->symtab, func_name, decl, 1); | ||||
|     if (prev != NULL) { | ||||
|         if (prev->type != NT_DECL_FUNC) { | ||||
|             error("the symbol duplicate old is %d, new is func", prev->type); | ||||
|             LOG_ERROR("the symbol duplicate old is %d, new is func", prev->type); | ||||
|         } | ||||
|         // TODO check redeclare func is match | ||||
|         if (type == NT_FUNC) { | ||||
|             // TODO Free decl; | ||||
|             free(decl); | ||||
|             rt._free(decl); | ||||
|             decl = prev; | ||||
|             goto FUNC; | ||||
|         } | ||||
| @ -151,7 +151,7 @@ void parse_func(parser_t* parser) { | ||||
| FUNC: | ||||
|     // 该data临时用于判断是否重复定义 | ||||
|     if (decl->decl_func.def != NULL) { | ||||
|         error("redefinition of function %s", func_name); | ||||
|         LOG_ERROR("redefinition of function %s", func_name); | ||||
|     } | ||||
|  | ||||
|     ast_node_t* node = new_ast_node(); | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "ast.h" | ||||
|  | ||||
| #ifndef PROG_MAX_NODE_SIZE | ||||
| #define PROG_MAX_NODE_SIZE (1024 * 4) | ||||
| @ -13,7 +13,7 @@ void parse_prog(parser_t* parser) { | ||||
|      * same as | ||||
|      * Program := Declaration* Definition* | ||||
|      */ | ||||
|     tok_buf_t *tokbuf = &(parser->tokbuf); | ||||
|     tok_stream_t *tokbuf = &(parser->tokbuf); | ||||
|     parser->root = new_ast_node(); | ||||
|     ast_node_t* node; | ||||
|     parser->root->type = NT_ROOT; | ||||
|  | ||||
| @ -1,8 +1,8 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "ast.h" | ||||
|  | ||||
| ast_node_t* parse_stmt(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &parser->tokbuf; | ||||
|     tok_stream_t* tokbuf = &parser->tokbuf; | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
|     ast_node_t* node = new_ast_node(); | ||||
| @ -66,7 +66,7 @@ ast_node_t* parse_stmt(parser_t* parser) { | ||||
|         node->do_while_stmt.body = parse_stmt(parser); | ||||
|         ttype = peek_tok_type(tokbuf); | ||||
|         if (ttype != TOKEN_WHILE) { | ||||
|             error("expected while after do"); | ||||
|             LOG_ERROR("expected while after do"); | ||||
|         } | ||||
|         pop_tok(tokbuf); | ||||
|         expect_pop_tok(tokbuf, TOKEN_L_PAREN); | ||||
| @ -83,7 +83,7 @@ ast_node_t* parse_stmt(parser_t* parser) { | ||||
|         pop_tok(tokbuf); | ||||
|         ttype = peek_tok_type(tokbuf); | ||||
|         if (ttype != TOKEN_L_PAREN) { | ||||
|             error("expected ( after for"); | ||||
|             LOG_ERROR("expected ( after for"); | ||||
|         } | ||||
|         pop_tok(tokbuf); | ||||
|  | ||||
| @ -170,7 +170,7 @@ ast_node_t* parse_stmt(parser_t* parser) { | ||||
|         // find symbol table | ||||
|         ttype = peek_tok_type(tokbuf); | ||||
|         if (ttype != TOKEN_IDENT) { | ||||
|             error("expect identifier after goto"); | ||||
|             LOG_ERROR("expect identifier after goto"); | ||||
|         } | ||||
|         expect_pop_tok(tokbuf, TOKEN_SEMICOLON); | ||||
|         // TODO filling label | ||||
| @ -211,7 +211,7 @@ ast_node_t* parse_stmt(parser_t* parser) { | ||||
|     case TOKEN_CASE: { | ||||
|         // TODO label switch | ||||
|         pop_tok(tokbuf); | ||||
|         error("unimplemented switch label"); | ||||
|         LOG_ERROR("unimplemented switch label"); | ||||
|         node->label_stmt.label = parse_expr(parser); | ||||
|         // TODO 该表达式为const int | ||||
|         expect_pop_tok(tokbuf, TOKEN_COLON); | ||||
| @ -234,7 +234,7 @@ ast_node_t* parse_stmt(parser_t* parser) { | ||||
|         flush_peek_tok(tokbuf); | ||||
|         ttype = peek_tok_type(tokbuf); | ||||
|         if (ttype != TOKEN_SEMICOLON) { | ||||
|             error("exp must end with \";\""); | ||||
|             LOG_ERROR("exp must end with \";\""); | ||||
|         } | ||||
|         pop_tok(tokbuf); | ||||
|         node->type = NT_STMT_EXPR; | ||||
|  | ||||
| @ -1,10 +1,10 @@ | ||||
| #include "../ast.h" | ||||
| #include "../parser.h" | ||||
| #include "../type.h" | ||||
| #include "ast.h" | ||||
|  | ||||
| ast_node_t* new_ast_ident_node(tok_t* tok) { | ||||
|     if (tok->type != TOKEN_IDENT) { | ||||
|         error("syntax error: want identifier but got %d", tok->type); | ||||
|         LOG_ERROR("syntax error: want identifier but got %d", tok->type); | ||||
|     } | ||||
|     ast_node_t* node = new_ast_node(); | ||||
|     node->type = NT_TERM_IDENT; | ||||
| @ -13,7 +13,7 @@ ast_node_t* new_ast_ident_node(tok_t* tok) { | ||||
|     return node; | ||||
| } | ||||
|  | ||||
| ast_node_t* expect_pop_ident(tok_buf_t* tokbuf) { | ||||
| 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); | ||||
| @ -22,7 +22,7 @@ ast_node_t* expect_pop_ident(tok_buf_t* tokbuf) { | ||||
| } | ||||
|  | ||||
| ast_node_t* parse_type(parser_t* parser) { | ||||
|     tok_buf_t* tokbuf = &parser->tokbuf; | ||||
|     tok_stream_t* tokbuf = &parser->tokbuf; | ||||
|     flush_peek_tok(tokbuf); | ||||
|     tok_type_t ttype = peek_tok_type(tokbuf); | ||||
|     data_type_t dtype; | ||||
| @ -35,7 +35,7 @@ ast_node_t* parse_type(parser_t* parser) { | ||||
|         case TOKEN_FLOAT:   dtype = TYPE_FLOAT; break; | ||||
|         case TOKEN_DOUBLE:  dtype = TYPE_DOUBLE; break; | ||||
|         default: | ||||
|             error("无效的类型说明符"); | ||||
|             LOG_ERROR("无效的类型说明符"); | ||||
|     } | ||||
|  | ||||
|     ast_node_t* node = new_ast_node(); | ||||
|  | ||||
| @ -1,13 +1,16 @@ | ||||
| #include <lib/core.h> | ||||
| #include "parser.h" | ||||
| #include "type.h" | ||||
|  | ||||
| void init_parser(parser_t* parser, lexer_t* lexer, symtab_t* symtab) { | ||||
|     init_lib_core(); | ||||
|      | ||||
|     parser->cur_node = NULL; | ||||
|     parser->root = NULL; | ||||
|  | ||||
|     parser->lexer = lexer; | ||||
|     parser->symtab = symtab; | ||||
|     init_tokbuf(&parser->tokbuf, lexer, (get_tokbuf_func)get_valid_token); | ||||
|     init_tokbuf(&parser->tokbuf, lexer, (tok_stream_get_func)get_valid_token); | ||||
|     parser->tokbuf.cap = sizeof(parser->TokenBuffer) / sizeof(parser->TokenBuffer[0]); | ||||
|     parser->tokbuf.buf = parser->TokenBuffer; | ||||
| } | ||||
|  | ||||
| @ -1,20 +1,19 @@ | ||||
| #ifndef __PARSER_H__ | ||||
| #define __PARSER_H__ | ||||
|  | ||||
| #include "../frontend.h" | ||||
|  | ||||
| #include "../lexer/lexer.h" | ||||
| #include "ast.h" | ||||
| typedef struct lexer lexer_t; | ||||
| typedef struct symtab symtab_t; | ||||
| #define PARSER_MAX_TOKEN_QUEUE 16 | ||||
|  | ||||
| typedef struct parser { | ||||
|     struct ASTNode* root; | ||||
|     struct ASTNode* cur_node; | ||||
|     ast_node_t* root; | ||||
|     ast_node_t* cur_node; | ||||
|      | ||||
|     lexer_t* lexer; | ||||
|     symtab_t* symtab; | ||||
|     tok_buf_t tokbuf; | ||||
|     tok_stream_t tokbuf; | ||||
|     tok_t TokenBuffer[PARSER_MAX_TOKEN_QUEUE]; | ||||
|     int err_level; | ||||
| } parser_t; | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| // symtab.c | ||||
| #include "../../frontend.h" | ||||
| #include <lib/core.h> | ||||
| #include "scope.h" | ||||
| #include "symtab.h" | ||||
|  | ||||
| @ -24,7 +25,7 @@ void symtab_enter_scope(symtab_t* symtab) { | ||||
| void symtab_leave_scope(symtab_t* symtab) { | ||||
|     Scope * scope = symtab->cur_scope; | ||||
|     if (scope == NULL) { | ||||
|         error("cannot leave NULL scope or global scope"); | ||||
|         LOG_ERROR("cannot leave NULL scope or global scope"); | ||||
|     } | ||||
|     symtab->cur_scope = symtab->cur_scope->parent; | ||||
|     scope_destroy(scope); | ||||
| @ -35,7 +36,7 @@ void* symtab_add_symbol(symtab_t* symtab, const char* name, void* ast_node, int | ||||
|     void* node = scope_lookup_current(scope, name); | ||||
|     if (node != NULL) { | ||||
|         if (!can_duplicate) { | ||||
|             error("duplicate symbol %s", name); | ||||
|             LOG_ERROR("duplicate symbol %s", name); | ||||
|         } | ||||
|         return node; | ||||
|     } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user