Skip to content

Commit 6ff2445

Browse files
committed
remove sexp/
1 parent cdc7b92 commit 6ff2445

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+131
-2011
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
remove `sexp/`
1+
fix `examples/list.scm`
22
review diary

examples/list.scm

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
(define nil
2-
(lambda (nil-case ::-case)
3-
nil-case))
4-
5-
(define (:: head tail)
6-
(lambda (nil-case ::-case)
7-
(::-case head tail
8-
(tail nil-case ::-case))))
9-
10-
(define (rec-List target nil-case ::-case)
11-
(target nil-case ::-case))
12-
13-
(import zero add1 "./nat-church.scm")
14-
15-
(define (length l)
16-
(rec-List l
17-
zero
18-
(lambda (head target almost)
19-
(add1 almost))))
20-
21-
(import true "./boolean.scm")
22-
23-
(assert-equal (length nil) zero)
24-
(assert-equal (length (:: true nil)) (add1 zero))
25-
(assert-equal (length (:: true (:: true nil))) (add1 (add1 zero)))
26-
27-
(define (append left right)
28-
(rec-List left
29-
right
30-
(lambda (head target almost)
31-
(:: head almost))))
32-
33-
(assert-equal (append nil nil) nil)
34-
(assert-equal (append nil (:: true nil)) (:: true nil))
35-
(assert-equal (append (:: true nil) nil) (:: true nil))
36-
(assert-equal (append (:: true nil) (:: true nil)) (:: true (:: true nil)))
37-
(assert-equal (append (:: true (:: true nil))
38-
(:: true (:: true nil)))
39-
(:: true (:: true (:: true (:: true nil)))))
1+
;; (define nil
2+
;; (lambda (nil-case ::-case)
3+
;; nil-case))
4+
5+
;; (define (:: head tail)
6+
;; (lambda (nil-case ::-case)
7+
;; (::-case head tail
8+
;; (tail nil-case ::-case))))
9+
10+
;; (define (rec-List target nil-case ::-case)
11+
;; (target nil-case ::-case))
12+
13+
;; (import zero add1 "./nat-church.scm")
14+
15+
;; (define (length l)
16+
;; (rec-List l
17+
;; zero
18+
;; (lambda (head target almost)
19+
;; (add1 almost))))
20+
21+
;; (import true "./boolean.scm")
22+
23+
;; (assert-equal (length nil) zero)
24+
;; (assert-equal (length (:: true nil)) (add1 zero))
25+
;; (assert-equal (length (:: true (:: true nil))) (add1 (add1 zero)))
26+
27+
;; (define (append left right)
28+
;; (rec-List left
29+
;; right
30+
;; (lambda (head target almost)
31+
;; (:: head almost))))
32+
33+
;; (assert-equal (append nil nil) nil)
34+
;; (assert-equal (append nil (:: true nil)) (:: true nil))
35+
;; (assert-equal (append (:: true nil) nil) (:: true nil))
36+
;; (assert-equal (append (:: true nil) (:: true nil)) (:: true (:: true nil)))
37+
;; (assert-equal (append (:: true (:: true nil))
38+
;; (:: true (:: true nil)))
39+
;; (:: true (:: true (:: true (:: true nil)))))

examples/tests/let.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(let ([id (lambda (x) x)])
1+
(let ((id (lambda (x) x)))
22
(id id))

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@xieyuheng/commander.js": "^0.1.3",
26-
"@xieyuheng/x-data.js": "^0.1.1",
26+
"@xieyuheng/x-data.js": "^0.1.4",
2727
"dedent": "^1.6.0",
2828
"regexp-match-indices": "^1.0.2"
2929
},

src/lang/run/load.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { ParsingError } from "@xieyuheng/x-data.js"
12
import fs from "node:fs"
2-
import { ParsingError } from "../../sexp/index.ts"
33
import { createMod, modResolve, type Mod } from "../mod/index.ts"
44
import { parseStmts } from "../syntax/index.ts"
55

src/lang/syntax/matchExp.ts

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
1-
import {
2-
cons,
3-
match,
4-
matchList,
5-
matchSymbol,
6-
v,
7-
type Sexp,
8-
} from "../../sexp/index.ts"
1+
import * as X from "@xieyuheng/x-data.js"
92
import * as Exps from "../exp/index.ts"
103
import { type Exp } from "../exp/index.ts"
114
import {
125
substitutionFromBindings,
136
type Binding,
147
} from "../substitution/index.ts"
158

