stage1 修复设计问题 实现ast2ir

This commit is contained in:
zzy
2026-08-05 08:14:23 +08:00
parent 8ac59dfaa1
commit 2ccee5f1cf
14 changed files with 3694 additions and 91 deletions

View File

@@ -11,6 +11,7 @@
#include <string.h>
#include "spl_ast.h"
#include "spl_ast2ir.h"
#include "spl_lexer.h"
#include "spl_sema.h"
#include "spl_tok.h"
@@ -102,6 +103,34 @@ static void dump_sema(const char *src, const char *fname) {
spl_ast_drop(&ast);
}
static void dump_ir(const char *src, const char *fname) {
spl_tok_vec_t toks = spl_lex(src, fname);
spl_ast_t ast;
spl_ast_init(&ast, &toks);
spl_ast_prase(&ast);
spl_ast_valid(&ast);
spl_sema_t sema;
spl_sema_init(&sema);
sema.ast = &ast;
spl_sema_run(&sema);
spl_sema_check(&sema);
if (sema.error_count) {
printf("sema errors=%d, skip IR\n", sema.error_count);
spl_sema_drop(&sema);
spl_ast_drop(&ast);
return;
}
spl_ast2ir_t a2ir;
spl_ast2ir_init(&a2ir, &sema);
spl_ast2ir_run(&a2ir);
if (a2ir.err_count)
printf("ast2ir errors=%d\n", a2ir.err_count);
spl_ir_dump(&a2ir.ir, &sema.type);
spl_ast2ir_drop(&a2ir);
spl_sema_drop(&sema);
spl_ast_drop(&ast);
}
static int cmd_dump(const char *flags, const char *path) {
long len;
char *src = read_file(path, &len);
@@ -110,12 +139,15 @@ static int cmd_dump(const char *flags, const char *path) {
int do_tokens = strstr(flags, "tokens") != NULL || strcmp(flags, "all") == 0;
int do_ast = strstr(flags, "ast") != NULL || strcmp(flags, "all") == 0;
int do_sema = strstr(flags, "sema") != NULL || strcmp(flags, "all") == 0;
int do_ir = strstr(flags, "ir") != NULL || strcmp(flags, "all") == 0;
if (do_tokens)
dump_tokens(src, path);
if (do_ast)
dump_ast(src, path);
if (do_sema)
dump_sema(src, path);
if (do_ir)
dump_ir(src, path);
free(src);
return 0;
}
@@ -127,7 +159,7 @@ int main(int argc, char **argv) {
}
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
LOG_INFO("splc0 <in> <out> compile (.spl -> .sir, 阶段 B)\n");
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,sema,all\n");
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,sema,ir,all\n");
return 0;
}
int argi = 1;