From 29eec737c9749ebbea3a29152241118df8bd422f Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Sat, 30 Jul 2022 21:56:31 +0200 Subject: [PATCH] :art: new and decl shenanigans --- src/lib/object_op.c | 23 +++++++++++++++++++++++ src/parse/check.c | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/lib/object_op.c b/src/lib/object_op.c index bf6f3c3c..30c44658 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -40,6 +40,26 @@ static OP_CHECK(opck_object_at) { return bin->rhs->type; } +static OP_CHECK(opck_object_instance) { + Exp_Binary *bin = (Exp_Binary*)data; + Exp rhs = bin->rhs; + if (rhs->exp_type != ae_exp_decl) + return NULL; + if (rhs->d.exp_decl.td->array) + return NULL; + Exp lhs = bin->lhs; + Exp e = exp_self(bin); + Exp_Decl *const decl = &e->d.exp_decl; + e->exp_type = ae_exp_decl; + decl->td = cpy_type_decl(env->gwion->mp, rhs->d.exp_decl.td); + decl->vd = rhs->d.exp_decl.vd; + decl->type = rhs->type; + decl->args = lhs; + free_exp(env->gwion->mp, rhs); + CHECK_ON(check_exp(env, e)); + return e->type; +} + ANN void unset_local(const Emitter emit, void *const l); static OP_EMIT(opem_object_at) { const Exp_Binary *bin = (Exp_Binary *)data; @@ -439,6 +459,9 @@ GWION_IMPORT(object_op) { GWI_BB(gwi_oper_add(gwi, opck_object_at)) GWI_BB(gwi_oper_emi(gwi, opem_object_at)) GWI_BB(gwi_oper_end(gwi, ":=>", NULL)) + GWI_BB(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, "@Compound", NULL)) + GWI_BB(gwi_oper_add(gwi, opck_object_instance)) + GWI_BB(gwi_oper_end(gwi, "=>", NULL)) GWI_BB(gwi_oper_ini(gwi, "Object", "Object", "bool")) GWI_BB(gwi_oper_end(gwi, "==", EqObject)) GWI_BB(gwi_oper_end(gwi, "!=", NeqObject)) diff --git a/src/parse/check.c b/src/parse/check.c index 2fcea6a9..f61233d0 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -919,6 +919,23 @@ ANN static Type check_exp_binary(const Env env, const Exp_Binary *bin) { bin->rhs->exp_type == ae_exp_decl && bin->rhs->d.exp_decl.type == env->gwion->type[et_auto]; if (is_auto) bin->rhs->d.exp_decl.type = bin->lhs->type; + // allow foo => new C to mean new C(foo) + if(bin->op == insert_symbol("=>") && + bin->rhs->exp_type == ae_exp_unary && bin->rhs->d.exp_unary.unary_type == unary_td && + !bin->rhs->d.exp_unary.ctor.td->array && + !bin->rhs->d.exp_unary.ctor.exp) { + Exp lhs = bin->lhs; + Exp rhs = bin->rhs; + Exp e = exp_self(bin); + Exp_Unary *const unary = &e->d.exp_unary; + e->exp_type = ae_exp_unary; + unary->unary_type = unary_td; + unary->op = insert_symbol("new"); + unary->ctor.td = cpy_type_decl(env->gwion->mp, bin->rhs->d.exp_unary.ctor.td); + unary->ctor.exp = lhs; + free_exp(env->gwion->mp, rhs); + return check_exp(env, e); + } CHECK_OO(check_exp(env, bin->rhs)); if (is_auto) { assert(bin->rhs->type == bin->lhs->type); -- 2.43.0