From: Jérémie Astor Date: Sat, 23 Jan 2021 19:36:51 +0000 (+0100) Subject: :art: unary folding X-Git-Tag: nightly~1025 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=b3e255787e381dfe22dbc810afc256a57a991b79;p=gwion.git :art: unary folding --- diff --git a/src/lib/prim.c b/src/lib/prim.c index 8be89bf9..8ccaa31f 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -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)) diff --git a/src/parse/check.c b/src/parse/check.c index e302bf6d..16184bf5 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -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; }