-
Notifications
You must be signed in to change notification settings - Fork 0
/
lispInterpreter3.js
434 lines (337 loc) · 10.1 KB
/
lispInterpreter3.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const process = require('process')
const readline = require('readline')
const interpret = (input) => evaluate(input)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.on('close', function () {
process.exit(0)
})
repl()
function repl () {
rl.question('\n=>', function (input) {
if (input === ':q' || input === ':quit') {
rl.close()
}
try {
console.log(interpret(input))
} catch (err) {
console.log(err)
} finally {
repl()
}
})
}
const globalEnv = {
'+': (args) => {
if (args.length !== 2) return null
return args[0] + args[1]
},
'-': (args) => {
if (args.length !== 2) return null
return args[0] - args[1]
},
'*': (args) => {
if (args.length !== 2) return null
const ans = args[0] * args[1]
return ans
},
'/': (args) => {
if (args.length !== 2) return null
return args[0] / args[1]
},
'>': (args) => {
if (args.length !== 2) return null
return args[0] > args[1]
},
'<': (args) => {
if (args.length !== 2) return null
return args[0] < args[1]
},
'=': (args) => {
if (args.length !== 2) return null
return args[0] === args[1]
},
'>=': (args) => {
if (args.length !== 2) return null
return args[0] >= args[1]
},
'<=': (args) => {
if (args.length !== 2) return null
return args[0] <= args[1]
},
map: (mapper, args) => args.map(mapper),
list: (args) => args,
cons: ([head, tail]) => [head, ...(Array.isArray(tail) ? tail : [tail])],
pi: 3.14,
sqrt: (arg) => {
if (arg.length !== 1) return null
return Math.sqrt(arg[0])
}
}
const evaluate = (input) => {
// console.log('in eval')
input = input.replace(/[(]/g, ' ( ').replace(/[)]/g, ' ) ')
let output = bracketParser(input, globalEnv) || numberParser(input, globalEnv) || symbolParser(input, globalEnv)
if (!output) {
// console.log('return evaluate')
return 'invalid expression'
}
while (Array.isArray(output) && typeof output[output.length - 1] === 'string' && output.length > 1) {
output = output.slice(0, output.length - 1)
if (output.length === 1 && Array.isArray(output[0])) output = output[0]
}
// potential threats with block for slicing the array
// console.log('return evaluate')
return output[0]
}
const expressionParser = (input, env = globalEnv) => {
// console.log('in expression')
input = input.trim()
const output = symbolParser(input, env) || numberParser(input, env) || bracketParser(input, env) || procedureParser(input, env) || specialFormParser(input, env)
// console.log('return expression')
return output
}
const symbolParser = (input, env) => {
// console.log('in symbol')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
input = input.slice(literal.length)
const value = env[literal]
if (value !== undefined && typeof value !== 'function') {
// console.log('return symbol')
return [value, input]
}
return null
}
const numberParser = (input, env) => {
// console.log('in num')
const pattern = /^(-)?(0|[1-9]\d*)(\.\d+)?((e|E)(\+|-)?\d+)*/
const value = input.match(pattern)
if (value) {
input = input.slice(value[0].length)
// console.log('return num')
return [parseFloat(value[0]), input]
}
return null
}
const bracketParser = (input, env) => {
// console.log('in bracket')
input = input.trim()
if (input[0] !== '(') return null
input = input.slice(1)
if (input.trim().startsWith('(')) return null
const parsed = expressionParser(input, env)
if (parsed === null) return null
if (!Array.isArray(parsed)) {
// console.log('return bracket')
return [parsed, '']
}
input = parsed[1].trim()
if (input[0] === ')') {
// console.log('return bracket')
return [parsed[0], input.slice(1)]
}
}
const procedureParser = (input, env) => {
// console.log('in procedure')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
input = input.slice(literal.length)
const procedure = env[literal]
if (typeof procedure === 'function') {
const args = getArguments(input, env)
const procedureOp = procedure(args[0])
if (Array.isArray(args) && procedureOp !== null) {
// console.log('return procedure')
return [procedureOp, args[1]]
}
}
return null
}
const getArguments = (input, env) => {
// console.log(' in getarg')
input = input.trim()
let parsed = expressionParser(input, env)
let arg = parsed[0]
while (Array.isArray(arg) && typeof arg[arg.length - 1] === 'string') {
arg = arg[0]
if (!arg) return null
}
const args = []
args.push(arg)
input = parsed[1].trim()
// console.log('onebyone', args)
while (!parsed[1].trim().startsWith(')')) {
parsed = expressionParser(parsed[1], env)
let arg = parsed[0]
while (Array.isArray(arg) && typeof arg[arg.length - 1] === 'string') {
arg = arg[0]
if (!arg) return null
}
args.push(arg)
}
// console.log('onebyone', args)
input = parsed[1].trim()
// console.log('return getarg')
return [args, input]
}
const specialFormParser = (input, env) => {
// console.log('in specialform')
for (const prop in specialForms) {
const parsed = specialForms[prop](input, env)
if (parsed) {
// console.log('return specialform')
return parsed
}
}
return null
}
const defineParser = (input, env) => {
// console.log('in define')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'define') return null
input = input.slice(literal.length).trim()
const symbol = input.match(/[^\s]+/)[0]
input = input.slice(symbol.length)
const parsed = expressionParser(input)
if (!parsed) return null
env[symbol] = parsed[0]
// console.log('return define')
return Array.isArray(parsed) ? [symbol, parsed[1]] : symbol
}
const lambdaParser = (input, env) => {
// console.log('in lambda')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'lambda') return null
input = input.slice(literal.length)
input = input.trim().slice(1).trim()
let parameters = input.match(/[^)]+/)[0]
input = input.slice(parameters.length).trim().slice(1).trim()
let bracketCount = 0
let position = 0
for (let i = 0; i < input.length; i++) {
if (input[i] === '(') bracketCount++
if (input[i] === ')') bracketCount--
if (bracketCount < 0) {
position = i
break
}
}
parameters = parameters.trim().split(/\s+/)
const func = function (localArgs) {
const localEnv = Object.create(env)
parameters.forEach((param, i) => {
localEnv[param] = localArgs[i]
})
const parsed = expressionParser(input, localEnv)
return parsed
}
// console.log('return lambda', input)
const rest = input.slice(position)
return [func, rest]
}
const ifParser = (input, env) => {
// console.log('in if')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'if') return null
input = input.slice(literal.length)
const parsed1 = expressionParser(input, env)
const test = parsed1[0]
const parsed2 = expressionParser(parsed1[1], env)
const conseq = parsed2[0]
let bracketCount = 0
let position = 0
input = parsed2[1]
for (let i = 0; i < input.length; i++) {
if (input[i] === '(') bracketCount++
if (input[i] === ')') bracketCount--
if (bracketCount < 0) {
position = i
break
}
}
input = input.slice(position)
if (test) return [conseq, input]
const parsed3 = expressionParser(parsed2[1], env)
const alt = parsed3[0]
const rest = parsed3[1]
// console.log('return if')
return [alt, rest]
}
const setParser = (input, env) => {
// console.log('in set')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'set!') return null
input = input.slice(literal.length).trim()
const symbol = input.match(/[^\s]+/)[0]
input = input.slice(symbol.length)
if (env[symbol] && typeof env[symbol] !== 'function') {
const value = expressionParser(input, env)
env[symbol] = value
// console.log('return set')
return [symbol, input]
}
return null
}
const quoteParser = (input, env) => {
// console.log('in quote')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'quote') return null
input = input.slice(literal.length).trim()
if (input[0] !== '(') {
const string = input.match(/[^\s]+/)[0]
input = input.slice(string.length)
// console.log('return quote')
return [string, input]
}
input = input.slice(1)
const string = input.match(/[^)]+/)[0]
input = input.slice(string.length)
// console.log('return quote')
return [string, input.slice(1)]
}
const beginParser = (input, env) => {
// console.log('in begin')
input = input.trim()
const literal = input.match(/[^\s]+/)[0]
if (literal !== 'begin') return null
input = input.slice(literal.length).trim()
let value
let parsed = expressionParser(input, env)
while (input !== ')') {
parsed = expressionParser(parsed[1], env)
value = parsed[0]
input = parsed[1].trim()
}
// console.log('return begin')
return [value, input]
}
const specialForms = {
define: defineParser,
lambda: lambdaParser,
if: ifParser,
set: setParser,
quote: quoteParser,
begin: beginParser
}
// // console.log(evaluate('(define func (lambda (x) (if (= x 1) 1 0)))'))
// // console.log(evaluate('(func 1)'))
// // console.log(evaluate('(define r 1)'))
// // console.log(evaluate('(set! r 2)'))
// // console.log(evaluate('(r)'))
// // console.log(evaluate('(define r quote (nfkjefkejdk))'))
// // console.log(evaluate('(begin (define r 10) (* pi (* r r)))'))
// // console.log(evaluate('(define circle-area (lambda (r) (* pi (* r r)))'))
// // console.log(evaluate('(define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))'))
// // console.log(evaluate('(circle-area (fact 10))'))
// console.log(evaluate('(define fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))'))
// // console.log(evaluate('(define range (lambda (a b) (if (= a b) (quote ()) (cons a (range (+ a 1) b)))))'))
// // console.log(evaluate('(range 0 4)'))
// console.log(evaluate('(fib 10)'))