stage1 完成sema词法

This commit is contained in:
zzy
2026-08-04 10:20:57 +08:00
parent 74d7376039
commit 8ac59dfaa1
14 changed files with 2309 additions and 47 deletions

View File

@@ -12,6 +12,7 @@
#include "spl_ast.h"
#include "spl_lexer.h"
#include "spl_sema.h"
#include "spl_tok.h"
static char *read_file(const char *path, long *out_len) {
@@ -65,6 +66,42 @@ static void dump_ast(const char *src, const char *fname) {
spl_ast_drop(&ast);
}
static void dump_sema(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 = *
spl_sema_run(&sema);
spl_sema_check(&sema);
printf("Sema root_scope=%zu scopes=%zu errors=%d\n", sema.root_scope, sema.scopes.size,
sema.error_count);
for (usize i = 0; i < sema.scopes.size; i++) {
printf("scope[%zu] parent=%zu\n", i, sema.scopes.data[i].parent);
map_for(sema.scopes.data[i].symbols, mi) {
printf(" %s -> def#%zu\n", sema.scopes.data[i].symbols.data[mi].key,
sema.scopes.data[i].symbols.data[mi].val);
}
}
printf("TypeTable:\n");
for (usize i = 0; i < sema.type.type_table.size; i++) {
printf(" id#%zu type=", i);
spl_type_pure_dump(&sema.type, i);
printf("\n");
}
printf("DefTable:\n");
for (usize i = 0; i < sema.type.def_table.size; i++) {
printf(" def#%zu ", i);
spl_type_def_dump(&sema.type, i);
printf("\n");
}
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);
@@ -72,10 +109,13 @@ static int cmd_dump(const char *flags, const char *path) {
return 1;
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;
if (do_tokens)
dump_tokens(src, path);
if (do_ast)
dump_ast(src, path);
if (do_sema)
dump_sema(src, path);
free(src);
return 0;
}
@@ -87,15 +127,20 @@ 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,all\n");
LOG_INFO("splc0 --dump <flags> <file> dump: tokens,ast,sema,all\n");
return 0;
}
if (strcmp(argv[1], "--dump") == 0) {
if (argc < 4) {
int argi = 1;
if (argi >= argc) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]\n");
return 1;
}
if (strcmp(argv[argi], "--dump") == 0) {
if (argc < argi + 3) {
LOG_INFO("splc0: --dump need <flags> <file>\n");
return 1;
}
return cmd_dump(argv[2], argv[3]);
return cmd_dump(argv[argi + 1], argv[argi + 2]);
}
LOG_FATAL("splc0: compile todo\n");
return 1;