-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·204 lines (154 loc) · 4.16 KB
/
index.js
File metadata and controls
executable file
·204 lines (154 loc) · 4.16 KB
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
#!/usr/bin/env node
// Jasp!
var
fs = require("fs"),
clog = console.log,
cerr = console.error,
argv = require("yargs").argv,
fname = argv._.pop()
;
function usage () {
return "jasp <filename>\n";
}
if (!fname) {
cerr( usage() );
process.exit();
}
fname = (fname[0] === '/') ? fname : './' + fname;
var prog = JSON.parse( fs.readFileSync(fname, "utf8") ),
genv = jasp_env(false)
;
genv.defvar("+", function (x, y) { return x + y; });
genv.defvar("-", function (x, y) { return x - y; });
genv.defvar("*", function (x, y) { return x * y; });
genv.defvar("/", function (x, y) { return x / y; });
genv.defvar("%", function (x, y) { return x % y; });
genv.defvar("=", function (x, y) { return x === y; });
genv.defvar(">", function (x, y) { return x > y; });
genv.defvar(">=", function (x, y) { return x >= y; });
genv.defvar("<", function (x, y) { return x < y; });
genv.defvar("<=", function (x, y) { return x <= y; });
genv.defvar("display", function (x) { console.log(x); });
function jasp_env (parental) {
var me = { },
iface = {
lookup: lookup,
defvar: defvar,
setvar: setvar,
extend: extend
}
;
function boo (name, msg) {
cerr("Symbol \"" + name + "\" " + msg);
process.exit();
}
function lookup (name) {
if (me[name] !== undefined) {
return me[name];
}
if (parental) {
return parental.lookup(name);
}
boo(name, "not defined!");
}
function defvar (name, val) {
if (me[name] !== undefined) {
boo(name, "already defined in this environment!");
}
me[name] = val;
}
function setvar (name, val) {
if (me[name] !== undefined) {
me[name] = val;
return null;
}
if (parental) {
return parental.setvar(name, val);
}
boo(name, "not defined!");
}
function extend (vars) {
var noob = jasp_env(iface);
Object.keys(vars).forEach(
function (k) {
noob.defvar(k, vars[k]);
}
);
return noob;
}
return iface;
}
function jasp_eval (code, env) {
if (code === undefined) {
cerr([ "WTF", code ]);
return null;
}
if (code.operator) {
var ctable = {
"define": jasp_define,
"set!": jasp_set,
"if": jasp_if,
"quote": jasp_quote,
"lambda": jasp_lambda,
"eval": function (c, env) { return jasp_eval(jasp_eval(c.code, env), env); }
};
var op = ctable[code.operator];
if (op) {
return op(code, env);
}
var fun = env.lookup(code.operator);
return fun.apply(null, jasp_seq(code["arguments"], env) );
}
else {
if (Array.isArray(code)) {
return jasp_seq(code, env).pop();
}
if ( (typeof code === "string") && code[0] === "$") {
return env.lookup(code.slice(1));
}
return code;
}
}
function jasp_define (code, env) {
env.defvar(code.name, null);
return env.setvar(code.name, jasp_eval(code.value, env));
}
function jasp_set (code, env) {
return env.setvar(code.name, jasp_eval(code.value, env));
}
function jasp_if (code, env) {
var is_true = jasp_eval(code.predicate, env);
if (is_true) {
return jasp_eval(code.then, env);
}
return jasp_eval(code.else, env);
}
function jasp_quote(code, env) {
return code.value;
}
function jasp_seq(code, env) {
function enveval (x) {
return jasp_eval(x, env);
}
return code.map(enveval);
}
function jasp_lambda (code, env) {
function jasp_apply (_, args) {
var ext = { };
code["arguments"].forEach(
function (name, idx) {
ext[name] = args[idx];
}
);
var noob = env.extend(ext);
return jasp_eval(code.body, noob);
}
return {
env: env,
formals: code["arguments"],
body: code.body,
apply: jasp_apply
};
}
// Ok, go!!!
jasp_eval(prog, genv);