stage1 实现部分sema 解析数组
This commit is contained in:
1 parent
8aa7a89c7e
commit
bcd26d3021
6 files changed
+1049
-593
No files matched your search
+21
-10
@@ -349,13 +349,13 @@ static void parse_param_list(parser_t *p, spl_ast_node_ref_vec_t *out) {
|
|||||||
if (peek(p) && peek(p)->type == TOK_COMMA)
|
if (peek(p) && peek(p)->type == TOK_COMMA)
|
||||||
advance(p);
|
advance(p);
|
||||||
break;
|
break;
|
||||||
} else if (t->type == KW_VOID) {
|
// } else if (t->type == KW_VOID) {
|
||||||
advance(p);
|
// advance(p);
|
||||||
t = peek(p);
|
// t = peek(p);
|
||||||
if (!t || t->type != TOK_R_PAREN) {
|
// if (!t || t->type != TOK_R_PAREN) {
|
||||||
SPL_FATAL(t, "expected ')' after void");
|
// SPL_FATAL(t, "expected ')' after void");
|
||||||
}
|
// }
|
||||||
break;
|
// break;
|
||||||
} else {
|
} else {
|
||||||
SPL_FATAL(t, "unexpected token in parameter list");
|
SPL_FATAL(t, "unexpected token in parameter list");
|
||||||
break;
|
break;
|
||||||
@@ -849,9 +849,10 @@ static spl_ast_node_ref_t parse_type_base(parser_t *p) {
|
|||||||
(len == 5 && strncmp(name, "usize", 5) == 0) ||
|
(len == 5 && strncmp(name, "usize", 5) == 0) ||
|
||||||
(len == 3 && strncmp(name, "f32", 3) == 0) ||
|
(len == 3 && strncmp(name, "f32", 3) == 0) ||
|
||||||
(len == 3 && strncmp(name, "f64", 3) == 0) ||
|
(len == 3 && strncmp(name, "f64", 3) == 0) ||
|
||||||
(len == 4 && strncmp(name, "bool", 4) == 0) ||
|
(len == 4 && strncmp(name, "bool", 4) == 0)
|
||||||
(len == 4 && strncmp(name, "void", 4) == 0) ||
|
// (len == 4 && strncmp(name, "void", 4) == 0) ||
|
||||||
(len == 3 && strncmp(name, "ptr", 3) == 0)) {
|
// (len == 3 && strncmp(name, "any", 3) == 0)
|
||||||
|
) {
|
||||||
advance(p);
|
advance(p);
|
||||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_PATH, bt);
|
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_PATH, bt);
|
||||||
spl_ast_node_t *tp = node_at(p->ast, ref);
|
spl_ast_node_t *tp = node_at(p->ast, ref);
|
||||||
@@ -886,6 +887,16 @@ static spl_ast_node_ref_t parse_type_base(parser_t *p) {
|
|||||||
return parse_type_comptime_call(p);
|
return parse_type_comptime_call(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (bt->type == KW_ANY) {
|
||||||
|
advance(p);
|
||||||
|
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_PATH, bt);
|
||||||
|
spl_ast_node_t *tp = node_at(p->ast, ref);
|
||||||
|
SET_STR_FIELD(p->ast, tp->type_path.name, bt);
|
||||||
|
vec_init(tp->type_path.generic_args);
|
||||||
|
vec_free(tag_list);
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
vec_free(tag_list);
|
vec_free(tag_list);
|
||||||
SPL_FATAL(bt, "unrecognized type");
|
SPL_FATAL(bt, "unrecognized type");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+1
-1
@@ -354,7 +354,7 @@ struct spl_ast_node {
|
|||||||
|
|
||||||
struct {
|
struct {
|
||||||
bool has_len;
|
bool has_len;
|
||||||
isize len;
|
usize len;
|
||||||
bool is_underscore_len;
|
bool is_underscore_len;
|
||||||
spl_ast_node_ref_t elem_type;
|
spl_ast_node_ref_t elem_type;
|
||||||
spl_ast_node_ref_vec_t elems;
|
spl_ast_node_ref_vec_t elems;
|
||||||
|
|||||||
+349
-256
File diff suppressed because it is too large.
Load diff
+1
-1
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
/* clang-format off */
|
/* clang-format off */
|
||||||
#define KEYWORD_TABLE \
|
#define KEYWORD_TABLE \
|
||||||
|
X(any , KW_ANY , SPL_V0) \
|
||||||
X(as , KW_AS , SPL_V0) \
|
X(as , KW_AS , SPL_V0) \
|
||||||
X(bool , KW_BOOL , SPL_V0) \
|
X(bool , KW_BOOL , SPL_V0) \
|
||||||
X(break , KW_BREAK , SPL_V0) \
|
X(break , KW_BREAK , SPL_V0) \
|
||||||
@@ -33,7 +34,6 @@
|
|||||||
X(undefined , KW_UNDEFINED , SPL_V0) \
|
X(undefined , KW_UNDEFINED , SPL_V0) \
|
||||||
X(union , KW_UNION , SPL_V0) \
|
X(union , KW_UNION , SPL_V0) \
|
||||||
X(var , KW_VAR , SPL_V0) \
|
X(var , KW_VAR , SPL_V0) \
|
||||||
X(void , KW_VOID , SPL_V0) \
|
|
||||||
X(while , KW_WHILE , SPL_V0) \
|
X(while , KW_WHILE , SPL_V0) \
|
||||||
X(_ , KW_UNDERSCORE , SPL_V0) \
|
X(_ , KW_UNDERSCORE , SPL_V0) \
|
||||||
// KEYWORD_TABLE
|
// KEYWORD_TABLE
|
||||||
|
|||||||
+603
-288
File diff suppressed because it is too large.
Load diff
+74
-37
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
typedef usize spl_type_id_t; /* 0 is error */
|
typedef usize spl_type_id_t; /* 0 is error */
|
||||||
typedef VEC(spl_type_id_t) spl_type_id_vec_t;
|
typedef VEC(spl_type_id_t) spl_type_id_vec_t;
|
||||||
|
typedef usize spl_lang_type_id_t; /* 0 is error */
|
||||||
|
typedef VEC(spl_lang_type_id_t) spl_lang_type_id_vec_t;
|
||||||
typedef usize spl_def_id_t; /* 0 is error */
|
typedef usize spl_def_id_t; /* 0 is error */
|
||||||
typedef VEC(spl_def_id_t) spl_def_id_vec_t;
|
typedef VEC(spl_def_id_t) spl_def_id_vec_t;
|
||||||
|
|
||||||
@@ -13,8 +15,8 @@ typedef enum {
|
|||||||
SPL_TYPE_ERROR,
|
SPL_TYPE_ERROR,
|
||||||
SPL_TYPE_NEVER, // bottom type
|
SPL_TYPE_NEVER, // bottom type
|
||||||
SPL_TYPE_ANY, // top type
|
SPL_TYPE_ANY, // top type
|
||||||
SPL_TYPE_INFER, // auto type
|
|
||||||
SPL_TYPE_UNIT,
|
SPL_TYPE_UNIT,
|
||||||
|
SPL_TYPE_INFER,
|
||||||
SPL_TYPE_BOOL,
|
SPL_TYPE_BOOL,
|
||||||
SPL_TYPE_INT,
|
SPL_TYPE_INT,
|
||||||
SPL_TYPE_FLOAT,
|
SPL_TYPE_FLOAT,
|
||||||
@@ -29,14 +31,6 @@ typedef enum {
|
|||||||
SPL_TYPE_SHAPE,
|
SPL_TYPE_SHAPE,
|
||||||
} spl_type_node_kind_t;
|
} spl_type_node_kind_t;
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
SPL_LANG_TYPE_NONE, // 普通底层类型(如 int, ptr 等)
|
|
||||||
SPL_LANG_TYPE_INFER, // 类型推断占位符
|
|
||||||
SPL_LANG_TYPE_SLICE, // 切片(底层 struct {ptr, len})
|
|
||||||
SPL_LANG_TYPE_TUPLE, // 元组(底层 struct,字段名为数字)
|
|
||||||
SPL_LANG_TYPE_RANGE, // 范围(可选)
|
|
||||||
} spl_lang_type_node_kind_t;
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SPL_TYPE_LAYOUT_AUTO,
|
SPL_TYPE_LAYOUT_AUTO,
|
||||||
SPL_TYPE_LAYOUT_EXTERN_C,
|
SPL_TYPE_LAYOUT_EXTERN_C,
|
||||||
@@ -57,7 +51,6 @@ typedef VEC(spl_type_member_t) spl_type_member_vec_t;
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
spl_type_node_kind_t kind;
|
spl_type_node_kind_t kind;
|
||||||
spl_lang_type_node_kind_t lang_kind;
|
|
||||||
spl_type_layout_t layout; // attribute
|
spl_type_layout_t layout; // attribute
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
@@ -89,6 +82,43 @@ typedef struct {
|
|||||||
} spl_type_node_t;
|
} spl_type_node_t;
|
||||||
typedef VEC(spl_type_node_t) spl_type_node_vec_t;
|
typedef VEC(spl_type_node_t) spl_type_node_vec_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SPL_LANG_TYPE_ERROR,
|
||||||
|
SPL_LANG_TYPE_INFER,
|
||||||
|
SPL_LANG_TYPE_NEVER,
|
||||||
|
SPL_LANG_TYPE_ANY,
|
||||||
|
SPL_LANG_TYPE_UNIT,
|
||||||
|
SPL_LANG_TYPE_BOOL,
|
||||||
|
SPL_LANG_TYPE_INT,
|
||||||
|
SPL_LANG_TYPE_FLOAT,
|
||||||
|
SPL_LANG_TYPE_PTR,
|
||||||
|
SPL_LANG_TYPE_ARRAY,
|
||||||
|
SPL_LANG_TYPE_SLICE,
|
||||||
|
SPL_LANG_TYPE_TUPLE,
|
||||||
|
SPL_LANG_TYPE_RANGE,
|
||||||
|
SPL_LANG_TYPE_FN,
|
||||||
|
SPL_LANG_TYPE_STRUCT,
|
||||||
|
SPL_LANG_TYPE_UNION,
|
||||||
|
SPL_LANG_TYPE_ENUM,
|
||||||
|
SPL_LANG_TYPE_SHAPE,
|
||||||
|
SPL_LANG_TYPE_VARARGS,
|
||||||
|
} spl_lang_type_kind_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
spl_lang_type_kind_t kind;
|
||||||
|
spl_type_id_t ir_type_id;
|
||||||
|
union {
|
||||||
|
spl_lang_type_id_t elem;
|
||||||
|
spl_lang_type_id_vec_t elems;
|
||||||
|
struct {
|
||||||
|
spl_lang_type_id_vec_t params;
|
||||||
|
spl_lang_type_id_t ret;
|
||||||
|
} fn;
|
||||||
|
} extra;
|
||||||
|
} spl_lang_type_node_t;
|
||||||
|
typedef VEC(spl_lang_type_node_t) spl_lang_type_vec_t;
|
||||||
|
typedef MAP(usize, spl_lang_type_id_t) spl_lang_type_map_t;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SPL_DEF_ERROR,
|
SPL_DEF_ERROR,
|
||||||
SPL_DEF_SCALAR,
|
SPL_DEF_SCALAR,
|
||||||
@@ -110,7 +140,7 @@ typedef struct {
|
|||||||
} builtin_kind;
|
} builtin_kind;
|
||||||
const char *name;
|
const char *name;
|
||||||
spl_dbg_node_t dbg_node;
|
spl_dbg_node_t dbg_node;
|
||||||
spl_type_id_t type_id;
|
spl_lang_type_id_t lang_id;
|
||||||
usize ast_node_ref;
|
usize ast_node_ref;
|
||||||
} spl_def_node_t;
|
} spl_def_node_t;
|
||||||
typedef VEC(spl_def_node_t) spl_def_node_vec_t;
|
typedef VEC(spl_def_node_t) spl_def_node_vec_t;
|
||||||
@@ -119,6 +149,8 @@ typedef MAP(usize, spl_type_id_t) spl_type_node_map_t;
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
spl_type_node_vec_t type_table;
|
spl_type_node_vec_t type_table;
|
||||||
spl_type_node_map_t type_map;
|
spl_type_node_map_t type_map;
|
||||||
|
spl_lang_type_vec_t lang_table;
|
||||||
|
spl_lang_type_map_t lang_map;
|
||||||
spl_def_node_vec_t def_table;
|
spl_def_node_vec_t def_table;
|
||||||
} spl_type_t;
|
} spl_type_t;
|
||||||
|
|
||||||
@@ -127,16 +159,23 @@ void spl_type_drop(spl_type_t *type);
|
|||||||
|
|
||||||
void spl_type_def_dump(spl_type_t *type, spl_def_id_t id);
|
void spl_type_def_dump(spl_type_t *type, spl_def_id_t id);
|
||||||
void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id);
|
void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id);
|
||||||
|
void spl_lang_type_dump(spl_type_t *type, spl_lang_type_id_t id);
|
||||||
|
|
||||||
spl_type_node_t *spl_type_node(const spl_type_t *type, spl_type_id_t id);
|
spl_type_node_t *spl_type_node(const spl_type_t *type, spl_type_id_t id);
|
||||||
|
spl_lang_type_node_t *spl_lang_type_node(const spl_type_t *type, spl_lang_type_id_t id);
|
||||||
spl_def_node_t *spl_def_node(const spl_type_t *type, spl_def_id_t id);
|
spl_def_node_t *spl_def_node(const spl_type_t *type, spl_def_id_t id);
|
||||||
|
|
||||||
/* 判断 sub 是否是 super 的子类型 值域包含关系 */
|
bool spl_lang_type_is_subtype(const spl_type_t *type, spl_lang_type_id_t sub,
|
||||||
|
spl_lang_type_id_t super);
|
||||||
bool spl_type_is_subtype(const spl_type_t *type, spl_type_id_t sub, spl_type_id_t super);
|
bool spl_type_is_subtype(const spl_type_t *type, spl_type_id_t sub, spl_type_id_t super);
|
||||||
|
|
||||||
spl_type_id_t spl_def_alloc(spl_type_t *type, spl_def_node_kind_t kind, const char *name,
|
spl_def_id_t spl_def_alloc(spl_type_t *type, spl_def_node_kind_t kind, const char *name,
|
||||||
spl_dbg_node_t dbg, usize ast_node_ref);
|
spl_dbg_node_t dbg, usize ast_node_ref);
|
||||||
void spl_def_resolve(spl_type_t *type, spl_def_id_t id, spl_type_id_t tid);
|
void spl_def_resolve(spl_type_t *type, spl_def_id_t id, spl_lang_type_id_t lang_id);
|
||||||
|
|
||||||
|
spl_type_id_t spl_lang_type_get_ir_type(spl_type_t *type, spl_lang_type_id_t lang_id);
|
||||||
|
spl_lang_type_id_t spl_def_get_lang_id(spl_type_t *type, spl_def_id_t def_id);
|
||||||
|
spl_type_id_t spl_def_get_type_id(spl_type_t *type, spl_def_id_t def_id);
|
||||||
|
|
||||||
spl_type_id_t spl_type_fn_param_tid(spl_type_t *type, spl_type_id_t fn_tid, usize idx);
|
spl_type_id_t spl_type_fn_param_tid(spl_type_t *type, spl_type_id_t fn_tid, usize idx);
|
||||||
const spl_type_member_t *spl_type_agg_member(spl_type_t *type, spl_type_id_t tid, const char *name);
|
const spl_type_member_t *spl_type_agg_member(spl_type_t *type, spl_type_id_t tid, const char *name);
|
||||||
@@ -154,29 +193,27 @@ spl_type_id_t spl_type_builder_agg(spl_type_t *type, spl_type_node_kind_t tk,
|
|||||||
spl_type_member_vec_t *variants, spl_type_id_t tag_type,
|
spl_type_member_vec_t *variants, spl_type_id_t tag_type,
|
||||||
spl_type_layout_t layout);
|
spl_type_layout_t layout);
|
||||||
|
|
||||||
spl_type_id_t spl_type_make_unit(spl_type_t *type);
|
spl_lang_type_id_t spl_lang_make_error(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_never(spl_type_t *type);
|
spl_lang_type_id_t spl_lang_make_infer(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_any(spl_type_t *type);
|
spl_lang_type_id_t spl_lang_make_never(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_bool(spl_type_t *type);
|
spl_lang_type_id_t spl_lang_make_any(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_int(spl_type_t *type, usize bits, int is_signed);
|
spl_lang_type_id_t spl_lang_make_unit(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_float(spl_type_t *type, usize bits);
|
spl_lang_type_id_t spl_lang_make_bool(spl_type_t *type);
|
||||||
spl_type_id_t spl_type_make_ptr(spl_type_t *type, spl_type_id_t pointee);
|
spl_lang_type_id_t spl_lang_make_int(spl_type_t *type, usize bits, int is_signed);
|
||||||
spl_type_id_t spl_type_make_array(spl_type_t *type, spl_type_id_t elem, usize len);
|
spl_lang_type_id_t spl_lang_make_float(spl_type_t *type, usize bits);
|
||||||
spl_type_id_t spl_type_make_slice(spl_type_t *type, spl_type_id_t elem);
|
spl_lang_type_id_t spl_lang_make_ptr(spl_type_t *type, spl_lang_type_id_t pointee);
|
||||||
spl_type_id_t spl_type_make_tuple(spl_type_t *type, spl_type_member_vec_t *variants);
|
spl_lang_type_id_t spl_lang_make_array(spl_type_t *type, spl_lang_type_id_t elem, usize len);
|
||||||
spl_type_id_t spl_type_make_range(spl_type_t *type, spl_type_id_t elem);
|
spl_lang_type_id_t spl_lang_make_slice(spl_type_t *type, spl_lang_type_id_t elem);
|
||||||
spl_type_id_t spl_type_make_fn(spl_type_t *type, spl_type_id_t ret, const spl_type_id_t *params,
|
spl_lang_type_id_t spl_lang_make_tuple(spl_type_t *type, const spl_lang_type_id_t *elems,
|
||||||
usize nparams);
|
usize count);
|
||||||
spl_type_id_t spl_type_make_agg(spl_type_t *type, spl_type_node_kind_t tk,
|
spl_lang_type_id_t spl_lang_make_range(spl_type_t *type, spl_lang_type_id_t elem);
|
||||||
spl_type_member_vec_t *variants, spl_type_id_t tag_type,
|
spl_lang_type_id_t spl_lang_make_fn(spl_type_t *type, spl_lang_type_id_t ret,
|
||||||
spl_type_layout_t layout);
|
const spl_lang_type_id_t *params, usize nparams);
|
||||||
spl_type_id_t spl_type_make_infer(spl_type_t *type);
|
spl_lang_type_id_t spl_lang_make_aggregate(spl_type_t *type, spl_lang_type_kind_t kind,
|
||||||
spl_type_id_t spl_type_make_slice(spl_type_t *type, spl_type_id_t elem_tid);
|
const char *name, const spl_lang_type_id_t *members,
|
||||||
spl_type_id_t spl_type_make_tuple(spl_type_t *type, spl_type_member_vec_t *variants);
|
usize nmembers);
|
||||||
spl_type_id_t spl_type_make_range(spl_type_t *type, spl_type_id_t elem_tid);
|
spl_lang_type_id_t spl_lang_make_varargs(spl_type_t *type);
|
||||||
|
|
||||||
spl_type_id_t spl_type_lookup_builtin(spl_type_t *type, const char *name);
|
spl_lang_type_id_t spl_type_lookup_builtin(spl_type_t *type, const char *name);
|
||||||
|
|
||||||
spl_type_id_t spl_def_get_type_id(spl_type_t *type, spl_def_id_t def_id);
|
|
||||||
|
|
||||||
#endif /* __SPL_TYPE_H__ */
|
#endif /* __SPL_TYPE_H__ */
|
||||||
Reference in new issue
Block a user