From 641dd8bae53cf08360a72305238c5afb2de8fd23 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Thu, 7 Sep 2023 21:43:40 +0900 Subject: [PATCH] customizable func ast --- src/Expr.ts | 2 +- src/expand.ts | 3 ++- src/func/call.ts | 3 ++- src/func/join.ts | 3 ++- src/util/f.ts | 11 +++++++++++ test/call.test.ts | 3 ++- test/expand.test.ts | 21 +++++++++++---------- 7 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 src/util/f.ts diff --git a/src/Expr.ts b/src/Expr.ts index d3b96e7..768250a 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -2,10 +2,10 @@ export type Expr = | {ref: string} | {literal: string} | {def: [string, Expr]} - | {join: [Expr, Expr]} | {or: [Expr, Expr]} | {and: [Expr, Expr]} | {symbol: string} | {call: [Expr, Expr]} + | {f: string, args: [Expr, Expr]} export const any = {symbol: "any"} \ No newline at end of file diff --git a/src/expand.ts b/src/expand.ts index be62f54..11f6ede 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -4,6 +4,7 @@ import { call, join } from "./func/mod.ts" import { match, P } from "ts-pattern" import { $ as Iter } from "iteruyo" import { $a, $b } from "util/select.ts" +import { f } from "util/f.ts" export * from "iteruyo" class LazyArray { @@ -61,7 +62,7 @@ export const expand = (query: Expr) => function*(expr: Expr): Generator { + .with(f({join: [$a, $b]}), ({a, b}) => { const aStash = new LazyArray(expand(a)(expr)) const bStash = new LazyArray(expand(b)(expr)) return Iter(fill(x => !aStash.at(x), y => !bStash.at(y))) diff --git a/src/func/call.ts b/src/func/call.ts index a3d0d08..29018c2 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -4,6 +4,7 @@ import { join } from "./join.ts" import { match, P } from "ts-pattern" import { $_, $a, $b } from "util/select.ts" +import { f } from "util/f.ts" export const call = (query: Expr, expr: Expr): Expr => { return match(query) @@ -20,7 +21,7 @@ export const call = (query: Expr, expr: Expr): Expr => { }) .otherwise(() => any) ) - .with({join: [$a, $b]}, ({a, b}) => { + .with(f({join: [$a, $b]}), ({a, b}) => { return join(call(a, expr), call(b, expr)) }) .otherwise(q => q) diff --git a/src/func/join.ts b/src/func/join.ts index bddb7c6..8792092 100644 --- a/src/func/join.ts +++ b/src/func/join.ts @@ -2,6 +2,7 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" import { $a, $b } from "util/select.ts" +import { f } from "util/f.ts" export const join = (a: Expr, b: Expr): Expr => match([a, b]) @@ -9,4 +10,4 @@ export const join = (a: Expr, b: Expr): Expr => [{literal: $a}, {literal: $b}], ({a, b}) => ({literal: a + b}), ) - .otherwise(() => ({join: [a, b]})) \ No newline at end of file + .otherwise(() => f({join: [a, b]})) \ No newline at end of file diff --git a/src/util/f.ts b/src/util/f.ts new file mode 100644 index 0000000..bbafe23 --- /dev/null +++ b/src/util/f.ts @@ -0,0 +1,11 @@ +import { Expr } from "../Expr.ts" + +export const f = + < + T extends string, + Es extends [unknown] | [unknown, unknown] + > + (o: {[k in T]: Es}) => { + const [[f, args]] = Object.entries(o) + return {f, args} as {f: T, args: Es} + } \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 109ac28..292e933 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -1,6 +1,7 @@ import { assertEquals } from "std/assert" import { call } from "../src/mod.ts" +import { f } from "util/f.ts" Deno.test("Call - Ref - And", () => { assertEquals( @@ -40,7 +41,7 @@ Deno.test("Call - Ref - Nested And", () => { Deno.test("Call - Join", () => { assertEquals( - call({join: [{ref: "a"}, {ref: "b"}]}, { + call(f({join: [{ref: "a"}, {ref: "b"}]}), { and: [ {def: ["a", {literal: "hello"}]}, {def: ["b", {literal: "world"}]}, diff --git a/test/expand.test.ts b/test/expand.test.ts index 8c15b0c..9d411ed 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,6 +1,7 @@ import { assertEquals } from "std/assert" import { Expr, expand, $ as Iter, any } from "../src/mod.ts" +import { f } from "util/f.ts" Deno.test("Expand - Literal", () => { assertEquals( @@ -29,7 +30,7 @@ Deno.test("Expand - Or", () => { Deno.test("Expand - Join", () => { assertEquals( [...expand( - { + f({ join: [ {or: [ {literal: "1"}, @@ -40,7 +41,7 @@ Deno.test("Expand - Join", () => { {literal: "4"}, ]}, ] - } + }) )(any)], [ {literal: "13"}, @@ -56,22 +57,22 @@ Deno.test("Expand - Recursion", () => { {or: [ {literal: ""}, {or: [ - {join: [ - {join: + f({join: [ + f({join: [ {literal: "("}, {ref: "pat"}, ] - }, + }), {literal: ")"}, - ]}, - {join: [ + ]}), + f({join: [ {ref: "pat"}, {or: [ {literal: "x"}, {literal: "-"}, ]}, - ]}, + ]}), ]}, ]} assertEquals( @@ -94,12 +95,12 @@ Deno.test("Expand - Recursion", () => { Deno.test("Expand - Join Refs", () => { assertEquals( [...expand( - { + f({ join: [ {ref: "a"}, {ref: "b"}, ] - } + }) )({and: [ {def: ["a", {or: [ {literal: "1"},