stage1 重构代码 语法上重新实现解包语法
This commit is contained in:
1 parent
2a84b61e3d
commit
54425a50c7
10 files changed
+2681
-2478
No files matched your search
+51
-78
@@ -4,9 +4,6 @@
|
||||
#include "../stage0/include/utils.h"
|
||||
#include "spl_attr.h"
|
||||
#include "spl_dbg.h"
|
||||
#include "spl_diag.h"
|
||||
#include "spl_lexer.h"
|
||||
#include "spl_mpa.h"
|
||||
#include "spl_tok.h"
|
||||
|
||||
/* clang-format off */
|
||||
@@ -74,7 +71,9 @@
|
||||
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)
|
||||
X(SPL_AST_PATTERN_LIST, V0, pattern_list) \
|
||||
X(SPL_AST_PATTERN_AGGREGATE, V0, pattern_aggregate) \
|
||||
X(SPL_AST_PATTERN_FIELD, V0, pattern_field)
|
||||
/* clang-format on */
|
||||
|
||||
typedef enum {
|
||||
@@ -143,9 +142,6 @@ typedef enum {
|
||||
SPL_UNARY_OP_DEREF,
|
||||
} 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;
|
||||
@@ -161,138 +157,113 @@ struct spl_ast_node {
|
||||
spl_attr_id_vec_t attrs;
|
||||
|
||||
union {
|
||||
/* SPL_AST_ROOT */
|
||||
struct {
|
||||
spl_ast_node_ref_t members; /* container_members 节点 */
|
||||
spl_ast_node_ref_t members;
|
||||
} root;
|
||||
|
||||
/* SPL_AST_CONTAINER_MEMBERS */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t external_tags; /* external_tag_item 节点列表 */
|
||||
spl_ast_node_ref_vec_t external_tags;
|
||||
spl_ast_node_ref_vec_t declarations;
|
||||
} container_members;
|
||||
|
||||
/* SPL_AST_FN_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t tag_list; /* tag_item 节点 */
|
||||
spl_ast_node_ref_vec_t tag_list;
|
||||
bool is_comptime;
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t params; /* param_decl 节点 */
|
||||
spl_ast_node_ref_t ret_type; /* 类型节点,可空 */
|
||||
spl_ast_node_ref_t body; /* 表达式节点,可空表示仅声明 */
|
||||
spl_ast_node_ref_vec_t params;
|
||||
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;
|
||||
spl_ast_node_ref_t pattern; /* 模式节点 */
|
||||
spl_ast_node_ref_t type; /* 类型节点,可空 */
|
||||
spl_ast_node_ref_t init_expr; /* 表达式节点,可空 */
|
||||
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; /* 类型节点 */
|
||||
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; /* 可空 */
|
||||
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: @!或#! */
|
||||
bool is_at;
|
||||
bool is_external;
|
||||
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 节点 */
|
||||
spl_ast_node_ref_t body;
|
||||
} 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 */
|
||||
spl_ast_node_ref_vec_t tag_list;
|
||||
spl_ast_node_ref_t block;
|
||||
} top_level_block;
|
||||
|
||||
/* SPL_AST_STATEMENT_RET */
|
||||
struct {
|
||||
spl_ast_node_ref_t expr; /* 可空 */
|
||||
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; /* 可空 */
|
||||
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; /* 可空 */
|
||||
spl_ast_node_ref_t label;
|
||||
} statement_continue;
|
||||
|
||||
/* SPL_AST_STATEMENT_DEFER */
|
||||
struct {
|
||||
spl_ast_node_ref_t body; /* block 或 statement 节点 */
|
||||
spl_ast_node_ref_t body;
|
||||
} statement_defer;
|
||||
|
||||
/* SPL_AST_STATEMENT_COMPTIME */
|
||||
struct {
|
||||
spl_ast_node_ref_t body; /* block 或 expr 节点 */
|
||||
spl_ast_node_ref_t body;
|
||||
} statement_comptime;
|
||||
|
||||
/* SPL_AST_STATEMENT_DECL */
|
||||
struct {
|
||||
spl_ast_node_ref_t decl; /* var/const/type 声明节点 */
|
||||
spl_ast_node_ref_t decl;
|
||||
} 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; /* 可空 */
|
||||
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 expr; /* 可空 */
|
||||
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 节点 */
|
||||
spl_ast_node_ref_t pattern;
|
||||
spl_ast_node_ref_t body;
|
||||
} match_arm;
|
||||
|
||||
/* SPL_AST_LABEL */
|
||||
struct {
|
||||
const char *name;
|
||||
} label;
|
||||
|
||||
/* ---- 表达式子类型 ---- */
|
||||
spl_mpa_int_t int_lit;
|
||||
spl_mpa_float_t float_lit;
|
||||
struct {
|
||||
@@ -305,7 +276,6 @@ struct spl_ast_node {
|
||||
struct {
|
||||
bool val;
|
||||
} bool_lit;
|
||||
/* null_lit, undefined_lit 无数据 */
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
@@ -356,13 +326,13 @@ struct spl_ast_node {
|
||||
bool has_len;
|
||||
usize len;
|
||||
bool is_underscore_len;
|
||||
spl_ast_node_ref_t elem_type; /* 可空 匿名时为 NULL */
|
||||
spl_ast_node_ref_t elem_type;
|
||||
spl_ast_node_ref_vec_t elems;
|
||||
} array_literal;
|
||||
|
||||
struct {
|
||||
const char *name; /* 可空 匿名时为 NULL */
|
||||
spl_ast_node_ref_vec_t fields; /* struct_init_item 节点列表 */
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t fields;
|
||||
} aggregate_literal;
|
||||
|
||||
struct {
|
||||
@@ -402,14 +372,14 @@ struct spl_ast_node {
|
||||
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 pattern;
|
||||
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 节点 */
|
||||
spl_ast_node_ref_vec_t arms;
|
||||
} match_expr;
|
||||
|
||||
struct {
|
||||
@@ -418,7 +388,6 @@ struct spl_ast_node {
|
||||
spl_ast_node_ref_t ret_type;
|
||||
} fn_type;
|
||||
|
||||
/* ---- 类型子类型 ---- */
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_vec_t generic_args;
|
||||
@@ -451,7 +420,7 @@ struct spl_ast_node {
|
||||
|
||||
struct {
|
||||
const char *kind_name;
|
||||
spl_ast_node_ref_vec_t members; /* aggregate_member 节点 */
|
||||
spl_ast_node_ref_vec_t members;
|
||||
} type_aggregate;
|
||||
|
||||
struct {
|
||||
@@ -459,13 +428,10 @@ struct spl_ast_node {
|
||||
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;
|
||||
@@ -474,10 +440,18 @@ struct spl_ast_node {
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t patterns;
|
||||
} pattern_tuple;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t patterns;
|
||||
} pattern_list;
|
||||
|
||||
struct {
|
||||
spl_ast_node_ref_vec_t fields;
|
||||
} pattern_aggregate;
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
spl_ast_node_ref_t pattern;
|
||||
} pattern_field;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -488,8 +462,6 @@ typedef struct spl_ast {
|
||||
spl_ast_node_vec_t node_buckets;
|
||||
spl_ast_cstr_ref_vec_t str_buckets;
|
||||
spl_ast_node_ref_t root;
|
||||
|
||||
spl_diag_t *diag;
|
||||
} spl_ast_t;
|
||||
typedef VEC(spl_ast_t *) spl_ast_ptr_vec_t;
|
||||
|
||||
@@ -497,9 +469,10 @@ 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);
|
||||
const char *spl_ast_node_name(spl_ast_node_t *node);
|
||||
|
||||
void spl_ast_parse(spl_ast_t *ast);
|
||||
void spl_ast_dump(spl_ast_t *ast, spl_ast_node_ref_t node);
|
||||
|
||||
const char *spl_ast_kind_name(spl_ast_node_kind_t kind);
|
||||
const char *spl_binary_op_name(spl_binary_op_t op);
|
||||
const char *spl_unary_op_name(spl_unary_op_t op);
|
||||
|
||||
#endif /* __SPL_AST_H__ */
|
||||
Reference in new issue
Block a user