From 415b061f86b6f30d601fad8fc2d04a21c09ea287 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 24 Sep 2019 19:20:11 +0200 Subject: [PATCH] :art: Implicitly cast int to enum --- include/type.h | 2 +- src/lib/prim.c | 25 ++++++++++++++----------- src/oo/type.c | 4 ++-- src/parse/scan0.c | 4 ++-- tests/import/enum.c | 4 ++-- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/type.h b/include/type.h index e55834ac..e69aa500 100644 --- a/include/type.h +++ b/include/type.h @@ -24,7 +24,7 @@ struct Type_ { ae_flag flag; }; -extern Type t_void, t_int, t_bool, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t_vec4, +extern Type t_void, t_int, t_enum, t_bool, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t_vec4, t_null, t_object, t_shred, t_fork, t_event, t_ugen, t_string, t_ptr, t_array, t_gack, t_function, t_fptr, t_varloop, t_vararg, t_lambda, t_class, t_union, t_undefined, t_auto, t_tuple; diff --git a/src/lib/prim.c b/src/lib/prim.c index 295e6809..5d8607e8 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -58,6 +58,11 @@ static GWION_IMPORT(int_r) { return GW_OK; } +static OP_CHECK(opck_i2enum) { + struct Implicit* imp = (struct Implicit*)data; + return imp->e->cast_to = imp->t; +} + static GWION_IMPORT(int_unary) { GWI_BB(gwi_oper_ini(gwi, NULL, "int", "int")) GWI_BB(gwi_oper_add(gwi, opck_unary_meta)) @@ -74,13 +79,16 @@ static GWION_IMPORT(int_unary) { return GW_OK; } -static GWION_IMPORT(int_values) { +static GWION_IMPORT(bool) { + GWI_OB((t_enum = gwi_mk_type(gwi, "@enum", SZ_INT, t_int))) + GWI_BB(gwi_add_type(gwi, t_enum)) + GWI_BB(gwi_oper_ini(gwi, "int", "@enum", NULL)) + GWI_BB(gwi_oper_add(gwi, opck_i2enum)) + GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) GWI_BB(gwi_enum_ini(gwi, "bool")) GWI_BB(gwi_enum_add(gwi, "false", 0)) GWI_BB(gwi_enum_add(gwi, "true", 1)) t_bool = gwi_enum_end(gwi); -// GWI_BB(gwi_item_ini(gwi, "bool", "maybe")) -// GWI_BB(gwi_item_end(gwi, 0, NULL)) gwi_reserve(gwi, "maybe"); struct SpecialId_ spid = { .type=t_bool, .exec=RegPushMaybe, .is_const=1 }; gwi_specialid(gwi, "maybe", &spid); @@ -93,8 +101,7 @@ static GWION_IMPORT(int) { GWI_BB(import_int_op(gwi)) GWI_BB(import_int_logical(gwi)) GWI_BB(import_int_r(gwi)) - GWI_BB(import_int_unary(gwi)) - return import_int_values(gwi); + return import_int_unary(gwi); } static GWION_IMPORT(values) { @@ -133,12 +140,7 @@ static GWION_IMPORT(values) { gwi_specialid(gwi, "now", &spid); return GW_OK; } -/* -static OP_CHECK(opck_chuck_now) { - Exp_Binary* bin = (Exp_Binary*)data; - ERR_N(exp_self(bin)->pos, _("can't assign 'now' to 'now'")) -} -*/ + static OP_CHECK(opck_implicit_f2i) { return t_null; } @@ -292,5 +294,6 @@ GWION_IMPORT(prim) { GWI_BB(import_floatint(gwi)) GWI_BB(import_dur(gwi)) GWI_BB(import_time(gwi)) + GWI_BB(import_bool(gwi)) return import_values(gwi); } diff --git a/src/oo/type.c b/src/oo/type.c index 418d0fb4..681e7441 100644 --- a/src/oo/type.c +++ b/src/oo/type.c @@ -44,7 +44,7 @@ Type new_type(MemPool p, const m_uint xid, const m_str name, const Type parent) type->name = name; type->e = mp_calloc(p, TypeInfo); type->e->parent = parent; - if(type->e->parent) { + if(t_object && parent && isa(parent, t_object) > 0) { type->size = parent->size; type->e->tuple = new_tupleform(p); } @@ -197,6 +197,6 @@ ANN m_uint get_depth(const Type type) { return depth; } -Type t_void, t_int, t_bool, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t_vec4, +Type t_void, t_int, t_enum, t_bool, t_float, t_dur, t_time, t_now, t_complex, t_polar, t_vec3, t_vec4, t_null, t_object, t_shred, t_fork, t_event, t_ugen, t_string, t_ptr, t_array, t_gack, t_function, t_fptr, t_varloop, t_vararg, t_lambda, t_class, t_union, t_undefined, t_auto, t_tuple; diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 6036a898..c6cd9d1e 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -150,10 +150,10 @@ ANN m_bool scan0_enum_def(const Env env, const Enum_Def edef) { 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); + const Type t = type_copy(env->gwion->mp, t_enum); t->xid = ++env->scope->type_xid; t->name = edef->xid ? s_name(edef->xid) : "int"; - t->e->parent = t_int; + t->e->parent = t_enum; const Nspc nspc = GET_FLAG(edef, global) ? env->global_nspc : env->curr; t->e->owner = nspc; edef->t = t; diff --git a/tests/import/enum.c b/tests/import/enum.c index 0804c25d..bdc84bbd 100644 --- a/tests/import/enum.c +++ b/tests/import/enum.c @@ -38,8 +38,8 @@ GWION_IMPORT(enum_test) { CHECK_OB(gwi_enum_end(gwi)) Type t_enum; - CHECK_OB((t_enum = gwi_mk_type(gwi, "Enum", 0, NULL))) - CHECK_BB(gwi_class_ini(gwi, t_enum, NULL, NULL)) + CHECK_OB((t_myenum = gwi_mk_type(gwi, "Enum", 0, NULL))) + CHECK_BB(gwi_class_ini(gwi, t_myenum, NULL, NULL)) CHECK_BB(gwi_enum_ini(gwi, 0)) CHECK_BB(gwi_enum_add(gwi, "ENUM0", 0)) CHECK_BB(gwi_enum_add(gwi, "ENUM1", 1)) -- 2.43.0