From 632c72a65ebed589df2ea35c3e484c11f81bf965 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 8 Oct 2019 15:16:16 +0200 Subject: [PATCH] :art: Improve tuple --- src/emit/emit.c | 2 +- src/lib/tuple.c | 13 +++++++++++-- tests/tuple/tuple2obj_cast_err.gw | 2 ++ tests/tuple/tuple2obj_err.gw | 2 ++ tests/tuple/tuple_member_float.gw | 4 ++++ 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/tuple/tuple2obj_cast_err.gw create mode 100644 tests/tuple/tuple2obj_err.gw create mode 100644 tests/tuple/tuple_member_float.gw diff --git a/src/emit/emit.c b/src/emit/emit.c index 3a98b371..f2cba9f8 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -445,7 +445,7 @@ ANN static inline m_bool tuple_index(const Emitter emit, struct ArrayAccessInfo assert(isa(info->array.type, emit->gwion->type[et_tuple]) > 0); const m_uint idx = info->array.exp->d.exp_primary.d.num; emit_except(emit, info->array.type); - tuple_access(emit, info->array.exp->d.exp_primary.d.num, info->array.depth ? 0 : info->is_var); + tuple_access(emit, info->array.exp->d.exp_primary.d.num, (info->array.depth -1)? 0 : info->is_var); if(!info->array.exp->next) return GW_OK; const Type type = (Type)vector_at(&info->array.type->e->tuple->types, idx); diff --git a/src/lib/tuple.c b/src/lib/tuple.c index f44cb88b..ccada017 100644 --- a/src/lib/tuple.c +++ b/src/lib/tuple.c @@ -165,6 +165,14 @@ static OP_CHECK(opck_at_tuple_object) { return bin->rhs->type; } +static OP_CHECK(opck_cast_tuple_object) { + const Exp_Cast *cast = (Exp_Cast*)data; + if(tuple_match(env, exp_self(cast)->type, cast->exp->type) < 0) + return env->gwion->type[et_null]; + exp_self(cast)->emit_var = 1; + return exp_self(cast)->type; +} + static INSTR(Tuple2Object) { const M_Object o = *(M_Object*)(shred->reg - SZ_INT*2); const Type t = (Type)instr->m_val; @@ -188,13 +196,13 @@ mk_opem_tuple2object(impl, struct Implicit *, exp->t) static OP_CHECK(opck_cast_tuple) { const Exp_Cast *cast = (Exp_Cast*)data; - CHECK_BO(tuple_match(env, exp_self(cast)->type, cast->exp->type)) + CHECK_BN(tuple_match(env, exp_self(cast)->type, cast->exp->type)) return exp_self(cast)->type; } static OP_CHECK(opck_impl_tuple) { struct Implicit *imp = (struct Implicit*)data; - CHECK_BO(tuple_match(env, imp->e->type, imp->t)) + CHECK_BN(tuple_match(env, imp->e->type, imp->t)) return imp->t; } @@ -342,6 +350,7 @@ GWION_IMPORT(tuple) { GWI_BB(gwi_oper_add(gwi, opck_at_tuple_object)) GWI_BB(gwi_oper_emi(gwi, opem_at_tuple_object)) GWI_BB(gwi_oper_end(gwi, "@=>", NULL)) + GWI_BB(gwi_oper_add(gwi, opck_cast_tuple_object)) GWI_BB(gwi_oper_emi(gwi, opem_cast_tuple_object)) GWI_BB(gwi_oper_end(gwi, "$", NULL)) GWI_BB(gwi_oper_emi(gwi, opem_impl_tuple_object)) diff --git a/tests/tuple/tuple2obj_cast_err.gw b/tests/tuple/tuple2obj_cast_err.gw new file mode 100644 index 00000000..f1330c35 --- /dev/null +++ b/tests/tuple/tuple2obj_cast_err.gw @@ -0,0 +1,2 @@ +#! [contains] no match found for operator +<(1) $ Object; diff --git a/tests/tuple/tuple2obj_err.gw b/tests/tuple/tuple2obj_err.gw new file mode 100644 index 00000000..98b6c175 --- /dev/null +++ b/tests/tuple/tuple2obj_err.gw @@ -0,0 +1,2 @@ +#! [contains] can't cast +<(1) @=> <~int~>Ptr ptr; diff --git a/tests/tuple/tuple_member_float.gw b/tests/tuple/tuple_member_float.gw new file mode 100644 index 00000000..0205d812 --- /dev/null +++ b/tests/tuple/tuple_member_float.gw @@ -0,0 +1,4 @@ +<(.2, #(1), 3) @=> <~float, complex, int~>Tuple @t; +t[0]; +t[1]; +3 => t[2]; -- 2.43.0