From abc62957c7a918b4bc1c533b59a9e7e860c5bcd2 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 29 May 2023 15:59:09 +0200 Subject: [PATCH] :art: enums assign at def --- ast | 2 +- fmt | 2 +- include/clean.h | 2 +- src/clean.c | 6 +---- src/emit/emit.c | 6 ----- src/env/nspc.c | 13 ++++------ src/import/import_enum.c | 53 ++++++---------------------------------- src/parse/scan0.c | 1 - src/parse/scan1.c | 21 ++++++++-------- src/parse/traverse.c | 3 ++- 10 files changed, 28 insertions(+), 81 deletions(-) diff --git a/ast b/ast index 1ae66ea3..f80e75e3 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 1ae66ea3a07f6282ce9c5bf60956c731addbecaa +Subproject commit f80e75e38b2616b68e2cc764d598a8e74123a1d7 diff --git a/fmt b/fmt index 73f7b396..c980939e 160000 --- a/fmt +++ b/fmt @@ -1 +1 @@ -Subproject commit 73f7b396500a94347857b600bf847d3ab6531767 +Subproject commit c980939e8fe7bb574b2a555dde1bbffc216d012f diff --git a/include/clean.h b/include/clean.h index 0c584f63..2769eb76 100644 --- a/include/clean.h +++ b/include/clean.h @@ -45,7 +45,7 @@ ANN static void clean_stmt_list(Clean *a, Stmt_List b); ANN static void clean_func_base(Clean *a, Func_Base *b); ANN static void clean_func_def(Clean *a, Func_Def b); ANN static void clean_class_def(Clean *a, Class_Def b); -ANN static void clean_enum_def(Clean *a, Enum_Def b); +//ANN static void clean_enum_def(Clean *a, Enum_Def b); ANN static void clean_union_def(Clean *a, Union_Def b); ANN static void clean_fptr_def(Clean *a, Fptr_Def b); ANN static void clean_type_def(Clean *a, Type_Def b); diff --git a/src/clean.c b/src/clean.c index 69aea438..d5308ab1 100644 --- a/src/clean.c +++ b/src/clean.c @@ -303,11 +303,7 @@ ANN void class_def_cleaner(const Gwion gwion, Class_Def b) { free_class_def(gwion->mp, b); } -ANN static void clean_enum_def(Clean *a NUSED, Enum_Def b) { - clean_id_list(a, b->list); - if (b->values.ptr) vector_release(&b->values); -} - +#define clean_enum_def clean_dummy ANN static void clean_union_list(Clean *a, Union_List b) { for(uint32_t i = 0; i < b->len; i++) { Union_Member *tgt = mp_vector_at(b, Union_Member, i); diff --git a/src/emit/emit.c b/src/emit/emit.c index 46a16e2b..9eb533a3 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -2368,12 +2368,6 @@ ANN static m_bool emit_type_def(const Emitter emit, const Type_Def tdef) { } ANN static m_bool emit_enum_def(const Emitter emit NUSED, const Enum_Def edef) { - LOOP_OPTIM - for (m_uint i = 0; i < vector_size(&edef->values); ++i) { - const Value v = (Value)vector_at(&edef->values, i); - set_vflag(v, vflag_builtin); - v->d.num = i; - } set_tflag(edef->type, tflag_emit); return GW_OK; } diff --git a/src/env/nspc.c b/src/env/nspc.c index 2480815e..f41291f4 100644 --- a/src/env/nspc.c +++ b/src/env/nspc.c @@ -15,14 +15,11 @@ ANN void nspc_commit(const Nspc nspc) { static inline void _free_nspc_value(const Nspc a, const Value v, Gwion gwion) { if(v->from->ctx && v->from->ctx->error) return; // this is quite a hack - if (GET_FLAG(v, static) && a->class_data) { - const m_bit *ptr = a->class_data + v->from->offset; - anytype_release(gwion->vm->cleaner_shred, v->type, ptr); - } else if (vflag(v, vflag_builtin) && v->d.ptr) { - const m_bit *ptr = (m_bit*)v->d.ptr; - //if(vflag(v, vflag_direct)) - anytype_release(gwion->vm->cleaner_shred, v->type, ptr); - } + const bool is_static = GET_FLAG(v, static) && a->class_data; + const m_bit *ptr = is_static + ? a->class_data + v->from->offset + : (m_bit*)v->d.ptr; + anytype_release(gwion->vm->cleaner_shred, v->type, ptr); value_remref(v, gwion); } diff --git a/src/import/import_enum.c b/src/import/import_enum.c index 4461c887..f7e05469 100644 --- a/src/import/import_enum.c +++ b/src/import/import_enum.c @@ -18,78 +18,39 @@ //! \arg the importer //! \arg string defining a primitive type //! why is return type m_int ? - -__attribute__ ((visibility ("default"))) ANN m_int gwi_enum_ini(const Gwi gwi, const m_str type) { CHECK_BB(ck_ini(gwi, ck_edef)); CHECK_OB((gwi->ck->xid = gwi_str2sym(gwi, type))); - vector_init(&gwi->ck->v); - gwi->ck->tmpl = new_mp_vector(gwi->gwion->mp, Symbol, 0); + gwi->ck->tmpl = new_mp_vector(gwi->gwion->mp, EnumValue, 0); return GW_OK; } -/* -// adds the id_list to the enum -// change that algo? -ANN static void add2list(struct ImportCK *ck, const ID_List list) { - if (!ck->tmpl) { - ck->tmpl = new_mp_vector(list, ; - } else { - ck->curr->next = list; - } - ck->curr = list; -} -*/ + //! add an enum entry //! \arg the importer //! \arg name of the entry //! TODO: change return type to m_bool ANN m_int gwi_enum_add(const Gwi gwi, const m_str name, const m_uint i) { CHECK_BB(ck_ok(gwi, ck_edef)); -// DECL_OB(const ID_List, list, = gwi_str2symlist(gwi, name)); - DECL_OB(const Symbol, xid, = gwi_str2sym(gwi, name)); - mp_vector_add(gwi->gwion->mp, &gwi->ck->tmpl, Symbol, xid); -// add2list(gwi->ck, list); - vector_add(&gwi->ck->v, (vtype)i); + const EnumValue ev = { .xid = xid, .num = i, .set = true}; + mp_vector_add(gwi->gwion->mp, &gwi->ck->tmpl, EnumValue, ev); return GW_OK; } -//! set enum values -//! \arg the importer -//! \arg a vector of values -//! \note [internal] -ANN static void import_enum_end(const Gwi gwi, const Vector v) { - ImportCK *ck = gwi->ck; - for (m_uint i = 0; i < vector_size(v); i++) { - const Value value = (Value)vector_at(v, i); - const m_uint addr = vector_at(&ck->v, i); - set_vflag(value, vflag_builtin); - value->d.num = addr ?: i; - } - // better clean ? -} - //! finish enum import //! \arg the importer -//! TODO: check what happens in inside template class ANN Type gwi_enum_end(const Gwi gwi) { CHECK_BO(ck_ok(gwi, ck_edef)); - if (!vector_size(&gwi->ck->v)) GWI_ERR_O("Enum is empty"); + if (!gwi->ck->tmpl->len) GWI_ERR_O("Enum is empty"); const Gwion gwion = gwi->gwion; const Enum_Def edef = new_enum_def(gwion->mp, gwi->ck->tmpl, gwi->ck->xid, gwi->loc); +// clean the vector gwi->ck->tmpl = NULL; const m_bool ret = traverse_enum_def(gwion->env, edef); - if (ret > 0) import_enum_end(gwi, &edef->values); - if (gwi->gwion->data->cdoc) { - gwfmt_indent(gwi->gwfmt); - gwfmt_enum_def(gwi->gwfmt, edef); - } + if (gwi->gwion->data->cdoc) gwfmt_enum_def(gwi->gwfmt, edef); const Type t = ret > 0 ? edef->type : NULL; - if (edef->values.ptr) vector_release(&edef->values); free_enum_def(gwion->mp, edef); - vector_release(&gwi->ck->v); - gwi->ck->v.ptr = NULL; ck_end(gwi); return t; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 83057805..8d3b37bf 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -203,7 +203,6 @@ ANN m_bool scan0_enum_def(const Env env, const Enum_Def edef) { CHECK_BB(scan0_defined(env, edef->xid, edef->pos)); DECL_BB(const m_bool, global, = scan0_global(env, edef->flag, edef->pos)); edef->type = enum_type(env, edef); - vector_init(&edef->values); if (global) env_pop(env, 0); return GW_OK; } diff --git a/src/parse/scan1.c b/src/parse/scan1.c index a28d1c67..e751020a 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -397,19 +397,18 @@ ANN m_bool scan1_enum_def(const Env env, const Enum_Def edef) { t->nspc = new_nspc(env->gwion->mp, t->name); const m_uint scope = env_push_type(env, t); ID_List list = edef->list; + m_uint last = 0; for(uint32_t i = 0; i < list->len; i++) { - Symbol xid = *mp_vector_at(list, Symbol, i); - const Value v = new_value(env, t, s_name(xid), edef->pos); + EnumValue ev = *mp_vector_at(list, EnumValue, i); + const Value v = new_value(env, t, s_name(ev.xid), edef->pos); + v->d.num = (ev.set ? ev.num : last); + last = v->d.num + 1; valuefrom(env, v->from); - nspc_add_value(env->curr, xid, v); - if (env->class_def) { - SET_FLAG(v, static); - SET_ACCESS(edef, v) - SET_ACCESS(edef, t) - } else - set_vflag(v, vflag_builtin); - SET_FLAG(v, const); - vector_add(&edef->values, (vtype)v); + nspc_add_value(env->curr, ev.xid, v); + SET_FLAG(v, static | ae_flag_const); + SET_ACCESS(edef, v) + SET_ACCESS(edef, t) + set_vflag(v, vflag_builtin); } env_pop(env, scope); return GW_OK; diff --git a/src/parse/traverse.c b/src/parse/traverse.c index e6e83db3..749a069b 100644 --- a/src/parse/traverse.c +++ b/src/parse/traverse.c @@ -44,7 +44,8 @@ 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_BB(scan2_enum_def(env, def)); - return check_enum_def(env, def); + // return check_enum_def(env, def); + return GW_OK; } ANN m_bool traverse_fptr_def(const Env env, const Fptr_Def def) { -- 2.43.0