-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
323 lines (295 loc) · 8.16 KB
/
index.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
// ------------------------------------------ ERRORS
class SchemeError extends Error {
constructor(message) {
super(message)
this.name = this.constructor.name
this.message = message
Error.captureStackTrace(this, this.constructor)
}
}
class ExistenceError extends SchemeError {
constructor(name,type) {
super(`${type} "${name}" not found.`)
this.data = {name,type}
}
}
class ParserError extends SchemeError {
constructor(command) {
super(`Cannot parse command "${command}".`)
this.data = {command}
}
}
// -------------- LET's WRAPP IT
let schemelang = (function(given_base={}){
// ------------------------------------------ INNER METHODS
function get_list(statement) {
let list = handle(statement)
return list.filter(item=>item!='list')
}
function is_string(statement) {
if(typeof statement == 'object') {
return false
}
var count = (statement.match(/"/g) || []).length
return statement.charAt(0) == '"'
&& statement.charAt(statement.length-1) == '"'
&& count == 2
}
// ------------------------------------------ SCHEME BASE
let base = given_base
let methods = {
'define':function(args) {
let name = args[0]
let value = args[1]
let ret = handle(value)
if(typeof ret === 'function') {
return methods[name] = ret
} else {
return base[name] = ret
}
},
'eval':function(args) {
let value = args[0]
while(typeof value == 'object' || typeof value == 'string') {
if(typeof value == 'object') {
value = value.filter(item=>item != 'list')
}
if(is_string(value)) {
return value
}
value = handle(value)
}
return value
},
'+':function(args) {
let end_args = args.map(arg=>handle(arg))
return end_args.reduce(function(acc,cur) {
return acc + cur
})
},
'-':function(args) {
let end_args = args.map(arg=>handle(arg))
if(end_args.length == 1) {
return -end_args[0]
}
return end_args.reduce(function(acc,cur) {
return acc - cur
})
},
'*':function(args) {
let end_args = args.map(arg=>handle(arg))
return end_args.reduce(function(acc,cur) {
return acc * cur
})
},
'/':function(args) {
let end_args = args.map(arg=>handle(arg))
if(end_args.length == 1) {
return 1/end_args[0]
}
return end_args.reduce(function(acc,cur) {
return acc / cur
})
},
'if':function(args) {
let cond = args[0]
let yes = args[1]
let no = args[2]
if(handle(cond)) {
return handle(yes)
} else {
return handle(no)
}
},
// -----------------------------------------------------
'>':function(args) {
return handle(args[0]) > handle(args[1])
},
'<':function(args) {
return handle(args[0]) < handle(args[1])
},
'>=':function(args) {
return handle(args[0]) >= handle(args[1])
},
'<=':function(args) {
return handle(args[0]) <= handle(args[1])
},
'=':function(args) {
return handle(args[0]) == handle(args[1])
},
'and':function(args) {
if(args.length == 0) {
return true
}
let car = handle(args[0])
let cdr = args.slice(1)
if(car === false) {
return false
}
if(cdr.length == 0) {
return car
}
return handle(['and'].concat(cdr))
},
'or':function(args) {
if(args.length == 0) {
return false
}
let car = handle(args[0])
let cdr = args.slice(1)
if(car !== false) {
return car
}
return handle(['or'].concat(cdr))
},
'not':function(args) {
let car = handle(args[0])
if(car !== false) {
return false
}
return !car
},
// -----------------------------------------------------
'null?':function(args) {
let list = get_list(args[0])
return list.length == 0
},
'abslist':function(args) {
return ['list'].concat(args)
},
'car':function(args) {
let list = get_list(args[0])
return list[0]
},
'cdr':function(args) {
let list = get_list(args[0])
return ['list'].concat(list.slice(1))
},
'cadr':function(args) {
return handle(['car',['cdr',args[0]]])
},
'cddr':function(args) {
return handle(['cdr',['cdr',args[0]]])
},
// -----------------------------------------------------
'append':function(args) {
let final_list = ['list']
for(let arg of args) {
let sub_list = get_list(arg)
for(let elm of sub_list) {
final_list.push(elm)
}
}
return final_list
},
'cons':function(args) {
let car = handle(args[0])
let cdr = get_list(args[1])
return ['list',car].concat(cdr)
},
'list':function(args) {
let list = args.map(item=>handle(item))
return handle(['abslist'].concat(list))
},
// -----------------------------------------------------
'clear':function() {
base = {}
return true
},
// -----------------------------------------------------
'lambda':function(args) {
let params = args[0]
let prog = args[1]
return function(inner_args) {
let old_base = JSON.stringify(base)
for(let id=0;id<params.length;++id) {
handle(['define',params[id],inner_args[id]])
}
let ret = handle(prog)
base = JSON.parse(old_base)
return ret
}
},
'let':function(args) {
let params = args[0]
let prog = args[1]
let old_base = JSON.stringify(base)
for(let param of params) {
handle(['define',param[0],param[1]])
}
let ret = handle(prog)
base = JSON.parse(old_base)
return ret
},
'exit':function(args) {
process.exit(0)
}
}
// ------------------------------------------ HANDLING METHODS
// -----------------
function handle(statement) {
if(typeof statement === 'string') {
if(is_string(statement)) {
return statement
}
if(!(statement in base)) {
throw new ExistenceError(statement,'variable')
}
return base[statement]
} else if (typeof statement === 'object') {
let meth = statement[0]
let inner_args = statement.slice(1)
if(!(meth in methods)) {
throw new ExistenceError(meth,'method')
}
return methods[meth](inner_args)
}
return statement
}
// -----------------
function parse(str) {
let str_next_index = 0
let strs = {}
let replacer = str
.replace(/"(.*?)"/g,function(_,string){
let index = str_next_index
str_next_index++
strs[index] = string
return '#'+index+'#'
})
.replace(/(\#t)/g,'true')
.replace(/(\#f)/g,'false')
.replace(/\'\(\)/g,'(abslist)')
.replace(/\'\(/g,'(abslist ')
.replace(/\(/g,'[')
.replace(/\)/g,']')
.replace(/\s/g,',')
.replace(/(\w+\?|\w+|[-!$%^&*()_+|~=`{}:";'<>?.\/])/g,function(match) {
try {
return JSON.parse(match)
} catch(e) {
return '"'+match+'"'
}
})
.replace(/#(.*?)#/g,function(_,index) {
return '"\\"'+strs[index]+'\\""'
})
try {
let obj_command = JSON.parse(replacer)
return obj_command
} catch(err) {
throw new ParserError(str)
}
}
// -----------------
function interpret(str) {
let array = parse(str)
return handle(array)
}
// ------------------------------------------ EXPORTS
this.interpret = interpret
this.parse = parse
this.handle = handle
})
// ------------------------------------------ MODULE EXPORTS
module.exports = schemelang