From: fennecdjay Date: Mon, 25 Nov 2019 00:32:10 +0000 (+0100) Subject: :art: Equality Ops for special types X-Git-Tag: nightly~2083 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=d09751d280d04dabaa17469fac4280ed9109a33e;p=gwion.git :art: Equality Ops for special types --- diff --git a/examples/complex.gw b/examples/complex.gw index 2d35de88..1a6bccb4 100644 --- a/examples/complex.gw +++ b/examples/complex.gw @@ -2,6 +2,8 @@ complex a, b; a; a.re; a.im; +a == b; +a != b; a + b; <<< a - b >>>; diff --git a/examples/polar.gw b/examples/polar.gw index 1343283c..d4d0e6a5 100644 --- a/examples/polar.gw +++ b/examples/polar.gw @@ -3,6 +3,8 @@ polar a; a.mod; a.phase; a; +a == b; +a != b; a + b; a - b; a * b; diff --git a/examples/vec3.gw b/examples/vec3.gw index a1c1a201..98dba0a8 100644 --- a/examples/vec3.gw +++ b/examples/vec3.gw @@ -1,5 +1,7 @@ Vec3 v, w; +<<< v == >>>; + <<< v + w >>>; <<< v - w >>>; <<< v * w >>>; @@ -14,7 +16,7 @@ Vec3 v, w; <<< -12 => v.z >>>; <<< @(.1, .2, .4) => v >>>; <<< v >>>; -<<< "set ", v.set(1,2,3) >>>; +<<< "set ", v.set(1,2,3) >>>; <<< "setAll ", v.setAll(1.2) >>>; <<< "should be 1.2 1.2 1.2 ", v >>>; <<< "Update ", v.update(2) >>>; diff --git a/examples/vec4.gw b/examples/vec4.gw index 5e1862b8..e876c603 100644 --- a/examples/vec4.gw +++ b/examples/vec4.gw @@ -1,6 +1,9 @@ Vec4 v, w; <<< v >>>; +<<< v == w>>>; +<<< v != w>>>; + <<< v + w>>>; <<< v - w>>>; <<< v * w>>>; diff --git a/include/import/oper.h b/include/import/oper.h index b2322c40..cf5bedcf 100644 --- a/include/import/oper.h +++ b/include/import/oper.h @@ -7,4 +7,13 @@ ANN m_int gwi_oper_emi(const Gwi gwi, const opem); ANN2(1) m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f); ANN m_int gwi_oper_cond(const Gwi, const m_str, const f_instr, const f_instr); +#define _EQUALITY_OPER(sz, sign) \ + POP_REG(shred, sz*2 - SZ_INT); \ + *(m_uint*)REG(-SZ_INT) = sign \ + memcmp(shred->reg + SZ_INT, shred->reg + SZ_INT + sz, sz); \ + +#define EQUALITY_OPER(name, sz) \ +static INSTR(name##_eq) { _EQUALITY_OPER(sz, !) } \ +static INSTR(name##_ne) { _EQUALITY_OPER(sz, !!) } + #endif diff --git a/src/lib/complex.c b/src/lib/complex.c index 41a83747..31ca8305 100644 --- a/src/lib/complex.c +++ b/src/lib/complex.c @@ -121,6 +121,8 @@ static GACK(gack_polar) { printf("%%(%.4f, %.4f*pi)", *(m_float*)VALUE, *(m_float*)(VALUE + SZ_FLOAT) / M_PI); } +EQUALITY_OPER(complex, SZ_COMPLEX) + GWION_IMPORT(complex) { // should be special const Type t_complex = gwi_class_spe(gwi, "complex", SZ_COMPLEX); @@ -140,6 +142,9 @@ GWION_IMPORT(complex) { GWI_BB(gwi_item_ini(gwi, "float", "phase")) GWI_BB(gwi_item_end(gwi, ae_flag_member, NULL)) GWI_BB(gwi_class_end(gwi)) + GWI_BB(gwi_oper_ini(gwi, "complex", "complex", "bool")) + GWI_BB(gwi_oper_end(gwi, "==", complex_eq)) + GWI_BB(gwi_oper_end(gwi, "!=", complex_ne)) GWI_BB(gwi_oper_ini(gwi, "complex", "complex", "complex")) GWI_BB(gwi_oper_end(gwi, "+", ComplexAdd)) GWI_BB(gwi_oper_end(gwi, "-", ComplexSub)) @@ -155,6 +160,9 @@ GWION_IMPORT(complex) { GWI_BB(gwi_oper_end(gwi, "*=>", ComplexRMul)) GWI_BB(gwi_oper_add(gwi, opck_rassign)) GWI_BB(gwi_oper_end(gwi, "/=>", ComplexRDiv)) + GWI_BB(gwi_oper_ini(gwi, "polar", "polar", "bool")) + GWI_BB(gwi_oper_end(gwi, "==", complex_eq)) + GWI_BB(gwi_oper_end(gwi, "!=", complex_ne)) GWI_BB(gwi_oper_ini(gwi, "polar", "polar", "polar")) GWI_BB(gwi_oper_add(gwi, opck_rassign)) GWI_BB(gwi_oper_end(gwi, "=>", ComplexRAssign)) diff --git a/src/lib/vec.c b/src/lib/vec.c index e1a25bcb..3cc2e837 100644 --- a/src/lib/vec.c +++ b/src/lib/vec.c @@ -160,6 +160,8 @@ static GACK(gack_vec3) { printf("%%(%.4f, %.4f, %.4f)", *(m_float*)VALUE, *(m_float*)(VALUE + SZ_FLOAT), *(m_float*)(VALUE + SZ_FLOAT*2)); } +EQUALITY_OPER(vec3, SZ_VEC3); + GWION_IMPORT(vec3) { const Type t_vec3 = gwi_class_spe(gwi, "Vec3", SZ_VEC3); gwi->gwion->type[et_vec3] = t_vec3; @@ -201,6 +203,9 @@ GWION_IMPORT(vec3) { GWI_BB(gwi_func_end(gwi, vec3_update_set_slew, ae_flag_none)) GWI_BB(gwi_class_end(gwi)) + GWI_BB(gwi_oper_ini(gwi, "Vec3", "Vec3", "bool")) + GWI_BB(gwi_oper_end(gwi, "==", vec3_eq)) + GWI_BB(gwi_oper_end(gwi, "!=", vec3_ne)) GWI_BB(gwi_oper_ini(gwi, "Vec3", "Vec3", "Vec3")) GWI_BB(gwi_oper_end(gwi, "+", Vec3Add)) GWI_BB(gwi_oper_end(gwi, "-", Vec3Sub)) @@ -308,6 +313,8 @@ static GACK(gack_vec4) { *(m_float*)(VALUE + SZ_FLOAT*3)); } +EQUALITY_OPER(vec4, SZ_VEC4); + GWION_IMPORT(vec4) { const Type t_vec4 = gwi_class_spe(gwi, "Vec4", SZ_VEC4); gwi->gwion->type[et_vec4] = t_vec4; @@ -329,6 +336,9 @@ GWION_IMPORT(vec4) { gwi_func_ini(gwi, "void", "normalize"); CHECK_BB(gwi_func_end(gwi, vec4_normalize, ae_flag_none)) CHECK_BB(gwi_class_end(gwi)) + GWI_BB(gwi_oper_ini(gwi, "Vec4", "Vec4", "bool")) + GWI_BB(gwi_oper_end(gwi, "==", vec4_eq)) + GWI_BB(gwi_oper_end(gwi, "!=", vec4_ne)) CHECK_BB(gwi_oper_ini(gwi, "Vec4", "Vec4", "Vec4")) CHECK_BB(gwi_oper_end(gwi, "+", Vec4Add)) CHECK_BB(gwi_oper_end(gwi, "-", Vec4Sub))