Skip to content

Commit

Permalink
customizable func ast
Browse files Browse the repository at this point in the history
  • Loading branch information
gnlow committed Sep 7, 2023
1 parent 791e944 commit 641dd8b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
3 changes: 2 additions & 1 deletion src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -61,7 +62,7 @@ export const expand = (query: Expr) => function*(expr: Expr): Generator<Expr, vo
)
})
})
.with({join: [$a, $b]}, ({a, b}) => {
.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)))
Expand Down
3 changes: 2 additions & 1 deletion src/func/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/func/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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])
.with(
[{literal: $a}, {literal: $b}],
({a, b}) => ({literal: a + b}),
)
.otherwise(() => ({join: [a, b]}))
.otherwise(() => f({join: [a, b]}))
11 changes: 11 additions & 0 deletions src/util/f.ts
Original file line number Diff line number Diff line change
@@ -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}
}
3 changes: 2 additions & 1 deletion test/call.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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"}]},
Expand Down
21 changes: 11 additions & 10 deletions test/expand.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -29,7 +30,7 @@ Deno.test("Expand - Or", () => {
Deno.test("Expand - Join", () => {
assertEquals(
[...expand(
{
f({
join: [
{or: [
{literal: "1"},
Expand All @@ -40,7 +41,7 @@ Deno.test("Expand - Join", () => {
{literal: "4"},
]},
]
}
})
)(any)],
[
{literal: "13"},
Expand All @@ -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(
Expand All @@ -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"},
Expand Down

0 comments on commit 641dd8b

Please sign in to comment.