From 7b002acda767f478a0c1dfd2ef325556aa86157c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Fri, 22 Apr 2022 11:26:54 +0200 Subject: [PATCH] :art: More Sifting --- include/lang_private.h | 2 +- src/lib/engine.c | 2 +- src/lib/{ctrl.c => sift.c} | 59 +++++++++++++------ .../{tree/ctrl_test.gw => sift/basic_sift.gw} | 0 tests/sift/sift.gw | 7 +++ tests/sift/sift_decl.gw | 3 + 6 files changed, 53 insertions(+), 20 deletions(-) rename src/lib/{ctrl.c => sift.c} (54%) rename tests/{tree/ctrl_test.gw => sift/basic_sift.gw} (100%) create mode 100644 tests/sift/sift.gw create mode 100644 tests/sift/sift_decl.gw diff --git a/include/lang_private.h b/include/lang_private.h index ef049894..61ea2602 100644 --- a/include/lang_private.h +++ b/include/lang_private.h @@ -19,6 +19,6 @@ ANN m_bool import_ref(const Gwi gwi); ANN m_bool import_deep_equal(const Gwi gwi); ANN m_bool import_dict(const Gwi gwi); ANN m_bool import_gack(const Gwi gwi); -ANN m_bool import_ctrl(const Gwi gwi); +ANN m_bool import_sift(const Gwi gwi); ANN m_bool import_locale(const Gwi gwi); #endif diff --git a/src/lib/engine.c b/src/lib/engine.c index 93f3d6c8..49c5afa8 100644 --- a/src/lib/engine.c +++ b/src/lib/engine.c @@ -181,7 +181,7 @@ ANN static m_bool import_core_libs(const Gwi gwi) { GWI_BB(import_dict(gwi)); GWI_BB(import_gack(gwi)); - GWI_BB(import_ctrl(gwi)); + GWI_BB(import_sift(gwi)); GWI_BB(import_locale(gwi)); diff --git a/src/lib/ctrl.c b/src/lib/sift.c similarity index 54% rename from src/lib/ctrl.c rename to src/lib/sift.c index 42006ae1..5a8c5e91 100644 --- a/src/lib/ctrl.c +++ b/src/lib/sift.c @@ -10,37 +10,52 @@ #include "traverse.h" #include "gwi.h" -static OP_CHECK(opck_ctrl) { +static OP_CHECK(opck_sift) { + Exp_Binary *bin = (Exp_Binary*)data; + const Exp lhs = bin->lhs; + Stmt stmt = lhs->d.exp_unary.code; + Stmt fst = mp_vector_at(stmt->d.stmt_flow.body->d.stmt_code.stmt_list, struct Stmt_, 0); + const Symbol chuck = insert_symbol(env->gwion->st, "=>"); + Exp next = new_exp_binary(env->gwion->mp, fst->d.stmt_exp.val, chuck, bin->rhs, bin->rhs->pos); + CHECK_BN(traverse_exp(env, next)); + fst->d.stmt_exp.val = next; + const Exp exp = exp_self(bin); + exp->exp_type = lhs->exp_type; + exp->d.exp_unary.code = lhs->d.exp_unary.code; + exp->type = lhs->type; + exp->d.exp_unary.unary_type = lhs->d.exp_unary.unary_type; + exp->d.exp_unary.op = lhs->d.exp_unary.op; + return NULL; +} +static OP_CHECK(opck_ctrl) { Exp_Binary *bin = (Exp_Binary*)data; MemPool mp = env->gwion->mp; - + if(bin->lhs->exp_type == ae_exp_decl) + ERR_N(bin->lhs->pos, _("can't use declaration to start sift `|>` operator")); const Symbol chuck = insert_symbol(env->gwion->st, "=>"); Exp exp = exp_self(data); - Exp func = cpy_exp(mp, exp); // set operator - -const Exp dot = new_exp_dot(mp, func->d.exp_binary.lhs, insert_symbol(env->gwion->st, "last"), func->pos); -const Exp call = new_exp_call(mp, dot, NULL, func->pos); -func->d.exp_binary.lhs = call; + Exp func = cpy_exp(mp, exp); + const Exp dot = new_exp_dot(mp, func->d.exp_binary.lhs, insert_symbol(env->gwion->st, "last"), func->pos); + const Exp call = new_exp_call(mp, dot, NULL, func->pos); + func->d.exp_binary.lhs = call; func->d.exp_binary.op = chuck; -traverse_exp(env, func); - + traverse_exp(env, func); struct Stmt_ one = { .d = { .stmt_exp = { .val = func }}, .stmt_type = ae_stmt_exp, .pos = func->pos }; Exp samp = new_prim_id(mp, insert_symbol(env->gwion->st, "samp"), func->pos); Exp _now = new_prim_id(mp, insert_symbol(env->gwion->st, "now"), func->pos); Exp time = new_exp_binary(mp, samp, chuck, _now, func->pos); + traverse_exp(env, time); struct Stmt_ two = { .d = { .stmt_exp = { .val = time }}, .stmt_type = ae_stmt_exp, .pos = func->pos }; -traverse_exp(env, time); + free_exp(mp, bin->lhs); + free_exp(mp, bin->rhs); Stmt_List slist = new_mp_vector(mp, sizeof(struct Stmt_), 2); mp_vector_set(slist, struct Stmt_, 0, one); mp_vector_set(slist, struct Stmt_, 1, two); - const Stmt stmt = new_stmt_code(mp, slist, func->pos); - free_exp(mp, bin->lhs); - free_exp(mp, bin->rhs); const Exp cond = new_prim_id(mp, insert_symbol(env->gwion->st, "true"), func->pos); check_exp(env, cond); @@ -50,10 +65,14 @@ traverse_exp(env, time); exp->d.exp_unary.unary_type = unary_code; exp->d.exp_unary.code = loop; exp->d.exp_unary.op = insert_symbol(env->gwion->st, "spork"); - return env->gwion->type[et_shred]; + + return NULL; } -GWION_IMPORT(ctrl) { +GWION_IMPORT(sift) { + GWI_BB(gwi_class_ini(gwi, "Sift", "Shred")); + GWI_BB(gwi_class_end(gwi)); + gwidoc(gwi, "This operator expands too\n" "#!- spork {{\n" "#!- while(true) {{\n" @@ -61,8 +80,12 @@ GWION_IMPORT(ctrl) { "#!- samp => now;\n" "#!- }\n" "#!- }"); - GWI_BB(gwi_oper_ini(gwi, "UGen", "@function", NULL)) - GWI_BB(gwi_oper_add(gwi, opck_ctrl)) - GWI_BB(gwi_oper_end(gwi, "|>", NULL)) + GWI_BB(gwi_oper_ini(gwi, "UGen", "@function", "Sift")); + GWI_BB(gwi_oper_add(gwi, opck_ctrl)); + GWI_BB(gwi_oper_end(gwi, "|>", NULL)); + + GWI_BB(gwi_oper_ini(gwi, "Sift", "@function", "Sift")); + GWI_BB(gwi_oper_add(gwi, opck_sift)); + GWI_BB(gwi_oper_end(gwi, "|>", NULL)); return GW_OK; } diff --git a/tests/tree/ctrl_test.gw b/tests/sift/basic_sift.gw similarity index 100% rename from tests/tree/ctrl_test.gw rename to tests/sift/basic_sift.gw diff --git a/tests/sift/sift.gw b/tests/sift/sift.gw new file mode 100644 index 00000000..eb68cf58 --- /dev/null +++ b/tests/sift/sift.gw @@ -0,0 +1,7 @@ +#! [contains] var: 2.0 + +var Impulse imp ~> blackhole; + +imp |> \a{ return 2.0; } |> \a{ <<< "var: ${a}" >>>; return 2.0; }; + +5::samp => now; diff --git a/tests/sift/sift_decl.gw b/tests/sift/sift_decl.gw new file mode 100644 index 00000000..6b0c2349 --- /dev/null +++ b/tests/sift/sift_decl.gw @@ -0,0 +1,3 @@ +#! [contains] can't use declaration +var Impulse imp |> \a{ return 2.0; }; +5::samp => now; -- 2.43.0