From 641dd8bae53cf08360a72305238c5afb2de8fd23 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Thu, 7 Sep 2023 21:43:40 +0900 Subject: [PATCH 01/35] 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"}, From 8ed82b725e8188d10ed6094cab13d7d0d6cf9597 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Fri, 8 Sep 2023 21:34:16 +0900 Subject: [PATCH 02/35] Update deno.yml --- .github/workflows/deno.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/deno.yml b/.github/workflows/deno.yml index 8820aeb..c741868 100644 --- a/.github/workflows/deno.yml +++ b/.github/workflows/deno.yml @@ -11,6 +11,8 @@ name: Deno on: push: branches-ignore: [] + tags: + - '**' pull_request: branches-ignore: [] From 70db8c46d7af7c75fad9342f7a4bc6bc366a960e Mon Sep 17 00:00:00 2001 From: Gnlow Date: Fri, 8 Sep 2023 22:23:25 +0900 Subject: [PATCH 03/35] add: `arrow` (only match literals) --- src/Expr.ts | 1 + src/func/call.ts | 11 ++++++++--- test/call.test.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Expr.ts b/src/Expr.ts index 768250a..3bcc237 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -6,6 +6,7 @@ export type Expr = | {and: [Expr, Expr]} | {symbol: string} | {call: [Expr, Expr]} + | {arrow: [Expr, Expr]} | {f: string, args: [Expr, Expr]} export const any = {symbol: "any"} \ No newline at end of file diff --git a/src/func/call.ts b/src/func/call.ts index 29018c2..b257ab3 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -21,8 +21,13 @@ export const call = (query: Expr, expr: Expr): Expr => { }) .otherwise(() => any) ) - .with(f({join: [$a, $b]}), ({a, b}) => { - return join(call(a, expr), call(b, expr)) - }) + .with({arrow: [{literal: $a}, $b]}, ({a, b}) => + match(expr) + .with({literal: a}, () => b) + .otherwise(() => any) + ) + .with(f({join: [$a, $b]}), ({a, b}) => + join(call(a, expr), call(b, expr)) + ) .otherwise(q => q) } \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 292e933..374d322 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -1,4 +1,7 @@ -import { assertEquals } from "std/assert" +import { + assertEquals, + assertNotEquals, +} from "std/assert" import { call } from "../src/mod.ts" import { f } from "util/f.ts" @@ -49,4 +52,27 @@ Deno.test("Call - Join", () => { }), {literal: "helloworld"}, ) +}) + +Deno.test("Call - Arrow - Match Literal", () => { + assertEquals( + call( + {arrow: [ + {literal: "hello"}, + {literal: "bye"}, + ]}, + {literal: "hello"}, + ), + {literal: "bye"}, + ) + assertNotEquals( + call( + {arrow: [ + {literal: "hello"}, + {literal: "bye"}, + ]}, + {literal: "hell"}, + ), + {literal: "bye"}, + ) }) \ No newline at end of file From e0691886911c94c67fabcd21c82c310615d935fb Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 02:10:15 +0900 Subject: [PATCH 04/35] add: add,sub,mul,div --- src/Expr.ts | 2 +- src/func/call.ts | 23 ++++++++++++++++++++--- src/func/join.ts | 4 ++-- src/func/math.ts | 19 +++++++++++++++++++ src/util/select.ts | 6 +++++- test/call.test.ts | 12 ++++++++++++ 6 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/func/math.ts diff --git a/src/Expr.ts b/src/Expr.ts index 3bcc237..5e6a17f 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -1,6 +1,6 @@ export type Expr = | {ref: string} - | {literal: string} + | {literal: string | number} | {def: [string, Expr]} | {or: [Expr, Expr]} | {and: [Expr, Expr]} diff --git a/src/func/call.ts b/src/func/call.ts index b257ab3..14a3350 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -1,9 +1,15 @@ import { Expr, any } from "../Expr.ts" import { and } from "./and.ts" import { join } from "./join.ts" +import { + add, + sub, + mul, + div, +} from "./math.ts" import { match, P } from "ts-pattern" -import { $_, $a, $b } from "util/select.ts" +import { $, $_, $a, $b } from "util/select.ts" import { f } from "util/f.ts" export const call = (query: Expr, expr: Expr): Expr => { @@ -26,8 +32,19 @@ export const call = (query: Expr, expr: Expr): Expr => { .with({literal: a}, () => b) .otherwise(() => any) ) - .with(f({join: [$a, $b]}), ({a, b}) => - join(call(a, expr), call(b, expr)) + .with( + {f: $("name"), args: $("args")}, + ({name, args}) => ( + { + join, + add, + sub, + mul, + div, + }[name as "join"]( + ...args.map(arg => call(arg, expr)) as typeof args + ) + ) ) .otherwise(q => q) } \ No newline at end of file diff --git a/src/func/join.ts b/src/func/join.ts index 8792092..69febdf 100644 --- a/src/func/join.ts +++ b/src/func/join.ts @@ -1,13 +1,13 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" -import { $a, $b } from "util/select.ts" +import { str$a, str$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}], + [{literal: str$a}, {literal: str$b}], ({a, b}) => ({literal: a + b}), ) .otherwise(() => f({join: [a, b]})) \ No newline at end of file diff --git a/src/func/math.ts b/src/func/math.ts new file mode 100644 index 0000000..8a46a45 --- /dev/null +++ b/src/func/math.ts @@ -0,0 +1,19 @@ +import { Expr } from "../Expr.ts" + +import { match } from "ts-pattern" +import { num$a, num$b } from "util/select.ts" + +const math = + (name: string, func: (a: number, b: number) => number) => + (a: Expr, b: Expr): Expr => + match([a, b]) + .with( + [{literal: num$a}, {literal: num$b}], + ({a, b}) => ({literal: func(a, b)}) + ) + .otherwise(() => ({f: name, args: [a, b]})) + +export const add = math("add", (a, b) => a + b) +export const sub = math("sub", (a, b) => a - b) +export const mul = math("mul", (a, b) => a * b) +export const div = math("div", (a, b) => a / b) \ No newline at end of file diff --git a/src/util/select.ts b/src/util/select.ts index 2dcbbde..41b93c2 100644 --- a/src/util/select.ts +++ b/src/util/select.ts @@ -3,4 +3,8 @@ import { P } from "ts-pattern" export const $ = P.select export const $_ = $() export const $a = $("a") -export const $b = $("b") \ No newline at end of file +export const $b = $("b") +export const str$a = $("a", P.string) +export const str$b = $("b", P.string) +export const num$a = $("a", P.number) +export const num$b = $("b", P.number) \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 374d322..1cc27a7 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -54,6 +54,18 @@ Deno.test("Call - Join", () => { ) }) +Deno.test("Call - Math", () => { + assertEquals( + call(f({mul: [{ref: "a"}, {ref: "b"}]}), { + and: [ + {def: ["a", {literal: 12}]}, + {def: ["b", {literal: 5}]}, + ] + }), + {literal: 60}, + ) +}) + Deno.test("Call - Arrow - Match Literal", () => { assertEquals( call( From 61bbfe3d20bdd3d953365186755529144333ebc8 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 02:19:08 +0900 Subject: [PATCH 05/35] nested defs --- src/func/call.ts | 4 ++++ test/call.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/func/call.ts b/src/func/call.ts index 14a3350..5763565 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -32,6 +32,10 @@ export const call = (query: Expr, expr: Expr): Expr => { .with({literal: a}, () => b) .otherwise(() => any) ) + .with({call: [$a, $b]}, ({a, b}) => call( + call(a, expr), + call(b, expr), + )) .with( {f: $("name"), args: $("args")}, ({name, args}) => ( diff --git a/test/call.test.ts b/test/call.test.ts index 1cc27a7..caa5a2f 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -42,6 +42,30 @@ Deno.test("Call - Ref - Nested And", () => { ) }) +Deno.test("Call - Ref - Nested Complex", () => { + assertEquals( + call({call: [{ref: "area"}, {ref: "square"}]}, + {and: [ + {def: [ + "square", + {and: [ + {def: ["w", {literal: 12}]}, + {def: ["h", {literal: 5}]}, + ]} + ]}, + {def: [ + "area", + f({mul: [ + {ref: "w"}, + {ref: "h"}, + ]}) + ]} + ]} + ), + {literal: 60}, + ) +}) + Deno.test("Call - Join", () => { assertEquals( call(f({join: [{ref: "a"}, {ref: "b"}]}), { From d714ff1f2b49900e72dc3876f72c50e6b1cf283c Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 12:39:32 +0900 Subject: [PATCH 06/35] add: arrow capture (no type check yet) --- src/Expr.ts | 1 + src/func/call.ts | 5 +++++ test/call.test.ts | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Expr.ts b/src/Expr.ts index 5e6a17f..17d409c 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -7,6 +7,7 @@ export type Expr = | {symbol: string} | {call: [Expr, Expr]} | {arrow: [Expr, Expr]} + | {capture: [string, Expr]} | {f: string, args: [Expr, Expr]} export const any = {symbol: "any"} \ No newline at end of file diff --git a/src/func/call.ts b/src/func/call.ts index 5763565..776b6b0 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -32,6 +32,11 @@ export const call = (query: Expr, expr: Expr): Expr => { .with({literal: a}, () => b) .otherwise(() => any) ) + .with({arrow: [{capture: $a}, $b]}, ({a: [name, _type], b}) => // TODO: Type Checking + match(expr) + .with($_, () => call(b, {def: [name, expr]})) + .otherwise(() => any) + ) .with({call: [$a, $b]}, ({a, b}) => call( call(a, expr), call(b, expr), diff --git a/test/call.test.ts b/test/call.test.ts index caa5a2f..1eb4a32 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -3,7 +3,7 @@ import { assertNotEquals, } from "std/assert" -import { call } from "../src/mod.ts" +import { call, any } from "../src/mod.ts" import { f } from "util/f.ts" Deno.test("Call - Ref - And", () => { @@ -111,4 +111,17 @@ Deno.test("Call - Arrow - Match Literal", () => { ), {literal: "bye"}, ) +}) + +Deno.test("Call - Arrow - Capture", () => { + assertEquals( + call( + {arrow: [ + {capture: ["n", any]}, + f({mul: [{ref: "n"}, {literal: 2}]}), + ]}, + {literal: 123}, + ), + {literal: 246}, + ) }) \ No newline at end of file From ab52e58769f88e4326fcf624f08470c366aafb9a Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 12:51:07 +0900 Subject: [PATCH 07/35] seperate file --- test/arrow.test.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ test/call.test.ts | 36 ------------------------------------ 2 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 test/arrow.test.ts diff --git a/test/arrow.test.ts b/test/arrow.test.ts new file mode 100644 index 0000000..a6264d1 --- /dev/null +++ b/test/arrow.test.ts @@ -0,0 +1,43 @@ +import { + assertEquals, + assertNotEquals, +} from "std/assert" + +import { call, any } from "../src/mod.ts" +import { f } from "util/f.ts" + +Deno.test("Arrow - Match Literal", () => { + assertEquals( + call( + {arrow: [ + {literal: "hello"}, + {literal: "bye"}, + ]}, + {literal: "hello"}, + ), + {literal: "bye"}, + ) + assertNotEquals( + call( + {arrow: [ + {literal: "hello"}, + {literal: "bye"}, + ]}, + {literal: "hell"}, + ), + {literal: "bye"}, + ) +}) + +Deno.test("Arrow - Capture", () => { + assertEquals( + call( + {arrow: [ + {capture: ["n", any]}, + f({mul: [{ref: "n"}, {literal: 2}]}), + ]}, + {literal: 123}, + ), + {literal: 246}, + ) +}) \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 1eb4a32..b372de2 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -88,40 +88,4 @@ Deno.test("Call - Math", () => { }), {literal: 60}, ) -}) - -Deno.test("Call - Arrow - Match Literal", () => { - assertEquals( - call( - {arrow: [ - {literal: "hello"}, - {literal: "bye"}, - ]}, - {literal: "hello"}, - ), - {literal: "bye"}, - ) - assertNotEquals( - call( - {arrow: [ - {literal: "hello"}, - {literal: "bye"}, - ]}, - {literal: "hell"}, - ), - {literal: "bye"}, - ) -}) - -Deno.test("Call - Arrow - Capture", () => { - assertEquals( - call( - {arrow: [ - {capture: ["n", any]}, - f({mul: [{ref: "n"}, {literal: 2}]}), - ]}, - {literal: 123}, - ), - {literal: 246}, - ) }) \ No newline at end of file From 456de8beb8dfceb22cf8b34674ec767e9867072b Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 14:17:05 +0900 Subject: [PATCH 08/35] feat: multiple match --- src/func/call.ts | 4 ++++ test/arrow.test.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/func/call.ts b/src/func/call.ts index 776b6b0..ef23361 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -41,6 +41,10 @@ export const call = (query: Expr, expr: Expr): Expr => { call(a, expr), call(b, expr), )) + .with({and: [$a, $b]}, ({a, b}) => and( + call(a, expr), + call(b, expr), + )) .with( {f: $("name"), args: $("args")}, ({name, args}) => ( diff --git a/test/arrow.test.ts b/test/arrow.test.ts index a6264d1..63a38e1 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -29,6 +29,25 @@ Deno.test("Arrow - Match Literal", () => { ) }) +Deno.test("Arrow - Multiple Match", () => { + assertEquals( + call( + {and: [ + {arrow: [ + {literal: "1"}, + {literal: "2"}, + ]}, + {arrow: [ + {literal: "2"}, + {literal: "4"}, + ]}, + ]}, + {literal: "2"}, + ), + {literal: "4"}, + ) +}) + Deno.test("Arrow - Capture", () => { assertEquals( call( From 9cbd9bae87b788b58e947b32a811a6724f0b97d8 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 14:37:20 +0900 Subject: [PATCH 09/35] refactor: `call` match with `expr` --- src/func/call.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/func/call.ts b/src/func/call.ts index ef23361..0120833 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -13,8 +13,8 @@ import { $, $_, $a, $b } from "util/select.ts" import { f } from "util/f.ts" export const call = (query: Expr, expr: Expr): Expr => { - return match(query) - .with({ref: $_}, name => + return match([query, expr]) + .with([{ref: $_}, P.any], name => match(expr) .with({def: [name, $_]}, value => { return value @@ -27,26 +27,26 @@ export const call = (query: Expr, expr: Expr): Expr => { }) .otherwise(() => any) ) - .with({arrow: [{literal: $a}, $b]}, ({a, b}) => + .with([{arrow: [{literal: $a}, $b]}, P.any], ({a, b}) => match(expr) .with({literal: a}, () => b) .otherwise(() => any) ) - .with({arrow: [{capture: $a}, $b]}, ({a: [name, _type], b}) => // TODO: Type Checking + .with([{arrow: [{capture: $a}, $b]}, P.any], ({a: [name, _type], b}) => // TODO: Type Checking match(expr) .with($_, () => call(b, {def: [name, expr]})) .otherwise(() => any) ) - .with({call: [$a, $b]}, ({a, b}) => call( + .with([{call: [$a, $b]}, P.any], ({a, b}) => call( call(a, expr), call(b, expr), )) - .with({and: [$a, $b]}, ({a, b}) => and( + .with([{and: [$a, $b]}, P.any], ({a, b}) => and( call(a, expr), call(b, expr), )) .with( - {f: $("name"), args: $("args")}, + [{f: $("name"), args: $("args")}, P.any], ({name, args}) => ( { join, @@ -59,5 +59,5 @@ export const call = (query: Expr, expr: Expr): Expr => { ) ) ) - .otherwise(q => q) + .otherwise(_ => query) } \ No newline at end of file From 22bd3cb7eee5e361341d19824e6393551850bfcb Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 16:36:19 +0900 Subject: [PATCH 10/35] refactor --- src/func/call.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/func/call.ts b/src/func/call.ts index 0120833..ab7fab7 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -14,29 +14,33 @@ import { f } from "util/f.ts" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) - .with([{ref: $_}, P.any], name => - match(expr) - .with({def: [name, $_]}, value => { - return value - }) - .with({and: [$a, $b]}, ({a, b}) => { - return and( + .with( + [{ref: P.any}, {def: [P.any, $_]}], + ([{ref}, {def: [name, _val]}]) => ref == name, + val => val + ) + .with( + [{ref: $("name")}, {and: [$a, $b]}], + ({name, a, b}) => + and( call({ref: name}, a), call({ref: name}, b), ) - }) - .otherwise(() => any) ) - .with([{arrow: [{literal: $a}, $b]}, P.any], ({a, b}) => - match(expr) - .with({literal: a}, () => b) - .otherwise(() => any) + .with([{ref: P.any}, P.any], () => any) + .with( + [{arrow: [{literal: P.any}, $_]}, {literal: P.any}], + ([{arrow: [{literal: a1}, _]}, {literal: a2}]) => a1 == a2, + val => val ) - .with([{arrow: [{capture: $a}, $b]}, P.any], ({a: [name, _type], b}) => // TODO: Type Checking + .with( + [{arrow: [{capture: $a}, $b]}, P.any], + ({a: [name, _type], b}) => // TODO: Type Checking match(expr) .with($_, () => call(b, {def: [name, expr]})) .otherwise(() => any) ) + .with([{arrow: P.any}, P.any], () => any) .with([{call: [$a, $b]}, P.any], ({a, b}) => call( call(a, expr), call(b, expr), From de8b6315543db9baf408cc33179d9622bfcbe59d Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 9 Sep 2023 16:43:07 +0900 Subject: [PATCH 11/35] feat: automatic lifting for junction --- src/func/call.ts | 5 +++++ src/func/mod.ts | 3 ++- src/func/or.ts | 10 ++++++++++ test/arrow.test.ts | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/func/or.ts diff --git a/src/func/call.ts b/src/func/call.ts index ab7fab7..60c7c57 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -1,5 +1,6 @@ import { Expr, any } from "../Expr.ts" import { and } from "./and.ts" +import { or } from "./or.ts" import { join } from "./join.ts" import { add, @@ -14,6 +15,10 @@ import { f } from "util/f.ts" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) + .with( + [$("q"), {or: [$a, $b]}], + ({a, b, q}) => or(call(q, a), call(q, b)) + ) .with( [{ref: P.any}, {def: [P.any, $_]}], ([{ref}, {def: [name, _val]}]) => ref == name, diff --git a/src/func/mod.ts b/src/func/mod.ts index 92c6283..a639518 100644 --- a/src/func/mod.ts +++ b/src/func/mod.ts @@ -1,3 +1,4 @@ export * from "./and.ts" export * from "./call.ts" -export * from "./join.ts" \ No newline at end of file +export * from "./join.ts" +export * from "./or.ts" \ No newline at end of file diff --git a/src/func/or.ts b/src/func/or.ts new file mode 100644 index 0000000..016ed82 --- /dev/null +++ b/src/func/or.ts @@ -0,0 +1,10 @@ +import { Expr, any } from "../Expr.ts" + +import { match } from "ts-pattern" +import { $_ } from "util/select.ts" + +export const or = (a: Expr, b: Expr): Expr => + match([a, b]) + .with([$_, any], _ => any) + .with([any, $_], _ => any) + .otherwise(_ => ({or: [a, b]})) \ No newline at end of file diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 63a38e1..6670d34 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -59,4 +59,17 @@ Deno.test("Arrow - Capture", () => { ), {literal: 246}, ) +}) + +Deno.test("Arrow - Junction", () => { + assertEquals( + call( + {arrow: [ + {capture: ["n", any]}, + f({mul: [{ref: "n"}, {literal: 2}]}), + ]}, + {or: [{literal: 10}, {literal: 20}]}, + ), + {or: [{literal: 20}, {literal: 40}]}, + ) }) \ No newline at end of file From 4fe3295f927a531d1e3c880a27a5470c7884ff44 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Tue, 12 Sep 2023 01:48:45 +0900 Subject: [PATCH 12/35] export util/f --- src/mod.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mod.ts b/src/mod.ts index 8dcb9c1..c72cad8 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -1,4 +1,6 @@ export * from "./func/mod.ts" export * from "./expand.ts" export * from "./Expr.ts" -export * from "./run.ts" \ No newline at end of file +export * from "./run.ts" + +export * from "./util/f.ts" From 1ee91eb26d2c03fde8dc10d15b544d69776bf33f Mon Sep 17 00:00:00 2001 From: Gnlow Date: Tue, 12 Sep 2023 01:54:17 +0900 Subject: [PATCH 13/35] refactor: `def` as normal binExpr --- src/Expr.ts | 2 +- src/func/call.ts | 6 +++--- test/call.test.ts | 30 +++++++++++++++--------------- test/expand.test.ts | 6 +++--- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Expr.ts b/src/Expr.ts index 17d409c..cca2d2c 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -1,7 +1,7 @@ export type Expr = | {ref: string} | {literal: string | number} - | {def: [string, Expr]} + | {def: [Expr, Expr]} | {or: [Expr, Expr]} | {and: [Expr, Expr]} | {symbol: string} diff --git a/src/func/call.ts b/src/func/call.ts index 60c7c57..2d949b7 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -20,8 +20,8 @@ export const call = (query: Expr, expr: Expr): Expr => { ({a, b, q}) => or(call(q, a), call(q, b)) ) .with( - [{ref: P.any}, {def: [P.any, $_]}], - ([{ref}, {def: [name, _val]}]) => ref == name, + [{ref: P.any}, {def: [{ref: P.any}, $_]}], + ([{ref}, {def: [{ref: name}, _val]}]) => ref == name, val => val ) .with( @@ -42,7 +42,7 @@ export const call = (query: Expr, expr: Expr): Expr => { [{arrow: [{capture: $a}, $b]}, P.any], ({a: [name, _type], b}) => // TODO: Type Checking match(expr) - .with($_, () => call(b, {def: [name, expr]})) + .with($_, () => call(b, {def: [{ref: name}, expr]})) .otherwise(() => any) ) .with([{arrow: P.any}, P.any], () => any) diff --git a/test/call.test.ts b/test/call.test.ts index b372de2..a5436e9 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -10,8 +10,8 @@ Deno.test("Call - Ref - And", () => { assertEquals( call({ref: "a"}, { and: [ - {def: ["a", {literal: "hello"}]}, - {def: ["b", {literal: "world"}]}, + {def: [{ref: "a"}, {literal: "hello"}]}, + {def: [{ref: "b"}, {literal: "world"}]}, ] }), {literal: "hello"}, @@ -19,8 +19,8 @@ Deno.test("Call - Ref - And", () => { assertEquals( call({ref: "b"}, { and: [ - {def: ["a", {literal: "hello"}]}, - {def: ["b", {literal: "world"}]}, + {def: [{ref: "a"}, {literal: "hello"}]}, + {def: [{ref: "b"}, {literal: "world"}]}, ] }), {literal: "world"}, @@ -31,10 +31,10 @@ Deno.test("Call - Ref - Nested And", () => { assertEquals( call({ref: "b"}, { and: [ - {def: ["a", {literal: "hello"}]}, + {def: [{ref: "a"}, {literal: "hello"}]}, {and: [ - {def: ["b", {literal: "world"}]}, - {def: ["c", {literal: "1234"}]}, + {def: [{ref: "b"}, {literal: "world"}]}, + {def: [{ref: "c"}, {literal: "1234"}]}, ]} ] }), @@ -47,14 +47,14 @@ Deno.test("Call - Ref - Nested Complex", () => { call({call: [{ref: "area"}, {ref: "square"}]}, {and: [ {def: [ - "square", + {ref: "square"}, {and: [ - {def: ["w", {literal: 12}]}, - {def: ["h", {literal: 5}]}, + {def: [{ref: "w"}, {literal: 12}]}, + {def: [{ref: "h"}, {literal: 5}]}, ]} ]}, {def: [ - "area", + {ref: "area"}, f({mul: [ {ref: "w"}, {ref: "h"}, @@ -70,8 +70,8 @@ Deno.test("Call - Join", () => { assertEquals( call(f({join: [{ref: "a"}, {ref: "b"}]}), { and: [ - {def: ["a", {literal: "hello"}]}, - {def: ["b", {literal: "world"}]}, + {def: [{ref: "a"}, {literal: "hello"}]}, + {def: [{ref: "b"}, {literal: "world"}]}, ] }), {literal: "helloworld"}, @@ -82,8 +82,8 @@ Deno.test("Call - Math", () => { assertEquals( call(f({mul: [{ref: "a"}, {ref: "b"}]}), { and: [ - {def: ["a", {literal: 12}]}, - {def: ["b", {literal: 5}]}, + {def: [{ref: "a"}, {literal: 12}]}, + {def: [{ref: "b"}, {literal: 5}]}, ] }), {literal: 60}, diff --git a/test/expand.test.ts b/test/expand.test.ts index 9d411ed..f2d04f5 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -76,7 +76,7 @@ Deno.test("Expand - Recursion", () => { ]}, ]} assertEquals( - Iter(expand({ref: "pat"})({def: ["pat", pat]})).take(10).toArray(), + Iter(expand({ref: "pat"})({def: [{ref: "pat"}, pat]})).take(10).toArray(), [ { literal: "" }, { literal: "()" }, @@ -102,11 +102,11 @@ Deno.test("Expand - Join Refs", () => { ] }) )({and: [ - {def: ["a", {or: [ + {def: [{ref: "a"}, {or: [ {literal: "1"}, {literal: "2"}, ]}]}, - {def: ["b", {or: [ + {def: [{ref: "b"}, {or: [ {literal: "3"}, {literal: "4"}, ]}]}, From ab8427cb727ae8cbf718729b2aced05169639ec2 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 02:08:11 +0900 Subject: [PATCH 14/35] feat: more expandable funcs --- src/expand.ts | 12 ++++++------ src/func/mod.ts | 10 +++++++++- test/expand.test.ts | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/expand.ts b/src/expand.ts index 11f6ede..6ce7326 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -1,9 +1,9 @@ import { Expr } from "./Expr.ts" -import { call, join } from "./func/mod.ts" +import { call, expandable } from "./func/mod.ts" import { match, P } from "ts-pattern" import { $ as Iter } from "iteruyo" -import { $a, $b } from "util/select.ts" +import { $, $a, $b } from "util/select.ts" import { f } from "util/f.ts" export * from "iteruyo" @@ -47,11 +47,11 @@ export const expand = (query: Expr) => function*(expr: Expr): Generator function*(expr: Expr): Generator { + .with({f: $("f"), args: [$a, $b]}, ({f, 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))) - .map(([x, y]) => join(aStash.at(x), bStash.at(y))) + .map(([x, y]) => expandable[f](aStash.at(x), bStash.at(y))) }) .otherwise(x => [x]) } diff --git a/src/func/mod.ts b/src/func/mod.ts index a639518..c058865 100644 --- a/src/func/mod.ts +++ b/src/func/mod.ts @@ -1,4 +1,12 @@ export * from "./and.ts" export * from "./call.ts" export * from "./join.ts" -export * from "./or.ts" \ No newline at end of file +export * from "./math.ts" +export * from "./or.ts" + +import { join, or, add, sub, mul, div } from "./mod.ts" + +import { Expr } from "../Expr.ts" + +export const expandable: Record Expr> = + { join, or, add, sub, mul, div } \ No newline at end of file diff --git a/test/expand.test.ts b/test/expand.test.ts index f2d04f5..9a0d4fc 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -118,4 +118,25 @@ Deno.test("Expand - Join Refs", () => { {literal: "24"}, ], ) +}) + +Deno.test("Expand - Recursive Math", () => { + assertEquals( + Iter(expand({ref: "nat"})({def: [ + {ref: "nat"}, + {or: [ + {literal: 1}, + f({add: [ + {ref: "nat"}, + {literal: 1} + ]}) + ]} + + ]})).take(3).toArray(), + [ + {literal: 1}, + {literal: 2}, + {literal: 3}, + ], + ) }) \ No newline at end of file From d2db9eac3a8789b079862b09fbf63761c1f736c5 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 02:21:28 +0900 Subject: [PATCH 15/35] use external importMap --- deno.json | 7 +------ import_map.json | 8 ++++++++ mod.ts | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 import_map.json create mode 100644 mod.ts diff --git a/deno.json b/deno.json index 6152bef..fd963d9 100644 --- a/deno.json +++ b/deno.json @@ -2,10 +2,5 @@ "tasks": { "build_npm": "deno run -A scripts/build_npm.ts" }, - "imports": { - "ts-pattern": "npm:ts-pattern@5.0.5", - "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", - "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", - "util/": "./src/util/" - } + "importMap": "import_map.json" } \ No newline at end of file diff --git a/import_map.json b/import_map.json new file mode 100644 index 0000000..25a736d --- /dev/null +++ b/import_map.json @@ -0,0 +1,8 @@ +{ + "imports": { + "ts-pattern": "npm:ts-pattern@5.0.5", + "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", + "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", + "util/": "./src/util/" + } +} \ No newline at end of file diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..8b096c3 --- /dev/null +++ b/mod.ts @@ -0,0 +1 @@ +export * from "./src/mod.ts" \ No newline at end of file From bdafa9febfa073ae88d62ba2fc92bca165206981 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 02:26:30 +0900 Subject: [PATCH 16/35] wtf --- deno.json | 8 +++++++- import_map.json | 8 -------- 2 files changed, 7 insertions(+), 9 deletions(-) delete mode 100644 import_map.json diff --git a/deno.json b/deno.json index fd963d9..129cc30 100644 --- a/deno.json +++ b/deno.json @@ -2,5 +2,11 @@ "tasks": { "build_npm": "deno run -A scripts/build_npm.ts" }, - "importMap": "import_map.json" + "imports": { + "ts-pattern": "npm:ts-pattern@5.0.5", + "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", + "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", + "util": "./src/util/mod.ts", + "util/": "./src/util/" + } } \ No newline at end of file diff --git a/import_map.json b/import_map.json deleted file mode 100644 index 25a736d..0000000 --- a/import_map.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "imports": { - "ts-pattern": "npm:ts-pattern@5.0.5", - "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", - "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", - "util/": "./src/util/" - } -} \ No newline at end of file From 5651142a60273fedb034467b50fe9d02edd9db6f Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 02:37:45 +0900 Subject: [PATCH 17/35] wtff --- deno.json | 3 +-- src/expand.ts | 3 +-- src/func/and.ts | 2 +- src/func/call.ts | 4 ++-- src/func/join.ts | 4 ++-- src/func/math.ts | 2 +- src/func/or.ts | 2 +- src/run.ts | 2 +- src/util/mod.ts | 2 ++ test/arrow.test.ts | 2 +- test/call.test.ts | 2 +- test/expand.test.ts | 2 +- 12 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 src/util/mod.ts diff --git a/deno.json b/deno.json index 129cc30..e860f70 100644 --- a/deno.json +++ b/deno.json @@ -6,7 +6,6 @@ "ts-pattern": "npm:ts-pattern@5.0.5", "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", - "util": "./src/util/mod.ts", - "util/": "./src/util/" + "$util": "./src/util/mod.ts" } } \ No newline at end of file diff --git a/src/expand.ts b/src/expand.ts index 6ce7326..0c7f6cc 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -3,8 +3,7 @@ import { call, expandable } 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" +import { $, $a, $b, f } from "$util" export * from "iteruyo" class LazyArray { diff --git a/src/func/and.ts b/src/func/and.ts index 5f7e19f..d8e1061 100644 --- a/src/func/and.ts +++ b/src/func/and.ts @@ -1,7 +1,7 @@ import { Expr, any } from "../Expr.ts" import { match } from "ts-pattern" -import { $_ } from "util/select.ts" +import { $_ } from "$util" export const and = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/func/call.ts b/src/func/call.ts index 2d949b7..ac4e230 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -10,8 +10,8 @@ import { } from "./math.ts" import { match, P } from "ts-pattern" -import { $, $_, $a, $b } from "util/select.ts" -import { f } from "util/f.ts" +import { $, $_, $a, $b } from "$util" +import { f } from "$util" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) diff --git a/src/func/join.ts b/src/func/join.ts index 69febdf..c4a0bf7 100644 --- a/src/func/join.ts +++ b/src/func/join.ts @@ -1,8 +1,8 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" -import { str$a, str$b } from "util/select.ts" -import { f } from "util/f.ts" +import { str$a, str$b } from "$util" +import { f } from "$util" export const join = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/func/math.ts b/src/func/math.ts index 8a46a45..a891cd0 100644 --- a/src/func/math.ts +++ b/src/func/math.ts @@ -1,7 +1,7 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" -import { num$a, num$b } from "util/select.ts" +import { num$a, num$b } from "$util" const math = (name: string, func: (a: number, b: number) => number) => diff --git a/src/func/or.ts b/src/func/or.ts index 016ed82..db75167 100644 --- a/src/func/or.ts +++ b/src/func/or.ts @@ -1,7 +1,7 @@ import { Expr, any } from "../Expr.ts" import { match } from "ts-pattern" -import { $_ } from "util/select.ts" +import { $_ } from "$util" export const or = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/run.ts b/src/run.ts index 0e033d1..bb83969 100644 --- a/src/run.ts +++ b/src/run.ts @@ -2,7 +2,7 @@ import { Expr } from "./Expr.ts" import { and, call } from "./func/mod.ts" import { match, P } from "ts-pattern" -import { $, $a, $b } from "util/select.ts" +import { $, $a, $b } from "$util" export const run = (expr: Expr): Expr => { return match(expr) diff --git a/src/util/mod.ts b/src/util/mod.ts new file mode 100644 index 0000000..3088073 --- /dev/null +++ b/src/util/mod.ts @@ -0,0 +1,2 @@ +export * from "./f.ts" +export * from "./select.ts" diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 6670d34..dd4d44d 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -4,7 +4,7 @@ import { } from "std/assert" import { call, any } from "../src/mod.ts" -import { f } from "util/f.ts" +import { f } from "$util" Deno.test("Arrow - Match Literal", () => { assertEquals( diff --git a/test/call.test.ts b/test/call.test.ts index a5436e9..e2cc378 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -4,7 +4,7 @@ import { } from "std/assert" import { call, any } from "../src/mod.ts" -import { f } from "util/f.ts" +import { f } from "$util" Deno.test("Call - Ref - And", () => { assertEquals( diff --git a/test/expand.test.ts b/test/expand.test.ts index 9a0d4fc..44db5b3 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,7 +1,7 @@ import { assertEquals } from "std/assert" import { Expr, expand, $ as Iter, any } from "../src/mod.ts" -import { f } from "util/f.ts" +import { f } from "$util" Deno.test("Expand - Literal", () => { assertEquals( From a9cdf42cf60034b9ef8ad62824be803beb743042 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 13:35:28 +0900 Subject: [PATCH 18/35] whyyyyyyyyyyy --- deno.json | 3 +-- src/expand.ts | 2 +- src/func/and.ts | 2 +- src/func/call.ts | 3 +-- src/func/join.ts | 3 +-- src/func/math.ts | 2 +- src/func/or.ts | 2 +- src/run.ts | 2 +- test/arrow.test.ts | 2 +- test/call.test.ts | 2 +- test/expand.test.ts | 2 +- 11 files changed, 11 insertions(+), 14 deletions(-) diff --git a/deno.json b/deno.json index e860f70..49405d6 100644 --- a/deno.json +++ b/deno.json @@ -5,7 +5,6 @@ "imports": { "ts-pattern": "npm:ts-pattern@5.0.5", "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", - "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts", - "$util": "./src/util/mod.ts" + "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts" } } \ No newline at end of file diff --git a/src/expand.ts b/src/expand.ts index 0c7f6cc..98f3341 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -3,7 +3,7 @@ import { call, expandable } from "./func/mod.ts" import { match, P } from "ts-pattern" import { $ as Iter } from "iteruyo" -import { $, $a, $b, f } from "$util" +import { $, $a, $b, f } from "./util/mod.ts" export * from "iteruyo" class LazyArray { diff --git a/src/func/and.ts b/src/func/and.ts index d8e1061..8e43e7e 100644 --- a/src/func/and.ts +++ b/src/func/and.ts @@ -1,7 +1,7 @@ import { Expr, any } from "../Expr.ts" import { match } from "ts-pattern" -import { $_ } from "$util" +import { $_ } from "../util/mod.ts" export const and = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/func/call.ts b/src/func/call.ts index ac4e230..452cb16 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -10,8 +10,7 @@ import { } from "./math.ts" import { match, P } from "ts-pattern" -import { $, $_, $a, $b } from "$util" -import { f } from "$util" +import { f, $, $_, $a, $b } from "../util/mod.ts" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) diff --git a/src/func/join.ts b/src/func/join.ts index c4a0bf7..c585227 100644 --- a/src/func/join.ts +++ b/src/func/join.ts @@ -1,8 +1,7 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" -import { str$a, str$b } from "$util" -import { f } from "$util" +import { f, str$a, str$b } from "../util/mod.ts" export const join = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/func/math.ts b/src/func/math.ts index a891cd0..ad5ba3d 100644 --- a/src/func/math.ts +++ b/src/func/math.ts @@ -1,7 +1,7 @@ import { Expr } from "../Expr.ts" import { match } from "ts-pattern" -import { num$a, num$b } from "$util" +import { num$a, num$b } from "../util/mod.ts" const math = (name: string, func: (a: number, b: number) => number) => diff --git a/src/func/or.ts b/src/func/or.ts index db75167..b8c5176 100644 --- a/src/func/or.ts +++ b/src/func/or.ts @@ -1,7 +1,7 @@ import { Expr, any } from "../Expr.ts" import { match } from "ts-pattern" -import { $_ } from "$util" +import { $_ } from "../util/mod.ts" export const or = (a: Expr, b: Expr): Expr => match([a, b]) diff --git a/src/run.ts b/src/run.ts index bb83969..5ade280 100644 --- a/src/run.ts +++ b/src/run.ts @@ -2,7 +2,7 @@ import { Expr } from "./Expr.ts" import { and, call } from "./func/mod.ts" import { match, P } from "ts-pattern" -import { $, $a, $b } from "$util" +import { $, $a, $b } from "./util/mod.ts" export const run = (expr: Expr): Expr => { return match(expr) diff --git a/test/arrow.test.ts b/test/arrow.test.ts index dd4d44d..2ef79b9 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -4,7 +4,7 @@ import { } from "std/assert" import { call, any } from "../src/mod.ts" -import { f } from "$util" +import { f } from "../src/util/mod.ts" Deno.test("Arrow - Match Literal", () => { assertEquals( diff --git a/test/call.test.ts b/test/call.test.ts index e2cc378..0b5f229 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -4,7 +4,7 @@ import { } from "std/assert" import { call, any } from "../src/mod.ts" -import { f } from "$util" +import { f } from "../src/util/mod.ts" Deno.test("Call - Ref - And", () => { assertEquals( diff --git a/test/expand.test.ts b/test/expand.test.ts index 44db5b3..b7ec3a6 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,7 +1,7 @@ import { assertEquals } from "std/assert" import { Expr, expand, $ as Iter, any } from "../src/mod.ts" -import { f } from "$util" +import { f } from "../src/util/mod.ts" Deno.test("Expand - Literal", () => { assertEquals( From 3b60ed936a94bcf1c6ba4e7b1c75f0b0f8f77fa1 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 17 Sep 2023 13:49:50 +0900 Subject: [PATCH 19/35] delete importMap --- deno.json | 5 ----- deps.ts | 9 +++++++++ src/expand.ts | 6 +++--- src/func/and.ts | 2 +- src/func/call.ts | 4 ++-- src/func/join.ts | 2 +- src/func/math.ts | 2 +- src/func/or.ts | 2 +- src/run.ts | 2 +- src/util/select.ts | 2 +- test/arrow.test.ts | 2 +- test/call.test.ts | 2 +- test/expand.test.ts | 4 ++-- 13 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 deps.ts diff --git a/deno.json b/deno.json index 49405d6..c0d485e 100644 --- a/deno.json +++ b/deno.json @@ -1,10 +1,5 @@ { "tasks": { "build_npm": "deno run -A scripts/build_npm.ts" - }, - "imports": { - "ts-pattern": "npm:ts-pattern@5.0.5", - "iteruyo": "https://deno.land/x/iteruyo@v0.3.0/mod.ts", - "std/assert": "https://deno.land/std@0.201.0/assert/mod.ts" } } \ No newline at end of file diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..2ba8fb3 --- /dev/null +++ b/deps.ts @@ -0,0 +1,9 @@ +export { + match, + P, +} from "npm:ts-pattern@5.0.5" +export { $ as Iter } from "https://deno.land/x/iteruyo@v0.3.0/mod.ts" +export { + assertEquals, + assertNotEquals, +} from "https://deno.land/std@0.201.0/assert/mod.ts" \ No newline at end of file diff --git a/src/expand.ts b/src/expand.ts index 98f3341..38ac9fe 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -1,10 +1,10 @@ import { Expr } from "./Expr.ts" import { call, expandable } from "./func/mod.ts" -import { match, P } from "ts-pattern" -import { $ as Iter } from "iteruyo" +import { match, P } from "../deps.ts" +import { Iter } from "../deps.ts" import { $, $a, $b, f } from "./util/mod.ts" -export * from "iteruyo" +export * from "../deps.ts" class LazyArray { memory: T[] = [] diff --git a/src/func/and.ts b/src/func/and.ts index 8e43e7e..88521af 100644 --- a/src/func/and.ts +++ b/src/func/and.ts @@ -1,6 +1,6 @@ import { Expr, any } from "../Expr.ts" -import { match } from "ts-pattern" +import { match } from "../../deps.ts" import { $_ } from "../util/mod.ts" export const and = (a: Expr, b: Expr): Expr => diff --git a/src/func/call.ts b/src/func/call.ts index 452cb16..e682363 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -9,8 +9,8 @@ import { div, } from "./math.ts" -import { match, P } from "ts-pattern" -import { f, $, $_, $a, $b } from "../util/mod.ts" +import { match, P } from "../../deps.ts" +import { $, $_, $a, $b } from "../util/mod.ts" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) diff --git a/src/func/join.ts b/src/func/join.ts index c585227..b519589 100644 --- a/src/func/join.ts +++ b/src/func/join.ts @@ -1,6 +1,6 @@ import { Expr } from "../Expr.ts" -import { match } from "ts-pattern" +import { match } from "../../deps.ts" import { f, str$a, str$b } from "../util/mod.ts" export const join = (a: Expr, b: Expr): Expr => diff --git a/src/func/math.ts b/src/func/math.ts index ad5ba3d..d9b6488 100644 --- a/src/func/math.ts +++ b/src/func/math.ts @@ -1,6 +1,6 @@ import { Expr } from "../Expr.ts" -import { match } from "ts-pattern" +import { match } from "../../deps.ts" import { num$a, num$b } from "../util/mod.ts" const math = diff --git a/src/func/or.ts b/src/func/or.ts index b8c5176..fd15780 100644 --- a/src/func/or.ts +++ b/src/func/or.ts @@ -1,6 +1,6 @@ import { Expr, any } from "../Expr.ts" -import { match } from "ts-pattern" +import { match } from "../../deps.ts" import { $_ } from "../util/mod.ts" export const or = (a: Expr, b: Expr): Expr => diff --git a/src/run.ts b/src/run.ts index 5ade280..c698fa9 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,7 +1,7 @@ import { Expr } from "./Expr.ts" import { and, call } from "./func/mod.ts" -import { match, P } from "ts-pattern" +import { match, P } from "../deps.ts" import { $, $a, $b } from "./util/mod.ts" export const run = (expr: Expr): Expr => { diff --git a/src/util/select.ts b/src/util/select.ts index 41b93c2..32b1610 100644 --- a/src/util/select.ts +++ b/src/util/select.ts @@ -1,4 +1,4 @@ -import { P } from "ts-pattern" +import { P } from "../../deps.ts" export const $ = P.select export const $_ = $() diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 2ef79b9..1ad838c 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertNotEquals, -} from "std/assert" +} from "../deps.ts" import { call, any } from "../src/mod.ts" import { f } from "../src/util/mod.ts" diff --git a/test/call.test.ts b/test/call.test.ts index 0b5f229..406153f 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertNotEquals, -} from "std/assert" +} from "../deps.ts" import { call, any } from "../src/mod.ts" import { f } from "../src/util/mod.ts" diff --git a/test/expand.test.ts b/test/expand.test.ts index b7ec3a6..fddd589 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,6 +1,6 @@ -import { assertEquals } from "std/assert" +import { assertEquals } from "../deps.ts" -import { Expr, expand, $ as Iter, any } from "../src/mod.ts" +import { Expr, expand, Iter, any } from "../src/mod.ts" import { f } from "../src/util/mod.ts" Deno.test("Expand - Literal", () => { From 8d683aa0e26f5defd0b5229ef8875bdcaf844fdf Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 16:55:55 +0900 Subject: [PATCH 20/35] ast helper function for testing --- src/func/literal.ts | 3 ++ src/func/mod.ts | 1 + test/arrow.test.ts | 47 ++++++++++++++++------------- test/call.test.ts | 45 +++++++++++++++------------- test/expand.test.ts | 73 +++++++++++++++++++++++++-------------------- 5 files changed, 95 insertions(+), 74 deletions(-) create mode 100644 src/func/literal.ts diff --git a/src/func/literal.ts b/src/func/literal.ts new file mode 100644 index 0000000..cc48747 --- /dev/null +++ b/src/func/literal.ts @@ -0,0 +1,3 @@ +export const literal = + (value: string | number) => + ({literal: value}) \ No newline at end of file diff --git a/src/func/mod.ts b/src/func/mod.ts index c058865..b206271 100644 --- a/src/func/mod.ts +++ b/src/func/mod.ts @@ -1,6 +1,7 @@ export * from "./and.ts" export * from "./call.ts" export * from "./join.ts" +export * from "./literal.ts" export * from "./math.ts" export * from "./or.ts" diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 1ad838c..61ff243 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -3,29 +3,34 @@ import { assertNotEquals, } from "../deps.ts" -import { call, any } from "../src/mod.ts" +import { + call, + any, + + literal, +} from "../src/mod.ts" import { f } from "../src/util/mod.ts" Deno.test("Arrow - Match Literal", () => { assertEquals( call( {arrow: [ - {literal: "hello"}, - {literal: "bye"}, + literal("hello"), + literal("bye"), ]}, - {literal: "hello"}, + literal("hello"), ), - {literal: "bye"}, + literal("bye"), ) assertNotEquals( call( {arrow: [ - {literal: "hello"}, - {literal: "bye"}, + literal("hello"), + literal("bye"), ]}, - {literal: "hell"}, + literal("hell"), ), - {literal: "bye"}, + literal("bye"), ) }) @@ -34,17 +39,17 @@ Deno.test("Arrow - Multiple Match", () => { call( {and: [ {arrow: [ - {literal: "1"}, - {literal: "2"}, + literal("1"), + literal("2"), ]}, {arrow: [ - {literal: "2"}, - {literal: "4"}, + literal("2"), + literal("4"), ]}, ]}, - {literal: "2"}, + literal("2"), ), - {literal: "4"}, + literal("4"), ) }) @@ -53,11 +58,11 @@ Deno.test("Arrow - Capture", () => { call( {arrow: [ {capture: ["n", any]}, - f({mul: [{ref: "n"}, {literal: 2}]}), + f({mul: [{ref: "n"}, literal(2)]}), ]}, - {literal: 123}, + literal(123), ), - {literal: 246}, + literal(246), ) }) @@ -66,10 +71,10 @@ Deno.test("Arrow - Junction", () => { call( {arrow: [ {capture: ["n", any]}, - f({mul: [{ref: "n"}, {literal: 2}]}), + f({mul: [{ref: "n"}, literal(2)]}), ]}, - {or: [{literal: 10}, {literal: 20}]}, + {or: [literal(10), literal(20)]}, ), - {or: [{literal: 20}, {literal: 40}]}, + {or: [literal(20), literal(40)]}, ) }) \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 406153f..f6ecc37 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -3,27 +3,32 @@ import { assertNotEquals, } from "../deps.ts" -import { call, any } from "../src/mod.ts" +import { + call, + any, + + literal, +} from "../src/mod.ts" import { f } from "../src/util/mod.ts" Deno.test("Call - Ref - And", () => { assertEquals( call({ref: "a"}, { and: [ - {def: [{ref: "a"}, {literal: "hello"}]}, - {def: [{ref: "b"}, {literal: "world"}]}, + {def: [{ref: "a"}, literal("hello")]}, + {def: [{ref: "b"}, literal("world")]}, ] }), - {literal: "hello"}, + literal("hello"), ) assertEquals( call({ref: "b"}, { and: [ - {def: [{ref: "a"}, {literal: "hello"}]}, - {def: [{ref: "b"}, {literal: "world"}]}, + {def: [{ref: "a"}, literal("hello")]}, + {def: [{ref: "b"}, literal("world")]}, ] }), - {literal: "world"}, + literal("world"), ) }) @@ -31,14 +36,14 @@ Deno.test("Call - Ref - Nested And", () => { assertEquals( call({ref: "b"}, { and: [ - {def: [{ref: "a"}, {literal: "hello"}]}, + {def: [{ref: "a"}, literal("hello")]}, {and: [ - {def: [{ref: "b"}, {literal: "world"}]}, - {def: [{ref: "c"}, {literal: "1234"}]}, + {def: [{ref: "b"}, literal("world")]}, + {def: [{ref: "c"}, literal("1234")]}, ]} ] }), - {literal: "world"}, + literal("world"), ) }) @@ -49,8 +54,8 @@ Deno.test("Call - Ref - Nested Complex", () => { {def: [ {ref: "square"}, {and: [ - {def: [{ref: "w"}, {literal: 12}]}, - {def: [{ref: "h"}, {literal: 5}]}, + {def: [{ref: "w"}, literal(12)]}, + {def: [{ref: "h"}, literal(5)]}, ]} ]}, {def: [ @@ -62,7 +67,7 @@ Deno.test("Call - Ref - Nested Complex", () => { ]} ]} ), - {literal: 60}, + literal(60), ) }) @@ -70,11 +75,11 @@ Deno.test("Call - Join", () => { assertEquals( call(f({join: [{ref: "a"}, {ref: "b"}]}), { and: [ - {def: [{ref: "a"}, {literal: "hello"}]}, - {def: [{ref: "b"}, {literal: "world"}]}, + {def: [{ref: "a"}, literal("hello")]}, + {def: [{ref: "b"}, literal("world")]}, ] }), - {literal: "helloworld"}, + literal("helloworld"), ) }) @@ -82,10 +87,10 @@ Deno.test("Call - Math", () => { assertEquals( call(f({mul: [{ref: "a"}, {ref: "b"}]}), { and: [ - {def: [{ref: "a"}, {literal: 12}]}, - {def: [{ref: "b"}, {literal: 5}]}, + {def: [{ref: "a"}, literal(12)]}, + {def: [{ref: "b"}, literal(5)]}, ] }), - {literal: 60}, + literal(60), ) }) \ No newline at end of file diff --git a/test/expand.test.ts b/test/expand.test.ts index fddd589..dbee1bf 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,14 +1,21 @@ import { assertEquals } from "../deps.ts" -import { Expr, expand, Iter, any } from "../src/mod.ts" +import { + Expr, + expand, + Iter, + any, + literal, + or, +} from "../src/mod.ts" import { f } from "../src/util/mod.ts" Deno.test("Expand - Literal", () => { assertEquals( [...expand( - {literal: "a"} + literal("a") )(any)], - [{literal: "a"}], + [literal("a")], ) }) @@ -16,13 +23,13 @@ Deno.test("Expand - Or", () => { assertEquals( [...expand( {or: [ - {literal: "a"}, - {literal: "b"}, + literal("a"), + literal("b"), ]} )(any)], [ - {literal: "a"}, - {literal: "b"}, + literal("a"), + literal("b"), ], ) }) @@ -33,21 +40,21 @@ Deno.test("Expand - Join", () => { f({ join: [ {or: [ - {literal: "1"}, - {literal: "2"}, + literal("1"), + literal("2"), ]}, {or: [ - {literal: "3"}, - {literal: "4"}, + literal("3"), + literal("4"), ]}, ] }) )(any)], [ - {literal: "13"}, - {literal: "23"}, - {literal: "14"}, - {literal: "24"}, + literal("13"), + literal("23"), + literal("14"), + literal("24"), ], ) }) @@ -55,22 +62,22 @@ Deno.test("Expand - Join", () => { Deno.test("Expand - Recursion", () => { const pat: Expr = {or: [ - {literal: ""}, + literal(""), {or: [ f({join: [ f({join: [ - {literal: "("}, + literal("("), {ref: "pat"}, ] }), - {literal: ")"}, + literal(")"), ]}), f({join: [ {ref: "pat"}, {or: [ - {literal: "x"}, - {literal: "-"}, + literal("x"), + literal("-"), ]}, ]}), ]}, @@ -103,19 +110,19 @@ Deno.test("Expand - Join Refs", () => { }) )({and: [ {def: [{ref: "a"}, {or: [ - {literal: "1"}, - {literal: "2"}, + literal("1"), + literal("2"), ]}]}, {def: [{ref: "b"}, {or: [ - {literal: "3"}, - {literal: "4"}, + literal("3"), + literal("4"), ]}]}, ]})], [ - {literal: "13"}, - {literal: "23"}, - {literal: "14"}, - {literal: "24"}, + literal("13"), + literal("23"), + literal("14"), + literal("24"), ], ) }) @@ -125,18 +132,18 @@ Deno.test("Expand - Recursive Math", () => { Iter(expand({ref: "nat"})({def: [ {ref: "nat"}, {or: [ - {literal: 1}, + literal(1), f({add: [ {ref: "nat"}, - {literal: 1} + literal(1) ]}) ]} ]})).take(3).toArray(), [ - {literal: 1}, - {literal: 2}, - {literal: 3}, + literal(1), + literal(2), + literal(3), ], ) }) \ No newline at end of file From 872a752b4109842329218f10677cd4354de06237 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 16:59:43 +0900 Subject: [PATCH 21/35] ast helper - `or` --- test/arrow.test.ts | 5 +++-- test/expand.test.ts | 36 ++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 61ff243..5ca092e 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -8,6 +8,7 @@ import { any, literal, + or, } from "../src/mod.ts" import { f } from "../src/util/mod.ts" @@ -73,8 +74,8 @@ Deno.test("Arrow - Junction", () => { {capture: ["n", any]}, f({mul: [{ref: "n"}, literal(2)]}), ]}, - {or: [literal(10), literal(20)]}, + or(literal(10), literal(20)), ), - {or: [literal(20), literal(40)]}, + or(literal(20), literal(40)), ) }) \ No newline at end of file diff --git a/test/expand.test.ts b/test/expand.test.ts index dbee1bf..af1ba2e 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -22,10 +22,10 @@ Deno.test("Expand - Literal", () => { Deno.test("Expand - Or", () => { assertEquals( [...expand( - {or: [ + or( literal("a"), literal("b"), - ]} + ) )(any)], [ literal("a"), @@ -39,14 +39,14 @@ Deno.test("Expand - Join", () => { [...expand( f({ join: [ - {or: [ + or( literal("1"), literal("2"), - ]}, - {or: [ + ), + or( literal("3"), literal("4"), - ]}, + ), ] }) )(any)], @@ -61,9 +61,9 @@ Deno.test("Expand - Join", () => { Deno.test("Expand - Recursion", () => { const pat: Expr = - {or: [ + or( literal(""), - {or: [ + or( f({join: [ f({join: [ @@ -75,13 +75,13 @@ Deno.test("Expand - Recursion", () => { ]}), f({join: [ {ref: "pat"}, - {or: [ + or( literal("x"), literal("-"), - ]}, + ), ]}), - ]}, - ]} + ), + ) assertEquals( Iter(expand({ref: "pat"})({def: [{ref: "pat"}, pat]})).take(10).toArray(), [ @@ -109,14 +109,14 @@ Deno.test("Expand - Join Refs", () => { ] }) )({and: [ - {def: [{ref: "a"}, {or: [ + {def: [{ref: "a"}, or( literal("1"), literal("2"), - ]}]}, - {def: [{ref: "b"}, {or: [ + )]}, + {def: [{ref: "b"}, or( literal("3"), literal("4"), - ]}]}, + )]}, ]})], [ literal("13"), @@ -131,13 +131,13 @@ Deno.test("Expand - Recursive Math", () => { assertEquals( Iter(expand({ref: "nat"})({def: [ {ref: "nat"}, - {or: [ + or( literal(1), f({add: [ {ref: "nat"}, literal(1) ]}) - ]} + ) ]})).take(3).toArray(), [ From f3c8376e9dacef8f5d4c66cf74a35e5edb9a8f37 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:03:00 +0900 Subject: [PATCH 22/35] ast helper - `join` --- test/call.test.ts | 3 ++- test/expand.test.ts | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/call.test.ts b/test/call.test.ts index f6ecc37..d7fb623 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -7,6 +7,7 @@ import { call, any, + join, literal, } from "../src/mod.ts" import { f } from "../src/util/mod.ts" @@ -73,7 +74,7 @@ Deno.test("Call - Ref - Nested Complex", () => { Deno.test("Call - Join", () => { assertEquals( - call(f({join: [{ref: "a"}, {ref: "b"}]}), { + call(join({ref: "a"}, {ref: "b"}), { and: [ {def: [{ref: "a"}, literal("hello")]}, {def: [{ref: "b"}, literal("world")]}, diff --git a/test/expand.test.ts b/test/expand.test.ts index af1ba2e..1430664 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -5,6 +5,8 @@ import { expand, Iter, any, + + join, literal, or, } from "../src/mod.ts" @@ -64,22 +66,20 @@ Deno.test("Expand - Recursion", () => { or( literal(""), or( - f({join: [ - f({join: - [ - literal("("), - {ref: "pat"}, - ] - }), + join( + join( + literal("("), + {ref: "pat"}, + ), literal(")"), - ]}), - f({join: [ + ), + join( {ref: "pat"}, or( literal("x"), literal("-"), ), - ]}), + ), ), ) assertEquals( From fc35f9457645145cbe946389b2bf9b2113a9c848 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:04:08 +0900 Subject: [PATCH 23/35] ast helper - `join` (2) --- test/expand.test.ts | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/test/expand.test.ts b/test/expand.test.ts index 1430664..cd1b6f9 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -39,18 +39,16 @@ Deno.test("Expand - Or", () => { Deno.test("Expand - Join", () => { assertEquals( [...expand( - f({ - join: [ - or( - literal("1"), - literal("2"), - ), - or( - literal("3"), - literal("4"), - ), - ] - }) + join( + or( + literal("1"), + literal("2"), + ), + or( + literal("3"), + literal("4"), + ), + ) )(any)], [ literal("13"), @@ -102,12 +100,10 @@ Deno.test("Expand - Recursion", () => { Deno.test("Expand - Join Refs", () => { assertEquals( [...expand( - f({ - join: [ - {ref: "a"}, - {ref: "b"}, - ] - }) + join( + {ref: "a"}, + {ref: "b"}, + ) )({and: [ {def: [{ref: "a"}, or( literal("1"), From 1ef1ceeae2de888bad4243820fc444e3cebf5e04 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:08:59 +0900 Subject: [PATCH 24/35] ast helper - `ref` --- src/func/basic.ts | 13 ++++++++++++ src/func/literal.ts | 3 --- src/func/mod.ts | 2 +- test/arrow.test.ts | 6 ++++-- test/call.test.ts | 48 +++++++++++++++++++++++---------------------- test/expand.test.ts | 22 +++++++++++---------- 6 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 src/func/basic.ts delete mode 100644 src/func/literal.ts diff --git a/src/func/basic.ts b/src/func/basic.ts new file mode 100644 index 0000000..9a19b42 --- /dev/null +++ b/src/func/basic.ts @@ -0,0 +1,13 @@ +import { Expr } from "../Expr.ts"; + +export const literal = + (value: string | number) => + ({literal: value}) + +export const ref = + (name: string) => + ({ref: name}) + +export const def = + (name: string, expr: Expr) => + ({def: [name, expr]}) \ No newline at end of file diff --git a/src/func/literal.ts b/src/func/literal.ts deleted file mode 100644 index cc48747..0000000 --- a/src/func/literal.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const literal = - (value: string | number) => - ({literal: value}) \ No newline at end of file diff --git a/src/func/mod.ts b/src/func/mod.ts index b206271..88b464e 100644 --- a/src/func/mod.ts +++ b/src/func/mod.ts @@ -1,7 +1,7 @@ export * from "./and.ts" export * from "./call.ts" export * from "./join.ts" -export * from "./literal.ts" +export * from "./basic.ts" export * from "./math.ts" export * from "./or.ts" diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 5ca092e..ed5ff49 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -7,6 +7,8 @@ import { call, any, + ref, + def, literal, or, } from "../src/mod.ts" @@ -59,7 +61,7 @@ Deno.test("Arrow - Capture", () => { call( {arrow: [ {capture: ["n", any]}, - f({mul: [{ref: "n"}, literal(2)]}), + f({mul: [ref("n"), literal(2)]}), ]}, literal(123), ), @@ -72,7 +74,7 @@ Deno.test("Arrow - Junction", () => { call( {arrow: [ {capture: ["n", any]}, - f({mul: [{ref: "n"}, literal(2)]}), + f({mul: [ref("n"), literal(2)]}), ]}, or(literal(10), literal(20)), ), diff --git a/test/call.test.ts b/test/call.test.ts index d7fb623..9e58a5f 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -7,6 +7,8 @@ import { call, any, + ref, + def, join, literal, } from "../src/mod.ts" @@ -14,19 +16,19 @@ import { f } from "../src/util/mod.ts" Deno.test("Call - Ref - And", () => { assertEquals( - call({ref: "a"}, { + call(ref("a"), { and: [ - {def: [{ref: "a"}, literal("hello")]}, - {def: [{ref: "b"}, literal("world")]}, + {def: [ref("a"), literal("hello")]}, + {def: [ref("b"), literal("world")]}, ] }), literal("hello"), ) assertEquals( - call({ref: "b"}, { + call(ref("b"), { and: [ - {def: [{ref: "a"}, literal("hello")]}, - {def: [{ref: "b"}, literal("world")]}, + {def: [ref("a"), literal("hello")]}, + {def: [ref("b"), literal("world")]}, ] }), literal("world"), @@ -35,12 +37,12 @@ Deno.test("Call - Ref - And", () => { Deno.test("Call - Ref - Nested And", () => { assertEquals( - call({ref: "b"}, { + call(ref("b"), { and: [ - {def: [{ref: "a"}, literal("hello")]}, + {def: [ref("a"), literal("hello")]}, {and: [ - {def: [{ref: "b"}, literal("world")]}, - {def: [{ref: "c"}, literal("1234")]}, + {def: [ref("b"), literal("world")]}, + {def: [ref("c"), literal("1234")]}, ]} ] }), @@ -50,20 +52,20 @@ Deno.test("Call - Ref - Nested And", () => { Deno.test("Call - Ref - Nested Complex", () => { assertEquals( - call({call: [{ref: "area"}, {ref: "square"}]}, + call({call: [ref("area"), ref("square")]}, {and: [ {def: [ - {ref: "square"}, + ref("square"), {and: [ - {def: [{ref: "w"}, literal(12)]}, - {def: [{ref: "h"}, literal(5)]}, + {def: [ref("w"), literal(12)]}, + {def: [ref("h"), literal(5)]}, ]} ]}, {def: [ - {ref: "area"}, + ref("area"), f({mul: [ - {ref: "w"}, - {ref: "h"}, + ref("w"), + ref("h"), ]}) ]} ]} @@ -74,10 +76,10 @@ Deno.test("Call - Ref - Nested Complex", () => { Deno.test("Call - Join", () => { assertEquals( - call(join({ref: "a"}, {ref: "b"}), { + call(join(ref("a"), ref("b")), { and: [ - {def: [{ref: "a"}, literal("hello")]}, - {def: [{ref: "b"}, literal("world")]}, + {def: [ref("a"), literal("hello")]}, + {def: [ref("b"), literal("world")]}, ] }), literal("helloworld"), @@ -86,10 +88,10 @@ Deno.test("Call - Join", () => { Deno.test("Call - Math", () => { assertEquals( - call(f({mul: [{ref: "a"}, {ref: "b"}]}), { + call(f({mul: [ref("a"), ref("b")]}), { and: [ - {def: [{ref: "a"}, literal(12)]}, - {def: [{ref: "b"}, literal(5)]}, + {def: [ref("a"), literal(12)]}, + {def: [ref("b"), literal(5)]}, ] }), literal(60), diff --git a/test/expand.test.ts b/test/expand.test.ts index cd1b6f9..46ceeab 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -9,6 +9,8 @@ import { join, literal, or, + ref, + def, } from "../src/mod.ts" import { f } from "../src/util/mod.ts" @@ -67,12 +69,12 @@ Deno.test("Expand - Recursion", () => { join( join( literal("("), - {ref: "pat"}, + ref("pat"), ), literal(")"), ), join( - {ref: "pat"}, + ref("pat"), or( literal("x"), literal("-"), @@ -81,7 +83,7 @@ Deno.test("Expand - Recursion", () => { ), ) assertEquals( - Iter(expand({ref: "pat"})({def: [{ref: "pat"}, pat]})).take(10).toArray(), + Iter(expand(ref("pat"))({def: [ref("pat"), pat]})).take(10).toArray(), [ { literal: "" }, { literal: "()" }, @@ -101,15 +103,15 @@ Deno.test("Expand - Join Refs", () => { assertEquals( [...expand( join( - {ref: "a"}, - {ref: "b"}, + ref("a"), + ref("b"), ) )({and: [ - {def: [{ref: "a"}, or( + {def: [ref("a"), or( literal("1"), literal("2"), )]}, - {def: [{ref: "b"}, or( + {def: [ref("b"), or( literal("3"), literal("4"), )]}, @@ -125,12 +127,12 @@ Deno.test("Expand - Join Refs", () => { Deno.test("Expand - Recursive Math", () => { assertEquals( - Iter(expand({ref: "nat"})({def: [ - {ref: "nat"}, + Iter(expand(ref("nat"))({def: [ + ref("nat"), or( literal(1), f({add: [ - {ref: "nat"}, + ref("nat"), literal(1) ]}) ) From a88d3446a569ca52c7b0e290de9b00a86f6da089 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:14:04 +0900 Subject: [PATCH 25/35] ast helper - `def` --- src/func/basic.ts | 2 +- test/call.test.ts | 26 +++++++++++++------------- test/expand.test.ts | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/func/basic.ts b/src/func/basic.ts index 9a19b42..7c1e876 100644 --- a/src/func/basic.ts +++ b/src/func/basic.ts @@ -9,5 +9,5 @@ export const ref = ({ref: name}) export const def = - (name: string, expr: Expr) => + (name: Expr, expr: Expr): Expr => ({def: [name, expr]}) \ No newline at end of file diff --git a/test/call.test.ts b/test/call.test.ts index 9e58a5f..c1cef0f 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -18,8 +18,8 @@ Deno.test("Call - Ref - And", () => { assertEquals( call(ref("a"), { and: [ - {def: [ref("a"), literal("hello")]}, - {def: [ref("b"), literal("world")]}, + def(ref("a"), literal("hello")), + def(ref("b"), literal("world")), ] }), literal("hello"), @@ -27,8 +27,8 @@ Deno.test("Call - Ref - And", () => { assertEquals( call(ref("b"), { and: [ - {def: [ref("a"), literal("hello")]}, - {def: [ref("b"), literal("world")]}, + def(ref("a"), literal("hello")), + def(ref("b"), literal("world")), ] }), literal("world"), @@ -39,10 +39,10 @@ Deno.test("Call - Ref - Nested And", () => { assertEquals( call(ref("b"), { and: [ - {def: [ref("a"), literal("hello")]}, + def(ref("a"), literal("hello")), {and: [ - {def: [ref("b"), literal("world")]}, - {def: [ref("c"), literal("1234")]}, + def(ref("b"), literal("world")), + def(ref("c"), literal("1234")), ]} ] }), @@ -57,8 +57,8 @@ Deno.test("Call - Ref - Nested Complex", () => { {def: [ ref("square"), {and: [ - {def: [ref("w"), literal(12)]}, - {def: [ref("h"), literal(5)]}, + def(ref("w"), literal(12)), + def(ref("h"), literal(5)), ]} ]}, {def: [ @@ -78,8 +78,8 @@ Deno.test("Call - Join", () => { assertEquals( call(join(ref("a"), ref("b")), { and: [ - {def: [ref("a"), literal("hello")]}, - {def: [ref("b"), literal("world")]}, + def(ref("a"), literal("hello")), + def(ref("b"), literal("world")), ] }), literal("helloworld"), @@ -90,8 +90,8 @@ Deno.test("Call - Math", () => { assertEquals( call(f({mul: [ref("a"), ref("b")]}), { and: [ - {def: [ref("a"), literal(12)]}, - {def: [ref("b"), literal(5)]}, + def(ref("a"), literal(12)), + def(ref("b"), literal(5)), ] }), literal(60), diff --git a/test/expand.test.ts b/test/expand.test.ts index 46ceeab..bcd86fa 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -83,7 +83,7 @@ Deno.test("Expand - Recursion", () => { ), ) assertEquals( - Iter(expand(ref("pat"))({def: [ref("pat"), pat]})).take(10).toArray(), + Iter(expand(ref("pat"))(def(ref("pat"), pat))).take(10).toArray(), [ { literal: "" }, { literal: "()" }, From cce4d8b6d283294c5dc09a6eaaa1f5b488290be6 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:15:57 +0900 Subject: [PATCH 26/35] ast helper - `def` (2) --- test/call.test.ts | 8 ++++---- test/expand.test.ts | 13 ++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/test/call.test.ts b/test/call.test.ts index c1cef0f..dc59031 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -54,20 +54,20 @@ Deno.test("Call - Ref - Nested Complex", () => { assertEquals( call({call: [ref("area"), ref("square")]}, {and: [ - {def: [ + def( ref("square"), {and: [ def(ref("w"), literal(12)), def(ref("h"), literal(5)), ]} - ]}, - {def: [ + ), + def( ref("area"), f({mul: [ ref("w"), ref("h"), ]}) - ]} + ) ]} ), literal(60), diff --git a/test/expand.test.ts b/test/expand.test.ts index bcd86fa..daffd8c 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -107,14 +107,14 @@ Deno.test("Expand - Join Refs", () => { ref("b"), ) )({and: [ - {def: [ref("a"), or( + def(ref("a"), or( literal("1"), literal("2"), - )]}, - {def: [ref("b"), or( + )), + def(ref("b"), or( literal("3"), literal("4"), - )]}, + )), ]})], [ literal("13"), @@ -127,7 +127,7 @@ Deno.test("Expand - Join Refs", () => { Deno.test("Expand - Recursive Math", () => { assertEquals( - Iter(expand(ref("nat"))({def: [ + Iter(expand(ref("nat"))(def( ref("nat"), or( literal(1), @@ -136,8 +136,7 @@ Deno.test("Expand - Recursive Math", () => { literal(1) ]}) ) - - ]})).take(3).toArray(), + ))).take(3).toArray(), [ literal(1), literal(2), From ae51aec9e75c5ee7589a207e5c0beda88fb452f4 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:22:29 +0900 Subject: [PATCH 27/35] ast helper - `and` --- test/arrow.test.ts | 5 ++-- test/call.test.ts | 61 +++++++++++++++++++++++++-------------------- test/expand.test.ts | 5 ++-- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/test/arrow.test.ts b/test/arrow.test.ts index ed5ff49..d49e8a0 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -7,6 +7,7 @@ import { call, any, + and, ref, def, literal, @@ -40,7 +41,7 @@ Deno.test("Arrow - Match Literal", () => { Deno.test("Arrow - Multiple Match", () => { assertEquals( call( - {and: [ + and( {arrow: [ literal("1"), literal("2"), @@ -49,7 +50,7 @@ Deno.test("Arrow - Multiple Match", () => { literal("2"), literal("4"), ]}, - ]}, + ), literal("2"), ), literal("4"), diff --git a/test/call.test.ts b/test/call.test.ts index dc59031..696f078 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -7,6 +7,7 @@ import { call, any, + and, ref, def, join, @@ -16,50 +17,54 @@ import { f } from "../src/util/mod.ts" Deno.test("Call - Ref - And", () => { assertEquals( - call(ref("a"), { - and: [ + call( + ref("a"), + and( def(ref("a"), literal("hello")), def(ref("b"), literal("world")), - ] - }), + ) + ), literal("hello"), ) assertEquals( - call(ref("b"), { - and: [ + call( + ref("b"), + and( def(ref("a"), literal("hello")), def(ref("b"), literal("world")), - ] - }), + ) + ), literal("world"), ) }) Deno.test("Call - Ref - Nested And", () => { assertEquals( - call(ref("b"), { - and: [ + call( + ref("b"), + and( def(ref("a"), literal("hello")), - {and: [ + and( def(ref("b"), literal("world")), def(ref("c"), literal("1234")), - ]} - ] - }), + ) + ) + ), literal("world"), ) }) Deno.test("Call - Ref - Nested Complex", () => { assertEquals( - call({call: [ref("area"), ref("square")]}, - {and: [ + call( + {call: [ref("area"), ref("square")]}, + and( def( ref("square"), - {and: [ + and( def(ref("w"), literal(12)), def(ref("h"), literal(5)), - ]} + ) ), def( ref("area"), @@ -68,7 +73,7 @@ Deno.test("Call - Ref - Nested Complex", () => { ref("h"), ]}) ) - ]} + ) ), literal(60), ) @@ -76,24 +81,26 @@ Deno.test("Call - Ref - Nested Complex", () => { Deno.test("Call - Join", () => { assertEquals( - call(join(ref("a"), ref("b")), { - and: [ + call( + join(ref("a"), ref("b")), + and( def(ref("a"), literal("hello")), def(ref("b"), literal("world")), - ] - }), + ) + ), literal("helloworld"), ) }) Deno.test("Call - Math", () => { assertEquals( - call(f({mul: [ref("a"), ref("b")]}), { - and: [ + call( + f({mul: [ref("a"), ref("b")]}), + and( def(ref("a"), literal(12)), def(ref("b"), literal(5)), - ] - }), + ) + ), literal(60), ) }) \ No newline at end of file diff --git a/test/expand.test.ts b/test/expand.test.ts index daffd8c..251ff4d 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -1,4 +1,5 @@ import { assertEquals } from "../deps.ts" +import { and } from "../mod.ts"; import { Expr, @@ -106,7 +107,7 @@ Deno.test("Expand - Join Refs", () => { ref("a"), ref("b"), ) - )({and: [ + )(and( def(ref("a"), or( literal("1"), literal("2"), @@ -115,7 +116,7 @@ Deno.test("Expand - Join Refs", () => { literal("3"), literal("4"), )), - ]})], + ))], [ literal("13"), literal("23"), From 48e4ce5641af5779813a972f2b25835744c7350a Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:24:44 +0900 Subject: [PATCH 28/35] ast helper - `arrow` --- src/func/basic.ts | 6 +++++- test/arrow.test.ts | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/func/basic.ts b/src/func/basic.ts index 7c1e876..1e3611d 100644 --- a/src/func/basic.ts +++ b/src/func/basic.ts @@ -10,4 +10,8 @@ export const ref = export const def = (name: Expr, expr: Expr): Expr => - ({def: [name, expr]}) \ No newline at end of file + ({def: [name, expr]}) + +export const arrow = + (from: Expr, to: Expr): Expr => + ({arrow: [from, to]}) \ No newline at end of file diff --git a/test/arrow.test.ts b/test/arrow.test.ts index d49e8a0..ea598ab 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -8,6 +8,7 @@ import { any, and, + arrow, ref, def, literal, @@ -18,20 +19,20 @@ import { f } from "../src/util/mod.ts" Deno.test("Arrow - Match Literal", () => { assertEquals( call( - {arrow: [ + arrow( literal("hello"), literal("bye"), - ]}, + ), literal("hello"), ), literal("bye"), ) assertNotEquals( call( - {arrow: [ + arrow( literal("hello"), literal("bye"), - ]}, + ), literal("hell"), ), literal("bye"), @@ -42,14 +43,14 @@ Deno.test("Arrow - Multiple Match", () => { assertEquals( call( and( - {arrow: [ + arrow( literal("1"), literal("2"), - ]}, - {arrow: [ + ), + arrow( literal("2"), literal("4"), - ]}, + ), ), literal("2"), ), @@ -60,10 +61,10 @@ Deno.test("Arrow - Multiple Match", () => { Deno.test("Arrow - Capture", () => { assertEquals( call( - {arrow: [ + arrow( {capture: ["n", any]}, f({mul: [ref("n"), literal(2)]}), - ]}, + ), literal(123), ), literal(246), @@ -73,10 +74,10 @@ Deno.test("Arrow - Capture", () => { Deno.test("Arrow - Junction", () => { assertEquals( call( - {arrow: [ + arrow( {capture: ["n", any]}, f({mul: [ref("n"), literal(2)]}), - ]}, + ), or(literal(10), literal(20)), ), or(literal(20), literal(40)), From 7f0818a6fc8b884a8faaa952ed13b6629731ce03 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:28:24 +0900 Subject: [PATCH 29/35] ast helper - `capture` --- src/func/basic.ts | 6 +++++- test/arrow.test.ts | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/func/basic.ts b/src/func/basic.ts index 1e3611d..c501385 100644 --- a/src/func/basic.ts +++ b/src/func/basic.ts @@ -14,4 +14,8 @@ export const def = export const arrow = (from: Expr, to: Expr): Expr => - ({arrow: [from, to]}) \ No newline at end of file + ({arrow: [from, to]}) + +export const capture = + (name: string, type: Expr): Expr => + ({capture: [name, type]}) \ No newline at end of file diff --git a/test/arrow.test.ts b/test/arrow.test.ts index ea598ab..d027896 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -9,6 +9,7 @@ import { and, arrow, + capture, ref, def, literal, @@ -62,7 +63,7 @@ Deno.test("Arrow - Capture", () => { assertEquals( call( arrow( - {capture: ["n", any]}, + capture("n", any), f({mul: [ref("n"), literal(2)]}), ), literal(123), @@ -75,7 +76,7 @@ Deno.test("Arrow - Junction", () => { assertEquals( call( arrow( - {capture: ["n", any]}, + capture("n", any), f({mul: [ref("n"), literal(2)]}), ), or(literal(10), literal(20)), From ddac9028e89fd4ce66a89e393bf827b45b8bfa90 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:29:59 +0900 Subject: [PATCH 30/35] ast helper - `mul` --- test/arrow.test.ts | 5 +++-- test/call.test.ts | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/arrow.test.ts b/test/arrow.test.ts index d027896..8c83549 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -10,6 +10,7 @@ import { and, arrow, capture, + mul, ref, def, literal, @@ -64,7 +65,7 @@ Deno.test("Arrow - Capture", () => { call( arrow( capture("n", any), - f({mul: [ref("n"), literal(2)]}), + mul(ref("n"), literal(2)), ), literal(123), ), @@ -77,7 +78,7 @@ Deno.test("Arrow - Junction", () => { call( arrow( capture("n", any), - f({mul: [ref("n"), literal(2)]}), + mul(ref("n"), literal(2)), ), or(literal(10), literal(20)), ), diff --git a/test/call.test.ts b/test/call.test.ts index 696f078..3b9358d 100644 --- a/test/call.test.ts +++ b/test/call.test.ts @@ -2,6 +2,7 @@ import { assertEquals, assertNotEquals, } from "../deps.ts" +import { mul } from "../mod.ts"; import { call, @@ -68,10 +69,10 @@ Deno.test("Call - Ref - Nested Complex", () => { ), def( ref("area"), - f({mul: [ + mul( ref("w"), ref("h"), - ]}) + ) ) ) ), @@ -95,7 +96,7 @@ Deno.test("Call - Join", () => { Deno.test("Call - Math", () => { assertEquals( call( - f({mul: [ref("a"), ref("b")]}), + mul(ref("a"), ref("b")), and( def(ref("a"), literal(12)), def(ref("b"), literal(5)), From 49a3879e7736c86c2161ce7908cbd854cda40a37 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 17:33:52 +0900 Subject: [PATCH 31/35] ast helper - etc. --- test/expand.test.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/test/expand.test.ts b/test/expand.test.ts index 251ff4d..bb52336 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -7,6 +7,7 @@ import { Iter, any, + add, join, literal, or, @@ -86,16 +87,16 @@ Deno.test("Expand - Recursion", () => { assertEquals( Iter(expand(ref("pat"))(def(ref("pat"), pat))).take(10).toArray(), [ - { literal: "" }, - { literal: "()" }, - { literal: "x" }, - { literal: "(())" }, - { literal: "()x" }, - { literal: "(x)" }, - { literal: "-" }, - { literal: "((()))" }, - { literal: "()-" }, - { literal: "(()x)" }, + literal(""), + literal("()"), + literal("x"), + literal("(())"), + literal("()x"), + literal("(x)"), + literal("-"), + literal("((()))"), + literal("()-"), + literal("(()x)"), ], ) }) @@ -132,10 +133,10 @@ Deno.test("Expand - Recursive Math", () => { ref("nat"), or( literal(1), - f({add: [ + add( ref("nat"), literal(1) - ]}) + ) ) ))).take(3).toArray(), [ From c4c0296b0ee63313550fa5f5bf02fcd2b0b379fc Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sat, 23 Sep 2023 20:56:54 +0900 Subject: [PATCH 32/35] Update expand.test.ts --- test/expand.test.ts | 161 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 145 insertions(+), 16 deletions(-) diff --git a/test/expand.test.ts b/test/expand.test.ts index bb52336..d93d42d 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -8,8 +8,12 @@ import { any, add, + arrow, + call, + capture, join, literal, + sub, or, ref, def, @@ -64,7 +68,7 @@ Deno.test("Expand - Join", () => { }) Deno.test("Expand - Recursion", () => { - const pat: Expr = + const pat = or( literal(""), or( @@ -84,23 +88,148 @@ Deno.test("Expand - Recursion", () => { ), ), ) - assertEquals( - Iter(expand(ref("pat"))(def(ref("pat"), pat))).take(10).toArray(), - [ - literal(""), - literal("()"), - literal("x"), - literal("(())"), - literal("()x"), - literal("(x)"), - literal("-"), - literal("((()))"), - literal("()-"), - literal("(()x)"), - ], - ) + assertEquals( + Iter(expand(ref("pat"))(def(ref("pat"), pat))).take(10).toArray(), + [ + literal(""), + literal("()"), + literal("x"), + literal("(())"), + literal("()x"), + literal("(x)"), + literal("-"), + literal("((()))"), + literal("()-"), + literal("(()x)"), + ], + ) }) +Deno.test("Expand - Recursion With External Function", () => { + const paren = + arrow( + capture("pat", any), + join( + join( + literal("("), + ref("pat"), + ), + literal(")"), + ) + ) + const pat = + or( + literal(""), + or( + join( + join( + literal("("), + ref("pat"), + ), + literal(")"), + ), + join( + ref("pat"), + or( + literal("x"), + literal("-"), + ), + ), + ), + ) + const expr = + and( + def( + ref("pat"), + pat, + ), + def( + ref("paren"), + paren, + ), + ) + assertEquals( + Iter(expand(ref("pat"))(expr)).take(10).toArray(), + [ + literal(""), + literal("()"), + literal("x"), + literal("(())"), + literal("()x"), + literal("(x)"), + literal("-"), + literal("((()))"), + literal("()-"), + literal("(()x)"), + ], + ) +}) +/* +Deno.test("Expand - Recursion", () => { + const pat_0 = + arrow( + literal(1), + literal(""), + ) + const pat_n = + arrow( + capture("n", any), + or( + join( + join( + literal("("), + call( + ref("pat"), + sub(ref("n"), literal(1)) + ), + ), + literal(")"), + ), + join( + call( + ref("pat"), + sub(ref("n"), literal(1)) + ), + or( + literal("x"), + literal("-"), + ), + ), + ), + ) + const pat = and( + pat_0, + pat_n, + ) + const result = + Iter( + expand + ( + call( + ref("pat"), + literal("0"), + ) + ) + (def(ref("pat"), pat)) + ).toArray() + + assertEquals( + result, + [ + literal(""), + literal("()"), + literal("x"), + literal("(())"), + literal("()x"), + literal("(x)"), + literal("-"), + literal("((()))"), + literal("()-"), + literal("(()x)"), + ], + ) +}) +*/ Deno.test("Expand - Join Refs", () => { assertEquals( [...expand( From 1fe5a4e05c0df733b58f879d32542ec00cc25dbc Mon Sep 17 00:00:00 2001 From: Gnlow Date: Sun, 24 Sep 2023 00:21:33 +0900 Subject: [PATCH 33/35] impl: and(or(....), ...) --- src/Expr.ts | 3 ++- src/func/and.ts | 32 +++++++++++++++++++++++++++----- src/func/or.ts | 10 +++++----- src/util/select.ts | 9 ++++++++- test/expand.test.ts | 20 ++++++++++++++++++++ 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/Expr.ts b/src/Expr.ts index cca2d2c..de9e435 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -10,4 +10,5 @@ export type Expr = | {capture: [string, Expr]} | {f: string, args: [Expr, Expr]} -export const any = {symbol: "any"} \ No newline at end of file +export const any = {symbol: "any"} +export const non = {symbol: "non"} \ No newline at end of file diff --git a/src/func/and.ts b/src/func/and.ts index 88521af..a4edaf3 100644 --- a/src/func/and.ts +++ b/src/func/and.ts @@ -1,10 +1,32 @@ -import { Expr, any } from "../Expr.ts" +import { Expr, any, non } from "../Expr.ts" -import { match } from "../../deps.ts" -import { $_ } from "../util/mod.ts" +import { match, P } from "../../deps.ts" +import { $, $a, $b, commu } from "../util/mod.ts" + +import { or } from "./or.ts" export const and = (a: Expr, b: Expr): Expr => match([a, b]) - .with([$_, any], x => x) - .with([any, $_], x => x) + .with( + commu([ + {or: [$("a1"), $("a2")] as const}, + $b, + ]), + ({a1, a2, b}) => { + return or( + and(a1!, b!), + and(a2!, b!), + ) + } + ) + .with( + [{literal: $a}, {literal: $b}], + ({a, b}) => { + return a === b + ? {literal: a} + : non + } + ) + .with(commu([P.any, non]), () => non) + .with(commu([$a, any]), ({a}) => a!) .otherwise(_ => ({and: [a, b]})) \ No newline at end of file diff --git a/src/func/or.ts b/src/func/or.ts index fd15780..0b0b39d 100644 --- a/src/func/or.ts +++ b/src/func/or.ts @@ -1,10 +1,10 @@ -import { Expr, any } from "../Expr.ts" +import { Expr, any, non } from "../Expr.ts" -import { match } from "../../deps.ts" -import { $_ } from "../util/mod.ts" +import { match, P } from "../../deps.ts" +import { $a, commu } from "../util/mod.ts" export const or = (a: Expr, b: Expr): Expr => match([a, b]) - .with([$_, any], _ => any) - .with([any, $_], _ => any) + .with(commu([$a, non]), ({a}) => a!) + .with(commu([P.any, any]), () => any) .otherwise(_ => ({or: [a, b]})) \ No newline at end of file diff --git a/src/util/select.ts b/src/util/select.ts index 32b1610..4320edc 100644 --- a/src/util/select.ts +++ b/src/util/select.ts @@ -7,4 +7,11 @@ export const $b = $("b") export const str$a = $("a", P.string) export const str$b = $("b", P.string) export const num$a = $("a", P.number) -export const num$b = $("b", P.number) \ No newline at end of file +export const num$b = $("b", P.number) +export const commu = + + ([a, b]: [A, B]) => + P.union( + [a, b], + [b, a], + ) \ No newline at end of file diff --git a/test/expand.test.ts b/test/expand.test.ts index d93d42d..7d2de51 100644 --- a/test/expand.test.ts +++ b/test/expand.test.ts @@ -274,4 +274,24 @@ Deno.test("Expand - Recursive Math", () => { literal(3), ], ) +}) + +Deno.test("Expand - Logic", () => { + assertEquals( + Iter( + expand( + and( + or( + literal(1), + literal(2), + ), + or( + literal(2), + literal(3), + ), + ) + )(any) + ).toArray(), + [literal(2)] + ) }) \ No newline at end of file From ceb63a02e62b7a2892017e42f2e18591477c14f5 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Mon, 25 Sep 2023 22:01:27 +0900 Subject: [PATCH 34/35] feat: guard --- src/Expr.ts | 2 ++ src/func/and.ts | 8 ++++++++ src/func/basic.ts | 10 +++++++++- src/func/call.ts | 4 ++++ test/guard.test.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/guard.test.ts diff --git a/src/Expr.ts b/src/Expr.ts index de9e435..2d3cd00 100644 --- a/src/Expr.ts +++ b/src/Expr.ts @@ -8,6 +8,8 @@ export type Expr = | {call: [Expr, Expr]} | {arrow: [Expr, Expr]} | {capture: [string, Expr]} + | {guard: Expr} + | {js_arrow: (x: Expr) => Expr} | {f: string, args: [Expr, Expr]} export const any = {symbol: "any"} diff --git a/src/func/and.ts b/src/func/and.ts index a4edaf3..e684ca3 100644 --- a/src/func/and.ts +++ b/src/func/and.ts @@ -3,6 +3,7 @@ import { Expr, any, non } from "../Expr.ts" import { match, P } from "../../deps.ts" import { $, $a, $b, commu } from "../util/mod.ts" +import { call } from "./call.ts" import { or } from "./or.ts" export const and = (a: Expr, b: Expr): Expr => @@ -27,6 +28,13 @@ export const and = (a: Expr, b: Expr): Expr => : non } ) + .with( + commu([ + {guard: $a}, + $b, + ]), + ({a, b}) => call(a!, b!) + ) .with(commu([P.any, non]), () => non) .with(commu([$a, any]), ({a}) => a!) .otherwise(_ => ({and: [a, b]})) \ No newline at end of file diff --git a/src/func/basic.ts b/src/func/basic.ts index c501385..49c201c 100644 --- a/src/func/basic.ts +++ b/src/func/basic.ts @@ -18,4 +18,12 @@ export const arrow = export const capture = (name: string, type: Expr): Expr => - ({capture: [name, type]}) \ No newline at end of file + ({capture: [name, type]}) + +export const guard = + (f: Expr) => + ({guard: f}) + +export const js_arrow = + (f: (x: Expr) => Expr) => + ({js_arrow: f}) \ No newline at end of file diff --git a/src/func/call.ts b/src/func/call.ts index e682363..183f032 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -67,5 +67,9 @@ export const call = (query: Expr, expr: Expr): Expr => { ) ) ) + .with( + [{js_arrow: $("f")}, $a], + ({f, a}) => f(a) + ) .otherwise(_ => query) } \ No newline at end of file diff --git a/test/guard.test.ts b/test/guard.test.ts new file mode 100644 index 0000000..b0f3fb5 --- /dev/null +++ b/test/guard.test.ts @@ -0,0 +1,42 @@ +import { + assertEquals, + assertNotEquals, +} from "../deps.ts" + +import { + call, + any, + + and, + arrow, + capture, + mul, + ref, + def, + literal, + or, + js_arrow, + guard, + non, +} from "../src/mod.ts" + +Deno.test("Guard - Number", () => { + assertEquals( + and( + guard( + js_arrow( + x => + ("literal" in x && + typeof x?.literal == "number") + ? x + : non + ) + ), + or( + literal(123), + literal("456"), + ) + ), + literal(123), + ) +}) \ No newline at end of file From 77d3a59f458f6122f8c7610e2b1c0e62d0f09b70 Mon Sep 17 00:00:00 2001 From: Gnlow Date: Mon, 25 Sep 2023 23:53:06 +0900 Subject: [PATCH 35/35] feat: Typed Capture --- src/func/call.ts | 22 +++++++++++++++------- src/func/math.ts | 10 +++++++--- src/mod.ts | 1 + src/std/mod.ts | 22 ++++++++++++++++++++++ test/arrow.test.ts | 28 +++++++++++++++++++++++++++- test/guard.test.ts | 11 ++--------- 6 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 src/std/mod.ts diff --git a/src/func/call.ts b/src/func/call.ts index 183f032..b49855c 100644 --- a/src/func/call.ts +++ b/src/func/call.ts @@ -1,4 +1,4 @@ -import { Expr, any } from "../Expr.ts" +import { Expr, any, non } from "../Expr.ts" import { and } from "./and.ts" import { or } from "./or.ts" import { join } from "./join.ts" @@ -10,10 +10,11 @@ import { } from "./math.ts" import { match, P } from "../../deps.ts" -import { $, $_, $a, $b } from "../util/mod.ts" +import { $, $_, $a, $b, commu } from "../util/mod.ts" export const call = (query: Expr, expr: Expr): Expr => { return match([query, expr]) + .with([P.any, non], () => any) .with( [$("q"), {or: [$a, $b]}], ({a, b, q}) => or(call(q, a), call(q, b)) @@ -39,9 +40,12 @@ export const call = (query: Expr, expr: Expr): Expr => { ) .with( [{arrow: [{capture: $a}, $b]}, P.any], - ({a: [name, _type], b}) => // TODO: Type Checking + ({a: [name, type], b}) => // TODO: Type Checking match(expr) - .with($_, () => call(b, {def: [{ref: name}, expr]})) + .with($_, () => call(b, {def: [ + {ref: name}, + and(expr, type), + ]})) .otherwise(() => any) ) .with([{arrow: P.any}, P.any], () => any) @@ -53,10 +57,14 @@ export const call = (query: Expr, expr: Expr): Expr => { call(a, expr), call(b, expr), )) + .with( + [{f: P.any, args: commu([non, P.any])}, P.any], + () => any + ) .with( [{f: $("name"), args: $("args")}, P.any], - ({name, args}) => ( - { + ({name, args}) => { + return { join, add, sub, @@ -65,7 +73,7 @@ export const call = (query: Expr, expr: Expr): Expr => { }[name as "join"]( ...args.map(arg => call(arg, expr)) as typeof args ) - ) + } ) .with( [{js_arrow: $("f")}, $a], diff --git a/src/func/math.ts b/src/func/math.ts index d9b6488..86be446 100644 --- a/src/func/math.ts +++ b/src/func/math.ts @@ -1,12 +1,16 @@ -import { Expr } from "../Expr.ts" +import { Expr, non, any } from "../Expr.ts" -import { match } from "../../deps.ts" -import { num$a, num$b } from "../util/mod.ts" +import { P, match } from "../../deps.ts" +import { num$a, num$b, commu } from "../util/mod.ts" const math = (name: string, func: (a: number, b: number) => number) => (a: Expr, b: Expr): Expr => match([a, b]) + .with( + commu([non, P.any]), + () => any + ) .with( [{literal: num$a}, {literal: num$b}], ({a, b}) => ({literal: func(a, b)}) diff --git a/src/mod.ts b/src/mod.ts index c72cad8..a082437 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -1,4 +1,5 @@ export * from "./func/mod.ts" +export * from "./std/mod.ts" export * from "./expand.ts" export * from "./Expr.ts" export * from "./run.ts" diff --git a/src/std/mod.ts b/src/std/mod.ts new file mode 100644 index 0000000..76b3f03 --- /dev/null +++ b/src/std/mod.ts @@ -0,0 +1,22 @@ +import { guard, js_arrow } from "../func/basic.ts" +import { Expr, non } from "../Expr.ts" + +const predicate = + (check: (x: Expr) => boolean) => + guard( + js_arrow( + x => check(x) ? x : non + ) + ) + +export const num = + predicate(x => + "literal" in x && + typeof x?.literal == "number" + ) + +export const str = + predicate(x => + "literal" in x && + typeof x?.literal == "string" + ) \ No newline at end of file diff --git a/test/arrow.test.ts b/test/arrow.test.ts index 8c83549..3305ad5 100644 --- a/test/arrow.test.ts +++ b/test/arrow.test.ts @@ -15,6 +15,9 @@ import { def, literal, or, + + num, + str, } from "../src/mod.ts" import { f } from "../src/util/mod.ts" @@ -84,4 +87,27 @@ Deno.test("Arrow - Junction", () => { ), or(literal(20), literal(40)), ) -}) \ No newline at end of file +}) + +Deno.test("Arrow - Typed Capture", () => { + assertEquals( + call( + arrow( + capture("n", num), + mul(ref("n"), literal(2)), + ), + literal(123), + ), + literal(246), + ) + assertEquals( + call( + arrow( + capture("n", str), + mul(ref("n"), literal(2)), + ), + literal(123), + ), + any, + ) +}) diff --git a/test/guard.test.ts b/test/guard.test.ts index b0f3fb5..070f56a 100644 --- a/test/guard.test.ts +++ b/test/guard.test.ts @@ -18,20 +18,13 @@ import { js_arrow, guard, non, + num, } from "../src/mod.ts" Deno.test("Guard - Number", () => { assertEquals( and( - guard( - js_arrow( - x => - ("literal" in x && - typeof x?.literal == "number") - ? x - : non - ) - ), + num, or( literal(123), literal("456"),