]> Nishi Git Mirror - gwion.git/commitdiff
:art: More Sifting
authorJérémie Astor <fennecdjay@gmail.com>
Fri, 22 Apr 2022 09:26:54 +0000 (11:26 +0200)
committerJérémie Astor <fennecdjay@gmail.com>
Fri, 22 Apr 2022 09:26:54 +0000 (11:26 +0200)
include/lang_private.h
src/lib/engine.c
src/lib/sift.c [moved from src/lib/ctrl.c with 54% similarity]
tests/sift/basic_sift.gw [moved from tests/tree/ctrl_test.gw with 100% similarity]
tests/sift/sift.gw [new file with mode: 0644]
tests/sift/sift_decl.gw [new file with mode: 0644]

index ef0498940a09abcb8d06c4c027c003a89b21c95e..61ea2602955259d6c50224c05e5fb4a260c650c7 100644 (file)
@@ -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
index 93f3d6c8fa5bdf3e6816798ca158ffdf6d566ff6..49c5afa8b2ac5d308f4ae9d734fd8d83ae8bc0b3 100644 (file)
@@ -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));
 
 
similarity index 54%
rename from src/lib/ctrl.c
rename to src/lib/sift.c
index 42006ae1682f768f5e97ce22f87fa802d77218ae..5a8c5e91fe6e106aabceeae21c9aff868a9e8bd3 100644 (file)
 #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/sift/sift.gw b/tests/sift/sift.gw
new file mode 100644 (file)
index 0000000..eb68cf5
--- /dev/null
@@ -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 (file)
index 0000000..6b0c234
--- /dev/null
@@ -0,0 +1,3 @@
+#! [contains] can't use declaration
+var Impulse imp |> \a{ return 2.0; };
+5::samp => now;