-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.js
224 lines (206 loc) · 4.45 KB
/
lisp.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
const readline = require('readline-sync')
class Symbol {
constructor(token) {
Object.assign(this, { token })
}
}
class Lambda {
constructor(params, body, env) {
Object.assign(this, { params, body, env })
}
}
class Env {
constructor(parent) {
this.parent = parent
this.values = {}
}
create() {
return new Env(this)
}
set(key, value) {
this.values[key] = value
return this
}
find(key) {
if (Object.keys(this.values).includes(key)) {
return this
}
if (this.parent) {
return this.parent.find(key)
}
return undefined
}
get(key) {
const env = this.find(key)
if (env) {
return env.values[key]
}
return undefined
}
}
const tokensToAST = (tokens) => {
const token = tokens.shift()
if (token === '(') {
const ast = []
while (tokens[0] !== ')') {
ast.push(tokensToAST(tokens))
}
tokens.shift()
return ast
} else if (token.match(/^'$/g)) {
const ast = []
ast.push(new Symbol('quote'))
ast.push(tokensToAST(tokens))
return ast
} else if (token.match(/^-?[0-9]+$/g)) {
return parseInt(token)
} else if (token.match(/^"(?:\\"|[^"])*"$/g)) {
return token
.substr(1, token.length - 2)
.replace(/\\\\/g, '\\')
.replace(/\\"/g, '"')
} else if (token.match(/^(?:true|false)$/g)) {
return token === 'true'
} else if (token.match(/^nil$/g)) {
return null
} else {
return new Symbol(token)
}
}
const astToString = (ast, escaped) => {
if (Array.isArray(ast)) {
return `(${ast.map((a) => astToString(a, escaped)).join(' ')})`
} else if (typeof ast === 'string') {
if (escaped === true) {
ast = `"${ast.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`
}
return ast
} else if (ast instanceof Symbol) {
return ast.token
} else {
return `${ast}`
}
}
const read = (str) => {
return tokensToAST(str.match(/("(?:\\"|[^"])*"|[\(\)']|[^\(\)\s'"]+)/g))
}
const _eval = (ast, env) => {
while (true) {
if (Array.isArray(ast)) {
const [head, ...tail] = ast
if (head instanceof Symbol) {
if (head.token === 'quote') {
return tail[0]
}
if (head.token === 'if') {
let i = 0
const predicate = eval(tail[0], env)
if (!predicate) {
i += 1
}
ast = tail[i + 1]
continue
}
if (head.token === 'def!') {
const key = tail[0]
const value = eval(tail[1], env)
env.set(key.token, value)
return value
}
if (head.token === 'do') {
let index = 0
while (index < tail.length - 1) {
eval(tail[index], env)
index += 1
}
if (index === tail.length) {
return null
}
ast = tail[index]
continue
}
if (head.token === 'let*') {
env = env.create()
for (let index = 0; index < tail[0].length; index += 2) {
const key = tail[0][index + 0]
const value = eval(tail[0][index + 1], env)
env.set(key.token, value)
}
ast = tail[1]
continue
}
if (head.token === 'fn*') {
const [params, body] = tail
return new Lambda(params, body, env.create())
}
const func = eval(head, env)
const args = tail.map((e) => eval(e, env))
if (func instanceof Lambda) {
for (let index = 0; index < tail.length; index++) {
const key = func.params[index]
const value = args[index]
func.env.set(key.token, value)
}
ast = func.body
env = func.env
continue
} else {
return func.apply(func, args)
}
}
} else if (ast instanceof Symbol) {
return env.get(ast.token)
} else {
return ast
}
}
}
let running = true
let debug = false
const eval = (ast, env) => {
const res = _eval(ast, env)
if (debug === true) {
console.log(`${astToString(ast, true)}: ${astToString(res, true)}`)
}
return res
}
const prnt = (exp) => {
if (exp !== undefined) {
console.log(astToString(exp, true))
}
return null
}
const core = Object.assign(require('./core'), {
debug() {
debug = !debug
return undefined
},
exit() {
running = false
return undefined
},
str(...args) {
return args.map((a) => astToString(a)).join('')
},
read(str) {
return read(str)
},
eval(ast) {
return eval(ast, env)
},
print(exp) {
return prnt(exp)
},
require(path) {
return eval(read(`(eval (read (str "(do " (read-file "${path}") " nil)")))`), env)
},
})
const env = Object.entries(core).reduce((env, [k, v]) => env.set(k, v), new Env())
eval(read('(require "core.lisp")'), env)
eval(read('(help)'), env)
while (running) {
const str = readline.question('lisp-node> ')
if (str !== '') {
prnt(eval(read(str), env))
}
}