ai: 重新设计stage1的spl 让ai重构代码

This commit is contained in:
zzy committed 2026-12-02 12:40:36 +08:00
1 parent 032102f1d7
commit e531aab2d3
44 files changed
+699 -413

No files matched your search

+9 -6
View File
@@ -1,6 +1,6 @@
#define COMMON_CFLAGS "-Wall", "-Wextra", "-D_CRT_SECURE_NO_WARNINGS", "-D_CRT_NONSTDC_NO_DEPRECATE"
#define SPL_CFLAGS COMMON_CFLAGS, "-O0", "-g"
#define SPL_CFLAGS COMMON_CFLAGS, "-I.", "-Istage1/include", "-O0", "-g"
#define SPL_OPTIMIZATION_FLAG COMMON_CFLAGS, "-O2"
#define nob_cc_flags(cmd) nob_cmd_append(cmd, SPL_CFLAGS)
#undef UNICODE
@@ -19,7 +19,7 @@
#define BUILD_FOLDER "build/"
// Content of the .cflags stamp file; must match SPL_CFLAGS above.
#define CFLAGS_STR "-Wall -Wextra -O0 -g"
#define CFLAGS_STR "-Wall -Wextra -I. -Istage1/include -O0 -g"
// Stamp file whose mtime is refreshed whenever the flags change so that all
// object files get rebuilt.
static const char *CFLAGS_STAMP = BUILD_FOLDER ".cflags";
@@ -544,7 +544,8 @@ static bool test_parse_expect(const char *fname, test_expect_t *expect) {
expect->pending = true;
continue;
}
if (nob_sv_eq(key, nob_sv_from_cstr("location"))) { if (expect->has_location) {
if (nob_sv_eq(key, nob_sv_from_cstr("location"))) {
if (expect->has_location) {
malformed = true;
break;
}
@@ -588,6 +589,7 @@ typedef struct {
static const test_suite_t TEST_SUITES[] = {
{"stage1", "./tests/stage1/"},
{"spl_core", "./tests/spl_core/"},
{"stage2", "./tests/stage2/"},
{"stage3", "./tests/stage3/"},
};
@@ -671,7 +673,8 @@ static void test_pending_compile_and_run(const char *fname, test_ctx_t *ctx) {
nob_log(NOB_INFO, "%s: pending (run failed as expected)", fname);
ctx->pending += 1;
} else {
nob_log(NOB_ERROR, "%s: pending test passed unexpectedly; remove `// Expect pending`", fname);
nob_log(NOB_ERROR, "%s: pending test passed unexpectedly; remove `// Expect pending`",
fname);
ctx->failed += 1;
}
nob_cmd_result_free(result);
@@ -1026,8 +1029,8 @@ static int cli_test(nob_cli_t *cli) {
bool walked = walk_spl_files(suite->dir, &cli->test_filters, test_run_file, &ctx);
if (ctx.no_expect > 0)
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed, %d pending (%d without `// Expect`)", suite->name,
target, ctx.passed, ctx.failed, ctx.pending, ctx.no_expect);
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed, %d pending (%d without `// Expect`)",
suite->name, target, ctx.passed, ctx.failed, ctx.pending, ctx.no_expect);
else
nob_log(NOB_INFO, "%s (`%s`): %d passed, %d failed, %d pending", suite->name, target,
ctx.passed, ctx.failed, ctx.pending);
+2 -2
View File
@@ -10,12 +10,12 @@
// 不含 '*' 的条目按字面路径处理;某个模式一条都没匹配到会直接报错。
static const char *stage0_headers[] = {"stage0/*.h"};
static const char *splc1_headers[] = {"stage0/*.h", "stage1/*.h"};
static const char *splc1_headers[] = {"stage0/*.h", "stage1/**/*.h"};
static const char *spl_cli_srcs[] = {"stage0/spl_cli.c", VM_SRCS};
static const char *vm_test_srcs[] = {"stage0/test_spl_vm.c", VM_SRCS};
static const char *splc1_srcs[] = {"stage1/*.c", "stage0/spl_mcode.c"};
static const char *splc1_srcs[] = {"stage1/**/*.c", "stage0/spl_mcode.c"};
#define CRT_DEPS \
"stage0/spl_rt.h", "stage0/spl_rt_core.h", "stage0/spl_asp.h", \
+116 -86
View File
@@ -5,8 +5,12 @@
* -g, --debug-cli interactive debugger (REPL)
* --entry <name> entry function (default: main)
* --trace print every instruction
* --profile native sampling profiler
* --profile-interval <N> profile sampling interval (default: 10000)
* -h, --help show help
*
* 选项可以出现在 <file.sir> 之前或之间;`--` 之后全部作为 spl 程序参数。
*
* spl 程序 argv:argv[0]=file.sir,argv[1..]=args。
* REPL 配合 splc0 -g(.sir 尾部内嵌 debug 段:IR 行 + VAR 行)可显示
* 当前 IR 节点/源行/局部变量。
@@ -21,103 +25,121 @@
#include <stdlib.h>
#include <string.h>
#define SCC_ARGPARSE_IMPLEMENTATION
#define SCC_OPTPARSER_IMPLEMENTATION
#include "../nob/argparse.h"
/* ================================================================
* 命令行解析
* ================================================================ */
typedef struct {
int debug;
int debug_cli;
int trace;
int disasm;
int profile;
cbool debug;
cbool debug_cli;
cbool trace;
cbool disasm;
cbool profile;
int profile_interval;
const char *profile_out;
int stats;
cbool stats;
const char *entry;
const char *path; /* file.sir */
const char **args; /* spl 程序参数(file.sir 之后) */
int nargs;
scc_argparse_list_t args; /* spl 程序参数(file.sir 之后) */
} cli_opts_t;
static void usage(void) {
fprintf(stderr, "Usage: spl_cli [options] <file.sir> [args...]\n"
" -d, --debug stack-canary checks\n"
" -g, --debug-cli interactive debugger (REPL)\n"
" --disasm disasm dump sir\n"
" --entry <name> entry function (default: main)\n"
" --trace print every instruction\n"
" --profile[=N] native sampling profiler (every N insns, default 10000)\n"
" --profile-out <p> profile output prefix (default build/profile)\n"
" --stats print executed instructions / call count at exit\n"
" -h, --help show help\n");
static void add_bool_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
cbool *store) {
scc_argparse_opt_t opt;
scc_argparse_opt_init(&opt, sn, ln, desc);
scc_argparse_spec_setup_bool(&opt.spec, store);
scc_argparse_cmd_add_opt(cmd, &opt);
}
/* options 可穿插;`--` 终止 option 解析(本身不作为 file/args)。
* 第一个非 option 为 file.sir,其后为 spl args。 */
static int parse_args(int argc, const char **argv, cli_opts_t *o) {
static void add_string_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
const char **store) {
scc_argparse_opt_t opt;
scc_argparse_opt_init(&opt, sn, ln, desc);
scc_argparse_spec_setup_string(&opt.spec, store);
scc_argparse_cmd_add_opt(cmd, &opt);
}
static void add_int_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
int *store) {
scc_argparse_opt_t opt;
scc_argparse_opt_init(&opt, sn, ln, desc);
scc_argparse_spec_setup_int(&opt.spec, store);
scc_argparse_cmd_add_opt(cmd, &opt);
}
static void add_arg_string(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
const char **store, cbool required) {
scc_argparse_arg_t arg;
scc_argparse_arg_init(&arg, name, desc);
scc_argparse_spec_setup_string(&arg.spec, store);
arg.spec.flag_required = required;
scc_argparse_cmd_add_arg(cmd, &arg);
}
static void add_arg_list(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
scc_argparse_list_t *store) {
scc_argparse_arg_t arg;
scc_argparse_arg_init(&arg, name, desc);
scc_argparse_spec_setup_list(&arg.spec, store);
scc_argparse_cmd_add_arg(cmd, &arg);
}
/* argparse 的 BOOL 会忽略 `--profile=500` 的尾部值;显式拦掉,避免间隔被悄悄丢掉。 */
static int reject_inline_profile(int argc, const char **argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--") == 0)
break;
if (strncmp(argv[i], "--profile=", 10) == 0) {
fprintf(stderr,
"spl_cli: `%s` is not supported, use `--profile --profile-interval N`\n",
argv[i]);
return -1;
}
}
return 0;
}
/* `--` 之后 optparse 不再识别选项,剩余 token 全部落进 args。 */
static int parse_cli(int argc, const char **argv, cli_opts_t *o) {
memset(o, 0, sizeof *o);
o->entry = "main";
int i = 1;
for (; i < argc; i++) {
const char *a = argv[i];
if (a[0] == '-' && a[1]) {
if (strcmp(a, "--") == 0) {
i++;
break;
}
if (strcmp(a, "-d") == 0 || strcmp(a, "--debug") == 0) {
o->debug = 1;
} else if (strcmp(a, "-g") == 0 || strcmp(a, "--debug-cli") == 0) {
o->debug_cli = 1;
} else if (strcmp(a, "--trace") == 0) {
o->trace = 1;
} else if (strcmp(a, "--disasm") == 0) {
o->disasm = 1;
} else if (strcmp(a, "--stats") == 0) {
o->stats = 1;
} else if (strcmp(a, "--profile") == 0) {
o->profile = 1;
} else if (strncmp(a, "--profile=", 10) == 0) {
o->profile = 1;
o->profile_interval = (int)strtol(a + 10, NULL, 10);
} else if (strcmp(a, "--profile-out") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "spl_cli: --profile-out needs a prefix\n");
if (reject_inline_profile(argc, argv) != 0)
return -1;
scc_argparse_t parser;
scc_argparse_init(&parser, "spl_cli", "SIR VM launcher + gdb-style debugger");
scc_argparse_cmd_t *root = scc_argparse_get_root(&parser);
add_bool_opt(root, 'd', "debug", "stack-canary checks", &o->debug);
add_bool_opt(root, 'g', "debug-cli", "interactive debugger (REPL)", &o->debug_cli);
add_bool_opt(root, 0, "disasm", "disasm dump sir", &o->disasm);
add_string_opt(root, 0, "entry", "entry function (default: main)", &o->entry);
add_bool_opt(root, 0, "trace", "print every instruction", &o->trace);
add_bool_opt(root, 0, "profile", "native sampling profiler", &o->profile);
add_int_opt(root, 0, "profile-interval", "profile sampling interval in insns (default: 10000)",
&o->profile_interval);
add_string_opt(root, 0, "profile-out", "profile output prefix (default: build/profile)",
&o->profile_out);
add_bool_opt(root, 0, "stats", "print executed instructions / call count at exit",
&o->stats);
add_arg_string(root, "file", "SIR bytecode file", &o->path, true);
add_arg_list(root, "args", "arguments passed to the spl program", &o->args);
int rc = scc_argparse_parse(&parser, argc, argv);
if (parser.out.count > 0)
fwrite(parser.out.items, 1, parser.out.count, stdout);
scc_argparse_drop(&parser);
if (rc != SCC_ARGPARSE_ERR_NONE) {
scc_vec_free(o->args);
/* 1 表示 -h/--version 已处理,按成功退出。 */
return rc == SCC_ARGPARSE_ERR_PNT_DEFAULT ? 1 : -1;
}
o->profile_out = argv[++i];
} else if (strcmp(a, "--entry") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "spl_cli: --entry needs a name\n");
return -1;
}
o->entry = argv[++i];
} else if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
usage();
return -1;
} else {
fprintf(stderr, "spl_cli: unknown option '%s'\n", a);
usage();
return -1;
}
} else {
break;
}
}
/* 跳过前导 `--`(pipeline 调用会带 `--` 分隔 file 与 args) */
while (i < argc && strcmp(argv[i], "--") == 0)
i++;
if (i >= argc) {
fprintf(stderr, "spl_cli: missing <file.sir>\n");
usage();
return -1;
}
o->path = argv[i++];
while (i < argc && strcmp(argv[i], "--") == 0)
i++;
o->args = argv + i;
o->nargs = argc - i;
return 0;
}
@@ -640,17 +662,20 @@ static void repl_loop(dbg_t *d) {
int main(int argc, const char **argv) {
cli_opts_t o;
if (parse_args(argc, argv, &o) != 0)
return 1;
int cli_rc = parse_cli(argc, argv, &o);
if (cli_rc != 0)
return cli_rc > 0 ? 0 : 1;
spl_prog_t prog;
if (spl_prog_load_from_file(o.path, &prog) != 0) {
fprintf(stderr, "spl_cli: cannot load '%s'\n", o.path);
scc_vec_free(o.args);
return 1;
}
if (o.disasm) {
spl_prog_dump(&prog);
scc_vec_free(o.args);
return 0;
}
spl_syscall_register(&prog);
@@ -671,6 +696,7 @@ int main(int argc, const char **argv) {
fprintf(stderr, "spl_cli: failed to load prog\n");
spl_vm_drop(&vm);
spl_prog_drop(&prog);
scc_vec_free(o.args);
return 1;
}
if (o.profile) {
@@ -680,21 +706,24 @@ int main(int argc, const char **argv) {
}
/* spl 程序 argv:argv[0]=file.sir(程序名),argv[1..]=args */
const char **spl_argv = (const char **)malloc(sizeof(char *) * (size_t)(o.nargs + 1));
int nargs = (int)o.args.count;
const char **spl_argv = (const char **)malloc(sizeof(char *) * (size_t)(nargs + 1));
if (!spl_argv) {
spl_vm_drop(&vm);
spl_prog_drop(&prog);
scc_vec_free(o.args);
return 1;
}
spl_argv[0] = o.path;
for (int i = 0; i < o.nargs; i++)
spl_argv[i + 1] = o.args[i];
for (int i = 0; i < nargs; i++)
spl_argv[i + 1] = o.args.items[i];
if (spl_vm_prepare(&vm, o.entry, o.nargs + 1, spl_argv, NULL) != 0) {
if (spl_vm_prepare(&vm, o.entry, nargs + 1, spl_argv, NULL) != 0) {
fprintf(stderr, "spl_cli: entry point '%s' not found\n", o.entry);
free(spl_argv);
spl_vm_drop(&vm);
spl_prog_drop(&prog);
scc_vec_free(o.args);
return 1;
}
@@ -735,6 +764,7 @@ int main(int argc, const char **argv) {
free(spl_argv);
spl_vm_drop(&vm);
spl_prog_drop(&prog);
scc_vec_free(o.args);
if (ret < 0)
return 1;
+22 -12
View File
@@ -105,6 +105,8 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_CONTAINER_MEMBERS:
dump_vec(ast, node->container_members.external_tags, stack);
if (node->container_members.module)
dump_node(ast, node->container_members.module, stack, false);
dump_vec(ast, node->container_members.declarations, stack);
break;
@@ -113,8 +115,18 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_vec(ast, node->tag_item.args, stack);
break;
case SPL_AST_MOD_DECL:
dump_vec(ast, node->mod_decl.decorator_list, stack);
dump_vec(ast, node->mod_decl.decls, stack);
break;
case SPL_AST_IMPORT_DECL:
dump_vec(ast, node->import_decl.decorator_list, stack);
break;
case SPL_AST_FN_DECL:
dump_vec(ast, node->fn_decl.tag_list, stack);
dump_vec(ast, node->fn_decl.decorator_list, stack);
dump_vec(ast, node->fn_decl.attr_list, stack);
dump_vec(ast, node->fn_decl.params, stack);
if (node->fn_decl.ret_type)
dump_node(ast, node->fn_decl.ret_type, stack, false);
@@ -124,6 +136,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_VAR_DECL:
case SPL_AST_CONST_DECL:
dump_vec(ast, node->var_const_decl.decorator_list, stack);
dump_vec(ast, node->var_const_decl.attr_list, stack);
if (node->var_const_decl.pattern)
dump_node(ast, node->var_const_decl.pattern, stack, false);
@@ -134,6 +147,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
break;
case SPL_AST_TYPE_DECL:
dump_vec(ast, node->type_decl.decorator_list, stack);
dump_vec(ast, node->type_decl.attr_list, stack);
if (node->type_decl.pattern)
dump_node(ast, node->type_decl.pattern, stack, false);
@@ -142,6 +156,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
break;
case SPL_AST_MEMBER_DECL:
dump_vec(ast, node->member_decl.decorator_list, stack);
dump_vec(ast, node->member_decl.attr_list, stack);
if (node->member_decl.type_expr)
dump_node(ast, node->member_decl.type_expr, stack, false);
@@ -155,13 +170,9 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_node(ast, node->param_decl.type_expr, stack, true);
break;
case SPL_AST_COMPTIME_DECL:
if (node->comptime_decl.body)
dump_node(ast, node->comptime_decl.body, stack, true);
break;
case SPL_AST_TOP_LEVEL_BLOCK:
dump_vec(ast, node->top_level_block.tag_list, stack);
dump_vec(ast, node->top_level_block.decorator_list, stack);
dump_vec(ast, node->top_level_block.attr_list, stack);
if (node->top_level_block.block)
dump_node(ast, node->top_level_block.block, stack, true);
break;
@@ -184,7 +195,6 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
break;
case SPL_AST_STATEMENT_DEFER:
case SPL_AST_STATEMENT_COMPTIME:
if (node->statement_defer.body)
dump_node(ast, node->statement_defer.body, stack, true);
break;
@@ -292,8 +302,8 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_vec(ast, node->aggregate_literal.fields, stack);
break;
case SPL_AST_EXPR_COMPTIME_CALL:
dump_vec(ast, node->comptime_call.args, stack);
case SPL_AST_EXPR_COMPILE_CALL:
dump_vec(ast, node->compile_call.args, stack);
break;
case SPL_AST_EXPR_BLOCK:
@@ -394,8 +404,8 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_vec(ast, node->type_aggregate.members, stack);
break;
case SPL_AST_TYPE_COMPTIME_CALL:
dump_vec(ast, node->type_comptime_call.args, stack);
case SPL_AST_TYPE_COMPILE_CALL:
dump_vec(ast, node->type_compile_call.args, stack);
break;
case SPL_AST_PATTERN_IDENT:
File renamed without changes.
+1 -1
View File
@@ -1,5 +1,5 @@
#include "spl_diag.h"
#include "../stage0/include/color.h"
#include "stage0/include/color.h"
#include <stdarg.h>
File renamed without changes.
File renamed without changes.
@@ -2,7 +2,7 @@
#include "vm_opt.h"
#include "../stage0/include/core_map.h"
#include "stage0/include/core_map.h"
#include <stdlib.h>
+63 -72
View File
@@ -1,13 +1,15 @@
/* splc0.c - SPL compiler CLI (stage 1, 引导用)
/* splc1.c - SPL compiler CLI (stage 1, 引导用)
*
* splc0 --dump tokens|ast|sema|ir|vm <file> dump 前端产物
* splc0 <in> <out> 编译
* splc1 [--dump <none|lex|ast|sema|ir|vm|c>] [-g] [--opt] <in> <out>
*/
#define __SCC_LOG_IMPL_IMPORT_SRC__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#define SCC_ARGPARSE_IMPLEMENTATION
#define SCC_OPTPARSER_IMPLEMENTATION
#include "nob/argparse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ir_opt.h"
@@ -421,81 +423,70 @@ cleanup:
return rc;
}
typedef struct {
const char *in;
const char *out;
int dump_choice;
cbool gen_debug;
cbool opt;
} splc1_cli_t;
/* 顺序必须与 dump_t 一致:choices 存的是下标,这里直接当下标用。 */
static const char *DUMP_MODE_NAMES[] = {"none", "lex", "ast", "sema", "ir", "vm", "c"};
static void add_bool_opt(scc_argparse_cmd_t *cmd, char sn, const char *ln, const char *desc,
cbool *store) {
scc_argparse_opt_t opt;
scc_argparse_opt_init(&opt, sn, ln, desc);
scc_argparse_spec_setup_bool(&opt.spec, store);
scc_argparse_cmd_add_opt(cmd, &opt);
}
static void add_arg_string(scc_argparse_cmd_t *cmd, const char *name, const char *desc,
const char **store, cbool required) {
scc_argparse_arg_t arg;
scc_argparse_arg_init(&arg, name, desc);
scc_argparse_spec_setup_string(&arg.spec, store);
arg.spec.flag_required = required;
scc_argparse_cmd_add_arg(cmd, &arg);
}
int main(int argc, char **argv) {
if (argc < 2) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]");
return 1;
}
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
LOG_INFO("splc0 <in> <out> compile (.spl -> .sir, stage B)");
LOG_INFO("splc0 --dump <flags> <file> dump: lex,ast,sema,ir,vm,none");
return 0;
}
int opt = 0;
for (int ai = 1; ai < argc; ai++)
if (strcmp(argv[ai], "--opt") == 0)
opt = 1;
splc1_cli_t cli = {0};
cli.dump_choice = DUMP_NONE;
int argi = 1;
if (argi >= argc) {
LOG_FATAL("Usage: splc0 [--dump <flags>] <in> [out]");
return 1;
}
scc_argparse_t parser;
scc_argparse_init(&parser, "splc1", "The first spl compiler.");
scc_argparse_cmd_t *root = scc_argparse_get_root(&parser);
dump_t dump = DUMP_NONE;
if (strcmp(argv[argi], "--dump") == 0) {
if (argc < argi + 3) {
LOG_INFO("splc0: --dump need <flags> <file>");
return 1;
}
scc_argparse_opt_t dump_opt;
scc_argparse_opt_init(&dump_opt, 0, "dump", "dump a front-end product");
scc_argparse_spec_setup_choices(&dump_opt.spec, DUMP_MODE_NAMES,
(int)(sizeof DUMP_MODE_NAMES / sizeof DUMP_MODE_NAMES[0]),
&cli.dump_choice);
scc_argparse_opt_set_group(&dump_opt, "Dump");
scc_argparse_cmd_add_opt(root, &dump_opt);
const char *flags = argv[argi + 1];
if (strcmp(flags, "lex") == 0) {
dump = DUMP_TOKEN;
} else if (strcmp(flags, "ast") == 0) {
dump = DUMP_AST;
} else if (strcmp(flags, "sema") == 0) {
dump = DUMP_SEMA;
} else if (strcmp(flags, "ir") == 0) {
dump = DUMP_IR;
} else if (strcmp(flags, "vm") == 0) {
dump = DUMP_VM;
} else if (strcmp(flags, "c") == 0) {
dump = DUMP_C;
} else if (strcmp(flags, "none") == 0) {
dump = DUMP_NONE;
} else {
LOG_FATAL("splc0: --dump takes one of lex|ast|sema|ir|vm|c|none, got `%s`", flags);
return 1;
}
argi += 2;
}
/* splc0 [-g] <in> <out> */
if (argc < argi + 2) {
LOG_FATAL("Usage: splc0 <in> <out> [-g] or splc0 --dump <flags> <file>\n");
return 1;
}
int gen_debug = 0;
while (argi < argc - 1) {
if (strcmp(argv[argi], "-g") == 0) {
gen_debug = 1;
argi++;
} else if (strcmp(argv[argi], "--opt") == 0) {
opt = 1;
argi++;
} else {
break;
}
}
if (argc < argi + 2) {
LOG_FATAL("Usage: splc0 <in> <out> [-g] [--opt] or splc0 --dump <flags> <file>\n");
return 1;
add_bool_opt(root, 'g', NULL, "emit debug info", &cli.gen_debug);
add_bool_opt(root, 0, "opt", "enable IR/bytecode optimizations", &cli.opt);
add_arg_string(root, "in", "source file", &cli.in, true);
add_arg_string(root, "out", "output file", &cli.out, true);
int rc = scc_argparse_parse(&parser, argc, (const char **)argv);
if (parser.out.count > 0)
fwrite(parser.out.items, 1, parser.out.count, stdout);
if (rc != SCC_ARGPARSE_ERR_NONE) {
scc_argparse_drop(&parser);
return rc == SCC_ARGPARSE_ERR_PNT_DEFAULT ? 0 : 1;
}
scc_argparse_drop(&parser);
long len;
char *src = read_file(argv[argi], &len);
char *src = read_file(cli.in, &len);
if (!src)
return 1;
int rc = compile_spl(src, argv[argi], argv[argi + 1], gen_debug, dump, opt);
rc = compile_spl(src, cli.in, cli.out, cli.gen_debug, (dump_t)cli.dump_choice, cli.opt);
free(src);
return rc;
}
File renamed without changes.
+34 -17
View File
@@ -1,7 +1,7 @@
#ifndef __SPL_AST_H__
#define __SPL_AST_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_attr.h"
#include "spl_dbg.h"
#include "spl_tok.h"
@@ -13,7 +13,8 @@
X(SPL_AST_CONTAINER_MEMBERS, V0, container_members) \
X(SPL_AST_EXTERNAL_TAG_ITEM, V0, external_tag_item) \
X(SPL_AST_TAG_ITEM, V0, tag_item) \
X(SPL_AST_COMPTIME_DECL, V0, comptime_decl) \
X(SPL_AST_MOD_DECL, V0, mod_decl) \
X(SPL_AST_IMPORT_DECL, V0, import_decl) \
X(SPL_AST_TOP_LEVEL_BLOCK, V0, top_level_block) \
X(SPL_AST_FN_DECL, V0, fn_decl) \
X(SPL_AST_VAR_DECL, V0, var_decl) \
@@ -25,7 +26,6 @@
X(SPL_AST_STATEMENT_BREAK, V0, statement_break) \
X(SPL_AST_STATEMENT_CONTINUE, V0, statement_continue) \
X(SPL_AST_STATEMENT_DEFER, V0, statement_defer) \
X(SPL_AST_STATEMENT_COMPTIME, V0, statement_comptime) \
X(SPL_AST_STATEMENT_DECL, V0, statement_decl) \
X(SPL_AST_STATEMENT_EXPR, V0, statement_expr) \
X(SPL_AST_AGGREGATE_MEMBER, V0, aggregate_member) \
@@ -50,7 +50,7 @@
X(SPL_AST_EXPR_TUPLE, V0, tuple) \
X(SPL_AST_EXPR_ARRAY_LIT, V0, array_literal) \
X(SPL_AST_EXPR_AGGREGATE_LIT, V0, aggregate_literal) \
X(SPL_AST_EXPR_COMPTIME_CALL, V0, comptime_call) \
X(SPL_AST_EXPR_COMPILE_CALL, V0, compile_call) \
X(SPL_AST_EXPR_BLOCK, V0, block) \
X(SPL_AST_EXPR_IF, V0, if_expr) \
X(SPL_AST_EXPR_IFVAR, V0, ifvar_expr) \
@@ -66,7 +66,7 @@
X(SPL_AST_TYPE_FN, V0, type_fn) \
X(SPL_AST_TYPE_PAREN, V0, type_paren) \
X(SPL_AST_TYPE_AGGREGATE, V0, type_aggregate) \
X(SPL_AST_TYPE_COMPTIME_CALL, V0, type_comptime_call) \
X(SPL_AST_TYPE_COMPILE_CALL, V0, type_compile_call) \
X(SPL_AST_PATTERN_IDENT, V0, pattern_ident) \
X(SPL_AST_PATTERN_UNDERSCORE, V0, pattern_underscore) \
X(SPL_AST_PATTERN_DOT, V0, pattern_dot) \
@@ -143,6 +143,12 @@ typedef enum {
SPL_UNARY_OP_DEREF,
} spl_unary_op_t;
typedef enum {
SPL_FN_KIND_NORMAL,
SPL_FN_KIND_META,
SPL_FN_KIND_MACRO,
} spl_fn_kind_t;
struct spl_ast_node;
typedef struct spl_ast_node spl_ast_node_t;
typedef usize spl_ast_node_ref_t;
@@ -164,12 +170,14 @@ struct spl_ast_node {
struct {
spl_ast_node_ref_vec_t external_tags;
spl_ast_node_ref_t module;
spl_ast_node_ref_vec_t declarations;
} container_members;
struct {
spl_ast_node_ref_vec_t tag_list;
bool is_comptime;
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_fn_kind_t fn_kind;
const char *name;
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
@@ -177,6 +185,7 @@ struct spl_ast_node {
} fn_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t type;
@@ -184,12 +193,14 @@ struct spl_ast_node {
} var_const_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t pattern;
spl_ast_node_ref_t type_expr;
} type_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
const char *name;
spl_ast_node_ref_t type_expr;
@@ -210,11 +221,22 @@ struct spl_ast_node {
} tag_item;
struct {
spl_ast_node_ref_t body;
} comptime_decl;
spl_ast_node_ref_vec_t decorator_list;
const char *path;
bool has_body;
spl_ast_node_ref_vec_t decls;
} mod_decl;
struct {
spl_ast_node_ref_vec_t tag_list;
spl_ast_node_ref_vec_t decorator_list;
bool is_path;
const char *path;
const char *alias;
} import_decl;
struct {
spl_ast_node_ref_vec_t decorator_list;
spl_ast_node_ref_vec_t attr_list;
spl_ast_node_ref_t block;
} top_level_block;
@@ -234,9 +256,6 @@ struct spl_ast_node {
struct {
spl_ast_node_ref_t body;
} statement_defer;
struct {
spl_ast_node_ref_t body;
} statement_comptime;
struct {
spl_ast_node_ref_t decl;
} statement_decl;
@@ -339,7 +358,7 @@ struct spl_ast_node {
struct {
const char *name;
spl_ast_node_ref_vec_t args;
} comptime_call;
} compile_call;
struct {
spl_ast_node_ref_vec_t stmts;
@@ -384,7 +403,6 @@ struct spl_ast_node {
} match_expr;
struct {
bool is_comptime;
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
} fn_type;
@@ -410,7 +428,6 @@ struct spl_ast_node {
} type_slice;
struct {
bool is_comptime;
spl_ast_node_ref_vec_t params;
spl_ast_node_ref_t ret_type;
} type_fn;
@@ -427,7 +444,7 @@ struct spl_ast_node {
struct {
const char *name;
spl_ast_node_ref_vec_t args;
} type_comptime_call;
} type_compile_call;
struct {
const char *name;
File renamed without changes.
@@ -1,7 +1,7 @@
#ifndef __SPL_ATTR_H__
#define __SPL_ATTR_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_mpa.h"
typedef enum {
File renamed without changes.
@@ -1,7 +1,7 @@
#ifndef __SPL_DIAG_H__
#define __SPL_DIAG_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_dbg.h"
typedef enum {
@@ -1,7 +1,7 @@
#ifndef __SPL_FOLD_H__
#define __SPL_FOLD_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
/* 通用 intrusive 哈希集合,对应 LLVM FoldingSet 的桶链部分。
- 节点表由调用方持有,节点自带 next 字段;
@@ -1,7 +1,7 @@
#ifndef __SPL_INTRIN_H__
#define __SPL_INTRIN_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_lang_type.h"
typedef enum {
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef __SPL_IR_H__
#define __SPL_IR_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_attr.h"
#include "spl_dbg.h"
#include "spl_type.h"
File renamed without changes.
@@ -6,7 +6,7 @@
#include "spl_layout.h"
#include "spl_type.h"
#include "../stage0/spl_mcode.h"
#include "stage0/spl_mcode.h"
typedef MAP(spl_ir_func_ref_t, isize) spl_extern_idx_map_t;
@@ -1,7 +1,7 @@
#ifndef __SPL_LANG_TYPE_H__
#define __SPL_LANG_TYPE_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_dbg.h"
#include "spl_mpa.h"
#include "spl_type.h"
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -8,6 +8,7 @@ typedef struct spl_parser {
spl_ast_t *ast;
spl_diag_t *diag;
usize pos;
usize last_tok_line;
int failed;
int stop_agg;
usize error_count;
File renamed without changes.
File renamed without changes.
@@ -2,7 +2,7 @@
#ifndef __SPL_TOK_H__
#define __SPL_TOK_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
/* clang-format off */
#define KEYWORD_TABLE \
@@ -10,26 +10,26 @@
X(as , KW_AS , SPL_V0) \
X(bool , KW_BOOL , SPL_V0) \
X(break , KW_BREAK , SPL_V0) \
X(catch , KW_CATCH , SPL_V0) \
X(comptime , KW_COMPTIME , SPL_V0) \
X(const , KW_CONST , SPL_V0) \
X(continue , KW_CONTINUE , SPL_V0) \
X(defer , KW_DEFER , SPL_V0) \
X(else , KW_ELSE , SPL_V0) \
X(enum , KW_ENUM , SPL_V0) \
X(errdefer , KW_ERRDEFER , SPL_V0) \
X(false , KW_FALSE , SPL_V0) \
X(fn , KW_FN , SPL_V0) \
X(for , KW_FOR , SPL_V0) \
X(if , KW_IF , SPL_V0) \
X(import , KW_IMPORT , SPL_V0) \
X(loop , KW_LOOP , SPL_V0) \
X(macro , KW_MACRO , SPL_V0) \
X(match , KW_MATCH , SPL_V0) \
X(meta , KW_META , SPL_V0) \
X(mod , KW_MOD , SPL_V0) \
X(null , KW_NULL , SPL_V0) \
X(ret , KW_RET , SPL_V0) \
X(shape , KW_SHAPE , SPL_V0) \
X(struct , KW_STRUCT , SPL_V0) \
X(true , KW_TRUE , SPL_V0) \
X(try , KW_TRY , SPL_V0) \
X(type , KW_TYPE , SPL_V0) \
X(undefined , KW_UNDEFINED , SPL_V0) \
X(union , KW_UNION , SPL_V0) \
@@ -93,11 +93,9 @@
X(";" , TOK_SEMICOLON , SPL_V0 ) \
X("," , TOK_COMMA , SPL_V0 ) \
X(":" , TOK_COLON , SPL_V0 ) \
X(":=" , TOK_COLON_ASSIGN , SPL_V0 ) \
X("." , TOK_DOT , SPL_V0 ) \
X(".." , TOK_RANGE , SPL_V0 ) \
X("..." , TOK_ELLIPSIS , SPL_V0 ) \
X("?" , TOK_COND , SPL_V0 ) \
X(ident , TOK_IDENT , SPL_V0 ) \
X(int , TOK_INT_LITERAL , SPL_V0 ) \
X(float , TOK_FLOAT_LITERAL , SPL_V0 ) \
@@ -1,7 +1,7 @@
#ifndef __SPL_TYPE_H__
#define __SPL_TYPE_H__
#include "../stage0/include/utils.h"
#include "stage0/include/utils.h"
#include "spl_fold.h"
/* 类型 intern 用的哈希混合(FNV-1a 风格,顺序敏感)。
+1 -1
View File
@@ -3,7 +3,7 @@
/* 字节码(VM 层)优化 pass:输入输出都是 spl_prog_t,纯函数式、可开关。 */
#include "../stage0/spl_mcode.h"
#include "stage0/spl_mcode.h"
void spl_prog_peephole(spl_prog_t *prog);
void spl_prog_opt(spl_prog_t *prog);
File renamed without changes.
File renamed without changes.
@@ -434,7 +434,6 @@ spl_tok_vec_t spl_lex(const char *source, const char *fname) {
TRY_OP2('|', '=', TOK_ASSIGN_OR, TOK_OR)
TRY_OP2('^', '=', TOK_ASSIGN_XOR, TOK_XOR)
TRY_OP2('-', '>', TOK_RIGHT_ARROW, TOK_SUB)
TRY_OP2(':', '=', TOK_COLON_ASSIGN, TOK_COLON)
/* Single-character operators */
TRY_OP1('+', TOK_ADD);
@@ -463,7 +462,6 @@ spl_tok_vec_t spl_lex(const char *source, const char *fname) {
TRY_OP1('#', TOK_SHARP);
TRY_OP1('@', TOK_AT);
TRY_OP1('$', TOK_DOLLAR);
TRY_OP1('?', TOK_COND);
LOG_ERROR("%s:%zu:%zu: unknown character '%c' (0x%02x)", fname ? fname : "<null>", line, col,
(c >= 32 && c < 127) ? c : '?', (unsigned char)c);
File renamed without changes.
@@ -1668,11 +1668,11 @@ static spl_ir_node_ref_t emit_expr_raw(spl_ast2ir_t *a, spl_ast_node_ref_t ref)
case SPL_AST_EXPR_SLICE:
return emit_expr_slice(a, n);
case SPL_AST_EXPR_COMPTIME_CALL: {
spl_intrin_t in = spl_intrin_lookup_call(n->comptime_call.name);
case SPL_AST_EXPR_COMPILE_CALL: {
spl_intrin_t in = spl_intrin_lookup_call(n->compile_call.name);
if (in == SPL_INTRIN_NONE)
return 0;
return ast2ir_intrin_call(a, n, in, &n->comptime_call.args);
return ast2ir_intrin_call(a, n, in, &n->compile_call.args);
}
default:
TODO();
@@ -1909,10 +1909,6 @@ static void emit_stmt(spl_ast2ir_t *a, spl_ast_node_ref_t ref) {
if (n->statement_defer.body)
vec_push(a->defer_stack, n->statement_defer.body);
return;
case SPL_AST_STATEMENT_COMPTIME:
if (n->statement_comptime.body)
emit_expr(a, n->statement_comptime.body);
return;
case SPL_AST_STATEMENT_DECL:
if (n->statement_decl.decl)
emit_decl(a, n->statement_decl.decl);
File renamed without changes.
File renamed without changes.
+381 -170
View File
@@ -87,11 +87,18 @@ static inline const spl_tok_t *peek(spl_parser_t *p) {
static inline const spl_tok_t *advance(spl_parser_t *p) {
const spl_tok_t *t = peek(p);
if (t && t->type != TOK_EOF)
if (t && t->type != TOK_EOF) {
p->last_tok_line = t->line;
p->pos++;
}
return t;
}
static inline int line_changed(const spl_parser_t *p) {
const spl_tok_t *t = &vec_at(p->ast->input, p->pos);
return t->line > p->last_tok_line;
}
static inline spl_ast_node_t *node_at(spl_ast_t *ast, spl_ast_node_ref_t ref) {
return &ast->node_buckets.data[ref];
}
@@ -209,7 +216,6 @@ static int can_start_expr(spl_tok_type_t t) {
case KW_UNDEFINED:
case TOK_IDENT:
case TOK_AT:
case TOK_SHARP:
case TOK_DOLLAR:
case TOK_L_PAREN:
case TOK_L_BRACKET:
@@ -220,6 +226,10 @@ static int can_start_expr(spl_tok_type_t t) {
case TOK_BIT_NOT:
case TOK_AND:
case TOK_MUL:
case KW_RET:
case KW_BREAK:
case KW_CONTINUE:
case KW_DEFER:
case KW_IF:
case KW_WHILE:
case KW_LOOP:
@@ -245,7 +255,10 @@ static int can_start_statement(spl_tok_type_t t) {
case KW_VAR:
case KW_CONST:
case KW_TYPE:
case KW_COMPTIME:
case KW_FN:
case KW_META:
case KW_MACRO:
case TOK_SHARP:
return 1;
default:
return 0;
@@ -266,13 +279,17 @@ static spl_ast_node_ref_t parse_expr(spl_parser_t *p);
static spl_ast_node_ref_vec_t parse_expr_list(spl_parser_t *p);
static spl_ast_node_ref_t parse_block(spl_parser_t *p);
static spl_ast_node_ref_t parse_type_expr(spl_parser_t *p);
static spl_ast_node_ref_t parse_type_comptime_call(spl_parser_t *p);
static spl_ast_node_ref_t parse_type_compile_call(spl_parser_t *p);
static spl_ast_node_ref_t parse_statement(spl_parser_t *p);
static spl_ast_node_ref_t parse_container_declaration(spl_parser_t *p);
static spl_ast_node_ref_t parse_param_decl_impl(spl_parser_t *p);
static spl_ast_node_ref_t parse_pattern(spl_parser_t *p);
static spl_ast_node_ref_t parse_pattern_atom(spl_parser_t *p);
static spl_ast_node_ref_t parse_pattern_aggregate(spl_parser_t *p, const spl_tok_t *start);
static void parse_decorators(spl_parser_t *p, spl_ast_node_ref_vec_t *out);
static void parse_attributes(spl_parser_t *p, spl_ast_node_ref_vec_t *out);
static void parse_external_tags(spl_parser_t *p, spl_ast_node_ref_vec_t *out);
static const char *parse_dotted_path(spl_parser_t *p, const char *what);
static spl_ast_node_ref_t parse_cast(spl_parser_t *p);
static spl_ast_node_ref_t parse_prefix(spl_parser_t *p);
@@ -280,7 +297,7 @@ static spl_ast_node_ref_t parse_postfix(spl_parser_t *p);
static spl_ast_node_ref_t parse_primary(spl_parser_t *p);
static spl_ast_node_ref_vec_t parse_aggregate_fields(spl_parser_t *p);
static spl_ast_node_ref_t parse_array_literal(spl_parser_t *p);
static spl_ast_node_ref_t parse_comptime_call(spl_parser_t *p);
static spl_ast_node_ref_t parse_compile_call(spl_parser_t *p);
static spl_ast_node_ref_t parse_if_expr(spl_parser_t *p);
static spl_ast_node_ref_t parse_while_expr(spl_parser_t *p);
static spl_ast_node_ref_t parse_loop_expr(spl_parser_t *p);
@@ -290,7 +307,6 @@ static spl_ast_node_ref_t parse_ret_stmt(spl_parser_t *p);
static spl_ast_node_ref_t parse_break_stmt(spl_parser_t *p);
static spl_ast_node_ref_t parse_continue_stmt(spl_parser_t *p);
static spl_ast_node_ref_t parse_defer_stmt(spl_parser_t *p);
static spl_ast_node_ref_t parse_comptime_stmt(spl_parser_t *p);
static spl_ast_node_ref_t parse_label(spl_parser_t *p);
/* ---------- Pattern ---------- */
@@ -395,6 +411,9 @@ static spl_ast_node_ref_t parse_pattern_atom(spl_parser_t *p) {
case KW_NULL:
advance(p);
return new_node(p, SPL_AST_EXPR_NULL_LIT, t);
case KW_UNDEFINED:
advance(p);
return new_node(p, SPL_AST_EXPR_UNDEFINED_LIT, t);
case TOK_IDENT: {
advance(p);
@@ -510,49 +529,129 @@ static spl_ast_node_ref_t parse_pattern(spl_parser_t *p) {
/* ---------- 声明 ---------- */
static void parse_tag_list(spl_parser_t *p, spl_ast_node_ref_vec_t *out, int is_external,
int allow_at) {
while (!p->failed) {
static const char *parse_dotted_path(spl_parser_t *p, const char *what) {
const spl_tok_t *t = peek(p);
if (!t || (t->type != TOK_AT && t->type != TOK_SHARP))
return;
if (!allow_at && t->type == TOK_AT)
return;
int is_at = (t->type == TOK_AT);
advance(p);
if (is_external) {
t = peek(p);
if (!t || t->type != TOK_NOT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected '!' in external tag");
return;
}
advance(p);
if (!t || t->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected %s", what);
return NULL;
}
usize cap = 64;
usize len = 0;
char *buf = malloc(cap);
if (!buf)
return NULL;
while (1) {
if (len + t->len + 2 > cap) {
cap = (len + t->len + 2) * 2;
char *grown = realloc(buf, cap);
if (!grown) {
free(buf);
return NULL;
}
buf = grown;
}
memcpy(buf + len, t->lexeme, t->len);
len += t->len;
advance(p);
if (!peek(p) || peek(p)->type != TOK_DOT)
break;
advance(p);
t = peek(p);
if (!t || t->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected tag name");
return;
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected identifier after '.'");
break;
}
buf[len++] = '.';
}
advance(p);
const char *name = intern_string(p->ast, buf, len);
free(buf);
return name;
}
static spl_ast_node_ref_t parse_tag_item(spl_parser_t *p, const spl_tok_t *start, int is_external,
int is_at) {
spl_ast_node_ref_t ref =
new_node(p, is_external ? SPL_AST_EXTERNAL_TAG_ITEM : SPL_AST_TAG_ITEM, t);
new_node(p, is_external ? SPL_AST_EXTERNAL_TAG_ITEM : SPL_AST_TAG_ITEM, start);
spl_ast_node_t *tag = node_at(p->ast, ref);
tag->tag_item.is_at = is_at;
tag->tag_item.is_external = is_external;
SET_STR_FIELD(p->ast, tag->tag_item.name, t);
vec_init(tag->tag_item.args);
t = peek(p);
if (is_at) {
const char *name = parse_dotted_path(p, "decorator name");
if (!name)
return ref;
node_at(p->ast, ref)->tag_item.name = name;
} else {
const spl_tok_t *t = peek(p);
if (!t || t->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected tag name");
return ref;
}
advance(p);
SET_STR_FIELD(p->ast, tag->tag_item.name, t);
}
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_L_PAREN) {
advance(p);
spl_ast_node_ref_vec_t args = parse_expr_list(p);
node_at(p->ast, ref)->tag_item.args = args;
if (!expect(p, TOK_R_PAREN, "')' in tag args"))
expect(p, TOK_R_PAREN, "')' in tag args");
}
return ref;
}
static void parse_attributes(spl_parser_t *p, spl_ast_node_ref_vec_t *out) {
while (!p->failed) {
const spl_tok_t *t = peek(p);
if (!t || t->type != TOK_SHARP)
return;
advance(p);
spl_ast_node_ref_t ref = parse_tag_item(p, t, 0, 0);
vec_push(*out, ref);
}
}
static void parse_decorators(spl_parser_t *p, spl_ast_node_ref_vec_t *out) {
while (!p->failed) {
const spl_tok_t *t = peek(p);
if (!t || t->type != TOK_AT)
return;
advance(p);
spl_ast_node_ref_t ref = parse_tag_item(p, t, 0, 1);
vec_push(*out, ref);
}
}
static void parse_external_tags(spl_parser_t *p, spl_ast_node_ref_vec_t *out) {
while (!p->failed) {
const spl_tok_t *t = peek(p);
if (!t || t->type != TOK_SHARP)
return;
usize save = p->pos;
usize save_line = p->last_tok_line;
advance(p);
const spl_tok_t *bang = peek(p);
if (!bang || bang->type != TOK_NOT) {
p->pos = save;
p->last_tok_line = save_line;
return;
}
advance(p);
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name, "expected external tag name");
return;
}
spl_ast_node_ref_t ref = parse_tag_item(p, t, 1, 0);
node_at(p->ast, ref)->tag_item.name =
intern_string(p->ast, name->lexeme, name->len);
vec_push(*out, ref);
}
}
@@ -565,7 +664,7 @@ static void parse_param_list(spl_parser_t *p, spl_ast_node_ref_vec_t *out) {
if (t->type == TOK_R_PAREN) {
break;
} else if (t->type == TOK_AT || t->type == TOK_SHARP || t->type == TOK_IDENT) {
} else if (t->type == TOK_SHARP || t->type == TOK_IDENT) {
spl_ast_node_ref_t pd = parse_param_decl_impl(p);
if (pd)
vec_push(*out, pd);
@@ -594,7 +693,7 @@ static void parse_param_list(spl_parser_t *p, spl_ast_node_ref_vec_t *out) {
static spl_ast_node_ref_t parse_param_decl_impl(spl_parser_t *p) {
spl_ast_node_ref_vec_t attr_list;
vec_init(attr_list);
parse_tag_list(p, &attr_list, 0, 1);
parse_attributes(p, &attr_list);
const spl_tok_t *name = peek(p);
if (!name || (name->type != TOK_IDENT && name->type != KW_UNDERSCORE)) {
@@ -609,7 +708,7 @@ static spl_ast_node_ref_t parse_param_decl_impl(spl_parser_t *p) {
return 0;
}
parse_tag_list(p, &attr_list, 0, 0);
parse_attributes(p, &attr_list);
spl_ast_node_ref_t type_expr = parse_type_expr(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_PARAM_DECL, name);
@@ -620,8 +719,9 @@ static spl_ast_node_ref_t parse_param_decl_impl(spl_parser_t *p) {
return ref;
}
static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t tag_list) {
const spl_tok_t *kw = peek(p);
static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t decorator_list,
spl_ast_node_ref_vec_t attr_list, spl_fn_kind_t fn_kind,
const spl_tok_t *kw) {
if (!kw || kw->type != KW_FN)
return 0;
advance(p);
@@ -629,13 +729,15 @@ static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name ? name : kw, "expected function name");
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
advance(p);
if (!expect(p, TOK_L_PAREN, "'(' in function declaration")) {
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
@@ -643,7 +745,8 @@ static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t
vec_init(params);
parse_param_list(p, &params);
if (!expect(p, TOK_R_PAREN, "')' in function declaration")) {
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
vec_free(params);
return 0;
}
@@ -664,15 +767,17 @@ static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t
} else {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_UNRECOGNIZED, t,
"function declaration must end with ';' or block");
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
vec_free(params);
return 0;
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_FN_DECL, kw);
spl_ast_node_t *fn = node_at(p->ast, ref);
MOVE_VEC(fn->fn_decl.tag_list, tag_list);
fn->fn_decl.is_comptime = 0;
MOVE_VEC(fn->fn_decl.decorator_list, decorator_list);
MOVE_VEC(fn->fn_decl.attr_list, attr_list);
fn->fn_decl.fn_kind = fn_kind;
SET_STR_FIELD(p->ast, fn->fn_decl.name, name);
fn->fn_decl.params = params;
fn->fn_decl.ret_type = ret_type;
@@ -680,27 +785,34 @@ static spl_ast_node_ref_t parse_fn_decl(spl_parser_t *p, spl_ast_node_ref_vec_t
return ref;
}
static spl_ast_node_ref_t parse_type_decl(spl_parser_t *p, spl_ast_node_ref_vec_t tag_list) {
static spl_ast_node_ref_t parse_type_decl(spl_parser_t *p, spl_ast_node_ref_vec_t decorator_list,
spl_ast_node_ref_vec_t attr_list) {
const spl_tok_t *kw = peek(p);
if (!kw || kw->type != KW_TYPE)
if (!kw || kw->type != KW_TYPE) {
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
advance(p);
spl_ast_node_ref_t pattern = parse_pattern(p);
if (!pattern) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, peek(p), "expected pattern after 'type'");
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
if (!expect(p, TOK_ASSIGN, "'=' in type declaration")) {
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_DECL, kw);
spl_ast_node_t *td = node_at(p->ast, ref);
MOVE_VEC(td->type_decl.attr_list, tag_list);
MOVE_VEC(td->type_decl.decorator_list, decorator_list);
MOVE_VEC(td->type_decl.attr_list, attr_list);
td->type_decl.pattern = pattern;
spl_ast_node_ref_t type_expr = parse_type_expr(p);
@@ -713,20 +825,31 @@ static spl_ast_node_ref_t parse_type_decl(spl_parser_t *p, spl_ast_node_ref_vec_
return ref;
}
static spl_ast_node_ref_t parse_var_const_decl(spl_parser_t *p, spl_ast_node_ref_vec_t tag_list,
int is_const) {
static spl_ast_node_ref_t parse_var_const_decl(spl_parser_t *p,
spl_ast_node_ref_vec_t decorator_list,
spl_ast_node_ref_vec_t attr_list, int is_const) {
const spl_tok_t *kw = peek(p);
if (!kw)
if (!kw) {
vec_free(decorator_list);
vec_free(attr_list);
return 0;
if (is_const && kw->type != KW_CONST)
}
if (is_const && kw->type != KW_CONST) {
vec_free(decorator_list);
vec_free(attr_list);
return 0;
if (!is_const && kw->type != KW_VAR)
}
if (!is_const && kw->type != KW_VAR) {
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
advance(p);
spl_ast_node_ref_t pattern = parse_pattern(p);
if (!pattern) {
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
@@ -749,7 +872,8 @@ static spl_ast_node_ref_t parse_var_const_decl(spl_parser_t *p, spl_ast_node_ref
spl_ast_node_ref_t ref = new_node(p, is_const ? SPL_AST_CONST_DECL : SPL_AST_VAR_DECL, kw);
spl_ast_node_t *vc = node_at(p->ast, ref);
MOVE_VEC(vc->var_const_decl.attr_list, tag_list);
MOVE_VEC(vc->var_const_decl.decorator_list, decorator_list);
MOVE_VEC(vc->var_const_decl.attr_list, attr_list);
vc->var_const_decl.pattern = pattern;
vc->var_const_decl.type = type_expr;
vc->var_const_decl.init_expr = init_expr;
@@ -760,10 +884,15 @@ static spl_ast_node_ref_t parse_var_const_decl(spl_parser_t *p, spl_ast_node_ref
return ref;
}
static spl_ast_node_ref_t parse_member_decl(spl_parser_t *p, spl_ast_node_ref_vec_t tag_list) {
static spl_ast_node_ref_t parse_member_decl(spl_parser_t *p,
spl_ast_node_ref_vec_t decorator_list,
spl_ast_node_ref_vec_t attr_list) {
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT)
if (!name || name->type != TOK_IDENT) {
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
advance(p);
spl_ast_node_ref_t type_expr = 0;
@@ -771,7 +900,7 @@ static spl_ast_node_ref_t parse_member_decl(spl_parser_t *p, spl_ast_node_ref_ve
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_COLON) {
advance(p);
parse_tag_list(p, &tag_list, 0, 0);
parse_attributes(p, &attr_list);
type_expr = parse_type_expr(p);
t = peek(p);
if (t && t->type == TOK_ASSIGN) {
@@ -785,7 +914,8 @@ static spl_ast_node_ref_t parse_member_decl(spl_parser_t *p, spl_ast_node_ref_ve
spl_ast_node_ref_t ref = new_node(p, SPL_AST_MEMBER_DECL, name);
spl_ast_node_t *md = node_at(p->ast, ref);
MOVE_VEC(md->member_decl.attr_list, tag_list);
MOVE_VEC(md->member_decl.decorator_list, decorator_list);
MOVE_VEC(md->member_decl.attr_list, attr_list);
SET_STR_FIELD(p->ast, md->member_decl.name, name);
md->member_decl.type_expr = type_expr;
md->member_decl.init_expr = init_expr;
@@ -796,61 +926,178 @@ static spl_ast_node_ref_t parse_member_decl(spl_parser_t *p, spl_ast_node_ref_ve
return ref;
}
static spl_ast_node_ref_t parse_comptime_decl(spl_parser_t *p) {
static spl_ast_node_ref_t parse_mod_decl(spl_parser_t *p, spl_ast_node_ref_vec_t decorator_list) {
const spl_tok_t *kw = peek(p);
if (!kw || kw->type != KW_COMPTIME)
if (!kw || kw->type != KW_MOD) {
vec_free(decorator_list);
return 0;
}
advance(p);
spl_ast_node_ref_t body = 0;
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_L_BRACE)
body = parse_block(p);
else
body = parse_expr(p);
const char *path = parse_dotted_path(p, "module path");
if (!path) {
vec_free(decorator_list);
return 0;
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_COMPTIME_DECL, kw);
node_at(p->ast, ref)->comptime_decl.body = body;
spl_ast_node_ref_t ref = new_node(p, SPL_AST_MOD_DECL, kw);
spl_ast_node_t *mod = node_at(p->ast, ref);
MOVE_VEC(mod->mod_decl.decorator_list, decorator_list);
mod->mod_decl.path = path;
mod->mod_decl.has_body = false;
vec_init(mod->mod_decl.decls);
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_SEMICOLON) {
advance(p);
return ref;
}
if (!t || t->type != TOK_L_BRACE) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "expected ';' or '{' after module path");
return ref;
}
advance(p);
mod = node_at(p->ast, ref);
mod->mod_decl.has_body = true;
while (!p->failed) {
t = peek(p);
if (!t || t->type == TOK_EOF) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t, "module missing '}'");
break;
}
if (t->type == TOK_R_BRACE) {
advance(p);
break;
}
spl_ast_node_ref_t decl = parse_container_declaration(p);
mod = node_at(p->ast, ref);
if (decl)
vec_push(mod->mod_decl.decls, decl);
else
break;
t = peek(p);
if (t && (t->type == TOK_COMMA || t->type == TOK_SEMICOLON))
advance(p);
}
return ref;
}
static spl_ast_node_ref_t parse_import_decl(spl_parser_t *p,
spl_ast_node_ref_vec_t decorator_list) {
const spl_tok_t *kw = peek(p);
if (!kw || kw->type != KW_IMPORT) {
vec_free(decorator_list);
return 0;
}
advance(p);
bool is_path = true;
const char *path = NULL;
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_STRING_LITERAL) {
advance(p);
usize len = 0;
path = parse_string_literal(p, t, &len);
is_path = false;
} else {
path = parse_dotted_path(p, "import path");
if (!path) {
vec_free(decorator_list);
return 0;
}
}
const char *alias = NULL;
t = peek(p);
if (t && t->type == KW_AS) {
advance(p);
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name, "expected alias after 'as'");
vec_free(decorator_list);
return 0;
}
advance(p);
alias = intern_string(p->ast, name->lexeme, name->len);
t = peek(p);
}
if (t && t->type == TOK_SEMICOLON)
advance(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_IMPORT_DECL, kw);
spl_ast_node_t *imp = node_at(p->ast, ref);
MOVE_VEC(imp->import_decl.decorator_list, decorator_list);
imp->import_decl.is_path = is_path;
imp->import_decl.path = path;
imp->import_decl.alias = alias;
return ref;
}
static spl_ast_node_ref_t parse_container_declaration(spl_parser_t *p) {
spl_ast_node_ref_vec_t tag_list;
vec_init(tag_list);
parse_tag_list(p, &tag_list, 0, 0);
spl_ast_node_ref_vec_t decorator_list;
vec_init(decorator_list);
parse_decorators(p, &decorator_list);
spl_ast_node_ref_vec_t attr_list;
vec_init(attr_list);
parse_attributes(p, &attr_list);
const spl_tok_t *t = peek(p);
if (!t || t->type == TOK_EOF) {
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
switch (t->type) {
case KW_FN:
return parse_fn_decl(p, tag_list);
return parse_fn_decl(p, decorator_list, attr_list, SPL_FN_KIND_NORMAL, t);
case KW_META:
case KW_MACRO: {
spl_fn_kind_t fn_kind = (t->type == KW_META) ? SPL_FN_KIND_META : SPL_FN_KIND_MACRO;
advance(p);
const spl_tok_t *fn_kw = peek(p);
if (!fn_kw || fn_kw->type != KW_FN) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, fn_kw, "expected 'fn' after '%s'",
fn_kind == SPL_FN_KIND_META ? "meta" : "macro");
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
return parse_fn_decl(p, decorator_list, attr_list, fn_kind, fn_kw);
}
case KW_TYPE:
return parse_type_decl(p, tag_list);
return parse_type_decl(p, decorator_list, attr_list);
case KW_VAR:
return parse_var_const_decl(p, tag_list, 0);
return parse_var_const_decl(p, decorator_list, attr_list, 0);
case KW_CONST:
return parse_var_const_decl(p, tag_list, 1);
return parse_var_const_decl(p, decorator_list, attr_list, 1);
case TOK_IDENT:
return parse_member_decl(p, tag_list);
case KW_COMPTIME:
vec_free(tag_list);
return parse_comptime_decl(p);
return parse_member_decl(p, decorator_list, attr_list);
case KW_MOD:
vec_free(attr_list);
return parse_mod_decl(p, decorator_list);
case KW_IMPORT:
vec_free(attr_list);
return parse_import_decl(p, decorator_list);
case TOK_L_BRACE: {
spl_ast_node_ref_t block = parse_block(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TOP_LEVEL_BLOCK, t);
spl_ast_node_t *tb = node_at(p->ast, ref);
MOVE_VEC(tb->top_level_block.tag_list, tag_list);
MOVE_VEC(tb->top_level_block.decorator_list, decorator_list);
MOVE_VEC(tb->top_level_block.attr_list, attr_list);
tb->top_level_block.block = block;
return ref;
}
default:
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_UNRECOGNIZED, t, "unrecognized declaration");
advance(p); /* 消费该 token,避免调用方在错误恢复时原地死循环 */
vec_free(tag_list);
vec_free(decorator_list);
vec_free(attr_list);
return 0;
}
}
@@ -871,7 +1118,6 @@ static spl_ast_node_ref_t parse_type_base(spl_parser_t *p) {
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_FN, bt);
spl_ast_node_t *fn_type = node_at(p->ast, ref);
fn_type->type_fn.is_comptime = 0;
vec_init(fn_type->type_fn.params);
while (!p->failed && peek(p) && peek(p)->type != TOK_R_PAREN) {
@@ -945,25 +1191,22 @@ static spl_ast_node_ref_t parse_type_base(spl_parser_t *p) {
return ref;
}
if (bt->type == TOK_AT || bt->type == TOK_SHARP)
return parse_type_comptime_call(p);
if (bt->type == TOK_AT)
return parse_type_compile_call(p);
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_UNRECOGNIZED, bt, "unrecognized type");
return 0;
}
static spl_ast_node_ref_t parse_type_comptime_call(spl_parser_t *p) {
static spl_ast_node_ref_t parse_type_compile_call(spl_parser_t *p) {
const spl_tok_t *start = peek(p);
if (!start || (start->type != TOK_AT && start->type != TOK_SHARP))
if (!start || start->type != TOK_AT)
return 0;
advance(p);
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name, "expected comptime name");
const char *name = parse_dotted_path(p, "compile-time call name");
if (!name)
return 0;
}
advance(p);
spl_ast_node_ref_vec_t args;
vec_init(args);
@@ -972,16 +1215,16 @@ static spl_ast_node_ref_t parse_type_comptime_call(spl_parser_t *p) {
if (t && t->type == TOK_L_PAREN) {
advance(p);
args = parse_expr_list(p);
if (!expect(p, TOK_R_PAREN, "')' in comptime type")) {
if (!expect(p, TOK_R_PAREN, "')' in compile-time type")) {
vec_free(args);
return 0;
}
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_COMPTIME_CALL, start);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_COMPILE_CALL, start);
spl_ast_node_t *cc = node_at(p->ast, ref);
SET_STR_FIELD(p->ast, cc->type_comptime_call.name, name);
cc->type_comptime_call.args = args;
cc->type_compile_call.name = name;
cc->type_compile_call.args = args;
return ref;
}
@@ -1369,6 +1612,14 @@ static spl_ast_node_ref_t parse_prefix(spl_parser_t *p) {
spl_unary_op_t uop;
switch (t->type) {
case KW_RET:
return parse_ret_stmt(p);
case KW_BREAK:
return parse_break_stmt(p);
case KW_CONTINUE:
return parse_continue_stmt(p);
case KW_DEFER:
return parse_defer_stmt(p);
case TOK_SUB:
uop = SPL_UNARY_OP_MINUS;
break;
@@ -1599,8 +1850,7 @@ static spl_ast_node_ref_t parse_primary(spl_parser_t *p) {
case TOK_L_BRACE:
return parse_block(p);
case TOK_AT:
case TOK_SHARP:
return parse_comptime_call(p);
return parse_compile_call(p);
case TOK_L_PAREN: {
advance(p);
@@ -1693,18 +1943,15 @@ static spl_ast_node_ref_t parse_array_literal(spl_parser_t *p) {
return ref;
}
static spl_ast_node_ref_t parse_comptime_call(spl_parser_t *p) {
static spl_ast_node_ref_t parse_compile_call(spl_parser_t *p) {
const spl_tok_t *start = peek(p);
if (!start || (start->type != TOK_AT && start->type != TOK_SHARP))
if (!start || start->type != TOK_AT)
return 0;
advance(p);
const spl_tok_t *name = peek(p);
if (!name || name->type != TOK_IDENT) {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name, "expected comptime name");
const char *name = parse_dotted_path(p, "compile-time call name");
if (!name)
return 0;
}
advance(p);
spl_ast_node_ref_vec_t args;
vec_init(args);
@@ -1713,16 +1960,16 @@ static spl_ast_node_ref_t parse_comptime_call(spl_parser_t *p) {
if (t && t->type == TOK_L_PAREN) {
advance(p);
args = parse_expr_list(p);
if (!expect(p, TOK_R_PAREN, "')' in comptime call")) {
if (!expect(p, TOK_R_PAREN, "')' in compile-time call")) {
vec_free(args);
return 0;
}
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_COMPTIME_CALL, start);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_COMPILE_CALL, start);
spl_ast_node_t *cc = node_at(p->ast, ref);
SET_STR_FIELD(p->ast, cc->comptime_call.name, name);
cc->comptime_call.args = args;
cc->compile_call.name = name;
cc->compile_call.args = args;
return ref;
}
@@ -1998,10 +2245,6 @@ static spl_ast_node_ref_t parse_ret_stmt(spl_parser_t *p) {
if (t && can_start_expr(t->type))
expr = parse_expr(p);
t = peek(p);
if (t && t->type == TOK_SEMICOLON)
advance(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_RET, kw);
node_at(p->ast, ref)->statement_ret.expr = expr;
return ref;
@@ -2019,10 +2262,6 @@ static spl_ast_node_ref_t parse_break_stmt(spl_parser_t *p) {
if (t && can_start_expr(t->type))
expr = parse_expr(p);
t = peek(p);
if (t && t->type == TOK_SEMICOLON)
advance(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_BREAK, kw);
node_at(p->ast, ref)->statement_break.label = label;
node_at(p->ast, ref)->statement_break.expr = expr;
@@ -2037,10 +2276,6 @@ static spl_ast_node_ref_t parse_continue_stmt(spl_parser_t *p) {
spl_ast_node_ref_t label = parse_label(p);
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_SEMICOLON)
advance(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_CONTINUE, kw);
node_at(p->ast, ref)->statement_continue.label = label;
return ref;
@@ -2060,9 +2295,6 @@ static spl_ast_node_ref_t parse_defer_stmt(spl_parser_t *p) {
body = parse_statement(p);
} else {
body = parse_expr(p);
t = peek(p);
if (t && t->type == TOK_SEMICOLON)
advance(p);
}
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_DEFER, kw);
@@ -2070,24 +2302,6 @@ static spl_ast_node_ref_t parse_defer_stmt(spl_parser_t *p) {
return ref;
}
static spl_ast_node_ref_t parse_comptime_stmt(spl_parser_t *p) {
const spl_tok_t *kw = peek(p);
if (!kw || kw->type != KW_COMPTIME)
return 0;
advance(p);
spl_ast_node_ref_t body = 0;
const spl_tok_t *t = peek(p);
if (t && t->type == TOK_L_BRACE)
body = parse_block(p);
else
body = parse_expr(p);
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_COMPTIME, kw);
node_at(p->ast, ref)->statement_comptime.body = body;
return ref;
}
static spl_ast_node_ref_t parse_statement(spl_parser_t *p) {
const spl_tok_t *t = peek(p);
if (!t)
@@ -2102,25 +2316,16 @@ static spl_ast_node_ref_t parse_statement(spl_parser_t *p) {
return parse_continue_stmt(p);
case KW_DEFER:
return parse_defer_stmt(p);
case KW_COMPTIME:
return parse_comptime_stmt(p);
case KW_FN:
case KW_META:
case KW_MACRO:
case KW_VAR:
case KW_CONST:
case KW_TYPE: {
spl_ast_node_ref_vec_t tag_list;
vec_init(tag_list);
parse_tag_list(p, &tag_list, 0, 0);
spl_ast_node_ref_t decl;
if (t->type == KW_VAR)
decl = parse_var_const_decl(p, tag_list, 0);
else if (t->type == KW_CONST)
decl = parse_var_const_decl(p, tag_list, 1);
else
decl = parse_type_decl(p, tag_list);
case KW_TYPE:
case TOK_SHARP: {
spl_ast_node_ref_t decl = parse_container_declaration(p);
if (!decl)
return 0;
spl_ast_node_ref_t ref = new_node(p, SPL_AST_STATEMENT_DECL, t);
node_at(p->ast, ref)->statement_decl.decl = decl;
return ref;
@@ -2167,10 +2372,12 @@ static spl_ast_node_ref_t parse_block(spl_parser_t *p) {
if (can_start_statement(t->type)) {
spl_ast_node_ref_t stmt = parse_statement(p);
block = node_at(p->ast, ref);
if (stmt)
vec_push(block->block.stmts, stmt);
else
if (!stmt)
break;
vec_push(block->block.stmts, stmt);
t = peek(p);
if (t && t->type == TOK_SEMICOLON)
advance(p);
} else if (can_start_expr(t->type)) {
spl_ast_node_ref_t expr = parse_expr(p);
if (!expr)
@@ -2192,7 +2399,7 @@ static spl_ast_node_ref_t parse_block(spl_parser_t *p) {
(k == SPL_AST_EXPR_BLOCK || k == SPL_AST_EXPR_IF || k == SPL_AST_EXPR_IFVAR ||
k == SPL_AST_EXPR_WHILE || k == SPL_AST_EXPR_LOOP || k == SPL_AST_EXPR_FOR ||
k == SPL_AST_EXPR_MATCH);
if (block_like)
if (block_like || line_changed(p))
vec_push(block->block.stmts, expr);
else {
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, t,
@@ -2215,6 +2422,7 @@ void spl_parser_init(spl_parser_t *parser) {
parser->ast = NULL;
parser->diag = NULL;
parser->pos = 0;
parser->last_tok_line = 0;
parser->failed = 0;
parser->stop_agg = 0;
parser->error_count = 0;
@@ -2241,6 +2449,7 @@ void spl_ast_parse(spl_parser_t *parser, spl_ast_t *ast) {
parser->ast = ast;
parser->pos = 0;
parser->last_tok_line = 0;
parser->failed = 0;
parser->stop_agg = 0;
@@ -2248,26 +2457,27 @@ void spl_ast_parse(spl_parser_t *parser, spl_ast_t *ast) {
vec_init(external_tags);
spl_ast_node_ref_vec_t declarations;
vec_init(declarations);
spl_ast_node_ref_t module_ref = 0;
while (!parser->failed) {
const spl_tok_t *t = peek(parser);
if (!t || t->type == TOK_EOF)
break;
if (t->type == TOK_AT || t->type == TOK_SHARP) {
usize save_pos = parser->pos;
const spl_tok_t *t2 = NULL;
parser->pos++;
t2 = peek(parser);
parser->pos = save_pos;
if (t2 && t2->type == TOK_NOT) {
parse_tag_list(parser, &external_tags, 1, 1);
if (t->type == TOK_SHARP) {
usize before = vec_size(external_tags);
parse_external_tags(parser, &external_tags);
if (vec_size(external_tags) > before)
continue;
}
}
spl_ast_node_ref_t decl = parse_container_declaration(parser);
if (decl) {
spl_ast_node_t *dn = node_at(ast, decl);
if (dn->kind == SPL_AST_MOD_DECL && !dn->mod_decl.has_body && module_ref == 0 &&
vec_size(declarations) == 0)
module_ref = decl;
else
vec_push(declarations, decl);
} else if (!parser->failed) {
const spl_tok_t *at = peek(parser);
@@ -2282,6 +2492,7 @@ void spl_ast_parse(spl_parser_t *parser, spl_ast_t *ast) {
spl_ast_node_ref_t container_members = new_node(parser, SPL_AST_CONTAINER_MEMBERS, NULL);
spl_ast_node_t *cm = node_at(ast, container_members);
cm->container_members.external_tags = external_tags;
cm->container_members.module = module_ref;
cm->container_members.declarations = declarations;
spl_ast_node_ref_t root = new_node(parser, SPL_AST_ROOT, NULL);
File renamed without changes.
File renamed without changes.
File renamed without changes.
+52 -21
View File
@@ -6,7 +6,7 @@
static spl_ast_node_ref_vec_t *node_attr_list(spl_ast_node_t *n) {
switch (n->kind) {
case SPL_AST_FN_DECL:
return &n->fn_decl.tag_list;
return &n->fn_decl.attr_list;
case SPL_AST_VAR_DECL:
case SPL_AST_CONST_DECL:
return &n->var_const_decl.attr_list;
@@ -17,7 +17,29 @@ static spl_ast_node_ref_vec_t *node_attr_list(spl_ast_node_t *n) {
case SPL_AST_MEMBER_DECL:
return &n->member_decl.attr_list;
case SPL_AST_TOP_LEVEL_BLOCK:
return &n->top_level_block.tag_list;
return &n->top_level_block.attr_list;
default:
return NULL;
}
}
static spl_ast_node_ref_vec_t *node_decorator_list(spl_ast_node_t *n) {
switch (n->kind) {
case SPL_AST_FN_DECL:
return &n->fn_decl.decorator_list;
case SPL_AST_VAR_DECL:
case SPL_AST_CONST_DECL:
return &n->var_const_decl.decorator_list;
case SPL_AST_TYPE_DECL:
return &n->type_decl.decorator_list;
case SPL_AST_MEMBER_DECL:
return &n->member_decl.decorator_list;
case SPL_AST_MOD_DECL:
return &n->mod_decl.decorator_list;
case SPL_AST_IMPORT_DECL:
return &n->import_decl.decorator_list;
case SPL_AST_TOP_LEVEL_BLOCK:
return &n->top_level_block.decorator_list;
default:
return NULL;
}
@@ -912,7 +934,7 @@ static spl_lang_type_id_t sema_infer_type_context(spl_sema_t *sema, spl_ast_node
n->resolved_lang_tid = result;
return result;
}
case SPL_AST_EXPR_COMPTIME_CALL: {
case SPL_AST_EXPR_COMPILE_CALL: {
spl_lang_type_id_t result = sema_infer(sema, ref);
n->resolved_lang_tid = result;
return result;
@@ -2348,13 +2370,13 @@ static spl_lang_type_id_t sema_infer_aggregate_lit(spl_sema_t *sema, spl_ast_nod
return agg;
}
static spl_lang_type_id_t sema_infer_comptime_call(spl_sema_t *sema, spl_ast_node_t *n) {
if (n->comptime_call.name && strcmp(n->comptime_call.name, "import") == 0) {
spl_lang_type_id_t t = sema_import(sema, n, &n->comptime_call.args);
static spl_lang_type_id_t sema_infer_compile_call(spl_sema_t *sema, spl_ast_node_t *n) {
if (n->compile_call.name && strcmp(n->compile_call.name, "import") == 0) {
spl_lang_type_id_t t = sema_import(sema, n, &n->compile_call.args);
n->resolved_lang_tid = t;
return t;
}
if (n->comptime_call.name && strcmp(n->comptime_call.name, "self") == 0) {
if (n->compile_call.name && strcmp(n->compile_call.name, "self") == 0) {
if (sema->current_aggregate_lang == 0) {
spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg,
"`@self` outside of aggregate");
@@ -2363,13 +2385,13 @@ static spl_lang_type_id_t sema_infer_comptime_call(spl_sema_t *sema, spl_ast_nod
n->resolved_lang_tid = sema->current_aggregate_lang;
return sema->current_aggregate_lang;
}
spl_intrin_t in = spl_intrin_lookup_call(n->comptime_call.name);
spl_intrin_t in = spl_intrin_lookup_call(n->compile_call.name);
if (in == SPL_INTRIN_NONE) {
spl_diag_error(sema->diag, SPL_DIAG_KIND_UNKNOWN_INTRIN, &n->dbg,
"unknown comptime call `@%s`", n->comptime_call.name);
"unknown compile-time call `@%s`", n->compile_call.name);
return 0;
}
spl_lang_type_id_t t = sema_intrin_call(sema, n, in, &n->comptime_call.args);
spl_lang_type_id_t t = sema_intrin_call(sema, n, in, &n->compile_call.args);
n->resolved_lang_tid = t;
return t;
}
@@ -2588,8 +2610,8 @@ static spl_lang_type_id_t sema_infer_type_paren(spl_sema_t *sema, spl_ast_node_t
return t;
}
static spl_lang_type_id_t sema_infer_type_comptime_call(spl_sema_t *sema, spl_ast_node_t *n) {
const char *name = n->type_comptime_call.name;
static spl_lang_type_id_t sema_infer_type_compile_call(spl_sema_t *sema, spl_ast_node_t *n) {
const char *name = n->type_compile_call.name;
if (name && strcmp(name, "self") == 0) {
if (sema->current_aggregate_lang == 0) {
spl_diag_error(sema->diag, SPL_DIAG_KIND_UNDEFINED_SYMBOL, &n->dbg,
@@ -2600,7 +2622,7 @@ static spl_lang_type_id_t sema_infer_type_comptime_call(spl_sema_t *sema, spl_as
return sema->current_aggregate_lang;
}
if (name && strcmp(name, "import") == 0) {
spl_lang_type_id_t t = sema_import(sema, n, &n->type_comptime_call.args);
spl_lang_type_id_t t = sema_import(sema, n, &n->type_compile_call.args);
n->resolved_lang_tid = t;
return t;
}
@@ -2745,8 +2767,21 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) {
if (raw && vec_size(*raw) > 0)
sema_resolve_attrs(sema, raw, &n->attrs);
spl_ast_node_ref_vec_t *decorators = node_decorator_list(n);
if (decorators && vec_size(*decorators) > 0 && n->kind != SPL_AST_MOD_DECL &&
n->kind != SPL_AST_IMPORT_DECL) {
spl_diag_error(sema->diag, SPL_DIAG_KIND_UNHANDLED_NODE, &n->dbg,
"decorator execution is not implemented yet");
}
switch (n->kind) {
case SPL_AST_MOD_DECL:
case SPL_AST_IMPORT_DECL:
spl_diag_error(sema->diag, SPL_DIAG_KIND_UNHANDLED_NODE, &n->dbg,
"module system is not implemented yet");
return 0;
case SPL_AST_EXPR_INT_LIT:
return sema_infer_int_lit(sema, n);
case SPL_AST_EXPR_FLOAT_LIT:
@@ -2793,8 +2828,8 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) {
case SPL_AST_EXPR_AGGREGATE_LIT:
return sema_infer_aggregate_lit(sema, n);
case SPL_AST_EXPR_COMPTIME_CALL:
return sema_infer_comptime_call(sema, n);
case SPL_AST_EXPR_COMPILE_CALL:
return sema_infer_compile_call(sema, n);
case SPL_AST_EXPR_BLOCK:
return sema_block_body(sema, n, 0);
@@ -2841,8 +2876,8 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) {
case SPL_AST_TYPE_AGGREGATE:
return sema_build_aggregate(sema, n, 0);
case SPL_AST_TYPE_COMPTIME_CALL:
return sema_infer_type_comptime_call(sema, n);
case SPL_AST_TYPE_COMPILE_CALL:
return sema_infer_type_compile_call(sema, n);
case SPL_AST_CONTAINER_MEMBERS:
return sema_infer_container_members(sema, n);
@@ -2904,10 +2939,6 @@ static spl_lang_type_id_t sema_infer(spl_sema_t *sema, spl_ast_node_ref_t ref) {
if (n->statement_defer.body)
sema_infer(sema, n->statement_defer.body);
return 0;
case SPL_AST_STATEMENT_COMPTIME:
if (n->statement_comptime.body)
sema_infer(sema, n->statement_comptime.body);
return 0;
case SPL_AST_STATEMENT_DECL:
if (n->statement_decl.decl)
sema_infer(sema, n->statement_decl.decl);
File renamed without changes.