]> Nishi Git Mirror - gwion.git/commitdiff
:art: Improve tuple
authorfennecdjay <astor.jeremie@wanadoo.fr>
Tue, 8 Oct 2019 13:16:16 +0000 (15:16 +0200)
committerfennecdjay <astor.jeremie@wanadoo.fr>
Tue, 8 Oct 2019 13:16:16 +0000 (15:16 +0200)
src/emit/emit.c
src/lib/tuple.c
tests/tuple/tuple2obj_cast_err.gw [new file with mode: 0644]
tests/tuple/tuple2obj_err.gw [new file with mode: 0644]
tests/tuple/tuple_member_float.gw [new file with mode: 0644]

index 3a98b371a63f3d97c0acc46581dd752c35b8591b..f2cba9f8817d067de27fe6c7b4dca2c211388553 100644 (file)
@@ -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);
index f44cb88b6d421921031ac4b2a774ba9ec273f728..ccada017648afb2e2c0119c84e059e8a99a4f675 100644 (file)
@@ -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 (file)
index 0000000..f1330c3
--- /dev/null
@@ -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 (file)
index 0000000..98b6c17
--- /dev/null
@@ -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 (file)
index 0000000..0205d81
--- /dev/null
@@ -0,0 +1,4 @@
+<(.2, #(1), 3) @=> <~float, complex, int~>Tuple @t;
+t[0];
+t[1];
+3 => t[2];