From 46e987da7c37715079bd5ac81237e6aa1f1e7530 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Thu, 17 Mar 2022 17:51:21 +0100 Subject: [PATCH] :art: some clean and static casting --- src/lib/prim.c | 40 +++++++++++++++++++++++++++++++++++----- src/parse/check.c | 6 ++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/lib/prim.c b/src/lib/prim.c index 7a4fce26..c9fc80c0 100644 --- a/src/lib/prim.c +++ b/src/lib/prim.c @@ -286,13 +286,44 @@ static GWION_IMPORT(int) { return GW_OK; } -static OP_CHECK(opck_cast_f2i) { return env->gwion->type[et_int]; } +static OP_CHECK(opck_cast_f2i) { + Exp_Cast *cast = (Exp_Cast*)data; + if(is_prim_float(cast->exp)) { + const m_float f = cast->exp->d.prim.d.fnum; + free_type_decl(env->gwion->mp, cast->td); + free_exp(env->gwion->mp, cast->exp); + Exp e = exp_self(cast); + e->exp_type = ae_exp_primary; + e->d.prim.prim_type = ae_prim_num; + e->d.prim.d.num = f; + } + return env->gwion->type[et_int]; +} -static OP_CHECK(opck_implicit_f2i) { return env->gwion->type[et_error]; } +ANN static void tofloat(Exp e, const m_int i) { + e->exp_type = ae_exp_primary; + e->d.prim.prim_type = ae_prim_float; + e->d.prim.d.fnum = i; +} -static OP_CHECK(opck_cast_i2f) { return env->gwion->type[et_float]; } +static OP_CHECK(opck_cast_i2f) { + Exp_Cast *cast = (Exp_Cast*)data; + if(is_prim_int(cast->exp)) { + const m_int i = cast->exp->d.prim.d.num; + free_type_decl(env->gwion->mp, cast->td); + free_exp(env->gwion->mp, cast->exp); + Exp e = exp_self(cast); + e->exp_type = ae_exp_primary; + e->d.prim.prim_type = ae_prim_float; + e->d.prim.d.fnum = i; + } + return env->gwion->type[et_float]; +} -static OP_CHECK(opck_implicit_i2f) { return env->gwion->type[et_float]; } +static OP_CHECK(opck_implicit_i2f) { + // TODO: same as in cast_i2f + return env->gwion->type[et_float]; +} #define CHECK_FF(op, check, func) _CHECK_OP(op, check, float_##func) #define CHECK_IF(op, check, func) _CHECK_OP(op, check, int_float_##func) @@ -401,7 +432,6 @@ static GWION_IMPORT(floatint) { CHECK_FI("*=>", rassign, r_mul) CHECK_FI("/=>", rassign, r_div) _CHECK_OP("$", cast_f2i, CastF2I) - _CHECK_OP("@implicit", implicit_f2i, CastF2I) GWI_BB(gwi_oper_ini(gwi, "float", "int", "bool")) GWI_BB(gwi_oper_add(gwi, opck_float_int_and)) GWI_BB(gwi_oper_end(gwi, "&&", float_int_and)) diff --git a/src/parse/check.c b/src/parse/check.c index 4b16d1e6..cadf1f17 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1433,8 +1433,10 @@ ANN static inline bool find_handler(const Handler_List handlers, const Symbol xi ANN static inline m_bool check_stmt_try_start(const restrict Env env, const Stmt_Try stmt) { - RET_NSPC(check_stmt(env, stmt->stmt))} ANN static inline m_bool - _check_stmt_try(const restrict Env env, const Stmt_Try stmt) { + RET_NSPC(check_stmt(env, stmt->stmt)) +} + +ANN static inline m_bool _check_stmt_try(const restrict Env env, const Stmt_Try stmt) { CHECK_BB(check_handler_list(env, stmt->handler)); vector_add(&env->scope->effects, 0); const m_bool ret = check_stmt_try_start(env, stmt); -- 2.43.0