stage1 基于新的ast实现部分sema
This commit is contained in:
1 parent
98b35a98e3
commit
8aa7a89c7e
5 files changed
+1277
-1022
No files matched your search
+9
-6
@@ -1310,7 +1310,8 @@ static spl_ast_node_ref_t parse_postfix(parser_t *p) {
|
||||
}
|
||||
} else if (t->type == TOK_L_BRACKET) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t begin = parse_expr(p);
|
||||
int range_prec = binary_op_info[SPL_OP_RANGE].prec;
|
||||
spl_ast_node_ref_t begin = parse_expr_prec(p, range_prec + 1);
|
||||
if (peek(p) && peek(p)->type == TOK_RANGE) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t end = 0;
|
||||
@@ -1432,15 +1433,17 @@ static spl_ast_node_ref_t parse_primary(parser_t *p) {
|
||||
} else if (nx->type == TOK_L_BRACKET) {
|
||||
// 匿名数组字面量:. [ elems ]
|
||||
advance(p); // 消费 '['
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_ANON_ARRAY_LIT, t);
|
||||
spl_ast_node_t *arr = node_at(p->ast, ref);
|
||||
arr->anon_array_literal.elems = parse_expr_list(p);
|
||||
// 先解析元素列表,避免节点扩容导致 arr 指针失效
|
||||
spl_ast_node_ref_vec_t elems = parse_expr_list(p);
|
||||
const spl_tok_t *close = peek(p);
|
||||
if (!close || close->type != TOK_R_BRACKET) {
|
||||
SPL_FATAL(close, "expected ']'");
|
||||
return ref;
|
||||
return 0;
|
||||
}
|
||||
advance(p);
|
||||
advance(p); // 消费 ']'
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_ANON_ARRAY_LIT, t);
|
||||
spl_ast_node_t *arr = node_at(p->ast, ref);
|
||||
arr->anon_array_literal.elems = elems;
|
||||
return ref;
|
||||
} else {
|
||||
SPL_FATAL(nx, "expected '{' or '[' after '.'");
|
||||
|
||||
+997
-929
File diff suppressed because it is too large.
Load diff
+208
-56
@@ -1,11 +1,17 @@
|
||||
#include "spl_type.h"
|
||||
|
||||
static usize spl_type_hash(spl_type_node_t n) {
|
||||
usize hash = n.kind << 20;
|
||||
usize hash = n.kind << 20; // FIXME least i32
|
||||
switch (n.kind) {
|
||||
case SPL_TYPE_ERROR:
|
||||
case SPL_TYPE_VOID:
|
||||
case SPL_TYPE_UNIT:
|
||||
case SPL_TYPE_ANY:
|
||||
case SPL_TYPE_NEVER:
|
||||
case SPL_TYPE_INFER:
|
||||
case SPL_TYPE_FORALL:
|
||||
case SPL_TYPE_EXISTS:
|
||||
case SPL_TYPE_BOOL:
|
||||
break;
|
||||
case SPL_TYPE_INT:
|
||||
hash += n.int_type.bits + n.int_type.is_signed;
|
||||
break;
|
||||
@@ -44,8 +50,14 @@ static int spl_type_eq(spl_type_node_t n1, spl_type_node_t n2) {
|
||||
return 1;
|
||||
switch (n1.kind) {
|
||||
case SPL_TYPE_ERROR:
|
||||
case SPL_TYPE_VOID:
|
||||
case SPL_TYPE_UNIT:
|
||||
case SPL_TYPE_ANY:
|
||||
case SPL_TYPE_NEVER:
|
||||
case SPL_TYPE_INFER:
|
||||
case SPL_TYPE_FORALL:
|
||||
case SPL_TYPE_EXISTS:
|
||||
case SPL_TYPE_BOOL:
|
||||
break;
|
||||
case SPL_TYPE_INT:
|
||||
if (n1.int_type.bits != n2.int_type.bits)
|
||||
return 1;
|
||||
@@ -138,6 +150,45 @@ spl_type_id_t spl_type_node_push(spl_type_t *type, spl_type_node_t *tn) {
|
||||
return id;
|
||||
}
|
||||
|
||||
bool spl_type_is_subtype(const spl_type_t *type, spl_type_id_t sub, spl_type_id_t super) {
|
||||
if (sub == 0 || super == 0)
|
||||
return false;
|
||||
if (sub == super)
|
||||
return true;
|
||||
|
||||
spl_type_node_t *nsub = spl_type_node(type, sub);
|
||||
spl_type_node_t *nsuper = spl_type_node(type, super);
|
||||
if (!nsub || !nsuper)
|
||||
return false;
|
||||
|
||||
// never 是任何类型的子类型 bottom
|
||||
if (nsub->kind == SPL_TYPE_NEVER)
|
||||
return true;
|
||||
// 任何类型是 any 的子类型 top
|
||||
if (nsuper->kind == SPL_TYPE_ANY)
|
||||
return true;
|
||||
|
||||
// 处理 comptime_int / comptime_float 到具体数值类型
|
||||
if (nsub->kind == SPL_TYPE_INT && nsuper->kind == SPL_TYPE_INT) {
|
||||
if (nsub->int_type.is_signed != nsuper->int_type.is_signed)
|
||||
return false;
|
||||
// comptime_int bits==0 可适配任何整数
|
||||
if (nsub->int_type.bits == 0)
|
||||
return true;
|
||||
return nsub->int_type.bits <= nsuper->int_type.bits;
|
||||
}
|
||||
|
||||
if (nsub->kind == SPL_TYPE_FLOAT && nsuper->kind == SPL_TYPE_FLOAT) {
|
||||
// comptime_float bits==0 可适配任何浮点
|
||||
if (nsub->float_type.bits == 0)
|
||||
return true;
|
||||
return nsub->float_type.bits <= nsuper->float_type.bits;
|
||||
}
|
||||
|
||||
// 其他类型 只有结构等价 相等 才视为子类型 已在前面处理
|
||||
return false;
|
||||
}
|
||||
|
||||
static const spl_type_member_t *spl_type_agg_member_find(spl_type_t *type, spl_type_id_t tid,
|
||||
const char *name, usize *out_idx) {
|
||||
spl_type_node_t *n = spl_type_node(type, tid);
|
||||
@@ -172,12 +223,14 @@ bool spl_type_agg_member_idx(spl_type_t *type, spl_type_id_t tid, const char *na
|
||||
spl_type_id_t spl_type_builder_simple(spl_type_t *type, spl_type_node_kind_t kind) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = kind;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return spl_type_node_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_builder_int(spl_type_t *type, usize bits, int is_signed) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = SPL_TYPE_INT;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
tn.int_type.bits = bits;
|
||||
tn.int_type.is_signed = is_signed;
|
||||
return spl_type_node_push(type, &tn);
|
||||
@@ -186,6 +239,7 @@ spl_type_id_t spl_type_builder_int(spl_type_t *type, usize bits, int is_signed)
|
||||
spl_type_id_t spl_type_builder_float(spl_type_t *type, usize bits) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = SPL_TYPE_FLOAT;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
tn.float_type.bits = bits;
|
||||
return spl_type_node_push(type, &tn);
|
||||
}
|
||||
@@ -193,6 +247,7 @@ spl_type_id_t spl_type_builder_float(spl_type_t *type, usize bits) {
|
||||
spl_type_id_t spl_type_builder_ptr(spl_type_t *type, spl_type_id_t pointee) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = SPL_TYPE_PTR;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
tn.ptr_type.pointee = pointee;
|
||||
return spl_type_node_push(type, &tn);
|
||||
}
|
||||
@@ -200,6 +255,7 @@ spl_type_id_t spl_type_builder_ptr(spl_type_t *type, spl_type_id_t pointee) {
|
||||
spl_type_id_t spl_type_builder_array(spl_type_t *type, spl_type_id_t element, usize len) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = SPL_TYPE_ARRAY;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
tn.array_type.element = element;
|
||||
tn.array_type.len = len;
|
||||
return spl_type_node_push(type, &tn);
|
||||
@@ -209,6 +265,7 @@ spl_type_id_t spl_type_builder_fn(spl_type_t *type, spl_type_id_t ret, const spl
|
||||
usize nparams) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = SPL_TYPE_FN;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
vec_init(tn.fn_type.params);
|
||||
for (usize i = 0; i < nparams; i++) {
|
||||
vec_push(tn.fn_type.params, params[i]);
|
||||
@@ -222,6 +279,7 @@ spl_type_id_t spl_type_builder_agg(spl_type_t *type, spl_type_node_kind_t tk,
|
||||
spl_type_layout_t layout) {
|
||||
spl_type_node_t tn = {0};
|
||||
tn.kind = tk;
|
||||
tn.lang_kind = SPL_LANG_TYPE_NONE;
|
||||
tn.layout = layout;
|
||||
tn.agg_type.variants = *variants;
|
||||
if (tk == SPL_TYPE_ENUM) {
|
||||
@@ -233,6 +291,134 @@ spl_type_id_t spl_type_builder_agg(spl_type_t *type, spl_type_node_kind_t tk,
|
||||
return spl_type_node_push(type, &tn);
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_unit(spl_type_t *type) {
|
||||
spl_type_id_t id = spl_type_builder_simple(type, SPL_TYPE_UNIT);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_bool(spl_type_t *type) {
|
||||
spl_type_id_t id = spl_type_builder_simple(type, SPL_TYPE_BOOL);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_any(spl_type_t *type) {
|
||||
spl_type_id_t id = spl_type_builder_simple(type, SPL_TYPE_ANY);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_never(spl_type_t *type) {
|
||||
spl_type_id_t id = spl_type_builder_simple(type, SPL_TYPE_NEVER);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_int(spl_type_t *type, usize bits, int is_signed) {
|
||||
spl_type_id_t id = spl_type_builder_int(type, bits, is_signed);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_float(spl_type_t *type, usize bits) {
|
||||
spl_type_id_t id = spl_type_builder_float(type, bits);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_ptr(spl_type_t *type, spl_type_id_t pointee) {
|
||||
spl_type_id_t id = spl_type_builder_ptr(type, pointee);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_array(spl_type_t *type, spl_type_id_t elem, usize len) {
|
||||
spl_type_id_t id = spl_type_builder_array(type, elem, len);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_fn(spl_type_t *type, spl_type_id_t ret, const spl_type_id_t *params,
|
||||
usize nparams) {
|
||||
spl_type_id_t id = spl_type_builder_fn(type, ret, params, nparams);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_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_layout_t layout) {
|
||||
spl_type_id_t id = spl_type_builder_agg(type, tk, variants, tag_type, layout);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_NONE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_slice(spl_type_t *type, spl_type_id_t elem) {
|
||||
// 底层构造 struct {ptr, len}
|
||||
spl_type_member_vec_t members;
|
||||
vec_init(members);
|
||||
|
||||
spl_type_id_t ptr_tid = spl_type_make_ptr(type, elem);
|
||||
spl_type_member_t member_ptr = {.name = "ptr", .type_id = ptr_tid};
|
||||
vec_push(members, member_ptr);
|
||||
|
||||
spl_type_id_t usize_tid = spl_type_make_int(type, sizeof(void *) * 8, false);
|
||||
spl_type_member_t member_len = {.name = "len", .type_id = usize_tid};
|
||||
vec_push(members, member_len);
|
||||
|
||||
spl_type_id_t id =
|
||||
spl_type_make_agg(type, SPL_TYPE_STRUCT, &members, 0, (spl_type_layout_t){0});
|
||||
vec_free(members); // make_agg 已拷贝成员,原向量可释放
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_SLICE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_tuple(spl_type_t *type, spl_type_member_vec_t *variants) {
|
||||
spl_type_id_t id =
|
||||
spl_type_make_agg(type, SPL_TYPE_STRUCT, variants, 0, (spl_type_layout_t){0});
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_TUPLE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_range(spl_type_t *type, spl_type_id_t elem) {
|
||||
spl_type_member_vec_t members;
|
||||
vec_init(members);
|
||||
|
||||
spl_type_member_t member_start = {.name = "start", .type_id = elem};
|
||||
vec_push(members, member_start);
|
||||
|
||||
spl_type_member_t member_end = {.name = "end", .type_id = elem};
|
||||
vec_push(members, member_end);
|
||||
|
||||
spl_type_id_t id =
|
||||
spl_type_make_agg(type, SPL_TYPE_STRUCT, &members, 0, (spl_type_layout_t){0});
|
||||
vec_free(members);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_RANGE;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_make_infer(spl_type_t *type) {
|
||||
// 使用底层 ERROR 类型作为占位,实际由 lang_kind 标识
|
||||
spl_type_id_t id = spl_type_builder_simple(type, SPL_TYPE_ERROR);
|
||||
if (id)
|
||||
spl_type_node(type, id)->lang_kind = SPL_LANG_TYPE_INFER;
|
||||
return id;
|
||||
}
|
||||
|
||||
spl_type_id_t spl_type_lookup_builtin(spl_type_t *type, const char *name) {
|
||||
if (!name || !*name)
|
||||
return 0;
|
||||
@@ -241,7 +427,7 @@ spl_type_id_t spl_type_lookup_builtin(spl_type_t *type, const char *name) {
|
||||
if (strcmp(name, "bool") == 0)
|
||||
return spl_type_builder_simple(type, SPL_TYPE_BOOL);
|
||||
if (strcmp(name, "void") == 0)
|
||||
return spl_type_builder_simple(type, SPL_TYPE_VOID);
|
||||
return spl_type_builder_simple(type, SPL_TYPE_UNIT);
|
||||
if (strcmp(name, "isize") == 0)
|
||||
return spl_type_builder_int(type, sizeof(void *) * 8, true);
|
||||
if (strcmp(name, "usize") == 0)
|
||||
@@ -273,43 +459,6 @@ spl_type_id_t spl_def_get_type_id(spl_type_t *type, spl_def_id_t def_id) {
|
||||
return def_node->type_id;
|
||||
}
|
||||
|
||||
spl_def_id_t spl_def_builder_void(spl_type_t *type) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_bool(spl_type_t *type) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_int(spl_type_t *type, usize bits, int is_signed) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_float(spl_type_t *type, usize bits) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_ptr(spl_type_t *type, spl_def_id_t pointee) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_array(spl_type_t *type, spl_def_id_t element, usize len) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_slice(spl_type_t *type, spl_type_id_t element) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_range(spl_type_t *type) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
spl_def_id_t spl_def_builder_fn(spl_type_t *type) {
|
||||
TODO();
|
||||
return 0;
|
||||
}
|
||||
|
||||
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_def_node_t n = {0};
|
||||
@@ -342,19 +491,6 @@ void spl_type_init(spl_type_t *type) {
|
||||
map_init(type->type_map, spl_type_map_hash, spl_type_map_cmp);
|
||||
vec_init(type->def_table);
|
||||
vec_push(type->def_table, (spl_def_node_t){0});
|
||||
|
||||
// spl_type_builder_simple(type, SPL_TYPE_VOID);
|
||||
// spl_type_builder_simple(type, SPL_TYPE_BOOL);
|
||||
// spl_type_builder_int(type, 8, false);
|
||||
// spl_type_builder_int(type, 16, false);
|
||||
// spl_type_builder_int(type, 32, false);
|
||||
// spl_type_builder_int(type, 64, false);
|
||||
// spl_type_builder_int(type, 8, true);
|
||||
// spl_type_builder_int(type, 16, true);
|
||||
// spl_type_builder_int(type, 32, true);
|
||||
// spl_type_builder_int(type, 64, true);
|
||||
// spl_type_builder_float(type, 32);
|
||||
// spl_type_builder_float(type, 64);
|
||||
}
|
||||
|
||||
void spl_type_drop(spl_type_t *type) {
|
||||
@@ -402,8 +538,21 @@ void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
|
||||
case SPL_TYPE_ERROR:
|
||||
printf("error");
|
||||
break;
|
||||
case SPL_TYPE_VOID:
|
||||
printf("void");
|
||||
case SPL_TYPE_UNIT:
|
||||
printf("unit");
|
||||
break;
|
||||
case SPL_TYPE_ANY:
|
||||
printf("any");
|
||||
break;
|
||||
case SPL_TYPE_NEVER:
|
||||
printf("never");
|
||||
break;
|
||||
case SPL_TYPE_INFER:
|
||||
printf("infer");
|
||||
break;
|
||||
case SPL_TYPE_FORALL:
|
||||
case SPL_TYPE_EXISTS:
|
||||
TODO();
|
||||
break;
|
||||
case SPL_TYPE_BOOL:
|
||||
printf("bool");
|
||||
@@ -437,10 +586,13 @@ void spl_type_pure_dump(spl_type_t *type, spl_type_id_t id) {
|
||||
switch (n->kind) {
|
||||
case SPL_TYPE_STRUCT:
|
||||
printf("struct{\n");
|
||||
break;
|
||||
case SPL_TYPE_UNION:
|
||||
printf("union{\n");
|
||||
break;
|
||||
case SPL_TYPE_ENUM:
|
||||
printf("enum{\n");
|
||||
break;
|
||||
case SPL_TYPE_SHAPE:
|
||||
printf("shape{\n");
|
||||
break;
|
||||
|
||||
+44
-12
@@ -11,7 +11,10 @@ typedef VEC(spl_def_id_t) spl_def_id_vec_t;
|
||||
|
||||
typedef enum {
|
||||
SPL_TYPE_ERROR,
|
||||
SPL_TYPE_VOID,
|
||||
SPL_TYPE_NEVER, // bottom type
|
||||
SPL_TYPE_ANY, // top type
|
||||
SPL_TYPE_INFER, // auto type
|
||||
SPL_TYPE_UNIT,
|
||||
SPL_TYPE_BOOL,
|
||||
SPL_TYPE_INT,
|
||||
SPL_TYPE_FLOAT,
|
||||
@@ -20,10 +23,20 @@ typedef enum {
|
||||
SPL_TYPE_STRUCT,
|
||||
SPL_TYPE_UNION,
|
||||
SPL_TYPE_ENUM,
|
||||
SPL_TYPE_SHAPE,
|
||||
SPL_TYPE_FN,
|
||||
SPL_TYPE_FORALL, // TODO delete
|
||||
SPL_TYPE_EXISTS, // TODO delete
|
||||
SPL_TYPE_SHAPE,
|
||||
} 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 {
|
||||
SPL_TYPE_LAYOUT_AUTO,
|
||||
SPL_TYPE_LAYOUT_EXTERN_C,
|
||||
@@ -44,6 +57,7 @@ typedef VEC(spl_type_member_t) spl_type_member_vec_t;
|
||||
|
||||
typedef struct {
|
||||
spl_type_node_kind_t kind;
|
||||
spl_lang_type_node_kind_t lang_kind;
|
||||
spl_type_layout_t layout; // attribute
|
||||
union {
|
||||
struct {
|
||||
@@ -63,11 +77,14 @@ typedef struct {
|
||||
struct {
|
||||
spl_type_member_vec_t variants;
|
||||
spl_type_id_t tag_type;
|
||||
} agg_type; // adt record aggregate
|
||||
} agg_type; // adt record
|
||||
struct {
|
||||
spl_type_id_vec_t params;
|
||||
spl_type_id_t ret;
|
||||
} fn_type;
|
||||
struct {
|
||||
// TODO
|
||||
} shape_type;
|
||||
};
|
||||
} spl_type_node_t;
|
||||
typedef VEC(spl_type_node_t) spl_type_node_vec_t;
|
||||
@@ -114,6 +131,9 @@ void spl_type_pure_dump(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_def_node_t *spl_def_node(const spl_type_t *type, spl_def_id_t id);
|
||||
|
||||
/* 判断 sub 是否是 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_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);
|
||||
@@ -134,17 +154,29 @@ 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_layout_t layout);
|
||||
|
||||
spl_type_id_t spl_type_make_unit(spl_type_t *type);
|
||||
spl_type_id_t spl_type_make_never(spl_type_t *type);
|
||||
spl_type_id_t spl_type_make_any(spl_type_t *type);
|
||||
spl_type_id_t spl_type_make_bool(spl_type_t *type);
|
||||
spl_type_id_t spl_type_make_int(spl_type_t *type, usize bits, int is_signed);
|
||||
spl_type_id_t spl_type_make_float(spl_type_t *type, usize bits);
|
||||
spl_type_id_t spl_type_make_ptr(spl_type_t *type, spl_type_id_t pointee);
|
||||
spl_type_id_t spl_type_make_array(spl_type_t *type, spl_type_id_t elem, usize len);
|
||||
spl_type_id_t spl_type_make_slice(spl_type_t *type, spl_type_id_t elem);
|
||||
spl_type_id_t spl_type_make_tuple(spl_type_t *type, spl_type_member_vec_t *variants);
|
||||
spl_type_id_t spl_type_make_range(spl_type_t *type, spl_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,
|
||||
usize nparams);
|
||||
spl_type_id_t spl_type_make_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_layout_t layout);
|
||||
spl_type_id_t spl_type_make_infer(spl_type_t *type);
|
||||
spl_type_id_t spl_type_make_slice(spl_type_t *type, spl_type_id_t elem_tid);
|
||||
spl_type_id_t spl_type_make_tuple(spl_type_t *type, spl_type_member_vec_t *variants);
|
||||
spl_type_id_t spl_type_make_range(spl_type_t *type, spl_type_id_t elem_tid);
|
||||
|
||||
spl_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);
|
||||
spl_def_id_t spl_def_builder_void(spl_type_t *type);
|
||||
spl_def_id_t spl_def_builder_bool(spl_type_t *type);
|
||||
spl_def_id_t spl_def_builder_int(spl_type_t *type, usize bits, int is_signed);
|
||||
spl_def_id_t spl_def_builder_float(spl_type_t *type, usize bits);
|
||||
spl_def_id_t spl_def_builder_ptr(spl_type_t *type, spl_def_id_t pointee);
|
||||
spl_def_id_t spl_def_builder_array(spl_type_t *type, spl_def_id_t element, usize len);
|
||||
spl_def_id_t spl_def_builder_slice(spl_type_t *type, spl_type_id_t element);
|
||||
spl_def_id_t spl_def_builder_range(spl_type_t *type);
|
||||
spl_def_id_t spl_def_builder_fn(spl_type_t *type);
|
||||
|
||||
#endif /* __SPL_TYPE_H__ */
|
||||
+19
-19
@@ -91,25 +91,25 @@ static int compile_spl(const char *src, const char *fname, const char *outpath,
|
||||
// memory leak
|
||||
return 0;
|
||||
}
|
||||
// spl_sema_t sema;
|
||||
// spl_type_t type;
|
||||
// spl_scope_t scope;
|
||||
// spl_type_init(&type);
|
||||
// spl_scope_init(&scope);
|
||||
// spl_sema_init(&sema, &ast, &type, &scope);
|
||||
// sema.ast = *
|
||||
// spl_sema_run(&sema);
|
||||
// if (sema.error_count) {
|
||||
// printf("sema errors=%d, no output\n", sema.error_count);
|
||||
// // memory leak
|
||||
// return 1;
|
||||
// }
|
||||
// if (dump == DUMP_SEMA) {
|
||||
// spl_sema_dump(&sema);
|
||||
// spl_ast_dump(&ast, ast.root);
|
||||
// // memory leak
|
||||
// return 0;
|
||||
// }
|
||||
spl_sema_t sema;
|
||||
spl_type_t type;
|
||||
spl_scope_t scope;
|
||||
spl_type_init(&type);
|
||||
spl_scope_init(&scope);
|
||||
spl_sema_init(&sema, &ast, &type, &scope);
|
||||
sema.ast = *
|
||||
spl_sema_run(&sema);
|
||||
if (sema.error_count) {
|
||||
printf("sema errors=%d, no output\n", sema.error_count);
|
||||
// memory leak
|
||||
return 1;
|
||||
}
|
||||
if (dump == DUMP_SEMA) {
|
||||
spl_sema_dump(&sema);
|
||||
spl_ast_dump(&ast, ast.root);
|
||||
// memory leak
|
||||
return 0;
|
||||
}
|
||||
|
||||
// spl_ast2ir_t a2ir;
|
||||
// spl_ir_builder_t ir_builder;
|
||||
|
||||
Reference in new issue
Block a user