-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlispy.js
287 lines (267 loc) · 8.85 KB
/
lispy.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const globalEnv = {
'+ ': (op1, op2) => Number(op1) + Number(op2),
'= ': (op1, op2) => Number(op1) === Number(op2),
'- ': (op1, op2) => Number(op1) - Number(op2),
'* ': (op1, op2) => Number(op1) * Number(op2),
'/ ': (op1, op2) => Number(op1) / Number(op2),
'< ': (op1, op2) => Number(op1) < Number(op2),
'> ': (op1, op2) => Number(op1) > Number(op2),
'<= ': (op1, op2) => Number(op1) <= Number(op2),
'>= ': (op1, op2) => Number(op1) >= Number(op2),
pi: Math.PI
}
let copyEnv = {}
const findFunc = (op, env = globalEnv) => {
try {
do {
if (env[op] !== undefined) return env
env = env.parent
} while (env.parent !== null || env.parent !== undefined)
return null
}
catch (e) {
return null
}
}
const findVal = (op, env = globalEnv) => {
op = symbolParser(op)
try {
do {
if (env[op[0]] !== undefined) return [env[op[0]], op[1]]
env = env.parent
} while (env.parent !== null || env.parent !== undefined)
return null
}
catch (e) {
return null
}
}
const contentParse = input => {
let result
input = spaceParser(input)
if (result = numberParser(input) || symbolParser(input)) return [result[0], result[1]]
if (input[0] === '(') {
result = '('
let count = 1
input = input.slice(1)
while (count >= 0 && input.length > 1) {
if (input[0] === '(') count++
if (input[0] === ')') count--
if (count === 0) {
result = result + ')'
input = input.slice(1)
input = spaceParser(input)
return [result, input]
}
result = result + input[0]
input = input.slice(1)
}
}
return null
}
const numberParser = input => {
const result = input.match(/^-?([1-9][0-9]*(\.[0-9]+)?((e|E)[-+]?[0-9]+)?|0(\.[0-9]+)?((e|E)[-+]?[0-9]+)?)/)
return result === null || result[0] === '' ? null : [result[0] * 1, input.slice(result[0].length)]
}
const spaceParser = input => {
const result = input.match(/^(\t|\n|\r| )+/)
return result === null ? input : input.slice(result[0].length)
}
const symbolParser = input => {
result = input.match(/^(([a-zA-Z_]+)|(\+ |- |>= |<= |> |< |= |\* |\/ ))/)
if (!result) return null
return [result[0], input.slice(result[0].length)]
}
const eval = (expr, env = globalEnv) => {
let result = sExpressionParser(expr, env)
return !result || result[1] !== '' ? 'Invalid' : result[0]
}
const sExpressionParser = (expr, env = globalEnv) => {
expr = expr.trim()
let result = specialFormParser(expr, env) || expressionParser(expr, env)
if (result) return result
return null
}
const specialFormParser = (expr, env = globalEnv) => {
if (expr.startsWith('(')) {
expr = spaceParser(expr.slice(1))
return (ifParser(expr, env) || defineParser(expr, env) || beginParser(expr, env) || quoteParser(expr) || lambdaParser(expr, env)) || lambdaEval(expr, env)
}
return null
}
const expressionParser = (expr, env = globalEnv) => {
if (expr.startsWith('(')) {
expr = spaceParser(expr.slice(1))
return procedurecall(expr, env)
}
if (!expr.length >= 1) return null
let atom = atomParse(expr, env)
if (!atom) return null
return [atom[0], atom[1]]
}
const atomParse = (expr, env = globalEnv) => {
expr = expr.trim()
const atom = numberParser(expr) || findVal(expr, env)
return atom
}
const procedurecall = (expr, env = globalEnv) => {
const operands = []
let op = symbolParser(spaceParser(expr))
if (op === null) return null
expr = op[1]
op = op[0]
if (op in globalEnv && typeof globalEnv[op] !== 'object') {
while (expr[0] !== ')') {
expr = spaceParser(expr)
const value = sExpressionParser(expr, env) || numberParser(expr) || atomParse(expr, env)
if (!value) return null
expr = value[1]
expr = spaceParser(expr)
operands.push(value[0])
}
if (operands.length === 1 && op === '/ ') operands.unshift(1)
if (operands.length === 1 && op === '- ') operands.unshift(0)
result = operands.reduce(globalEnv[op])
return [result, spaceParser(expr).slice(1)]
}
let func = findFunc(op, env)
if (func) {
let i = 0
copyEnv = JSON.parse(JSON.stringify(func[op]))
while (expr[0] !== ')' && expr.length > 1) {
expr = spaceParser(expr)
let arg = sExpressionParser(expr, env)
if (!arg) return null
let param = func[op]['args'][i]
copyEnv[param] = arg[0]
copyEnv['parent'] = JSON.parse(JSON.stringify(env))
expr = arg[1]
expr = spaceParser(expr)
i++
}
if (expr[0] !== ')') return null
let result = sExpressionParser(func[op]['body'], copyEnv)
if (!result) return null
return [result[0], spaceParser(expr.slice(1)), copyEnv]
}
return null
}
const ifParser = (expr, env = globalEnv) => {
if (!expr.startsWith('if')) return null
let condition, result
expr = spaceParser(expr.slice(2))
result = procedurecall(spaceParser(expr).slice(1), env)
if (!result) return null
condition = result[0]
expr = spaceParser(result[1])
if (condition) {
result = sExpressionParser(expr, env)
if (!result) return null
expr = contentParse(result[1])[1]
if (!expr) return null
expr = spaceParser(expr)
if (expr[0] !== ')') return null
return [result[0], expr.slice(1)]
}
expr = contentParse(result[1])[1]
result = sExpressionParser(expr, env)
if (!result) return null
expr = spaceParser(result[1])
if (expr[0] !== ')') return null
return [result[0], expr.slice(1)]
}
const defineParser = (expr, env) => {
if (!expr.startsWith('define')) return null
expr = spaceParser(expr.slice(6))
let symbol = symbolParser(expr)
if (!symbol) return null
expr = symbol[1]
symbol = symbol[0]
let value = sExpressionParser(expr)
if (!value) return null
expr = spaceParser(value[1])
if (expr[0] !== ')') return null
if (typeof value[0] === 'object') {
env[symbol] = value[0]
return [symbol, expr.slice(1)]
}
env[symbol] = value[0]
return [symbol, expr.slice(1)]
}
const beginParser = (expr, env = globalEnv) => {
let result
if (!expr.startsWith('begin')) return null
expr = spaceParser(expr.slice(5))
while (!expr.startsWith(')') && expr.length > 1) {
result = sExpressionParser(expr, env)
expr = spaceParser(result[1])
}
return expr !== ')' ? null : [result[0], expr.slice(1)]
}
const quoteParser = expr => {
if (!expr.startsWith('quote')) return null
expr = spaceParser(expr.slice(5))
let result = contentParse(expr)
if (!result) return null
if (spaceParser(result[1]) !== ')') return null
return [result[0], spaceParser(result[1]).slice(1)]
}
const lambdaParser = (expr, env = null) => {
let args = []
if (!expr.startsWith('lambda')) return null
expr = spaceParser(spaceParser(expr.slice(6)))
if (!expr[0] === '(') return null
expr = spaceParser(expr.slice(1))
do {
let arg = symbolParser(expr)
if (!arg) return null
args.push(arg[0])
expr = spaceParser(arg[1])
} while (!expr.startsWith(')'))
if (expr[0] !== ')') return null
expr = spaceParser(expr.slice(1))
let body = contentParse(expr)
if (!body) return null
env = JSON.parse(JSON.stringify(env))
return [{ 'args': args, 'body': body[0], 'parent': env }, spaceParser(body[1]).slice(1)]
}
const lambdaEval = (expr, env = globalEnv) => {
if (!expr.startsWith('(')) return null
let funcObj = sExpressionParser(expr, env)
if (funcObj === null) return null
expr = funcObj[1]
funcObj = funcObj[0]
let i = 0
while (expr[0] !== ')' && expr.length > 1) {
expr = spaceParser(expr)
let arg = sExpressionParser(expr, env)
if (!arg) return null
let param = funcObj['args'][i]
funcObj[param] = arg[0]
expr = arg[1]
expr = spaceParser(expr)
i++
}
if (expr[0] !== ')') return null
const result = sExpressionParser(funcObj['body'], funcObj)
if (result === null) return null
return [result[0], spaceParser(expr.slice(1))]
}
console.log(eval('(define circlearea (lambda (r) (* pi (* r r))))'))
console.log(eval('(define fact (lambda(x)(if(<= x 1) 1 (* x ( fact(- x 1 ) ) ))))'))
console.log(eval('(define sum (lambda(x y) (+ x y) ) )'))
console.log(eval('(define fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))'))
console.log(eval('( fib 5 )'))
console.log(eval('(sum (+ (fact (- 6 1) ) (fact 5)) 5 )'))
console.log(eval('(+ 2 3 (* 5 2 (* 1 2 ) ) 4 5 )'))
console.log(eval('( if ( < 3 2 ) 3 (if (> 4 3 ) 33 44 ))'))
console.log(eval('(circlearea (fact (fact 3)) )'))
console.log(eval('( begin ( + 2 3 ) (+ 4 5 ) (define e 4444 ) (+ 100 100))'))
console.log(eval('(quote ( begin ( + 2 3 ) (+ 4 5 ) (define e 4444 ) (+ 100 100)) )'))
console.log(eval('(- 2 (/ 2))'))
console.log(eval('(circlearea (fact (fact 3)) )'))
console.log(eval('(define repeat (lambda (f) (lambda (x) (f (f x)))))'))
console.log(eval('(define twice (lambda (x) (* 2 x)))'))
console.log(eval('( (repeat twice)10)'))
console.log(eval('( define k (lambda (f) (lambda (z)( lambda(e) (lambda(c)(+ e f z c) ) ))))'))
console.log(eval('((((k 2) 10) 20)100)'))