stage1 解决@self无法在文件内直接使用的问题

This commit is contained in:
zzy committed 2026-09-17 16:30:38 +08:00
1 parent acfbbeee8b
commit 2594a81034
3 files changed
+81 -46

No files matched your search

+57 -30
View File
@@ -288,32 +288,55 @@ static const char *stage_dir_of(const char *stage) {
}
typedef bool (*test_fn_t)(const char *fname, void *user_data);
static bool run_test(Nob_Files *files, test_fn_t test_once, void *user_data) {
const char *test_folder = "./tests/stage1/";
typedef struct {
Nob_Files *files;
test_fn_t test_once;
void *user_data;
} test_walk_ctx_t;
if (!prepare_build()) {
return 1;
}
static bool test_walk_entry(Nob_Walk_Entry entry) {
if (entry.type != NOB_FILE_REGULAR)
return true;
Nob_File_Paths paths = {0};
nob_read_entire_dir(test_folder, &paths);
for (int i = 0; i < files->count; i += 1) {
for (int j = 0; j < paths.count; j += 1) {
String_View sv = sv_from_cstr(paths.items[j]);
// printf("--- test " SV_Fmt " ---\n", SV_Arg(sv));
if (!strstr(paths.items[j], files->items[i])) {
continue;
}
const char *tname = nob_temp_sprintf("%s%s", test_folder, paths.items[j]);
printf("--- test %s ---\n", tname);
if (test_once(tname, user_data) == false)
return false;
}
const char *dot = strrchr(entry.path, '.');
if (!dot || strcmp(dot, ".spl") != 0)
return true;
test_walk_ctx_t *ctx = (test_walk_ctx_t *)entry.data;
bool run_all = (ctx->files->count == 0);
bool matched = run_all;
for (int i = 0; !matched && i < ctx->files->count; i += 1) {
if (strstr(entry.path, ctx->files->items[i]))
matched = true;
}
if (!matched)
return true;
printf("--- test %s ---\n", entry.path);
if (ctx->test_once(entry.path, ctx->user_data) == false)
return false;
return true;
}
static bool run_test(const char *stage, Nob_Files *files, test_fn_t test_once, void *user_data) {
const char *test_folder = stage_dir_of(stage);
if (!prepare_build())
return false;
if (!nob_file_exists(test_folder)) {
nob_log(NOB_INFO, "%s does not exist, skip", test_folder);
return true;
}
test_walk_ctx_t ctx = {
.files = files,
.test_once = test_once,
.user_data = user_data,
};
return nob_walk_dir(test_folder, test_walk_entry, .data = &ctx);
}
static bool delete_walk_entry(Nob_Walk_Entry entry) { return nob_delete_file(entry.path); }
static bool delete_directory_recursively(const char *dir_path) {
@@ -387,33 +410,37 @@ int main(int argc, char **argv) {
}
if (strcmp(command_name, "test") == 0) {
Nob_Files files = {0};
// 1. stage(可选,默认 stage1)
const char *stage = "stage1";
if (argc > 0 && is_stage_name(argv[0])) {
stage = nob_shift(argv, argc);
}
// 2. mode(可选)
test_fn_t tfn = test_once;
const char *mode_name = NULL;
if (argc > 0) {
mode_name = nob_shift(argv, argc);
if (strcmp(mode_name, "lex") == 0 || strcmp(mode_name, "ast") == 0 ||
strcmp(mode_name, "sema") == 0 || strcmp(mode_name, "ir") == 0 ||
strcmp(mode_name, "vm") == 0) {
if (is_dump_mode(argv[0])) {
tfn = test_once_with_dump;
} else if (strcmp(mode_name, "run") == 0) {
// HACK
mode_name = nob_shift(argv, argc);
} else if (strcmp(argv[0], "run") == 0) {
nob_shift(argv, argc);
// HACK:只对 stage1 有意义,跑上次生成的 build/test.sir
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, BUILD_FOLDER "spl_cli", "-d");
nob_cmd_append(&cmd, BUILD_FOLDER "test.sir");
return nob_cmd_run(&cmd) ? 0 : 1;
} else {
da_append(&files, mode_name);
}
}
// 3. 剩下的都当 pattern
Nob_Files files = {0};
while (argc > 0) {
da_append(&files, nob_shift(argv, argc));
}
return run_test(&files, tfn, (void *)mode_name) ? 0 : 1;
return run_test(stage, &files, tfn, (void *)mode_name) ? 0 : 1;
}
if (strcmp(command_name, "clean") == 0) {
if (nob_file_exists(BUILD_FOLDER)) {
if (!delete_directory_recursively(BUILD_FOLDER))