Skip to content

Commit

Permalink
🎨 enums assign at def
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed May 29, 2023
1 parent e833f28 commit abc6295
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 81 deletions.
2 changes: 1 addition & 1 deletion ast
2 changes: 1 addition & 1 deletion fmt
Submodule fmt updated 1 files
+20 −4 src/lint.c
2 changes: 1 addition & 1 deletion include/clean.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions src/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/emit/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 5 additions & 8 deletions src/env/nspc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
53 changes: 7 additions & 46 deletions src/import/import_enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 0 additions & 1 deletion src/parse/scan0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 10 additions & 11 deletions src/parse/scan1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/parse/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit abc6295

Please sign in to comment.