From 616952d3aa12ad3b0bc59a935ac4fd12eabd237f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Thu, 5 Mar 2020 22:34:00 +0100 Subject: [PATCH] :art: Constructor for tuple types --- src/emit/emit.c | 26 ++----- src/lib/tuple.c | 25 +++++++ src/lib/vec.c | 2 +- src/parse/check.c | 75 ++------------------ src/parse/scan1.c | 2 - src/parse/scan2.c | 2 - tests/tuple/array_tuple_array.gw | 2 +- tests/tuple/object_array_access_multi.gw | 4 +- tests/tuple/tuple2obj_cast_err.gw | 2 +- tests/tuple/tuple2obj_err.gw | 2 +- tests/tuple/tuple_aray_access.gw | 6 +- tests/tuple/tuple_aray_access_exceed.gw | 2 +- tests/tuple/tuple_aray_access_multi.gw | 4 +- tests/tuple/tuple_aray_access_not_prim.gw | 2 +- tests/tuple/tuple_array_access_invalid.gw | 2 +- tests/tuple/tuple_at.gw | 2 +- tests/tuple/tuple_at_err.gw | 2 +- tests/tuple/tuple_at_lit.gw | 2 +- tests/tuple/tuple_cast3.gw | 2 +- tests/tuple/tuple_implicit.gw | 4 +- tests/tuple/tuple_implicit2.gw | 2 +- tests/tuple/tuple_lit_error.gw | 2 +- tests/tuple/tuple_member_float.gw | 2 +- tests/tuple/tuple_skip.gw | 2 +- tests/tuple/tuple_skip2.gw | 4 +- tests/tuple/tuple_unpack_already_declared.gw | 2 +- 26 files changed, 63 insertions(+), 121 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index dc47c637..126bde8c 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -544,17 +544,6 @@ ANN static void emit_vec_addr(const Emitter emit, const m_uint sz) { instr->m_val = offset; } -ANN static m_bool emit_prim_vec(const Emitter emit, const Vec *vec) { - const ae_prim_t t = prim_self(vec)->prim_type; - CHECK_BB(emit_exp(emit, vec->exp, 0)); - m_int n = (m_int)((t == ae_prim_vec ? 3 : 2) - vec->dim + 1); - while(--n > 0) - emit_add_instr(emit, RegPushImm2); - if(prim_exp(vec)->emit_var) - emit_vec_addr(emit, prim_exp(vec)->type->size); - return GW_OK; -} - ANN static m_bool emit_prim_id(const Emitter emit, const Symbol *data) { struct SpecialId_ * spid = specialid_get(emit->gwion, *data); if(spid) @@ -562,13 +551,6 @@ ANN static m_bool emit_prim_id(const Emitter emit, const Symbol *data) { return emit_symbol(emit, prim_self(data)); } -ANN static m_bool emit_prim_tuple(const Emitter emit, const Tuple *tuple) { - CHECK_BB(emit_exp(emit, tuple->exp, 1)) - const Instr instr = emit_add_instr(emit, TupleCtor); - instr->m_val = (m_uint)prim_exp(tuple)->type; - return GW_OK; -} - ANN static m_bool emit_prim_num(const Emitter emit, const m_uint *num) { regpushi(emit, *num); return GW_OK; @@ -813,9 +795,11 @@ ANN static m_bool emit_exp_call(const Emitter emit, const Exp_Call* exp_call) { } if(exp_call->args) CHECK_BB(emit_exp(emit, exp_call->args, 1)) - const Exp e = exp_self(exp_call); - if(e->emit_var) - emit_vec_addr(emit, e->type->size); + struct Op_Import opi = { .op=insert_symbol("@ctor"), .lhs=exp_call->func->type->e->d.base_type, .data=(uintptr_t)exp_call, .pos=exp_self(exp_call)->pos }; + CHECK_OB(op_emit(emit, &opi)) + const Exp e = exp_self(exp_call); + if(e->emit_var) + emit_vec_addr(emit, e->type->size); return GW_OK; } diff --git a/src/lib/tuple.c b/src/lib/tuple.c index 5cdd75e4..b7d2402d 100644 --- a/src/lib/tuple.c +++ b/src/lib/tuple.c @@ -355,12 +355,37 @@ static OP_CHECK(opck_tuple) { return at_depth(env, &next); } +static OP_CHECK(tuple_ck) { + const Exp_Call *call = (Exp_Call*)data; + const Exp exp = call->args; + CHECK_OO(check_exp(env, exp)) + struct Vector_ v; + vector_init(&v); + Exp e = exp; + do vector_add(&v, (m_uint)e->type); + while((e = e->next)); + const Type ret = tuple_type(env, &v, exp_self(call)->pos); + vector_release(&v); + return ret; +} + +static OP_EMIT(tuple_em) { + const Exp_Call *call = (Exp_Call*)data; + const Instr instr = emit_add_instr(emit, TupleCtor); + instr->m_val = (m_uint)exp_self(call)->type; + return instr; +} + GWION_IMPORT(tuple) { const Type t_tuple = gwi_mk_type(gwi, "Tuple", SZ_INT, "Object"); gwi_add_type(gwi, t_tuple); SET_FLAG(t_tuple, checked | ae_flag_scan2 | ae_flag_check | ae_flag_emit); gwi->gwion->type[et_tuple] = t_tuple; SET_FLAG(t_tuple, abstract | ae_flag_template); + GWI_BB(gwi_oper_ini(gwi, "Tuple", NULL, NULL)) + GWI_BB(gwi_oper_add(gwi, tuple_ck)) + GWI_BB(gwi_oper_emi(gwi, tuple_em)) + GWI_BB(gwi_oper_end(gwi, "@ctor", NULL)) GWI_BB(gwi_oper_ini(gwi, "Object", "Tuple", NULL)) GWI_BB(gwi_oper_add(gwi, opck_at_tuple)) GWI_BB(gwi_oper_emi(gwi, opem_at_tuple)) diff --git a/src/lib/vec.c b/src/lib/vec.c index 03795d75..aaa92d88 100644 --- a/src/lib/vec.c +++ b/src/lib/vec.c @@ -166,7 +166,7 @@ EQUALITY_OPER(vec3, SZ_VEC3); OP_CHECK(vecx_ck) { Exp_Call *call = (Exp_Call*)data; Exp e = call->args, last = NULL; - int i = 0; + size_t i = 0; const Type t_float = env->gwion->type[et_float]; while(e) { CHECK_BO(check_implicit(env, e, t_float)) diff --git a/src/parse/check.c b/src/parse/check.c index 4e2de6a5..ef9868ea 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -292,46 +292,6 @@ ANN static Type check_prim_id(const Env env, const Symbol *data) { return prim_id_non_res(env, data); } -ANN static m_bool vec_value(const Env env, Exp e) { - CHECK_OB(check_exp(env, e)) - const Type t_float = env->gwion->type[et_float]; - do CHECK_BB(check_implicit(env, e, t_float)) - while((e = e->next)); - return GW_OK; -} - -struct VecInfo { - Type t; - m_str s; - m_uint n; -}; - -ANN static void vec_info(const Env env, const ae_prim_t t, struct VecInfo* v) { - if(t == ae_prim_complex) { - v->s = "complex"; - v->t = env->gwion->type[et_complex]; - v->n = 2; - } else if(t == ae_prim_vec) { - v->t = env->gwion->type[v->n == 4 ? et_vec4 : et_vec3]; - v->n = 4; - v->s = "vector"; - } else { - v->s = "polar"; - v->t = env->gwion->type[et_polar]; - v->n = 2; - } -} - -ANN static Type check_prim_vec(const Env env, const Vec *vec) { - const ae_prim_t t = prim_self(vec)->prim_type; - struct VecInfo info = { .n=vec->dim }; - vec_info(env, t, &info); - if(vec->dim > info.n) - ERR_O(vec->exp->pos, _("extraneous component of %s value..."), info.s) - CHECK_BO(vec_value(env, vec->exp)) - return info.t; -} - ANN static Type check_prim_hack(const Env env, const Exp *data) { if(env->func) UNSET_FLAG(env->func, pure); @@ -339,18 +299,6 @@ ANN static Type check_prim_hack(const Env env, const Exp *data) { return env->gwion->type[et_gack]; } -ANN static Type check_prim_tuple(const Env env, const Tuple *tuple) { - CHECK_OO(check_exp(env, tuple->exp)) - struct Vector_ v; - vector_init(&v); - Exp e = tuple->exp; - do vector_add(&v, (m_uint)e->type); - while((e = e->next)); - const Type ret = tuple_type(env, &v, prim_pos(tuple)); - vector_release(&v); - return ret; -} - #define describe_prim_xxx(name, type) \ ANN static Type check##_prim_##name(const Env env NUSED, const union prim_data* data NUSED) {\ return type; \ @@ -446,7 +394,6 @@ ANN static m_bool func_match_inner(const Env env, const Exp e, const Type t, ANN2(1,2) static Func find_func_match_actual(const Env env, Func func, const Exp args, const m_bool implicit, const m_bool specific) { -printf("func %p %s\n", func, func->name); do { Exp e = args; Arg_List e1 = func->def->base->args; @@ -745,20 +692,6 @@ ANN static Type check_exp_call_template(const Env env, Exp_Call *exp) { return func->def->base->ret_type; } -ANN static m_bool check_exp_call1_check(const Env env, const Exp exp) { - CHECK_OB(check_exp(env, exp)) - if(isa(exp->type, env->gwion->type[et_function]) < 0) { - if(isa(exp->type, env->gwion->type[et_class]) > 0) { -puts("here:!:"); -//const Type t = exp->type->e->d.base_type; -//const Func func = nspc_lookup_func0(t->e->owner, insert_symbol(t->name)); -//exp->type = func->value_ref->type; - } else - ERR_B(exp->pos, _("function call using a non-function value")) - } - return GW_OK; -} - ANN static Type check_lambda_call(const Env env, const Exp_Call *exp) { if(exp->args) CHECK_OO(check_exp(env, exp->args)) @@ -780,12 +713,16 @@ ANN static Type check_lambda_call(const Env env, const Exp_Call *exp) { } ANN Type check_exp_call1(const Env env, const Exp_Call *exp) { - CHECK_BO(check_exp_call1_check(env, exp->func)) + CHECK_OO(check_exp(env, exp->func)) if(isa(exp->func->type, env->gwion->type[et_function]) < 0) { + if(isa(exp->func->type, env->gwion->type[et_class]) < 0) + ERR_O(exp->func->pos, _("function call using a non-function value")) if(exp->args) CHECK_OO(check_exp(env, exp->args)) struct Op_Import opi = { .op=insert_symbol("@ctor"), .lhs=exp->func->type->e->d.base_type, .data=(uintptr_t)exp, .pos=exp_self(exp)->pos }; - return op_check(env, &opi); + const Type t = op_check(env, &opi); + exp_self(exp)->nspc = t ? t->e->owner : NULL; + return t; } if(exp->func->type == env->gwion->type[et_lambda]) return check_lambda_call(env, exp); diff --git a/src/parse/scan1.c b/src/parse/scan1.c index d1db4f31..8916cce0 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -137,8 +137,6 @@ ANN static inline m_bool scan1_prim(const Env env, const Exp_Primary* prim) { return scan1_exp(env, prim->d.array->exp); if(prim->prim_type == ae_prim_range) return scan1_range(env, prim->d.range); - if(prim->prim_type == ae_prim_tuple) - return scan1_exp(env, prim->d.tuple.exp); // if(prim->prim_type == ae_prim_unpack) // return scan1_exp(env, prim->d.tuple.exp); return GW_OK; diff --git a/src/parse/scan2.c b/src/parse/scan2.c index ba453323..f58c2694 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -120,8 +120,6 @@ ANN static inline m_bool scan2_prim(const Env env, const Exp_Primary* prim) { return scan2_exp(env, prim->d.array->exp); else if(prim->prim_type == ae_prim_range) return scan2_range(env, prim->d.range); - if(prim->prim_type == ae_prim_tuple) - return scan2_exp(env, prim->d.tuple.exp); return GW_OK; } diff --git a/tests/tuple/array_tuple_array.gw b/tests/tuple/array_tuple_array.gw index 8ed2d130..af90f45e 100644 --- a/tests/tuple/array_tuple_array.gw +++ b/tests/tuple/array_tuple_array.gw @@ -1,4 +1,4 @@ -<([1,2], "Tom") @=> <~int[], string~>Tuple @tup; +Tuple([1,2], "Tom") @=> <~int[], string~>Tuple @tup; #!<<>>; #!<<>>; diff --git a/tests/tuple/object_array_access_multi.gw b/tests/tuple/object_array_access_multi.gw index e9f2f9f8..37c78222 100644 --- a/tests/tuple/object_array_access_multi.gw +++ b/tests/tuple/object_array_access_multi.gw @@ -2,8 +2,8 @@ Object t[2]; #!<<< t[0] >>>; #!null @=> t[0]; <<< t >>>; -<("tom", 12) @=> t[0]; -#!t << <("tom", 12); +Tuple("tom", 12) @=> t[0]; +#!t << Tuple("tom", 12); #!<<< t.size() >>>; #!<<< t[0][0] >>>; #!<<< t[0][1] >>>; diff --git a/tests/tuple/tuple2obj_cast_err.gw b/tests/tuple/tuple2obj_cast_err.gw index f1330c35..151752a5 100644 --- a/tests/tuple/tuple2obj_cast_err.gw +++ b/tests/tuple/tuple2obj_cast_err.gw @@ -1,2 +1,2 @@ #! [contains] no match found for operator -<(1) $ Object; +Tuple(1) $ Object; diff --git a/tests/tuple/tuple2obj_err.gw b/tests/tuple/tuple2obj_err.gw index 98b6c175..c54366c7 100644 --- a/tests/tuple/tuple2obj_err.gw +++ b/tests/tuple/tuple2obj_err.gw @@ -1,2 +1,2 @@ #! [contains] can't cast -<(1) @=> <~int~>Ptr ptr; +Tuple(1) @=> <~int~>Ptr ptr; diff --git a/tests/tuple/tuple_aray_access.gw b/tests/tuple/tuple_aray_access.gw index 1c043df2..1ed8736d 100644 --- a/tests/tuple/tuple_aray_access.gw +++ b/tests/tuple/tuple_aray_access.gw @@ -1,6 +1,6 @@ -<<< <("Tom", 12)[0] >>> ; -#! <<< <("Tom", 12)[1] >>> ; +<<< Tuple("Tom", 12)[0] >>> ; +#! <<< Tuple("Tom", 12)[1] >>> ; -#! <<< <("Tom", 12) @=> <~string, int~>Tuple @t >>> ; +#! <<< Tuple("Tom", 12) @=> <~string, int~>Tuple @t >>> ; #! <<< t[0] >>> ; #! <<< t[1] >>> ; diff --git a/tests/tuple/tuple_aray_access_exceed.gw b/tests/tuple/tuple_aray_access_exceed.gw index 69fd159f..b7bcfa6f 100644 --- a/tests/tuple/tuple_aray_access_exceed.gw +++ b/tests/tuple/tuple_aray_access_exceed.gw @@ -1 +1 @@ -<("Tom", 12)[0][2]; +Tuple("Tom", 12)[0][2]; diff --git a/tests/tuple/tuple_aray_access_multi.gw b/tests/tuple/tuple_aray_access_multi.gw index 0a30f810..79350a2f 100644 --- a/tests/tuple/tuple_aray_access_multi.gw +++ b/tests/tuple/tuple_aray_access_multi.gw @@ -3,9 +3,9 @@ #!null @=> t[0]; <<< t >>>; <<< t[0] >>>; -<<< <("tom", 12) @=> t[0] >>> ; +<<< Tuple("tom", 12) @=> t[0] >>> ; -#!t << <("tom", 12); +#!t << Tuple("tom", 12); #!<<< t.size() >>>; #!<<< t[0][0] >>>; #!<<< t[0][1] >>>; diff --git a/tests/tuple/tuple_aray_access_not_prim.gw b/tests/tuple/tuple_aray_access_not_prim.gw index 3afc0e14..9320ed44 100644 --- a/tests/tuple/tuple_aray_access_not_prim.gw +++ b/tests/tuple/tuple_aray_access_not_prim.gw @@ -1,2 +1,2 @@ int i; -<("Tom", 12)[i]; +Tuple("Tom", 12)[i]; diff --git a/tests/tuple/tuple_array_access_invalid.gw b/tests/tuple/tuple_array_access_invalid.gw index 6b23d6b2..3439089b 100644 --- a/tests/tuple/tuple_array_access_invalid.gw +++ b/tests/tuple/tuple_array_access_invalid.gw @@ -1 +1 @@ -<(1.2,"test")[3]; +Tuple(1.2,"test")[3]; diff --git a/tests/tuple/tuple_at.gw b/tests/tuple/tuple_at.gw index a049f7a0..aa669c50 100644 --- a/tests/tuple/tuple_at.gw +++ b/tests/tuple/tuple_at.gw @@ -1,4 +1,4 @@ -<<< <("Tom", 6) @=> +<<< Tuple("Tom", 6) @=> <~string, int~>Tuple @tup >>>; <<< typeof(tup) >>>; <<< tup >>>; diff --git a/tests/tuple/tuple_at_err.gw b/tests/tuple/tuple_at_err.gw index 3ec6aa60..fe93e754 100644 --- a/tests/tuple/tuple_at_err.gw +++ b/tests/tuple/tuple_at_err.gw @@ -1,4 +1,4 @@ -<("Tom", 6) @=> +Tuple("Tom", 6) @=> <~float, int~>Tuple @tup; <<< typeof(tup) >>>; <<< tup >>>; diff --git a/tests/tuple/tuple_at_lit.gw b/tests/tuple/tuple_at_lit.gw index 981b5e02..91c50bdf 100644 --- a/tests/tuple/tuple_at_lit.gw +++ b/tests/tuple/tuple_at_lit.gw @@ -1,2 +1,2 @@ #! [contains] cannot assign -<("Tom", 6) @=> <("Pat", 22); +Tuple("Tom", 6) @=> Tuple("Pat", 22); diff --git a/tests/tuple/tuple_cast3.gw b/tests/tuple/tuple_cast3.gw index 6b974461..0a3aebd5 100644 --- a/tests/tuple/tuple_cast3.gw +++ b/tests/tuple/tuple_cast3.gw @@ -1 +1 @@ -<(1) $ <~int~>Tuple; +Tuple(1) $ <~int~>Tuple; diff --git a/tests/tuple/tuple_implicit.gw b/tests/tuple/tuple_implicit.gw index 419a94ee..54ead660 100644 --- a/tests/tuple/tuple_implicit.gw +++ b/tests/tuple/tuple_implicit.gw @@ -3,5 +3,5 @@ fun void test(<~string~>Tuple t) { <<< t[0] >>>; } -<( "Tom", 21) => test; -#!<("Tom") => test; +Tuple( "Tom", 21) => test; +#!Tuple("Tom") => test; diff --git a/tests/tuple/tuple_implicit2.gw b/tests/tuple/tuple_implicit2.gw index 3c3a6e65..b9e38f0c 100644 --- a/tests/tuple/tuple_implicit2.gw +++ b/tests/tuple/tuple_implicit2.gw @@ -2,4 +2,4 @@ class C { int i; } fun void test(C c) { } -<(1) => test; +Tuple(1) => test; diff --git a/tests/tuple/tuple_lit_error.gw b/tests/tuple/tuple_lit_error.gw index 2e89d2dd..9d5b5717 100644 --- a/tests/tuple/tuple_lit_error.gw +++ b/tests/tuple/tuple_lit_error.gw @@ -1 +1 @@ -<( new int); +Tuple( new int); diff --git a/tests/tuple/tuple_member_float.gw b/tests/tuple/tuple_member_float.gw index abb1b6e2..c6c9fca1 100644 --- a/tests/tuple/tuple_member_float.gw +++ b/tests/tuple/tuple_member_float.gw @@ -1,4 +1,4 @@ -<(.2, Vec3(1), 3) @=> <~float, Vec3, int~>Tuple @t; +Tuple(.2, Vec3(1), 3) @=> <~float, Vec3, int~>Tuple @t; t[0]; t[1]; 3 => t[2]; diff --git a/tests/tuple/tuple_skip.gw b/tests/tuple/tuple_skip.gw index 73c6499f..cd289269 100644 --- a/tests/tuple/tuple_skip.gw +++ b/tests/tuple/tuple_skip.gw @@ -1,2 +1,2 @@ -<(1, 2, 3) @=> >(b, _, c); +Tuple(1, 2, 3) @=> >(b, _, c); <<< c >>>; diff --git a/tests/tuple/tuple_skip2.gw b/tests/tuple/tuple_skip2.gw index bc0abe75..8b4f0e91 100644 --- a/tests/tuple/tuple_skip2.gw +++ b/tests/tuple/tuple_skip2.gw @@ -1,6 +1,6 @@ -<("Tom", 2, "Taxi driver") @=> <~string,_, string~>Tuple @t; +Tuple("Tom", 2, "Taxi driver") @=> <~string,_, string~>Tuple @t; <<< t>>>; <<< t[0]>>>; -<("Tom", 2.3, "Cook") @=> t; +Tuple("Tom", 2.3, "Cook") @=> t; #!<<< t[1]>>>; <<< t[2]>>>; diff --git a/tests/tuple/tuple_unpack_already_declared.gw b/tests/tuple/tuple_unpack_already_declared.gw index e44abee7..20178d96 100644 --- a/tests/tuple/tuple_unpack_already_declared.gw +++ b/tests/tuple/tuple_unpack_already_declared.gw @@ -1,3 +1,3 @@ #! [contains] already been defined in the same scope int i; -<(12) @=> >(i); +Tuple(12) @=> >(i); -- 2.43.0