Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
641dd8b
customizable func ast
gnlow Sep 7, 2023
8ed82b7
Update deno.yml
gnlow Sep 8, 2023
70db8c4
add: `arrow` (only match literals)
gnlow Sep 8, 2023
e069188
add: add,sub,mul,div
gnlow Sep 8, 2023
61bbfe3
nested defs
gnlow Sep 8, 2023
d714ff1
add: arrow capture (no type check yet)
gnlow Sep 9, 2023
ab52e58
seperate file
gnlow Sep 9, 2023
456de8b
feat: multiple match
gnlow Sep 9, 2023
9cbd9ba
refactor: `call` match with `expr`
gnlow Sep 9, 2023
22bd3cb
refactor
gnlow Sep 9, 2023
de8b631
feat: automatic lifting for junction
gnlow Sep 9, 2023
4fe3295
export util/f
gnlow Sep 11, 2023
1ee91eb
refactor: `def` as normal binExpr
gnlow Sep 11, 2023
ab8427c
feat: more expandable funcs
gnlow Sep 16, 2023
d2db9ea
use external importMap
gnlow Sep 16, 2023
bdafa9f
wtf
gnlow Sep 16, 2023
5651142
wtff
gnlow Sep 16, 2023
a9cdf42
whyyyyyyyyyyy
gnlow Sep 17, 2023
3b60ed9
delete importMap
gnlow Sep 17, 2023
8d683aa
ast helper function for testing
gnlow Sep 23, 2023
872a752
ast helper - `or`
gnlow Sep 23, 2023
f3c8376
ast helper - `join`
gnlow Sep 23, 2023
fc35f94
ast helper - `join` (2)
gnlow Sep 23, 2023
1ef1cee
ast helper - `ref`
gnlow Sep 23, 2023
a88d344
ast helper - `def`
gnlow Sep 23, 2023
cce4d8b
ast helper - `def` (2)
gnlow Sep 23, 2023
ae51aec
ast helper - `and`
gnlow Sep 23, 2023
48e4ce5
ast helper - `arrow`
gnlow Sep 23, 2023
7f0818a
ast helper - `capture`
gnlow Sep 23, 2023
ddac902
ast helper - `mul`
gnlow Sep 23, 2023
49a3879
ast helper - etc.
gnlow Sep 23, 2023
c4c0296
Update expand.test.ts
gnlow Sep 23, 2023
1fe5a4e
impl: and(or(....), ...)
gnlow Sep 23, 2023
ceb63a0
feat: guard
gnlow Sep 25, 2023
77d3a59
feat: Typed Capture
gnlow Sep 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ name: Deno
on:
push:
branches-ignore: []
tags:
- '**'
pull_request:
branches-ignore: []

Expand Down
6 changes: 0 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +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",
"util/": "./src/util/"
}
}
9 changes: 9 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src/mod.ts"
13 changes: 9 additions & 4 deletions src/Expr.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export type Expr =
| {ref: string}
| {literal: string}
| {def: [string, Expr]}
| {join: [Expr, Expr]}
| {literal: string | number}
| {def: [Expr, Expr]}
| {or: [Expr, Expr]}
| {and: [Expr, Expr]}
| {symbol: string}
| {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"}
export const any = {symbol: "any"}
export const non = {symbol: "non"}
18 changes: 9 additions & 9 deletions src/expand.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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"
export * from "iteruyo"
import { match, P } from "../deps.ts"
import { Iter } from "../deps.ts"
import { $, $a, $b, f } from "./util/mod.ts"
export * from "../deps.ts"

class LazyArray<T> {
memory: T[] = []
Expand Down Expand Up @@ -46,11 +46,11 @@ export const expand = (query: Expr) => function*(expr: Expr): Generator<Expr, vo
yield* match(call(query, expr))
.with({or: [$a, $b]}, function*({a, b}) {
yield* match([a, b])
.with([{literal: P.string}, P.any], function*() {
.with([{literal: P.any}, P.any], function*() {
yield a
yield* expand(b)(expr)
})
.with([P.any, {literal: P.string}], function*() {
.with([P.any, {literal: P.any}], function*() {
yield b
yield* expand(a)(expr)
})
Expand All @@ -61,11 +61,11 @@ export const expand = (query: Expr) => function*(expr: Expr): Generator<Expr, vo
)
})
})
.with({join: [$a, $b]}, ({a, b}) => {
.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])
}
Expand Down
40 changes: 35 additions & 5 deletions src/func/and.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import { Expr, any } from "../Expr.ts"
import { Expr, any, non } from "../Expr.ts"

import { match } from "ts-pattern"
import { $_ } from "util/select.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 =>
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([
{guard: $a},
$b,
]),
({a, b}) => call(a!, b!)
)
.with(commu([P.any, non]), () => non)
.with(commu([$a, any]), ({a}) => a!)
.otherwise(_ => ({and: [a, b]}))
29 changes: 29 additions & 0 deletions src/func/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Expr } from "../Expr.ts";

