-
Notifications
You must be signed in to change notification settings - Fork 0
/
jipsy.js
566 lines (484 loc) · 14.6 KB
/
jipsy.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/* @file jipsy.js
* @author Jonathan Pavlik
*
*/
const library = {
/*
* @function first
* @param {identifier[]} list - A list of identifiable types.
*
* @return {identifier} the first element of a list.
*/
first: function (list) {
//gets first item of list
if(!(list instanceof Array)) {
return `Error: non-list type`;
}
return list[0];
},
/*
* @function rest
* @param {identifier[]} list - A list of identifiable types.
*
* @return {identifier[]} the rest of a list.
*/
rest: function (list) {
//cuts of the first element of the list
if(!(list instanceof Array)) {
return `Error: non-list type`;
}
return list.slice(1);
},
/*
* @function print
* @param {identifier} the type to be printed.
*
* @return {identifier} they type being printed.
*/
print: function (param) {
//prints the argument and returns it
console.log(param);
return param;
}
};
//for storing user written functions and their logic
let written_funcs = {};
let written_funcs_logic = {};
//for storing all lisp functions
const lisp_functions = {
/*
* @function +
* Adds all numbers in a list
* @param {identifier[]} input - A list of identifiable types, presumably numbers.
* @param {object} context - the context of a identifier.
*
* @return {identifer} with a type of number.
*/
'+': function (input, context) {
let nums = input.splice(1);
nums.forEach((part, pos) => {
if(part instanceof Array) {
nums[pos].value = interpret(part, context);
}
});
let answer = {type: 'number'};
answer.value = nums.reduce((accum, curr) => {
return accum + curr.value;
}, 0);
return interpret(answer, context);
},
/*
* @function -
* Subtracts all numbers in a list
* @param {identifier[]} input - A list of identifiable types, presumably numbers.
* @param {object} context - the context of a identifier.
*
* @return {identifer} with a type of number.
*/
'-': function (input, context) {
let nums = input.splice(2);
nums.forEach((part, pos) => {
if(part instanceof Array) {
nums[pos].value = interpret(part, context);
}
});
let answer = {type: 'number'};
answer.value = nums.reduce((accum, curr) => {
return accum - curr.value;
}, interpret(input[1], context));
return interpret(answer, context);
},
/*
* @function *
* Multiplies all numbers in a list
* @param {identifier[]} input - A list of identifiable types, presumably numbers.
* @param {object} context - the context of a identifier.
*
* @return {identifer} with a type of number.
*/
'*': function (input, context) {
let nums = input.splice(2);
nums.forEach((part, pos) => {
if(part instanceof Array) {
nums[pos].value = interpret(part, context);
}
});
let answer = {type: 'number'};
answer.value = nums.reduce((accum, curr) => {
return accum * curr.value;
}, interpret(input[1], context));
return interpret(answer, context);
},
/*
* @function /
* Divides all numbers in a list
* @param {identifier[]} input - A list of identifiable types, presumably numbers.
* @param {object} context - the context of a identifier.
*
* @return {identifer} with a type of number.
*/
'/': (input, context) => {
let nums = input.splice(2);
nums.forEach((part, pos) => {
if(part instanceof Array) {
nums[pos].value = interpret(part, context);
}
});
let answer = {type: 'number'};
answer.value = nums.reduce((accum, curr) => {
return accum / curr.value;
}, interpret(input[1], context));
return interpret(answer, context);
},
/*
* @function %
* Modulos all numbers in a list
* @param {identifier[]} input - A list of identifiable types, presumably numbers.
* @param {object} context - the context of a identifier.
*
* @return {identifer} with a type of number.
*/
'%': (input, context) => {
let nums = input.splice(2);
nums.forEach((part, pos) => {
if(part instanceof Array) {
nums[pos].value = interpret(part, context);
}
});
let answer = {type: 'number'};
answer.value = nums.reduce((accum, curr) => {
return accum % curr.value;
}, interpret(input[1], context));
return interpret(answer, context);
},
/*
* @function defun
* let's the user define a function
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {string} an error message if there is an error with the syntax.
*/
defun: function (input, context) {
const func = input[1].value;
const args = input[2];
const logic = input[3];
let send_params = {};
if(!(func in written_funcs)) {
written_funcs_logic[func] = logic;
written_funcs[func] = (params) => {
if(args.length !== params.length) {
throw Error(`Expected ${args.length} number of arguments but received ${params.length}.`);
}
for(pos in params) {
send_params[args[pos].value] = params[pos].value;
}
let answer = interpret(parse(build_logic(written_funcs_logic[func], send_params)));
return answer;
};
} else {
return Error(`Function ${func} already defined`);
}
},
/*
* @function if
* provides if logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} the code that will be run.
*/
if: function (input, context) {
return interpret(input[1], context) ?
interpret(input[2], context) :
interpret(input[3], context);
},
/*
* @function eq
* provides equal logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether two things are equal
*/
eq: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) === interpret(args[1], context);
return interpret(answer, context);
},
/*
* @function neq
* provides not equal logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether two things are not equal
*/
neq: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) !== interpret(args[1], context);
return interpret(answer, context);
},
/*
* @function lt
* provides less than logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether one thing is less than another
*/
lt: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) < interpret(args[1], context);
return interpret(answer, context);
},
/*
* @function lte
* provides less than equal to logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether one thing is less than or equal to another
*/
lte: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) <= interpret(args[1], context);
return interpret(answer, context);
},
/*
* @function gt
* provides greater than logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether one thing is greater than another
*/
gt: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) > interpret(args[1], context);
return interpret(answer, context);
},
/*
* @function gte
* provides greater than equal to logic
* @param {identifier[]} list - A list of identifiable types
* @param {object} context - the context of a identifier.
*
* @return {indentifier} whether one thing is greater than or equal to another
*/
gte: function (input, context) {
let args = input.splice(1);
if(args.length > 2) {
return `Error: can not compare more than 2 args and recieved ${args.length}`;
}
let answer = {type: 'boolean'};
answer.value = interpret(args[0], context) >= interpret(args[1], context);
return interpret(answer, context);
},
let: function (input, context) {
let let_cont = input[1].reduce((cur, accum) => {
accum.scope[cur[0].value] = interpret(cur[1], context);
return accum;
}, new Context({}, context));
return interpret(input[2], context);
}
};
/*
* @class Context
* allows us to define a type known as context, that gives a param its scope and parent
*
*/
class Context {
constructor(scope, parent) {
this.scope = scope;
this.parent = parent;
};
get(identifier) {
if (identifier in this.scope) {
return this.scope[identifier];
} else if (this.parent !== undefined) {
return this.parent.get(identifier);
}
};
}
/*
* @function build_logic
* builds the logic for a function
* @param {function} logic - The logic for a function
* @param {identifier[]} params - the list of identifiers that are the paremeters for the function.
*
* @return {string} the function logic
*/
function build_logic (logic, params) {
//creates deep copy
let copy = JSON.parse(JSON.stringify(logic));
copy.forEach((item) => {
if(item.value in params) {
item.value = params[item.value];
}
});
let final = copy.splice(1).reduce((accum, param) => {
if(param instanceof Array) {
param.value = interpret(parse(build_logic(param, params)));
}
return accum + ` ${param.value}`;
}, `(${copy[0].value}`);
final += ')';
return final;
};
/*
* @function interpretList
* interprets a lisp list
* @param {identifier[]} input - A list of identifiable types.
* @param {object} context - the context of a identifier.
*
* @return the interpretation of a lisp list/funct
*/
function interpretList (input, context) {
const ident_val = input[0].value;
//console.log('in', input);
if (input.length > 0 && ident_val in lisp_functions) {
return lisp_functions[ident_val](input, context);
} else if (input.length > 0 && ident_val in written_funcs) {
const params = input.splice(1);
return written_funcs[ident_val].call(undefined, params);
} else {
let interpreted_values = input.map( (cur) => {
return interpret(cur, context);
});
if (interpreted_values[0] instanceof Function) {
return interpreted_values[0].apply(undefined, interpreted_values.slice(1));
} else {
return interpreted_values;
}
}
};
/*
* @function interpret
* interprets a lisp element
* @param {identifier} input - an type to be identified
* @param {object} context - the context of a identifier.
*
* @return the interpretation of a lisp element
*/
function interpret(input, context) {
if (context === undefined) {
return interpret(input, new Context(library));
} else if (input instanceof Array) {
return interpretList(input, context);
} else if (input.type === "identifier") {
return context.get(input.value);
} else if (input.type === "number" || input.type === "string") {
return input.value;
} else if (input.type === "boolean") {
return input.value;
}
};
/*
* @function categorize
* identifies the type of element in a lisp list
* @param {identifier} input an element
*
* @return {object} containg the type and value of an element
*/
function categorize(input) {
if(!isNaN(parseFloat(input))) {
return { type: 'number', value: parseFloat(input) };
} else if (input[0] === '"' && input.slice(-1) === '"') {
return { type: 'string', value: input.slice(1,-1) };
} else {
return { type: 'identifier', value: input };
}
};
/*
* @function parenthesise
* makes sure the lisp expression is properly parenthesized
* @param {string} input - the input string
* @param {character} list - of characters to keep track
*
* @return the sub lists within a lisp expression
*/
function parenthesize(input, list) {
if(list === undefined) {
return parenthesize(input, []);
} else {
let token = input.shift();
if(token === undefined) {
//no token get the end of the list
return list.pop();
} else if (token === "(") {
//beginning of a list
list.push(parenthesize(input, []));
return parenthesize(input, list);
} else if (token === ")") {
//end of a list
return list;
} else {
//reccursively go through
return parenthesize(input, list.concat(categorize(token)));
}
}
};
/*
* @function tokenize
* tokenizes the function
* @param {string} input - properly parses a string
*
* @return the parsed string
*/
function tokenize(input) {
return input.split('"')
.map((str, pos) => {
//put a space around lists
if(pos % 2 == 0) {
//not in string
return str.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ');
} else {
//replace all whitespace with characters
return str.replace(/ /g, "!ws!");
}
})
.join('"')
.trim()
.split(/\s+/)
.map((str) => {
//after rejoining all words replace temp ws character with
//real whitespace
return str.replace(/!ws!/g, " ");
});
};
/*
* @function parse
* parses the function
* @param {string} input - properly parses a string
*
* @return the evaluted lisp
*/
function parse(input) {
return parenthesize(tokenize(input));
};
module.exports = {
parse: parse,
interpret: interpret
};