deno.land / x / masx200_leetcode_test@10.6.5 / parse-lisp-expression / index.ts

نووسراو ببینە
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function parseAdd(expression: string): Expression { const content = expression.slice("(add ".length, -1); const list = parseList("(" + content + ")"); // console.log(list); if (Array.isArray(list) && list.length !== 2) { throw Error("Invalid expression"); } return buildExpression(["add", ...list]);}function parseLet(expression: string): Expression { const content = expression.slice("(let ".length, -1); const list = parseList("(" + content + ")"); // console.log(list); if (Array.isArray(list) && list.length < 3) { throw Error("Invalid expression"); } return buildExpression(["let", ...list]);}
function parseMult(expression: string): Expression { const content = expression.slice("(mult ".length, -1); const list = parseList("(" + content + ")"); // console.log(list); if (Array.isArray(list) && list.length !== 2) { throw Error("Invalid expression"); } return buildExpression(["mult", ...list]);}function buildExpression(list: string | number | ListArray): Expression { // console.log(list); if (Array.isArray(list) && list[0] === "let") { const variables = list.slice(1, -1).map(buildExpression); // console.log(variables); const declarations: VariableDeclarator[] = Array(variables.length / 2) .fill(0) .map((_, i) => { const ident = variables[i * 2]; if (ident.type !== "Identifier") { throw Error("Invalid identifier"); } return { type: "VariableDeclarator", id: ident, init: variables[i * 2 + 1], }; }); return { type: "LetExpression",
declarations: declarations, return: { type: "ReturnStatement", argument: buildExpression(list[list.length - 1]), }, }; } if (typeof list === "string") { return parseIdentifier(list); } if (typeof list === "number") { return parseNumeric(list); }
if (Array.isArray(list) && list.length == 0) { throw Error("Invalid expression"); } if (Array.isArray(list) && list[0] === "mult") { return { type: "MultExpression",
left: buildExpression(list[1]), right: buildExpression(list[2]), }; } if (Array.isArray(list) && list[0] === "add") { return { type: "AddExpression",
left: buildExpression(list[1]), right: buildExpression(list[2]), }; } throw Error("Not implemented");}export type ListArray = Array<ListArray | string | number>;function parseList(expression: string): ListArray { return JSON.parse( expression .replaceAll("(", "[") .replaceAll(")", "]") .replaceAll(" ", ",") .replaceAll(/[a-z]([a-z]|\d)*/g, (a) => '"' + a + '"'), );}
function parseNumeric(expression: string | number): Expression { return { type: "NumericLiteral", value: Number(expression) };}
function evaluate(expression: string): number { // console.log(expression); const ast = parse(expression); // console.log(ast); return calculate(ast, new ScopeList());}function parse(expression: string): Expression { if (expression.length === 0) { throw new Error("Empty expression"); }
if (/^\d+$/g.test(expression)) return parseNumeric(expression); if (expression.startsWith("(add ") && expression.endsWith(")")) { return parseAdd(expression); } if (expression.startsWith("(mult ") && expression.endsWith(")")) { return parseMult(expression); } if (expression.startsWith("(let ") && expression.endsWith(")")) { return parseLet(expression); } if (/^[a-z]([a-z]|\d)*$/g.test(expression)) { return parseIdentifier(expression); } throw new Error("Unsupported expression");}function parseIdentifier(expression: string): Expression { return { type: "Identifier", name: expression };}
function calculate(expression: Expression, scope: ScopeList): number { // console.log(expression, scope); switch (expression.type) { case "Identifier": return getVariable(scope, expression.name); case "NumericLiteral": return expression.value; case "MultExpression": return ( calculate(expression.left, scope) * calculate(expression.right, scope) ); case "AddExpression": return ( calculate(expression.left, scope) + calculate(expression.right, scope) ); case "LetExpression": { const newScope = new ScopeList(new Map(), scope); expression.declarations.forEach((declaration) => { newScope.variables.set( declaration.id.name, calculate(declaration.init, newScope), ); }); return calculate(expression.return.argument, newScope); } }}export type Expression = | Identifier | LetExpression | NumericLiteral | AddExpression | MultExpression;export class ScopeList { constructor( public readonly variables: Map<string, number> = new Map(), public parent: ScopeList | null | undefined = null, ) {}}export interface VariableDeclarator { type: "VariableDeclarator"; id: LVal; init: Expression;}export type LVal = Identifier;export interface LetExpression { type: "LetExpression"; declarations: Array<VariableDeclarator>; return: ReturnStatement;}export interface NumericLiteral { type: "NumericLiteral"; value: number;}export interface AddExpression { type: "AddExpression"; left: Expression; right: Expression;}export interface MultExpression { type: "MultExpression"; left: Expression; right: Expression;}export interface ReturnStatement { type: "ReturnStatement"; argument: Expression;}export interface Identifier { type: "Identifier"; name: string;}export default evaluate;export function getVariable(scope: ScopeList, name: string): number { if (!scope.parent && !scope.variables.has(name)) { throw Error("Variable not found:" + name); } const value = scope.variables.get(name); if (scope.variables.has(name) && typeof value !== "undefined") { return value; } if (scope.parent) return getVariable(scope.parent, name); throw Error("Variable not found:" + name);}
masx200_leetcode_test

Version Info

Tagged at
a year ago