From 7d840c28e282e010c7448609205bd513ef58478a Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 23 Jul 2019 19:34:25 +0200 Subject: [PATCH] :art: define enum in section --- ast | 2 +- include/parse.h | 3 ++- include/traverse.h | 10 +++++----- src/emit/emit.c | 8 ++++---- src/lib/import.c | 11 ++++++----- src/parse/check.c | 6 +++--- src/parse/cpy_ast.c | 14 ++++++++------ src/parse/scan0.c | 26 +++++++++++++------------- src/parse/scan1.c | 18 ++++++++---------- src/parse/scan2.c | 3 ++- src/parse/traverse.c | 10 +++++----- 11 files changed, 57 insertions(+), 54 deletions(-) diff --git a/ast b/ast index 5d8450be..f4fae743 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 5d8450be617d21ac1aca168dd9c5cf267bebd8f1 +Subproject commit f4fae7438c14986d17dd28220b78869021a3e718 diff --git a/include/parse.h b/include/parse.h index d9bf6c54..e8e45124 100644 --- a/include/parse.h +++ b/include/parse.h @@ -37,7 +37,8 @@ static const _exp_func exp_func[] = { #define DECL_SECTION_FUNC(prefix) \ static const _exp_func section_func[] = { \ - (_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def \ + (_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def, \ + (_exp_func)prefix##_enum_def \ }; \ ANN static inline m_bool prefix##_section(const void* a, /* const */ Section* section) { GWDEBUG_EXE \ void* d = §ion->d.stmt_list; \ diff --git a/include/traverse.h b/include/traverse.h index 8faf1020..00fa8a02 100644 --- a/include/traverse.h +++ b/include/traverse.h @@ -4,7 +4,7 @@ ANN m_bool traverse_ast(const Env, const Ast); ANN m_bool traverse_class_def(const Env, const Class_Def); ANN m_bool traverse_func_def(const Env, const Func_Def); ANN m_bool traverse_stmt_union(const Env, const Stmt_Union); -ANN m_bool traverse_stmt_enum(const Env, const Stmt_Enum); +ANN m_bool traverse_enum_def(const Env, const Enum_Def); ANN m_bool traverse_stmt_fptr(const Env, const Stmt_Fptr ); ANN m_bool traverse_stmt_type(const Env env, const Stmt_Type def); ANN m_bool traverse_decl(const Env, const Exp_Decl*); @@ -32,10 +32,10 @@ ANN m_bool scan1_stmt_union(const Env, const Stmt_Union); ANN m_bool scan2_stmt_union(const Env, const Stmt_Union); ANN m_bool check_stmt_union(const Env, const Stmt_Union); -ANN m_bool scan0_stmt_enum(const Env, const Stmt_Enum); -ANN m_bool scan1_stmt_enum(const Env, const Stmt_Enum); -//ANN m_bool scan2_stmt_enum(const Env, const Stmt_Enum); -ANN m_bool check_stmt_enum(const Env, const Stmt_Enum); +ANN m_bool scan0_enum_def(const Env, const Enum_Def); +ANN m_bool scan1_enum_def(const Env, const Enum_Def); +//ANN m_bool scan2_enum_def(const Env, const Enum_Def); +ANN m_bool check_enum_def(const Env, const Enum_Def); ANN m_bool scan0_stmt_type(const Env, const Stmt_Type); ANN m_bool scan1_stmt_type(const Env, const Stmt_Type); diff --git a/src/emit/emit.c b/src/emit/emit.c index 76a30554..c410d5a8 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1342,10 +1342,10 @@ ANN static m_bool emit_stmt_type(const Emitter emit, const Stmt_Type stmt) { return stmt->type->e->def ? emit_class_def(emit, stmt->type->e->def) : 1; } -ANN static m_bool emit_stmt_enum(const Emitter emit, const Stmt_Enum stmt) { +ANN static m_bool emit_enum_def(const Emitter emit, const Enum_Def edef) { LOOP_OPTIM - for(m_uint i = 0; i < vector_size(&stmt->values); ++i) { - const Value v = (Value)vector_at(&stmt->values, i); + for(m_uint i = 0; i < vector_size(&edef->values); ++i) { + const Value v = (Value)vector_at(&edef->values, i); if(!emit->env->class_def) { ALLOC_PTR(emit->gwion->mp, addr, m_uint, i); v->offset = emit_local(emit, SZ_INT, 0); @@ -1429,7 +1429,7 @@ static const _exp_func stmt_func[] = { (_exp_func)emit_stmt_for, (_exp_func)emit_stmt_auto, (_exp_func)emit_stmt_loop, (_exp_func)emit_stmt_if, (_exp_func)emit_stmt_code, (_exp_func)emit_stmt_switch, (_exp_func)emit_stmt_break, (_exp_func)emit_stmt_continue, (_exp_func)emit_stmt_return, - (_exp_func)emit_stmt_case, (_exp_func)emit_stmt_jump, (_exp_func)emit_stmt_enum, + (_exp_func)emit_stmt_case, (_exp_func)emit_stmt_jump, (_exp_func)dummy_func, (_exp_func)emit_stmt_type, (_exp_func)emit_stmt_union, }; diff --git a/src/lib/import.c b/src/lib/import.c index ef280069..80b4bbd9 100644 --- a/src/lib/import.c +++ b/src/lib/import.c @@ -652,14 +652,15 @@ ANN static void import_enum_end(const Gwi gwi, const Vector v) { ANN Type gwi_enum_end(const Gwi gwi) { DL_Enum* d = &gwi->enum_data; - const Stmt stmt = new_stmt_enum(gwi->gwion->mp, d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL); - if(traverse_stmt_enum(gwi->gwion->env, &stmt->d.stmt_enum) < 0) { + const Enum_Def edef = new_enum_def(gwi->gwion->mp, d->base, d->t ? insert_symbol(gwi->gwion->st, d->t) : NULL, + loc_cpy(gwi->gwion->mp, gwi->loc)); + if(traverse_enum_def(gwi->gwion->env, edef) < 0) { free_id_list(gwi->gwion->mp, d->base); return NULL; } - import_enum_end(gwi, &stmt->d.stmt_enum.values); - const Type t = stmt->d.stmt_enum.t; - free_stmt(gwi->gwion->mp, stmt); + import_enum_end(gwi, &edef->values); + const Type t =edef->t; + free_enum_def(gwi->gwion->mp, edef); return t; } diff --git a/src/parse/check.c b/src/parse/check.c index de70be55..f4cb87fb 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -865,9 +865,9 @@ ANN static inline Type check_exp(const Env env, const Exp exp) { return exp->type; } -ANN m_bool check_stmt_enum(const Env env, const Stmt_Enum stmt) { +ANN m_bool check_enum_def(const Env env, const Enum_Def edef) { if(env->class_def) { - ID_List list = stmt->list; + ID_List list = edef->list; do decl_static(env->curr, nspc_lookup_value0(env->curr, list->xid)); while((list = list->next)); } @@ -1087,7 +1087,7 @@ static const _exp_func stmt_func[] = { (_exp_func)check_stmt_for, (_exp_func)check_stmt_auto, (_exp_func)check_stmt_loop, (_exp_func)check_stmt_if, (_exp_func)check_stmt_code, (_exp_func)check_stmt_switch, (_exp_func)check_stmt_break, (_exp_func)check_stmt_continue, (_exp_func)check_stmt_return, - (_exp_func)check_stmt_case, (_exp_func)check_stmt_jump, (_exp_func)check_stmt_enum, + (_exp_func)check_stmt_case, (_exp_func)check_stmt_jump, (_exp_func)dummy_func, (_exp_func)check_stmt_type, (_exp_func)check_stmt_union, }; diff --git a/src/parse/cpy_ast.c b/src/parse/cpy_ast.c index 2f9f3c57..86db4f46 100644 --- a/src/parse/cpy_ast.c +++ b/src/parse/cpy_ast.c @@ -312,10 +312,12 @@ ANN static void cpy_stmt_switch(MemPool p, Stmt_Switch a, const Stmt_Switch src) a->stmt = cpy_stmt(p, src->stmt); } -ANN static void cpy_stmt_enum(MemPool p, Stmt_Enum a, const Stmt_Enum src) { +ANN static Enum_Def cpy_enum_def(MemPool p, const Enum_Def src) { + Enum_Def a = mp_calloc(p, Func_Base); a->list = cpy_id_list(p, src->list); a->xid = src->xid; a->flag = src->flag; + return a; } ANN Func_Base* cpy_func_base(MemPool p, const Func_Base* src) { @@ -393,9 +395,6 @@ ANN static Stmt cpy_stmt(MemPool p, const Stmt src) { case ae_stmt_switch: cpy_stmt_switch(p, &a->d.stmt_switch, &src->d.stmt_switch); break; - case ae_stmt_enum: - cpy_stmt_enum(p, &a->d.stmt_enum, &src->d.stmt_enum); - break; case ae_stmt_fptr: cpy_stmt_fptr(p, &a->d.stmt_fptr, &src->d.stmt_fptr); break; @@ -447,6 +446,9 @@ ANN static Section* cpy_section(MemPool p, const Section *src) { case ae_section_func: a->d.func_def = cpy_func_def(p, src->d.func_def); break; + case ae_section_enum: + a->d.enum_def = cpy_enum_def(p, src->d.enum_def); + break; } a->section_type = src->section_type; return a; @@ -473,7 +475,7 @@ ANN Class_Def cpy_class_def(MemPool p, const Class_Def src) { a->pos = loc_cpy(p, src->pos); return a; } - +/* ANN static Ast cpy_ast(MemPool p, const Ast src) { Ast a = mp_calloc(p, Ast); a->section = cpy_section(p, src->section); @@ -481,4 +483,4 @@ ANN static Ast cpy_ast(MemPool p, const Ast src) { a->next = cpy_ast(p, src->next); return a; } - +*/ diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 59cd50f6..0b053091 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -115,23 +115,23 @@ ANN m_bool scan0_stmt_type(const Env env, const Stmt_Type stmt) { return GW_OK; } -ANN m_bool scan0_stmt_enum(const Env env, const Stmt_Enum stmt) { - CHECK_BB(env_storage(env, stmt->flag, stmt_self(stmt)->pos)) - if(stmt->xid) { - const Value v = nspc_lookup_value1(env->curr, stmt->xid); +ANN m_bool scan0_enum_def(const Env env, const Enum_Def edef) { + CHECK_BB(env_storage(env, edef->flag, edef->pos)) + if(edef->xid) { + const Value v = nspc_lookup_value1(env->curr, edef->xid); if(v) - ERR_B(stmt_self(stmt)->pos, _("'%s' already declared as variable of type '%s'."), - s_name(stmt->xid), v->type->name) - CHECK_BB(scan0_defined(env, stmt->xid, stmt_self(stmt)->pos)) + ERR_B(edef->pos, _("'%s' already declared as variable of type '%s'."), + s_name(edef->xid), v->type->name) + CHECK_BB(scan0_defined(env, edef->xid, edef->pos)) } const Type t = type_copy(env->gwion->mp, t_int); t->xid = ++env->scope->type_xid; - t->name = stmt->xid ? s_name(stmt->xid) : "int"; + t->name = edef->xid ? s_name(edef->xid) : "int"; t->e->parent = t_int; - const Nspc nspc = GET_FLAG(stmt, global) ? env->global_nspc : env->curr; + const Nspc nspc = GET_FLAG(edef, global) ? env->global_nspc : env->curr; t->e->owner = nspc; - stmt->t = t; - if(stmt->xid) { + edef->t = t; + if(edef->xid) { add_type(env, nspc, t); mk_class(env, t); } @@ -221,8 +221,6 @@ ANN static m_bool scan0_stmt(const Env env, const Stmt stmt) { return scan0_stmt_fptr(env, &stmt->d.stmt_fptr); if(stmt->stmt_type == ae_stmt_type) return scan0_stmt_type(env, &stmt->d.stmt_type); - if(stmt->stmt_type == ae_stmt_enum) - return scan0_stmt_enum(env, &stmt->d.stmt_enum); if(stmt->stmt_type == ae_stmt_union) return scan0_stmt_union(env, &stmt->d.stmt_union); if(stmt->stmt_type == ae_stmt_code) @@ -286,6 +284,8 @@ ANN static m_bool scan0_section(const Env env, const Section* section) { return scan0_class_def(env, section->d.class_def); if(section->section_type == ae_section_func) return scan0_func_def(env, section->d.func_def); + if(section->section_type == ae_section_enum) + return scan0_enum_def(env, section->d.enum_def); return GW_OK; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 56cd35d6..76845ac9 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -211,22 +211,20 @@ ANN static inline m_bool scan1_stmt_case(const Env env, const Stmt_Exp stmt) { return scan1_exp(env, stmt->val); } -ANN m_bool scan1_stmt_enum(const Env env, const Stmt_Enum stmt) { - if(!stmt->t) - CHECK_BB(scan0_stmt_enum(env, stmt)) - ID_List list = stmt->list; +ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { + ID_List list = edef->list; do { - CHECK_BB(already_defined(env, list->xid, stmt_self(stmt)->pos)) - const Value v = new_value(env->gwion->mp, stmt->t, s_name(list->xid)); + CHECK_BB(already_defined(env, list->xid, edef->pos)) + const Value v = new_value(env->gwion->mp, edef->t, s_name(list->xid)); if(env->class_def) { v->owner_class = env->class_def; SET_FLAG(v, static); - SET_ACCESS(stmt, v) + SET_ACCESS(edef, v) } v->owner = env->curr; SET_FLAG(v, const | ae_flag_enum | ae_flag_checked); - nspc_add_value(stmt->t->e->owner, list->xid, v); - vector_add(&stmt->values, (vtype)v); + nspc_add_value(edef->t->e->owner, list->xid, v); + vector_add(&edef->values, (vtype)v); } while((list = list->next)); return GW_OK; } @@ -291,7 +289,7 @@ static const _exp_func stmt_func[] = { (_exp_func)scan1_stmt_for, (_exp_func)scan1_stmt_auto, (_exp_func)scan1_stmt_loop, (_exp_func)scan1_stmt_if, (_exp_func)scan1_stmt_code, (_exp_func)scan1_stmt_switch, (_exp_func)dummy_func, (_exp_func)dummy_func, (_exp_func)scan1_stmt_exp, - (_exp_func)scan1_stmt_case, (_exp_func)dummy_func, (_exp_func)scan1_stmt_enum, + (_exp_func)scan1_stmt_case, (_exp_func)dummy_func, (_exp_func)scan1_stmt_fptr, (_exp_func)scan1_stmt_type, (_exp_func)scan1_stmt_union, }; diff --git a/src/parse/scan2.c b/src/parse/scan2.c index f0ec7dd6..7993c559 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -286,7 +286,7 @@ static const _exp_func stmt_func[] = { (_exp_func)scan2_stmt_for, (_exp_func)scan2_stmt_auto, (_exp_func)scan2_stmt_loop, (_exp_func)scan2_stmt_if, (_exp_func)scan2_stmt_code, (_exp_func)scan2_stmt_switch, (_exp_func)dummy_func, (_exp_func)dummy_func, (_exp_func)scan2_stmt_exp, - (_exp_func)scan2_stmt_case, (_exp_func)scan2_stmt_jump, (_exp_func)dummy_func, + (_exp_func)scan2_stmt_case, (_exp_func)scan2_stmt_jump, (_exp_func)scan2_stmt_fptr, (_exp_func)scan2_stmt_type, (_exp_func)scan2_stmt_union, }; @@ -539,6 +539,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { return ret; } +#define scan2_enum_def dummy_func DECL_SECTION_FUNC(scan2) ANN static m_bool scan2_class_parent(const Env env, const Class_Def cdef) { diff --git a/src/parse/traverse.c b/src/parse/traverse.c index 00ee4ce5..4fda7c7e 100644 --- a/src/parse/traverse.c +++ b/src/parse/traverse.c @@ -32,11 +32,11 @@ ANN m_bool traverse_stmt_union(const Env env, const Stmt_Union def) { return check_stmt_union(env, def); } -ANN m_bool traverse_stmt_enum(const Env env, const Stmt_Enum def) { - CHECK_BB(scan0_stmt_enum(env, def)) - CHECK_BB(scan1_stmt_enum(env, def)) -// CHECK_BBscan2_stmt_enum(env, def)) - return check_stmt_enum(env, def); +ANN m_bool traverse_enum_def(const Env env, const Enum_Def def) { + CHECK_BB(scan0_enum_def(env, def)) + CHECK_BB(scan1_enum_def(env, def)) +// CHECK_BBscan2_enum_def(env, def)) + return check_enum_def(env, def); } ANN m_bool traverse_stmt_fptr(const Env env, const Stmt_Fptr def) { -- 2.43.0