-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsLisp.js
136 lines (119 loc) · 2.54 KB
/
jsLisp.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
var lispObject = {
/*
* Seven primitive operators
*/
quote: function(x) {
return x;
},
atom: function(x) {
if (x) {
var typeOfX = typeof x;
if (checkIsArray(x)) {
if (x.length < 1) {
return 1;
}
}
else if (typeOfX === 'number' || typeOfX === 'string') {
return 1;
}
}
},
eq: function(x,y) {
if (x === y) {
return 1;
}
},
car: function(x) {
if (checkIsArray(x)) {
return x.shift();
}
},
cdr: function(x) {
if (checkIsArray(x)) {
x.shift();
return x;
}
},
cons: function(x,y) {
if (x && checkIsArray(y)) {
return y.unshift(x);
}
},
cond: function(x) {
if (checkIsArray(x)) {
x.map(function(element) {
if (checkIsArray(element) && evaluate(element[0])) {
return element[1];
}
});
}
},
/*
* Some functions
*/
_null: function(x) {
return(eq(x, null));
},
_and: function(x,y) {
return eq(evaluate(x), evaluate(y))
},
_not: function(x) {
return cond([[x, []],[True, True]]);
},
_append: function(x, y) {
return "not inplemented";
},
evaluate: function(input) {
if (checkIsArray(input) && input.length > 1) {
var operator = input.shift();
if (input.length > 1) {
if ((typeof operator) === 'string') {
switch (operator) {
case "quote":
return this.quote(input);
case "atom":
return this.atom(input);
case "eq":
return this.eq(input);
case "car":
return this.car(input);
case "cdr":
return this.cdr(input);
case "cons":
return this.cons(input);
case "cond":
return this.cond(input);
case "_null":
return this._null(input);
case "_and":
return this._and(input);
case "_not":
return this._not(input);
default:
alert("oh crap. no such command");
break;
}
}
}
else if ( input.length == 1) {
if (checkIsArray(input[0])) {
this.evaluate(input[0]);
}
else {
arguments
}
}
}
}
}
var checkIsArray = function(input) {
var typeOfInput;
if (input) {
var typeOfInput = typeof input;
if (typeOfInput === 'object') {
if (input instanceof Array) {
return 1;
}
}
}
}