stage1 新的ast结构 使用pest描述语法
This commit is contained in:
1 parent
34df303b56
commit
98b35a98e3
12 files changed
+3344
-2168
No files matched your search
+424
-241
@@ -7,134 +7,147 @@
|
||||
#include "spl_tok.h"
|
||||
|
||||
/* clang-format off */
|
||||
#define SPL_AST_KIND_TABLE \
|
||||
X(SPL_AST_NONE, V0, none) \
|
||||
X(SPL_AST_CONTAINER_MEMBERS, V0, container_members) \
|
||||
X(SPL_AST_FN_DECL, V0, fn_decl) \
|
||||
X(SPL_AST_FN_DEFINE, V0, fn_define) \
|
||||
X(SPL_AST_TYPE_DECL, V0, type_decl) \
|
||||
X(SPL_AST_VAR_DECL, V0, var_decl) \
|
||||
X(SPL_AST_CONST_DECL, V0, const_decl) \
|
||||
X(SPL_AST_MEMBER_DECL, V0, member_decl) \
|
||||
X(SPL_AST__COMPTIME_STMT, V0, comptime_stmt) \
|
||||
X(SPL_AST__DIRECTIVE_BLOCK, V0, directive_block) \
|
||||
X(SPL_AST_PARAM_DECL, V0, param_decl) \
|
||||
X(SPL_AST_ATTR_ITEM, V0, attr_item) \
|
||||
X(SPL_AST_ARGG_INIT_ITEM, V0, argg_init_item) \
|
||||
X(SPL_AST_IF_STATEMENT, V0, if_statement) \
|
||||
X(SPL_AST_IFVAR_STATEMENT, V0, ifvar_statement) \
|
||||
X(SPL_AST_WHILE_STATEMENT, V0, while_statement) \
|
||||
X(SPL_AST_LOOP_STATEMENT, V0, loop_statement) \
|
||||
X(SPL_AST_FOR_STATEMENT, V0, for_statement) \
|
||||
X(SPL_AST_MATCH_STATEMENT, V0, match_statement) \
|
||||
X(SPL_AST_RET_STATEMENT, V0, ret_statement) \
|
||||
X(SPL_AST_BREAK_STATEMENT, V0, break_statement) \
|
||||
X(SPL_AST_CONTINUE_STATEMENT, V0, continue_statement) \
|
||||
X(SPL_AST_DEFER_STATEMENT, V0, defer_statement) \
|
||||
X(SPL_AST_TRY_STATEMENT, V0, try_statement) \
|
||||
X(SPL_AST_CATCH_STATEMENT, V0, catch_statement) \
|
||||
X(SPL_AST_ERRDEFER_STATEMEMT, V0, errdefer_statement) \
|
||||
X(SPL_AST_EXPR_STATEMENT, V0, expr_statement) \
|
||||
X(SPL_AST_PACKED_EXPR, V0, packed_expr) \
|
||||
X(SPL_AST_ASSIGN_EXPR, V0, assign_expr) \
|
||||
X(SPL_AST_ASSIGN_ADD_EXPR, V0, assign_add_expr) \
|
||||
X(SPL_AST_ASSIGN_SUB_EXPR, V0, assign_sub_expr) \
|
||||
X(SPL_AST_ASSIGN_MUL_EXPR, V0, assign_mul_expr) \
|
||||
X(SPL_AST_ASSIGN_DIV_EXPR, V0, assign_div_expr) \
|
||||
X(SPL_AST_ASSIGN_MOD_EXPR, V0, assign_mod_expr) \
|
||||
X(SPL_AST_ASSIGN_AND_EXPR, V0, assign_and_expr) \
|
||||
X(SPL_AST_ASSIGN_OR_EXPR, V0, assign_or_expr) \
|
||||
X(SPL_AST_ASSIGN_XOR_EXPR, V0, assign_xor_expr) \
|
||||
X(SPL_AST_ASSIGN_LSHIFT_EXPR, V0, assign_lshift_expr) \
|
||||
X(SPL_AST_ASSIGN_RSHIFT_EXPR, V0, assign_rshift_expr) \
|
||||
X(SPL_AST_BOOL_OR_EXPR, V0, bool_or_expr) \
|
||||
X(SPL_AST_BOOL_AND_EXPR, V0, bool_and_expr) \
|
||||
X(SPL_AST_BIT_OR_EXPR, V0, bit_or_expr) \
|
||||
X(SPL_AST_BIT_XOR_EXPR, V0, bit_xor_expr) \
|
||||
X(SPL_AST_BIT_AND_EXPR, V0, bit_and_expr) \
|
||||
X(SPL_AST_CMP_EQ_EXPR, V0, cmp_eq_expr) \
|
||||
X(SPL_AST_CMP_NE_EXPR, V0, cmp_ne_expr) \
|
||||
X(SPL_AST_CMP_LE_EXPR, V0, cmp_le_expr) \
|
||||
X(SPL_AST_CMP_GE_EXPR, V0, cmp_ge_expr) \
|
||||
X(SPL_AST_CMP_LT_EXPR, V0, cmp_lt_expr) \
|
||||
X(SPL_AST_CMP_GT_EXPR, V0, cmp_gt_expr) \
|
||||
X(SPL_AST_RANGE_EXPR, V0, range_expr) \
|
||||
X(SPL_AST_LSHIFT_EXPR, V0, lshift_expr) \
|
||||
X(SPL_AST_RSHIFT_EXPR, V0, rshift_expr) \
|
||||
X(SPL_AST_ADD_EXPR, V0, add_expr) \
|
||||
X(SPL_AST_SUB_EXPR, V0, sub_expr) \
|
||||
X(SPL_AST_MUL_EXPR, V0, mul_expr) \
|
||||
X(SPL_AST_DIV_EXPR, V0, div_expr) \
|
||||
X(SPL_AST_MOD_EXPR, V0, mod_expr) \
|
||||
X(SPL_AST_MINUS_EXPR, V0, minus_expr) \
|
||||
X(SPL_AST_NOT_EXPR, V0, not_expr) \
|
||||
X(SPL_AST_BIT_NOT_EXPR, V0, bit_not_expr) \
|
||||
X(SPL_AST_ADDRESS_EXPR, V0, address_expr) \
|
||||
X(SPL_AST_CALL_EXPR, V0, call_expr) \
|
||||
X(SPL_AST_FIELD_EXPR, V0, field_expr) \
|
||||
X(SPL_AST_DEREF_EXPR, V0, deref_expr) \
|
||||
X(SPL_AST_INDEX_EXPR, V0, index_expr) \
|
||||
X(SPL_AST_SLICE_EXPR, V0, slice_expr) \
|
||||
X(SPL_AST_AS_EXPR, V0, as_expr) \
|
||||
X(SPL_AST_EXPR_INTEGER_LIT, V0, integer_lit) \
|
||||
X(SPL_AST_EXPR_FLOAT_LIT, V0, float_lit) \
|
||||
X(SPL_AST_EXPR_CHAR_LIT, V0, char_lit) \
|
||||
X(SPL_AST_EXPR_STRING_LIT, V0, string_lit) \
|
||||
X(SPL_AST_EXPR_TRUE, V0, true_lit) \
|
||||
X(SPL_AST_EXPR_FALSE, V0, false_lit) \
|
||||
X(SPL_AST_EXPR_NULL, V0, null_lit) \
|
||||
X(SPL_AST_EXPR_UNDEFINED, V0, undefined_lit) \
|
||||
X(SPL_AST_EXPR_IDENT, V0, ident_expr) \
|
||||
X(SPL_AST_ARGGREGATE_INIT, V0, aggregate_init) \
|
||||
X(SPL_AST_EXPR_EXPR, V0, expr_expr) \
|
||||
X(SPL_AST_ARRAY_LIT, V0, array_lit) \
|
||||
X(SPL_AST_BUILTIN_EXPR, V0, builtin_expr) \
|
||||
X(SPL_AST_BLOCK_EXPR, V0, block_expr) \
|
||||
X(SPL_AST_BASE_TYPE_FN, V0, base_type_fn) \
|
||||
X(SPL_AST_BASE_TYPE_PATH, V0, base_type_path) \
|
||||
X(SPL_AST_TYPE_POINTER, V0, type_pointer) \
|
||||
X(SPL_AST_TYPE_ARRAY, V0, type_array) \
|
||||
X(SPL_AST_TYPE_SLICE, V0, type_slice) \
|
||||
X(SPL_AST_TYPE_STRUCT, V0, type_struct) \
|
||||
X(SPL_AST_TYPE_UNION, V0, type_union) \
|
||||
X(SPL_AST_TYPE_SHAPE, V0, type_shape) \
|
||||
X(SPL_AST_TYPE_ENUM, V0, type_enum) \
|
||||
X(SPL_AST_TYPE_VOID, V0, type_void) \
|
||||
X(SPL_AST_TYPE_BOOL, V0, type_bool) \
|
||||
X(SPL_AST_TYPE_OPAQUE, V0, type_opaque) \
|
||||
X(SPL_AST_TYPE_I8, V0, type_i8) \
|
||||
X(SPL_AST_TYPE_U8, V0, type_u8) \
|
||||
X(SPL_AST_TYPE_I16, V0, type_i16) \
|
||||
X(SPL_AST_TYPE_U16, V0, type_u16) \
|
||||
X(SPL_AST_TYPE_I32, V0, type_i32) \
|
||||
X(SPL_AST_TYPE_U32, V0, type_u32) \
|
||||
X(SPL_AST_TYPE_I64, V0, type_i64) \
|
||||
X(SPL_AST_TYPE_U64, V0, type_u64) \
|
||||
X(SPL_AST_TYPE_ISIZE, V0, type_isize) \
|
||||
X(SPL_AST_TYPE_USIZE, V0, type_usize) \
|
||||
X(SPL_AST_TYPE__F32, V0, type_f32) \
|
||||
X(SPL_AST_TYPE__F64, V0, type_f64) \
|
||||
X(SPL_AST_TYPE_ANY, V0, type_any) \
|
||||
X(SPL_AST_TYPE_IDENT, V0, type_ident) \
|
||||
|
||||
/* clang-format on*/
|
||||
#define SPL_AST_KIND_TABLE \
|
||||
X(SPL_AST_NONE, V0, none) \
|
||||
X(SPL_AST_ROOT, V0, root) \
|
||||
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_TOP_LEVEL_BLOCK, V0, top_level_block) \
|
||||
X(SPL_AST_FN_DECL, V0, fn_decl) \
|
||||
X(SPL_AST_VAR_DECL, V0, var_decl) \
|
||||
X(SPL_AST_CONST_DECL, V0, const_decl) \
|
||||
X(SPL_AST_TYPE_DECL, V0, type_decl) \
|
||||
X(SPL_AST_MEMBER_DECL, V0, member_decl) \
|
||||
X(SPL_AST_PARAM_DECL, V0, param_decl) \
|
||||
X(SPL_AST_STATEMENT_RET, V0, statement_ret) \
|
||||
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) \
|
||||
X(SPL_AST_STRUCT_INIT_ITEM, V0, struct_init_item) \
|
||||
X(SPL_AST_MATCH_ARM, V0, match_arm) \
|
||||
X(SPL_AST_LABEL, V0, label) \
|
||||
X(SPL_AST_EXPR_INT_LIT, V0, int_lit) \
|
||||
X(SPL_AST_EXPR_FLOAT_LIT, V0, float_lit) \
|
||||
X(SPL_AST_EXPR_CHAR_LIT, V0, char_lit) \
|
||||
X(SPL_AST_EXPR_STRING_LIT, V0, string_lit) \
|
||||
X(SPL_AST_EXPR_BOOL_LIT, V0, bool_lit) \
|
||||
X(SPL_AST_EXPR_NULL_LIT, V0, null_lit) \
|
||||
X(SPL_AST_EXPR_UNDEFINED_LIT, V0, undefined_lit) \
|
||||
X(SPL_AST_EXPR_IDENT, V0, ident) \
|
||||
X(SPL_AST_EXPR_UNARY, V0, unary) \
|
||||
X(SPL_AST_EXPR_BINARY, V0, binary) \
|
||||
X(SPL_AST_EXPR_CALL, V0, call) \
|
||||
X(SPL_AST_EXPR_FIELD, V0, field) \
|
||||
X(SPL_AST_EXPR_INDEX, V0, index) \
|
||||
X(SPL_AST_EXPR_SLICE, V0, slice) \
|
||||
X(SPL_AST_EXPR_CAST, V0, cast) \
|
||||
X(SPL_AST_EXPR_TUPLE, V0, tuple) \
|
||||
X(SPL_AST_EXPR_ARRAY_LIT, V0, array_literal) \
|
||||
X(SPL_AST_EXPR_ANON_ARRAY_LIT, V0, anon_array_literal) \
|
||||
X(SPL_AST_EXPR_AGGREGATE_LIT, V0, aggregate_literal) \
|
||||
X(SPL_AST_EXPR_COMPTIME_CALL, V0, comptime_call) \
|
||||
X(SPL_AST_EXPR_BLOCK, V0, block) \
|
||||
X(SPL_AST_EXPR_IF, V0, if_expr) \
|
||||
X(SPL_AST_EXPR_IFVAR, V0, ifvar_expr) \
|
||||
X(SPL_AST_EXPR_WHILE, V0, while_expr) \
|
||||
X(SPL_AST_EXPR_LOOP, V0, loop_expr) \
|
||||
X(SPL_AST_EXPR_FOR, V0, for_expr) \
|
||||
X(SPL_AST_EXPR_MATCH, V0, match_expr) \
|
||||
X(SPL_AST_EXPR_FN_TYPE, V0, fn_type) \
|
||||
X(SPL_AST_TYPE_PATH, V0, type_path) \
|
||||
X(SPL_AST_TYPE_POINTER, V0, type_pointer) \
|
||||
X(SPL_AST_TYPE_ARRAY, V0, type_array) \
|
||||
X(SPL_AST_TYPE_SLICE, V0, type_slice) \
|
||||
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_PATTERN_IDENT, V0, pattern_ident) \
|
||||
X(SPL_AST_PATTERN_UNDERSCORE, V0, pattern_underscore) \
|
||||
X(SPL_AST_PATTERN_DOT, V0, pattern_dot) \
|
||||
X(SPL_AST_PATTERN_TUPLE, V0, pattern_tuple) \
|
||||
X(SPL_AST_PATTERN_LIST, V0, pattern_list)
|
||||
/* clang-format on */
|
||||
|
||||
typedef enum {
|
||||
#ifdef X
|
||||
#undef X
|
||||
#endif
|
||||
#define X(name, ...) name,
|
||||
#define X(name, ...) name,
|
||||
SPL_AST_KIND_TABLE
|
||||
#undef X
|
||||
SPL_AST_COUNT,
|
||||
SPL_AST_COUNT,
|
||||
} spl_ast_node_kind_t;
|
||||
|
||||
const char *spl_ast_kind_name(spl_ast_node_kind_t kind);
|
||||
/* clang-format off */
|
||||
#define SPL_BINARY_OP_TABLE \
|
||||
X(SPL_OP_ASSIGN, 1, 'R', =) \
|
||||
X(SPL_OP_ASSIGN_ADD, 1, 'R', +=) \
|
||||
X(SPL_OP_ASSIGN_SUB, 1, 'R', -=) \
|
||||
X(SPL_OP_ASSIGN_MUL, 1, 'R', *=) \
|
||||
X(SPL_OP_ASSIGN_DIV, 1, 'R', /=) \
|
||||
X(SPL_OP_ASSIGN_MOD, 1, 'R', %=) \
|
||||
X(SPL_OP_ASSIGN_AND, 1, 'R', &=) \
|
||||
X(SPL_OP_ASSIGN_OR, 1, 'R', |=) \
|
||||
X(SPL_OP_ASSIGN_XOR, 1, 'R', ^=) \
|
||||
X(SPL_OP_ASSIGN_LSHIFT, 1, 'R', <<=) \
|
||||
X(SPL_OP_ASSIGN_RSHIFT, 1, 'R', >>=) \
|
||||
X(SPL_OP_BOOL_OR, 2, 'L', ||) \
|
||||
X(SPL_OP_BOOL_AND, 3, 'L', &&) \
|
||||
X(SPL_OP_BIT_OR, 4, 'L', |) \
|
||||
X(SPL_OP_BIT_XOR, 5, 'L', ^) \
|
||||
X(SPL_OP_BIT_AND, 6, 'L', &) \
|
||||
X(SPL_OP_CMP_EQ, 7, 'L', ==) \
|
||||
X(SPL_OP_CMP_NE, 7, 'L', !=) \
|
||||
X(SPL_OP_CMP_LE, 8, 'L', <=) \
|
||||
X(SPL_OP_CMP_GE, 8, 'L', >=) \
|
||||
X(SPL_OP_CMP_LT, 8, 'L', <) \
|
||||
X(SPL_OP_CMP_GT, 8, 'L', >) \
|
||||
X(SPL_OP_ARROW_LEFT, 9, 'L', <-) \
|
||||
X(SPL_OP_ARROW_RIGHT, 9, 'L', ->) \
|
||||
X(SPL_OP_ARROW_FAT_BOTH, 9, 'L', <=>) \
|
||||
X(SPL_OP_ARROW_THIN_BOTH, 9, 'L', <->) \
|
||||
X(SPL_OP_ARROW_FAT_RIGHT, 9, 'L', =>) \
|
||||
X(SPL_OP_RANGE, 10, 'L', ..) \
|
||||
X(SPL_OP_LSHIFT, 11, 'L', <<) \
|
||||
X(SPL_OP_RSHIFT, 11, 'L', >>) \
|
||||
X(SPL_OP_ADD, 12, 'L', +) \
|
||||
X(SPL_OP_SUB, 12, 'L', -) \
|
||||
X(SPL_OP_MUL, 13, 'L', *) \
|
||||
X(SPL_OP_DIV, 13, 'L', /) \
|
||||
X(SPL_OP_MOD, 13, 'L', %)
|
||||
/* clang-format on */
|
||||
|
||||
typedef enum {
|
||||
#ifdef X
|
||||
#undef X
|
||||
#endif
|
||||
#define X(name, ...) name,
|
||||
SPL_BINARY_OP_TABLE
|
||||
#undef X
|
||||
} spl_binary_op_t;
|
||||
|
||||
typedef enum {
|
||||
SPL_UNARY_OP_MINUS,
|
||||
SPL_UNARY_OP_NOT,
|
||||
SPL_UNARY_OP_BIT_NOT,
|
||||
SPL_UNARY_OP_ADDRESS,
|
||||
} spl_unary_op_t;
|
||||
|
||||
const char *spl_binary_op_name(spl_binary_op_t op);
|
||||
const char *spl_unary_op_name(spl_unary_op_t op);
|
||||
|
||||
struct spl_ast_node;
|
||||
typedef struct spl_ast_node spl_ast_node_t;
|
||||
typedef usize spl_ast_node_ref_t;
|
||||
typedef VEC(spl_ast_node_ref_t) spl_ast_node_ref_vec_t;
|
||||
|
||||
struct spl_ast_node {
|
||||
spl_ast_node_kind_t kind;
|
||||
spl_dbg_node_t dbg;
|
||||
@@ -143,168 +156,337 @@ struct spl_ast_node {
|
||||
usize resolved_def_id;
|
||||
usize resolved_type_id;
|
||||
};
|
||||
|
||||
union {
|
||||
spl_ast_node_ref_vec_t container_members; /* container_decl */
|
||||
|
||||
/* SPL_AST_ROOT */
|
||||
struct {
|
||||
const char *ident;
|
||||
spl_ast_node_ref_vec_t expr_list;
|
||||
} attr_item;
|
||||
spl_ast_node_ref_t members; /* container_members 节点 */
|
||||
} root;
|
||||
|
||||
/* SPL_AST_CONTAINER_MEMBERS */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
spl_ast_node_ref_vec_t external_tags; /* external_tag_item 节点列表 */
|
||||
spl_ast_node_ref_vec_t declarations;
|
||||
} container_members;
|
||||
|
||||
/* SPL_AST_FN_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t tag_list; /* tag_item 节点 */
|
||||
bool is_comptime;
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t param_list; /* param_decl */
|
||||
spl_ast_node_ref_t type_expr; /* ret type expr */
|
||||
spl_ast_node_ref_t block; /* block_expr */
|
||||
spl_ast_node_ref_vec_t params; /* param_decl 节点 */
|
||||
spl_ast_node_ref_t ret_type; /* 类型节点,可空 */
|
||||
spl_ast_node_ref_t body; /* 表达式节点,可空表示仅声明 */
|
||||
} fn_decl;
|
||||
|
||||
/* SPL_AST_VAR_DECL / SPL_AST_CONST_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
spl_ast_node_ref_t pattern; /* 模式节点 */
|
||||
spl_ast_node_ref_t type; /* 类型节点,可空 */
|
||||
spl_ast_node_ref_t init_expr; /* 表达式节点,可空 */
|
||||
} var_const_decl;
|
||||
|
||||
/* SPL_AST_TYPE_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr; /* 类型节点 */
|
||||
} type_decl;
|
||||
|
||||
/* SPL_AST_MEMBER_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr; /* 可空 */
|
||||
spl_ast_node_ref_t init_expr; /* 可空 */
|
||||
} member_decl;
|
||||
|
||||
/* SPL_AST_PARAM_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} param_decl;
|
||||
|
||||
/* SPL_AST_TAG_ITEM / SPL_AST_EXTERNAL_TAG_ITEM */
|
||||
struct {
|
||||
bool is_at; /* true: @, false: # */
|
||||
bool is_external; /* true: @!或#! */
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t args;
|
||||
} tag_item;
|
||||
|
||||
/* SPL_AST_COMPTIME_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_t body; /* block 或 expr 节点 */
|
||||
} comptime_decl;
|
||||
|
||||
/* SPL_AST_TOP_LEVEL_BLOCK */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t tag_list; /* tag_item 节点列表 */
|
||||
spl_ast_node_ref_t block; /* 表达式节点block */
|
||||
} top_level_block;
|
||||
|
||||
/* SPL_AST_STATEMENT_RET */
|
||||
struct {
|
||||
spl_ast_node_ref_t expr; /* 可空 */
|
||||
} statement_ret;
|
||||
|
||||
/* SPL_AST_STATEMENT_BREAK */
|
||||
struct {
|
||||
spl_ast_node_ref_t label; /* 可空 */
|
||||
spl_ast_node_ref_t expr; /* 可空 */
|
||||
} statement_break;
|
||||
|
||||
/* SPL_AST_STATEMENT_CONTINUE */
|
||||
struct {
|
||||
spl_ast_node_ref_t label; /* 可空 */
|
||||
} statement_continue;
|
||||
|
||||
/* SPL_AST_STATEMENT_DEFER */
|
||||
struct {
|
||||
spl_ast_node_ref_t body; /* block 或 statement 节点 */
|
||||
} statement_defer;
|
||||
|
||||
/* SPL_AST_STATEMENT_COMPTIME */
|
||||
struct {
|
||||
spl_ast_node_ref_t body; /* block 或 expr 节点 */
|
||||
} statement_comptime;
|
||||
|
||||
/* SPL_AST_STATEMENT_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_t decl; /* var/const/type 声明节点 */
|
||||
} statement_decl;
|
||||
|
||||
/* SPL_AST_STATEMENT_EXPR */
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
} statement_expr;
|
||||
|
||||
/* SPL_AST_AGGREGATE_MEMBER */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list;
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr; /* 可空 */
|
||||
spl_ast_node_ref_t init_expr; /* 可空 */
|
||||
} aggregate_member;
|
||||
|
||||
/* SPL_AST_STRUCT_INIT_ITEM */
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} type_decl;
|
||||
spl_ast_node_ref_t expr; /* 可空 */
|
||||
} struct_init_item;
|
||||
|
||||
/* SPL_AST_MATCH_ARM */
|
||||
struct {
|
||||
spl_ast_node_ref_t pattern; /* 模式节点 */
|
||||
spl_ast_node_ref_t body; /* block 或 expr 节点 */
|
||||
} match_arm;
|
||||
|
||||
/* SPL_AST_LABEL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} member_decl;
|
||||
} label;
|
||||
|
||||
/* ---- 表达式子类型 ---- */
|
||||
struct {
|
||||
isize val;
|
||||
} int_lit;
|
||||
struct {
|
||||
double val;
|
||||
} float_lit;
|
||||
struct {
|
||||
char val;
|
||||
} char_lit;
|
||||
struct {
|
||||
const char *data;
|
||||
} string_lit;
|
||||
struct {
|
||||
bool val;
|
||||
} bool_lit;
|
||||
/* null_lit, undefined_lit 无数据 */
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
const char *name;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
spl_ast_node_ref_t expr;
|
||||
} var_const_decl;
|
||||
|
||||
/* statement */
|
||||
} ident;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
spl_ast_node_ref_t if_block; /* block_expr */
|
||||
spl_ast_node_ref_t else_block; /* block_expr */
|
||||
} if_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t packed_expr;
|
||||
spl_ast_node_ref_t if_block; /* block_expr */
|
||||
spl_ast_node_ref_t else_block; /* block_expr */
|
||||
} ifvar_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
spl_ast_node_ref_t while_block; /* block_expr */
|
||||
} while_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t loop_block; /* block_expr */
|
||||
} loop_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t expr_vec;
|
||||
VEC(char *) ident_vec;
|
||||
spl_ast_node_ref_t block; /* block_expr */
|
||||
} for_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
spl_ast_node_ref_vec_t paced_exprs; /* packed_expr */
|
||||
spl_ast_node_ref_t match_block; /* block_expr */
|
||||
} match_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
} ret_statement;
|
||||
struct {
|
||||
} break_statement;
|
||||
struct {
|
||||
} continue_statement;
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t block_or_statement; /* 语句 */
|
||||
} defer_statement;
|
||||
spl_unary_op_t op;
|
||||
spl_ast_node_ref_t operand;
|
||||
} unary;
|
||||
|
||||
struct {
|
||||
const char *ident;
|
||||
const char *bind_ident;
|
||||
spl_ast_node_ref_t expr;
|
||||
} packed_expr;
|
||||
struct {
|
||||
spl_binary_op_t op;
|
||||
spl_ast_node_ref_t left;
|
||||
spl_ast_node_ref_t right;
|
||||
} op_expr;
|
||||
struct {
|
||||
spl_ast_node_ref_t postfix_expr;
|
||||
} prefix_expr;
|
||||
struct {
|
||||
spl_ast_node_ref_t primary_expr;
|
||||
union {
|
||||
spl_ast_node_ref_vec_t call_expr;
|
||||
const char *field_expr; /* field/method */
|
||||
spl_ast_node_ref_t index_expr;
|
||||
struct {
|
||||
spl_ast_node_ref_t begin;
|
||||
spl_ast_node_ref_t end;
|
||||
} slice_expr;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
};
|
||||
} postfix_expr;
|
||||
} binary;
|
||||
|
||||
struct {
|
||||
isize integer_expr;
|
||||
double float_expr;
|
||||
char char_lit_expr;
|
||||
const char *string_lit_expr; /* parsed c string */
|
||||
const char *ident;
|
||||
spl_ast_node_ref_t callee;
|
||||
spl_ast_node_ref_vec_t args;
|
||||
} call;
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t expr; /* aggregate_init_item */
|
||||
} aggregate_init;
|
||||
struct {
|
||||
spl_ast_node_ref_t obj;
|
||||
const char *field;
|
||||
} field;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t obj;
|
||||
spl_ast_node_ref_t index;
|
||||
} index;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t obj;
|
||||
spl_ast_node_ref_t begin;
|
||||
spl_ast_node_ref_t end;
|
||||
} slice;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t expr;
|
||||
struct {
|
||||
isize integer;
|
||||
spl_ast_node_ref_t type_expr;
|
||||
spl_ast_node_ref_vec_t expr_list;
|
||||
} array_lit_expr;
|
||||
struct {
|
||||
const char *ident;
|
||||
spl_ast_node_ref_vec_t expr_list;
|
||||
} builtin_expr;
|
||||
spl_ast_node_ref_vec_t block_expr; /* 语句 */
|
||||
} primary_expr;
|
||||
spl_ast_node_ref_t type;
|
||||
} cast;
|
||||
|
||||
struct {
|
||||
const char *ident;
|
||||
spl_ast_node_ref_t expr;
|
||||
} aggregate_init_item;
|
||||
spl_ast_node_ref_vec_t elems;
|
||||
} tuple;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t attr_list; /* attr_item */
|
||||
union {
|
||||
struct {
|
||||
VEC(const char *) ident_vec;
|
||||
} type_path;
|
||||
struct {
|
||||
spl_ast_node_ref_t element;
|
||||
spl_ast_node_ref_t size;
|
||||
} array_type;
|
||||
struct {
|
||||
spl_ast_node_ref_t element;
|
||||
} slice_type;
|
||||
struct {
|
||||
spl_ast_node_ref_t pointee;
|
||||
} pointer_type;
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t param_list; /* param_decl */
|
||||
spl_ast_node_ref_t type_expr;
|
||||
} fn_type;
|
||||
spl_ast_node_ref_vec_t aggregate_list; /* container_decl */
|
||||
};
|
||||
} type_expr;
|
||||
bool has_len;
|
||||
isize len;
|
||||
bool is_underscore_len;
|
||||
spl_ast_node_ref_t elem_type;
|
||||
spl_ast_node_ref_vec_t elems;
|
||||
} array_literal;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t elems;
|
||||
} anon_array_literal;
|
||||
|
||||
struct {
|
||||
const char *name; /* 可空,匿名时为 NULL */
|
||||
spl_ast_node_ref_vec_t fields; /* struct_init_item 节点列表 */
|
||||
} aggregate_literal;
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t args;
|
||||
} comptime_call;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t stmts;
|
||||
spl_ast_node_ref_t tail_expr;
|
||||
} block;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t cond;
|
||||
spl_ast_node_ref_t then_block;
|
||||
spl_ast_node_ref_t else_branch;
|
||||
} if_expr;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t var_decl;
|
||||
spl_ast_node_ref_t then_block;
|
||||
spl_ast_node_ref_t else_branch;
|
||||
} ifvar_expr;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t label;
|
||||
spl_ast_node_ref_t cond;
|
||||
spl_ast_node_ref_t body;
|
||||
spl_ast_node_ref_t else_branch;
|
||||
} while_expr;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t label;
|
||||
spl_ast_node_ref_t body;
|
||||
} loop_expr;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t label;
|
||||
spl_ast_node_ref_vec_t iter_exprs;
|
||||
spl_ast_node_ref_vec_t idents;
|
||||
spl_ast_node_ref_t body;
|
||||
spl_ast_node_ref_t else_branch;
|
||||
} for_expr;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t matched_expr;
|
||||
spl_ast_node_ref_vec_t arms; /* match_arm 节点 */
|
||||
} match_expr;
|
||||
|
||||
struct {
|
||||
bool is_comptime;
|
||||
spl_ast_node_ref_vec_t params;
|
||||
spl_ast_node_ref_t ret_type;
|
||||
} fn_type;
|
||||
|
||||
/* ---- 类型子类型 ---- */
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t generic_args;
|
||||
} type_path;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t pointee;
|
||||
} type_pointer;
|
||||
|
||||
struct {
|
||||
bool has_len;
|
||||
isize len;
|
||||
bool is_underscore_len;
|
||||
spl_ast_node_ref_t elem_type;
|
||||
} type_array;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_t elem_type;
|
||||
} type_slice;
|
||||
|
||||
struct {
|
||||
bool is_comptime;
|
||||
spl_ast_node_ref_vec_t params;
|
||||
spl_ast_node_ref_t ret_type;
|
||||
} type_fn;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t types;
|
||||
} type_paren;
|
||||
|
||||
struct {
|
||||
const char *kind_name;
|
||||
spl_ast_node_ref_vec_t members; /* aggregate_member 节点 */
|
||||
} type_aggregate;
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t args;
|
||||
} type_comptime_call;
|
||||
|
||||
/* ---- 模式子类型 ---- */
|
||||
struct {
|
||||
const char *name;
|
||||
} pattern_ident;
|
||||
|
||||
/* pattern_underscore 无数据 */
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_t sub_pattern;
|
||||
} pattern_dot;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t patterns;
|
||||
} pattern_tuple;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t patterns;
|
||||
} pattern_list;
|
||||
};
|
||||
};
|
||||
|
||||
typedef VEC(spl_ast_node_t) spl_ast_node_vec_t;
|
||||
typedef VEC(char*)spl_ast_cstr_ref_vec_t;
|
||||
typedef VEC(char *) spl_ast_cstr_ref_vec_t;
|
||||
typedef struct {
|
||||
int parsed;
|
||||
spl_tok_vec_t input;
|
||||
@@ -316,9 +498,10 @@ typedef struct {
|
||||
void spl_ast_init(spl_ast_t *ast, const spl_tok_vec_t *tok_vec /*move*/);
|
||||
void spl_ast_drop(spl_ast_t *ast);
|
||||
|
||||
spl_ast_node_t* spl_ast_node(spl_ast_t *ast, spl_ast_node_ref_t node);
|
||||
spl_ast_node_t *spl_ast_node(spl_ast_t *ast, spl_ast_node_ref_t node);
|
||||
const char *spl_ast_node_name(spl_ast_node_t *node);
|
||||
|
||||
void spl_ast_prase(spl_ast_t *ast);
|
||||
void spl_ast_parse(spl_ast_t *ast);
|
||||
void spl_ast_valid(spl_ast_t *ast);
|
||||
void spl_ast_dump(spl_ast_t *ast, spl_ast_node_ref_t node);
|
||||
|
||||
|
||||
Reference in new issue
Block a user