stage1 新的ast结构 使用pest描述语法
This commit is contained in:
1 parent
34df303b56
commit
98b35a98e3
12 files changed
+3344
-2168
No files matched your search
@@ -1,9 +1,11 @@
|
||||
#undef UNICODE
|
||||
#define NOB_IMPLEMENTATION
|
||||
|
||||
#define SPL_CFLAGS "-Wall", "-Wextra", "-O0", "-g", "-D_CRT_SECURE_NO_WARNINGS"
|
||||
#define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS)
|
||||
#undef UNICODE
|
||||
#define NOB_IMPLEMENTATION
|
||||
#include "nob.h"
|
||||
#undef NOB_IMPLEMENTATION
|
||||
#include "nob_popen.h"
|
||||
#ifdef _WIN32
|
||||
#include <consoleapi2.h>
|
||||
#endif
|
||||
@@ -15,28 +17,38 @@
|
||||
// object files get rebuilt.
|
||||
static const char *CFLAGS_STAMP = BUILD_FOLDER ".cflags";
|
||||
|
||||
/* clang-format off */
|
||||
#define VM_SRCS "stage0/spl_mcode.c", "stage0/spl_syscall.c", "stage0/spl_vm.c"
|
||||
static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS};
|
||||
static const char *test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS};
|
||||
static const char *splc0_srcs[] = {
|
||||
"stage1/splc0.c", "stage0/spl_mcode.c", "stage1/spl_ast.c", "stage1/spl_lexer.c",
|
||||
"stage1/spl_dumptree.c", "stage1/spl_type.c", "stage1/spl_sema.c", "stage1/spl_ir.c",
|
||||
"stage1/spl_ast2ir.c", "stage1/spl_ir2vm.c",
|
||||
"stage1/splc0.c",
|
||||
"stage1/spl_lexer.c",
|
||||
"stage1/spl_ast.c",
|
||||
"stage1/spl_dumptree.c",
|
||||
"stage1/spl_type.c",
|
||||
"stage1/spl_sema.c",
|
||||
// "stage1/spl_ir.c",
|
||||
// "stage1/spl_ast2ir.c",
|
||||
// "stage1/spl_ir2vm.c",
|
||||
// "stage0/spl_mcode.c",
|
||||
};
|
||||
|
||||
/* clang-format on */
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char **srcs;
|
||||
size_t srcs_count;
|
||||
} Exe_Target;
|
||||
} target_t;
|
||||
|
||||
static Exe_Target exe_targets[] = {
|
||||
static target_t exe_targets[] = {
|
||||
{"spl_cli", spl_cli_srcs, NOB_ARRAY_LEN(spl_cli_srcs)},
|
||||
{"test", test_srcs, NOB_ARRAY_LEN(test_srcs)},
|
||||
{"splc0", splc0_srcs, NOB_ARRAY_LEN(splc0_srcs)},
|
||||
};
|
||||
|
||||
static const Exe_Target *find_exe_target(const char *name) {
|
||||
static const target_t *find_exe_target(const char *name) {
|
||||
for (size_t i = 0; i < NOB_ARRAY_LEN(exe_targets); ++i) {
|
||||
if (strcmp(exe_targets[i].name, name) == 0) {
|
||||
return &exe_targets[i];
|
||||
@@ -105,7 +117,7 @@ static bool build_object(const char *src) {
|
||||
return nob_cmd_run(&cmd);
|
||||
}
|
||||
|
||||
static bool link_exe(const Exe_Target *target) {
|
||||
static bool link_exe(const target_t *target) {
|
||||
const char *exe = nob_temp_sprintf(BUILD_FOLDER "%s", target->name);
|
||||
const char **objs = (const char **)nob_temp_alloc(sizeof(const char *) * target->srcs_count);
|
||||
for (size_t i = 0; i < target->srcs_count; ++i) {
|
||||
@@ -129,7 +141,7 @@ static bool link_exe(const Exe_Target *target) {
|
||||
}
|
||||
|
||||
static bool build_target(const char *name) {
|
||||
const Exe_Target *target = find_exe_target(name);
|
||||
const target_t *target = find_exe_target(name);
|
||||
if (target == NULL) {
|
||||
nob_log(NOB_ERROR, "unknown target `%s`", name);
|
||||
return false;
|
||||
@@ -152,6 +164,143 @@ static bool build_all(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void parse_expect(const char *name, Nob_Cmd_Result *expect) {
|
||||
Nob_String_Builder sb = {0};
|
||||
nob_read_entire_file(name, &sb);
|
||||
Nob_String_View sv = nob_sb_to_sv(sb);
|
||||
while (sv.count > 0) {
|
||||
Nob_String_View line = nob_sv_chop_by_delim(&sv, '\n');
|
||||
line = nob_sv_trim_left(line); // 去掉前导空格
|
||||
|
||||
if (!nob_sv_starts_with(line, nob_sv_from_cstr("//"))) {
|
||||
break;
|
||||
}
|
||||
|
||||
Nob_String_View rest = nob_sv_from_parts(line.data + 2, line.count - 2);
|
||||
rest = nob_sv_trim_left(rest);
|
||||
|
||||
if (rest.count == 0)
|
||||
continue;
|
||||
|
||||
if (nob_sv_starts_with(rest, nob_sv_from_cstr("Expect"))) {
|
||||
break;
|
||||
}
|
||||
rest = nob_sv_from_parts(rest.data + 6, rest.count - 6);
|
||||
rest = nob_sv_trim_left(rest);
|
||||
|
||||
// 按 '=' 分割
|
||||
Nob_String_View key = nob_sv_chop_by_delim(&rest, '=');
|
||||
key = nob_sv_trim(key);
|
||||
Nob_String_View value = nob_sv_trim(rest);
|
||||
|
||||
if (nob_sv_eq(key, nob_sv_from_cstr("retcode"))) {
|
||||
int val = 0;
|
||||
for (size_t i = 0; i < value.count && isdigit(value.data[i]); ++i) {
|
||||
val = val * 10 + (value.data[i] - '0');
|
||||
}
|
||||
expect->exit_code = val;
|
||||
} else if (nob_sv_eq(key, nob_sv_from_cstr("stdout"))) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool test_once(const char *fname) {
|
||||
const char *compiler_path = "splc0";
|
||||
const char *cli_path = "spl_cli";
|
||||
const char *target_sir = BUILD_FOLDER "test.sir";
|
||||
|
||||
Nob_Cmd cmd = {0};
|
||||
if (!build_target(compiler_path)) {
|
||||
return 1;
|
||||
}
|
||||
cmd.count = 0;
|
||||
|
||||
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", compiler_path));
|
||||
nob_cmd_append(&cmd, fname, target_sir);
|
||||
if (!nob_cmd_run(&cmd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!build_target(cli_path)) {
|
||||
return 1;
|
||||
}
|
||||
cmd.count = 0;
|
||||
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", cli_path));
|
||||
nob_cmd_append(&cmd, target_sir);
|
||||
nob_cmd_run(&cmd);
|
||||
|
||||
Nob_Cmd_Result result = {0};
|
||||
|
||||
Nob_String_Builder cmd_str = {0};
|
||||
nob_cmd_render(cmd, &cmd_str);
|
||||
nob_sb_append_null(&cmd_str);
|
||||
nob_cmd_capture_popen(cmd_str.items, &result);
|
||||
// Expect retcode = number
|
||||
|
||||
Nob_Cmd_Result expect = {0};
|
||||
parse_expect(fname, &expect);
|
||||
|
||||
if (expect.exit_code != result.exit_code) {
|
||||
nob_log(NOB_ERROR, "Expect code `%d`, Result code `%d`\n", expect.exit_code,
|
||||
result.exit_code);
|
||||
return false;
|
||||
}
|
||||
|
||||
nob_cmd_result_free(result);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool test_once_ast(const char *fname) {
|
||||
const char *target_name = "splc0";
|
||||
if (!prepare_build())
|
||||
return 1;
|
||||
if (!build_target(target_name))
|
||||
return 1;
|
||||
Nob_Cmd cmd = {0};
|
||||
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
|
||||
nob_cmd_append(&cmd, "--dump", "ast", fname, BUILD_FOLDER "t.sir");
|
||||
return nob_cmd_run(&cmd);
|
||||
}
|
||||
|
||||
static bool test_once_sema(const char *fname) {
|
||||
const char *target_name = "splc0";
|
||||
if (!prepare_build())
|
||||
return 1;
|
||||
if (!build_target(target_name))
|
||||
return 1;
|
||||
Nob_Cmd cmd = {0};
|
||||
nob_cmd_append(&cmd, nob_temp_sprintf(BUILD_FOLDER "%s", target_name));
|
||||
nob_cmd_append(&cmd, "--dump", "sema", fname, BUILD_FOLDER "t.sir");
|
||||
return nob_cmd_run(&cmd);
|
||||
}
|
||||
|
||||
typedef bool (*test_fn_t)(const char *fname);
|
||||
static bool run_test(Nob_Files *files, test_fn_t test_once) {
|
||||
const char *test_folder = "./tests/stage1/";
|
||||
|
||||
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) == false)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!prepare_build()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool delete_walk_entry(Nob_Walk_Entry entry) { return nob_delete_file(entry.path); }
|
||||
|
||||
static bool delete_directory_recursively(const char *dir_path) {
|
||||
@@ -225,13 +374,24 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
|
||||
if (strcmp(command_name, "test") == 0) {
|
||||
if (!prepare_build())
|
||||
return 1;
|
||||
if (!build_target("test"))
|
||||
return 1;
|
||||
Nob_Cmd cmd = {0};
|
||||
nob_cmd_append(&cmd, BUILD_FOLDER "test");
|
||||
return nob_cmd_run(&cmd) ? 0 : 1;
|
||||
Nob_Files files = {0};
|
||||
test_fn_t tfn = test_once;
|
||||
if (argc > 0) {
|
||||
const char *mode_name = nob_shift(argv, argc);
|
||||
if (strcmp(mode_name, "ast") == 0) {
|
||||
tfn = test_once_ast;
|
||||
} else if (strcmp(mode_name, "sema") == 0) {
|
||||
tfn = test_once_sema;
|
||||
} else {
|
||||
da_append(&files, mode_name);
|
||||
}
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
da_append(&files, nob_shift(argv, argc));
|
||||
}
|
||||
|
||||
return run_test(&files, tfn) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (strcmp(command_name, "clean") == 0) {
|
||||
|
||||
Reference in new issue
Block a user