]> Nishi Git Mirror - gwion.git/commitdiff
:art: unary folding
authorJérémie Astor <fennecdjay@gmail.com>
Sat, 23 Jan 2021 19:36:51 +0000 (20:36 +0100)
committerJérémie Astor <fennecdjay@gmail.com>
Sat, 23 Jan 2021 19:36:51 +0000 (20:36 +0100)
src/lib/prim.c
src/parse/check.c

index 8be89bf91579b9f72192fb1aed31e0c8ae570750..8ccaa31f2a2ed9b5e193a6efcc137ecbba932680 100644 (file)
@@ -161,12 +161,29 @@ static OP_EMIT(opem_int_range) {
   return GW_OK;
 }
 
+#define UNARY_FOLD(name, TYPE, OP)                                    \
+static OP_CHECK(opck_int_##name) {                                    \
+  /*const*/ Exp_Unary *unary = (Exp_Unary*)data;                      \
+  CHECK_NN(opck_unary_meta(env, data))                                \
+  const Type t = env->gwion->type[TYPE];                              \
+  if(!exp_self(unary)->pos.first.line || !is_prim_int(unary->exp))    \
+    return t;                                                         \
+  const m_int num = OP unary->exp->d.prim.d.num;                      \
+  exp_self(unary)->exp_type = ae_exp_primary;                         \
+  exp_self(unary)->d.prim.prim_type = ae_prim_num;                    \
+  exp_self(unary)->d.prim.d.num = num;                                \
+  return t;                                                           \
+}
+
+UNARY_FOLD(negate, et_int, -)
+UNARY_FOLD(cmp, et_int, ~)
 static GWION_IMPORT(int_unary) {
   GWI_BB(gwi_oper_ini(gwi, NULL, "int", "int"))
-  GWI_BB(gwi_oper_add(gwi, opck_unary_meta))
+  GWI_BB(gwi_oper_add(gwi, opck_int_negate))
   GWI_BB(gwi_oper_end(gwi, "-", int_negate))
   CHECK_OP("++", unary, pre_inc)
   CHECK_OP("--", unary, pre_dec)
+  GWI_BB(gwi_oper_add(gwi, opck_int_cmp))
   GWI_BB(gwi_oper_end(gwi,  "~", int_cmp))
   GWI_BB(gwi_oper_ini(gwi, NULL, "int", NULL))
   GWI_BB(gwi_oper_add(gwi, opck_int_range))
index e302bf6dacd23d6f10906015159003d7d87912b1..16184bf595f4c758d242d8c62acb0f6b76004583 100644 (file)
@@ -716,11 +716,11 @@ ANN static Type check_exp_binary(const Env env, const Exp_Binary* bin) {
     bin->rhs->type = bin->lhs->type;
   struct Op_Import opi = { .op=bin->op, .lhs=bin->lhs->type,
     .rhs=bin->rhs->type, .data=(uintptr_t)bin, .pos=exp_self(bin)->pos, .op_type=op_binary };
+  exp_setuse(bin->lhs, 1);
+  exp_setuse(bin->rhs, 1);
   const Type ret = op_check(env, &opi);
   if(!ret && is_auto && exp_self(bin)->exp_type == ae_exp_binary)
     bin->rhs->d.exp_decl.list->self->value->type = env->gwion->type[et_auto];
-  exp_setuse(bin->lhs, 1);
-  exp_setuse(bin->rhs, 1);
   return ret;
 }