From: Jérémie Astor Date: Sun, 1 May 2022 10:21:12 +0000 (+0200) Subject: :art: USe Enum name as array size X-Git-Tag: nightly~275^2~6 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=9f2afe9b1913e24fbf944d7e25f8a50d23eb9ae1;p=gwion.git :art: USe Enum name as array size --- diff --git a/include/env/type.h b/include/env/type.h index 5a30a69f..33d05a0e 100644 --- a/include/env/type.h +++ b/include/env/type.h @@ -38,8 +38,9 @@ enum tflag { tflag_contract = 1 << 19, tflag_float = 1 << 20, tflag_union = 1 << 21, - tflag_error = 1 << 22, - tflag_ref = 1 << 23, + tflag_enum = 1 << 22, + tflag_error = 1 << 23, + tflag_ref = 1 << 24, } __attribute__((packed)); struct Type_ { diff --git a/src/lib/lib_class.c b/src/lib/lib_class.c index 55f337da..783c93f4 100644 --- a/src/lib/lib_class.c +++ b/src/lib/lib_class.c @@ -38,6 +38,21 @@ static OP_CHECK(opck_basic_ctor) { ERR_N(call->func->pos, _("can't call a non-callable value")); } +static OP_EMIT(opem_implicit_class) { + struct Implicit *imp = (struct Implicit*)data; + const Type t = actual_type(emit->gwion, imp->e->type); + const Instr instr = emit_add_instr(emit, RegPushImm); + instr->m_val = map_size(&t->nspc->info->value->map); + return GW_OK; emit_exp(emit, imp->e); +} + +static OP_CHECK(opck_implicit_class) { + struct Implicit *imp = (struct Implicit*)data; + const Type t = actual_type(env->gwion, imp->e->type); + if(tflag(t, tflag_enum)) return env->gwion->type[et_int]; + return env->gwion->type[et_error]; +} + GWION_IMPORT(class) { gwidoc(gwi, "Operators class types."); @@ -59,5 +74,11 @@ GWION_IMPORT(class) { GWI_BB(gwi_oper_add(gwi, opck_basic_ctor)) GWI_BB(gwi_oper_end(gwi, "@ctor", NULL)) + gwidoc(gwi, "Allow enum for array size"); + GWI_BB(gwi_oper_ini(gwi, "Class", "int", NULL)) + GWI_BB(gwi_oper_add(gwi, opck_implicit_class)) + GWI_BB(gwi_oper_emi(gwi, opem_implicit_class)) + GWI_BB(gwi_oper_end(gwi, "@implicit", NULL)) + return GW_OK; } diff --git a/src/parse/scan0.c b/src/parse/scan0.c index a0d9faf4..518ebbd6 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -232,6 +232,7 @@ ANN static Type enum_type(const Env env, const Enum_Def edef) { } add_type(env, env->curr, t); mk_class(env, t, edef->pos); + set_tflag(t, tflag_enum); if (global) env_pop(env, 0); // scan0_implicit_similar(env, t, env->gwion->type[et_int]); // scan0_implicit_similar(env, env->gwion->type[et_int], t);