-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheme.js
199 lines (186 loc) · 4.3 KB
/
scheme.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
// Env
// A dictionary of symbol-value pairs
var Env = function(vars, args, outer) {
vars = typeof vars !== "undefined" ? vars : [];
this.scope = {};
var that = this;
vars.forEach(function(val, i) {
that.scope[val] = args[i];
});
outer = typeof outer !== "undefined" ? outer : null;
this.outer = outer;
this.find = function(key) {
return this.scope[key] != null ? this.scope : this.outer.find(key);
};
this.add_methods = function(object) {
var that = this;
Object.getOwnPropertyNames(object).forEach(function(ele) {
if (typeof object[ele] === "function") {
that.scope[ele] = object[ele];
}
});
};
};
// The built-in symbols in the global Env
var built_ins = {
"+": function(x, y) {
return x + y;
},
"-": function(x, y) {
return x - y;
},
"*": function(x, y) {
return x * y;
},
"/": function(x, y) {
return x / y;
},
"<": function(x, y) {
return x < y;
},
"<=": function(x, y) {
return x <= y;
},
">": function(x, y) {
return x > y;
},
">=": function(x, y) {
return x >= y;
},
"%": function(x, y) {
return x % y;
},
length: function(x) {
return x.length;
},
cons: function(l, val) {
l.push(val);
return l;
},
car: function(l) {
return l[0];
},
cdr: function(l) {
return l.slice(1);
},
list: function() {
return Array.prototype.slice.call(arguments, 0);
},
"list?": function(e) {
return e instanceof Array;
},
"null?": function(e) {
return e === [];
},
"symbol?": function(e) {
return typeof e === "string";
}
};
function add_globals(env) {
env.add_methods(Math);
env.add_methods(built_ins);
return env;
}
// Interpreter
// Evaluates S-expressions
var Interpreter = function() {
this.global_env = add_globals(new Env());
this.parse_eval = function(exp) {
return this.eval(this.parse(exp));
};
this.eval = function(x, env) {
env = typeof env !== "undefined" ? env : this.global_env;
var that = this;
if (typeof x === "string") {
// variable reference
return env.find(x)[x];
} else if (!(x instanceof Array)) {
// literal
return x;
} else if (x[0] === "quote") {
// (quote exp)
return x[1];
} else if (x[0] === "if") {
// (if test conseq alt)
var test = x[1],
conseq = x[2],
alt = x[3];
return this.eval(this.eval(test, env) ? conseq : alt, env);
} else if (x[0] === "set!") {
// (set! var exp)
var vari = x[1],
exp = x[2];
env.find(vari)[vari] = this.eval(exp);
} else if (x[0] === "define") {
// (define var exp)
var vari = x[1],
exp = x[2];
env.scope[vari] = this.eval(exp, env);
} else if (x[0] === "lambda") {
// (lambda (var*) exp)
var vars = x[1],
exp = x[2];
return function(args) {
return that.eval(exp, new Env(vars, arguments, env));
};
} else if (x[0] === "begin") {
// (begin exp*)
var f;
for (var i = 1, l = x.length; i < l; i++) {
f = this.eval(x[i]);
}
return f;
} else {
var that = this;
var exps = x.map(function(exp) {
// (proc exp*)
return that.eval(exp, env);
});
var proc = exps.shift();
return proc.apply(this, exps);
}
};
this.parse = function(s) {
return this.read_from(this.tokenize(s));
};
this.tokenize = function(s) {
return s
.replace(/\(/g, " ( ")
.replace(/\)/g, " ) ")
.split(" ")
.filter(function(e) {
return e;
});
};
this.read_from = function(tokens) {
if (tokens.length === 0) throw "unexpected EOF";
token = tokens.shift();
if (token === "(") {
var L = [];
while (tokens[0] !== ")") {
L.push(this.read_from(tokens));
}
tokens.shift();
return L;
} else if (token === ")") {
throw "expected )";
} else {
return this.atom(token);
}
};
this.atom = function(token) {
var t = parseFloat(token);
return isNaN(t) ? token : t;
};
};
if (typeof exports != "object" || exports === undefined) {
// browser context
var scheme = {};
scheme.Interpreter = Interpreter;
scheme.Env = Env;
} // node.js context
else {
// Export these objects
exports.Interpreter = Interpreter;
exports.Env = Env;
}