From 93b230b9b509598bc5734095b5d64ba2123c6f33 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Thu, 28 Apr 2022 15:53:20 +0200 Subject: [PATCH] :bug: Fix match --- src/emit/emit.c | 11 ++--------- src/lib/deep_equal.c | 5 ++--- src/parse/check.c | 15 ++++++++++----- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/emit/emit.c b/src/emit/emit.c index 13cd6ad5..7ce2ef40 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -2724,13 +2724,9 @@ ANN static inline m_bool emit_exp1(const Emitter emit, const Exp e) { ANN static m_bool emit_case_head(const Emitter emit, const Exp base, const Exp e, const Symbol op, const Vector v) { CHECK_BB(emit_exp1(emit, base)); -// emit_exp_addref1(emit, base, -exp_size(base)); CHECK_BB(emit_exp1(emit, e)); -// emit_exp_addref1(emit, e, -exp_size(e)); const Exp_Binary bin = {.lhs = base, .rhs = e, .op = op}; - struct Exp_ ebin = { - .d = {.exp_binary = bin}, - }; + struct Exp_ ebin = { .d = {.exp_binary = bin}, .exp_type = ae_exp_binary, .pos = e->pos }; struct Op_Import opi = {.op = op, .lhs = base->type, .rhs = e->type, @@ -2772,7 +2768,6 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, if (!nspc_lookup_value1(emit->env->curr, e->d.prim.d.var)) { if (!n) { CHECK_BO(emit_exp(emit, base)); -// emit_exp_addref(emit, base, -exp_totalsize(base)); regpop(emit, base->type->size); } CHECK_BO(case_value(emit, base, e)); @@ -2810,9 +2805,7 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, regpush(emit, SZ_INT); CHECK_BO(emit_exp(emit, e)); const Exp_Binary bin = {.lhs = base, .rhs = e, .op = insert_symbol("?=")}; - struct Exp_ ebin = { - .d = {.exp_binary = bin}, - }; + struct Exp_ ebin = {.d = {.exp_binary = bin}, .pos = e->pos }; struct Op_Import opi = {.op = insert_symbol("?="), .lhs = base->type, .rhs = e->type, diff --git a/src/lib/deep_equal.c b/src/lib/deep_equal.c index cd379e94..b15510e5 100644 --- a/src/lib/deep_equal.c +++ b/src/lib/deep_equal.c @@ -24,10 +24,10 @@ static OP_##ACTION(op##action##_deep_##_t##_any) { \ }; \ return op_##_name(_data, &opi); \ } + static OP_CHECK(opck_deep_eq_any) { Exp_Binary *bin = data; bin->op = insert_symbol(env->gwion->st, "=="); - exp_self(bin)->exp_type = ae_exp_binary; DECL_ON(const Type, t, = check_exp(env, exp_self(bin))); return t; } @@ -35,7 +35,6 @@ static OP_CHECK(opck_deep_eq_any) { static OP_CHECK(opck_deep_ne_any) { Exp_Binary *bin = data; bin->op = insert_symbol(env->gwion->st, "!="); - exp_self(bin)->exp_type = ae_exp_binary; DECL_ON(const Type, t, = check_exp(env, exp_self(bin))); return t; } @@ -99,6 +98,7 @@ static bool deep_check(const Env env, const Exp_Binary *bin, const m_uint lsz = vector_size(l), rsz = vector_size(r); if(lsz && rsz >= lsz) { +// if(rsz >= lsz) { for(m_uint i = 0; i < lsz; i++) { const Value lval = (Value)vector_at(l, i), rval = (Value)vector_at(r, i); @@ -156,7 +156,6 @@ ANN static void deep_emit_init(const Emitter emit, struct DeepEmit *d, const m_i const Instr instr = emit_add_instr(emit, Reg2Mem); instr->m_val2 = offset; d->val->from->offset = instr->m_val = emit_localn(emit, d->val->type); - d->val->from->loc = d->exp->pos; } ANN static void deep_emit_release(const Emitter emit, struct DeepEmit *d) { diff --git a/src/parse/check.c b/src/parse/check.c index 44aa72b3..4bfef633 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1410,8 +1410,7 @@ ANN static Symbol case_op(const Env env, const Type base, const Exp e) { ANN static m_bool match_case_exp(const Env env, Exp e) { Exp last = e; - for (m_uint i = 0; i < vector_size(&env->scope->match->cond); - e = e->next, ++i) { + for (m_uint i = 0; i < vector_size(&env->scope->match->cond); e = e->next, ++i) { if (!e) ERR_B(last->pos, _("no enough to match")) last = e; const Exp base = (Exp)vector_at(&env->scope->match->cond, i); @@ -1422,14 +1421,20 @@ ANN static m_bool match_case_exp(const Env env, Exp e) { const Type t = check_exp(env, e); e->next = next; CHECK_OB(t); - Exp_Binary bin = {.lhs = base, .rhs = e, .op = op}; - struct Exp_ ebin = {.d = {.exp_binary = bin}, .exp_type = ae_exp_binary}; + Exp_Binary bin = {.lhs = cpy_exp(env->gwion->mp, base), .rhs = cpy_exp(env->gwion->mp, e), .op = op}; + struct Exp_ ebin = {.d = {.exp_binary = bin}, .exp_type = ae_exp_binary, .pos = e->pos }; struct Op_Import opi = {.op = op, .lhs = base->type, .rhs = e->type, .data = (uintptr_t)&ebin.d.exp_binary, .pos = e->pos}; - CHECK_OB(op_check(env, &opi)); + traverse_exp(env, &ebin); + const Type ret = op_check(env, &opi); + if(ebin.exp_type == ae_exp_binary) { + free_exp(env->gwion->mp, bin.lhs); + free_exp(env->gwion->mp, bin.rhs); + } + CHECK_OB(ret); } } if (e) ERR_B(e->pos, _("too many expression to match")) -- 2.43.0