16-
export function matchExp(sexp: Sexp): Exp {
17-
return match<Exp>(sexp, [
18-
[
19-
["lambda", v("names"), v("exp")],
20-
({ names, exp }) =>
21-
matchList(names, matchSymbol).reduceRight(
22-
(fn, name) => Exps.Fn(name, fn),
23-
matchExp(exp),
24-
),
25-
],
9+
const expMatcher: X.Matcher<Exp> = X.matcherChoice<Exp>([
10+
X.matcher("`(lambda ,names ,exp)", ({ names, exp }) =>
11+
X.dataToArray(names)
12+
.map(X.dataToString)
13+
.reduceRight((fn, name) => Exps.Fn(name, fn), matchExp(exp)),
14+
),
15+
16+
X.matcher("`(let ,bindings ,body)", ({ bindings, body }) =>
17+
Exps.Let(
18+
substitutionFromBindings(X.dataToArray(bindings).map(matchBinding)),
19+
matchExp(body),
20+
),
21+
),
2622

27-
[
28-
["let", v("bindings"), v("body")],
29-
({ bindings, body }) =>
30-
Exps.Let(
31-
substitutionFromBindings(matchList(bindings, matchBinding)),
32-
matchExp(body),
33-
),
34-
],
23+
X.matcher("(cons target args)", ({ target, args }) =>
24+
X.dataToArray(args)
25+
.map(matchExp)
26+
.reduce((result, arg) => Exps.Ap(result, arg), matchExp(target)),
27+
),
3528

36-
[
37-
cons(v("target"), v("args")),
38-
({ target, args }) =>
39-
matchList(args, matchExp).reduce(
40-
(result, arg) => Exps.Ap(result, arg),
41-
matchExp(target),
42-
),
43-
],
29+
X.matcher("name", ({ name }) => Exps.Var(X.dataToString(name))),
30+
])
4431

45-
[v("name"), ({ name }) => Exps.Var(matchSymbol(name))],
46-
])
32+
export function matchExp(data: X.Data): Exp {
33+
return X.match(expMatcher, data)
4734
}
4835

49-
export function matchBinding(sexp: Sexp): Binding {
50-
return match<Binding>(sexp, [
51-
[
52-
[v("name"), v("exp")],
53-
({ name, exp }) => ({
54-
name: matchSymbol(name),
55-
exp: matchExp(exp),
56-
}),
57-
],
58-
])
36+
export function matchBinding(data: X.Data): Binding {
37+
return X.match(
38+
X.matcher("`(,name ,exp)", ({ name, exp }) => ({
39+
name: X.dataToString(name),
40+
exp: matchExp(exp),
41+
})),
42+
data,
43+
)
5944
}

src/lang/syntax/matchStmt.ts

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,59 @@
1-
import {
2-
cons,
3-
listToArray,
4-
match,
5-
matchList,
6-
matchString,
7-
matchSymbol,
8-
v,
9-
type Sexp,
10-
} from "../../sexp/index.ts"
1+
import * as X from "@xieyuheng/x-data.js"
112
import * as Exps from "../exp/index.ts"
123
import * as Stmts from "../stmt/index.ts"
134
import { type Stmt } from "../stmt/index.ts"
145
import { matchExp } from "./matchExp.ts"
156

16-
export function matchStmt(sexp: Sexp): Stmt {
17-
return match<Stmt>(sexp, [
18-
[
19-
["define", cons(v("name"), v("args")), v("exp")],
20-
({ name, args, exp }) =>
21-
Stmts.Define(
22-
matchSymbol(name),
23-
matchList(args, matchSymbol).reduceRight(
24-
(fn, name) => Exps.Fn(name, fn),
25-
matchExp(exp),
26-
),
27-
),
28-
],
29-
30-
[
31-
["define", v("name"), v("exp")],
32-
({ name, exp }) => Stmts.Define(matchSymbol(name), matchExp(exp)),
33-
],
34-
35-
[
36-
cons("import", v("body")),
37-
({ body }) => {
38-
const sexps = listToArray(body)
39-
const url = sexps[sexps.length - 1]
40-
const entries = sexps.slice(0, sexps.length - 1)
41-
return Stmts.Import(matchString(url), entries.map(matchImportEntry))
42-
},
43-
],
44-
45-
[
46-
cons("assert-equal", v("exps")),
47-
({ exps }) => Stmts.AssertEqual(matchList(exps, matchExp)),
48-
],
49-
50-
[
51-
cons("assert-not-equal", v("exps")),
52-
({ exps }) => Stmts.AssertNotEqual(matchList(exps, matchExp)),
53-
],
54-
55-
[v("exp"), ({ exp }) => Stmts.Compute(matchExp(exp))],
56-
])
7+
const stmtMatcher: X.Matcher<Stmt> = X.matcherChoice<Stmt>([
8+
X.matcher("`(define ,(cons name args) ,exp)", ({ name, args, exp }) =>
9+
Stmts.Define(
10+
X.dataToString(name),
11+
X.dataToArray(args)
12+
.map(X.dataToString)
13+
.reduceRight((fn, name) => Exps.Fn(name, fn), matchExp(exp)),
14+
),
15+
),
16+
17+
X.matcher("`(define ,name ,exp)", ({ name, exp }) =>
18+
Stmts.Define(X.dataToString(name), matchExp(exp)),
19+
),
20+
21+
X.matcher("(cons 'import body)", ({ body }) => {
22+
const array = X.dataToArray(body)
23+
const url = array[array.length - 1]
24+
const entries = array.slice(0, array.length - 1)
25+
return Stmts.Import(X.dataToString(url), entries.map(matchImportEntry))
26+
}),
27+
28+
X.matcher("(cons 'import exps)", ({ exps }) =>
29+
Stmts.AssertEqual(X.dataToArray(exps).map(matchExp)),
30+
),
31+
32+
X.matcher("(cons 'assert-equal exps)", ({ exps }) =>
33+
Stmts.AssertEqual(X.dataToArray(exps).map(matchExp)),
34+
),
35+
36+
X.matcher("(cons 'assert-not-equal exps)", ({ exps }) =>
37+
Stmts.AssertNotEqual(X.dataToArray(exps).map(matchExp)),
38+
),
39+
40+
X.matcher("exp", ({ exp }) => Stmts.Compute(matchExp(exp))),
41+
])
42+
43+
export function matchStmt(data: X.Data): Stmt {
44+
return X.match(stmtMatcher, data)
5745
}
5846

59-
function matchImportEntry(sexp: Sexp): Stmts.ImportEntry {
60-
return match<Stmts.ImportEntry>(sexp, [
61-
[
62-
["rename", v("name"), v("rename")],
63-
({ name, rename }) => ({
64-
name: matchSymbol(name),
65-
rename: matchSymbol(rename),
66-
}),
67-
],
68-
69-
[v("name"), ({ name }) => ({ name: matchSymbol(name) })],
70-
])
47+
function matchImportEntry(data: X.Data): Stmts.ImportEntry {
48+
return X.match(
49+
X.matcherChoice([
50+
X.matcher("`(rename ,name ,rename)", ({ name, rename }) => ({
51+
name: X.dataToString(name),
52+
rename: X.dataToString(rename),
53+
})),
54+
55+
X.matcher("name", ({ name }) => ({ name: X.dataToString(name) })),
56+
]),
57+
data,
58+
)
7159
}

src/lang/syntax/parse.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
import { Parser as SexpParser } from "../../sexp/index.ts"
1+
import { parseDataArray } from "@xieyuheng/x-data.js"
22
import { type Stmt } from "../stmt/index.ts"
33
import { matchStmt } from "./matchStmt.ts"
44

5-
class Parser extends SexpParser {
6-
constructor() {
7-
super({
8-
quotes: [
9-
{ mark: "'", symbol: "quote" },
10-
{ mark: ",", symbol: "unquote" },
11-
{ mark: "`", symbol: "quasiquote" },
12-
],
13-
brackets: [
14-
{ start: "(", end: ")" },
15-
{ start: "[", end: "]" },
16-
],
17-
comments: [";"],
18-
})
19-
}
20-
}
21-
225
export function parseStmts(text: string): Array<Stmt> {
23-
const parser = new Parser()
24-
return parser.parseSexps(text).map(matchStmt)
6+
return parseDataArray(text).map(matchStmt)
257
}

src/sexp/errors/InternalError.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/sexp/errors/ParsingError.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/sexp/errors/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/sexp/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)