feat(ast): 添加内置类型定义和AST节点初始化函数
添加了完整的内置类型支持,包括整数、浮点数、字符、布尔等基本类型, 以及它们的有符号/无符号变体。同时添加了大量的AST节点初始化函数, 简化了AST节点的创建过程。 BREAKING CHANGE: 重构了AST表达式和声明结构,移除了冗余字段, 统一了命名规范,并修改了函数调用和成员访问的表示方式。
This commit is contained in:
55
src/main.c
55
src/main.c
@@ -158,30 +158,22 @@ static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
|
||||
}
|
||||
}
|
||||
|
||||
static void print_file(scc_lexer_tok_ring_t *ring, const char *file_name) {
|
||||
static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
|
||||
scc_lexer_tok_t tok = {0};
|
||||
int ret = 0;
|
||||
scc_file_t fp = null;
|
||||
cbool is_stdout = scc_strcmp(file_name, "-") == 0;
|
||||
if (!is_stdout) {
|
||||
fp = scc_fopen(file_name, SCC_FILE_WRITE);
|
||||
if (fp == null) {
|
||||
LOG_FATAL("Failed to open file %s", file_name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
scc_ring_next_consume(*ring, tok, ret);
|
||||
if (ret == false || tok.type == SCC_TOK_EOF) {
|
||||
break;
|
||||
}
|
||||
if (is_stdout) {
|
||||
if (fp == scc_stdout) {
|
||||
scc_printf("%s", scc_cstring_as_cstr(&tok.lexeme));
|
||||
} else {
|
||||
usize ret = scc_fwrite(fp, scc_cstring_as_cstr(&tok.lexeme),
|
||||
scc_cstring_len(&tok.lexeme));
|
||||
if (ret != scc_cstring_len(&tok.lexeme)) {
|
||||
LOG_FATAL("Failed to write to file %s", file_name);
|
||||
LOG_FATAL("Failed to write to file");
|
||||
}
|
||||
}
|
||||
scc_lexer_tok_drop(&tok);
|
||||
@@ -225,6 +217,18 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
}
|
||||
scc_argparse_drop(&argparse);
|
||||
|
||||
scc_file_t fp = null;
|
||||
if (config.output_file) {
|
||||
cbool is_stdout = scc_strcmp(config.output_file, "-") == 0;
|
||||
if (!is_stdout) {
|
||||
fp = scc_fopen(config.output_file, SCC_FILE_WRITE);
|
||||
if (fp == null) {
|
||||
LOG_FATAL("Failed to open file %s", config.output_file);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scc_sstream_t sstream;
|
||||
if (scc_sstream_init(&sstream, config.input_file, 1024)) {
|
||||
return 0;
|
||||
@@ -233,12 +237,12 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
scc_lexer_t lexer;
|
||||
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
|
||||
if (config.emit_lex) {
|
||||
scc_lexer_tok_ring_t *tok_ring = scc_lexer_to_ring(
|
||||
&lexer, 8, config.output_file == null ? false : true);
|
||||
if (config.output_file == null) {
|
||||
scc_lexer_tok_ring_t *tok_ring =
|
||||
scc_lexer_to_ring(&lexer, 8, fp == null ? false : true);
|
||||
if (fp == null) {
|
||||
print_ring(tok_ring, config.verbose);
|
||||
} else {
|
||||
print_file(tok_ring, config.output_file);
|
||||
print_file(tok_ring, fp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -260,7 +264,7 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
if (config.output_file == null) {
|
||||
print_ring(tok_ring, config.verbose);
|
||||
} else {
|
||||
print_file(tok_ring, config.output_file);
|
||||
print_file(tok_ring, fp);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -271,15 +275,20 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
scc_ast_translation_unit_t *translation_unit =
|
||||
scc_parse_translation_unit(&parser);
|
||||
|
||||
scc_parser_drop(&parser);
|
||||
scc_pproc_drop(&pproc);
|
||||
scc_lexer_drop(&lexer);
|
||||
scc_sstream_drop(&sstream);
|
||||
// scc_parser_drop(&parser);
|
||||
// scc_pproc_drop(&pproc);
|
||||
// scc_lexer_drop(&lexer);
|
||||
// scc_sstream_drop(&sstream);
|
||||
|
||||
if (config.emit_ast) {
|
||||
scc_tree_dump_ctx_t tree_dump;
|
||||
scc_tree_dump_ctx_init(&tree_dump, true, (void *)scc_fprintf,
|
||||
(void *)scc_stdout);
|
||||
if (fp == null) {
|
||||
scc_tree_dump_ctx_init(&tree_dump, true, (void *)scc_fprintf,
|
||||
(void *)scc_stdout);
|
||||
} else {
|
||||
scc_tree_dump_ctx_init(&tree_dump, false, (void *)scc_fprintf,
|
||||
(void *)fp);
|
||||
}
|
||||
scc_ast_dump_node(&tree_dump, (scc_ast_node_t *)translation_unit);
|
||||
scc_tree_dump_ctx_drop(&tree_dump);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user