export const literal =
(value: string | number) =>
({literal: value})

export const ref =
(name: string) =>
({ref: name})

export const def =
(name: Expr, expr: Expr): Expr =>
({def: [name, expr]})

export const arrow =
(from: Expr, to: Expr): Expr =>
({arrow: [from, to]})

export const capture =
(name: string, type: Expr): Expr =>
({capture: [name, type]})

export const guard =
(f: Expr) =>
({guard: f})

export const js_arrow =
(f: (x: Expr) => Expr) =>
({js_arrow: f})
88 changes: 72 additions & 16 deletions src/func/call.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,83 @@
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"
import {
add,
sub,
mul,
div,
} from "./math.ts"

import { match, P } from "ts-pattern"
import { $_, $a, $b } from "util/select.ts"
import { match, P } from "../../deps.ts"
import { $, $_, $a, $b, commu } from "../util/mod.ts"

export const call = (query: Expr, expr: Expr): Expr => {
return match(query)
.with({ref: $_}, name =>
match(expr)
.with({def: [name, $_]}, value => {
return value
})
.with({and: [$a, $b]}, ({a, b}) => {
return and(
return match([query, expr])
.with([P.any, non], () => any)
.with(
[$("q"), {or: [$a, $b]}],
({a, b, q}) => or(call(q, a), call(q, b))
)
.with(
[{ref: P.any}, {def: [{ref: P.any}, $_]}],
([{ref}, {def: [{ref: name}, _val]}]) => ref == name,
val => val
)
.with(
[{ref: $("name")}, {and: [$a, $b]}],
({name, a, b}) =>
and(
call({ref: name}, a),
call({ref: name}, b),
)
})
)
.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
match(expr)
.with($_, () => call(b, {def: [
{ref: name},
and(expr, type),
]}))
.otherwise(() => any)
)
.with({join: [$a, $b]}, ({a, b}) => {
return join(call(a, expr), call(b, expr))
})
.otherwise(q => q)
.with([{arrow: P.any}, P.any], () => any)
.with([{call: [$a, $b]}, P.any], ({a, b}) => call(
call(a, expr),
call(b, expr),
))
.with([{and: [$a, $b]}, P.any], ({a, b}) => and(
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}) => {
return {
join,
add,
sub,
mul,
div,
}[name as "join"](
...args.map(arg => call(arg, expr)) as typeof args
)
}
)
.with(
[{js_arrow: $("f")}, $a],
({f, a}) => f(a)
)
.otherwise(_ => query)
}
8 changes: 4 additions & 4 deletions src/func/join.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Expr } from "../Expr.ts"

import { match } from "ts-pattern"
import { $a, $b } from "util/select.ts"
import { match } from "../../deps.ts"
import { f, str$a, str$b } from "../util/mod.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(() => ({join: [a, b]}))
.otherwise(() => f({join: [a, b]}))
23 changes: 23 additions & 0 deletions src/func/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Expr, non, any } from "../Expr.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)})
)
.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)
12 changes: 11 additions & 1 deletion src/func/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export * from "./and.ts"
export * from "./call.ts"
export * from "./join.ts"
export * from "./join.ts"
export * from "./basic.ts"
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<string, (a: Expr, b: Expr) => Expr> =
{ join, or, add, sub, mul, div }
10 changes: 10 additions & 0 deletions src/func/or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Expr, any, non } from "../Expr.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(commu([$a, non]), ({a}) => a!)
.with(commu([P.any, any]), () => any)
.otherwise(_ => ({or: [a, b]}))
5 changes: 4 additions & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export * from "./func/mod.ts"
export * from "./std/mod.ts"
export * from "./expand.ts"
export * from "./Expr.ts"
export * from "./run.ts"
export * from "./run.ts"

export * from "./util/f.ts"
4 changes: 2 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 { match, P } from "../deps.ts"
import { $, $a, $b } from "./util/mod.ts"

export const run = (expr: Expr): Expr => {
return match(expr)
Expand Down
22 changes: 22 additions & 0 deletions src/std/mod.ts
Original file line number Diff line number Diff line change
@@ -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"
)
Loading