Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export const lisp = (src: string, onlyParse: boolean = false) => {
type LispValue = {
type: "null";
value: null;
} | {
type: "str";
value: string;
} | {
type: "num";
value: number;
} | {
type: "sym";
value: string;
} | {
type: "symp";
value: string;
} | {
type: "lambda";
value: Lambda;
} | {
type: "builtin";
value: string;
} | {
type: "plist";
value: Plist;
} | {
type: "list";
value: Array<LispValue>;
};
type List = Array<LispValue>;
type Plist = Record<string, LispValue>;
type Lambda = {
containsSpreadArg: boolean;
args: string[];
code: AstExpr;
};
type AstExpr =
| {
type: "str";
value: [
string,
];
}
| {
type: "num";
value: [
number,
];
}
| App
| {
type: "sym";
value: [
string,
];
}
| {
type: "symp";
value: [
string,
];
};
type App = {
type: "app";
value: Array<AstExpr>;
};
const makeStr = (value: string): AstExpr => ({
type: "str",
value: [value],
});
const makeNum = (value: number): AstExpr => ({
type: "num",
value: [value],
});
const makeSymp = (value: string): AstExpr => ({
type: "symp",
value: [value],
});
const makeSym = (value: string): AstExpr => {
return {
type: "sym",
value: [value],
};
};
const makeApply = (value: Array<AstExpr>): AstExpr => ({
type: "app",
value,
});
const parse = (src: string): Array<AstExpr> => {
let idx = 0;
const peek = (): string | undefined =>
idx >= src.length ? undefined : src[idx];
const matchEat = (expected: string): boolean | undefined => {
const char = peek();
if (char === undefined)
return undefined;
const match = char === expected;
if (match) {
idx += 1;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023