-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlixlib.js
executable file
·330 lines (303 loc) · 8.51 KB
/
lixlib.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
function counterGenerator() {
var count = 0;
return function () {
var ret = count;
count = (count + 1);
return ret;
};
}
var getCount = counterGenerator();
function generateFuncArgs(args, env) {
var ret = [];
for (var i = 0; i < args.length; i++) {
var varname = args[i][0];
ret.push(varname);
env[varname] = true;
}
return ret.join(", ");
}
var ctx0 = function (x) {
return x;
};
function generateSeq(expr, env, ctx) {
var ret = "";
var seq = expr[0];
var length = seq.length;
for (var i = 0; i < length; i++) {
var seqCtx = ctx0;
if (i === length - 1) {
seqCtx = ctx;
}
ret += generate(seq[i], env, seqCtx) + ";\n";
}
return ret;
}
function generateOr(expr, env, ctx) {
var ret = "(";
ret += generate(expr[0], env, ctx0);
ret += " || ";
ret += generate(expr[2], env, ctx0);
ret += ")";
return ctx(ret);
}
function generateAnd(expr, env, ctx) {
var ret = "(";
ret += generate(expr[0], env, ctx0);
ret += " && ";
ret += generate(expr[2], env, ctx0);
ret += ")";
return ctx(ret);
}
function generateIf(expr, env, ctx) {
var cases = expr[0];
var ret = [];
var retVar = '__if_retval__' + getCount();
var ifCtx = ctx;
// var ifCtx = function (v) {
// return retVar + ' = ' + v;
// };
for (var i = 0; i < cases.length; i++) {
// console.log(cases[i]);
if (cases[i][1] === 'else') {
var elsecase = '{\n' + generate(cases[i][0], env, ifCtx) + '\n}';
ret.push(elsecase);
} else if (cases[i][1] === 'if') {
ret.push(generateIf(cases[i], env, ctx0));
} else {
var ifcase = 'if (' + generate(cases[i][0], env, ctx0) + ') {\n'
+ generate(cases[i][1], env, ifCtx) + '\n}';
ret.push(ifcase);
}
}
// return 'var ' + retVar + ';\n' + ret.join(' else ') + ctx(retVar);
return ret.join(' else ');
}
var operateFuncName = {
"+": '__add__',
"-": '__sub__',
"*": '__mul__',
"/": '__div__',
"%": '__mod__',
">=": '__ge__',
">": '__gt__',
"==": '__eq__',
"<": '__lt__',
"<=": '__le__',
};
function generateAtomic(expr, env, ctx) {
var ret = expr[0];
if (expr[2] === '{var}') {
if (operateFuncName[expr[0]]) {
ret = operateFuncName[expr[0]];
} else {
ret = transformVarName(expr[0]);
}
}
return ctx(ret);
}
function generatePropertyName(expr, env, ctx) {
if (expr[1] === '{atomic}') {
var ret = generateAtomic(expr, env, ctx);
return ctx(ret);
} else if ((expr[1] === '{index}')) {
var ret = generateAtomic(expr[0], env, ctx0);
return ctx(ret);
}
}
function generateProperty(expr, env, ctx) {
var propertyName = generatePropertyName(expr[0], env, ctx0);
var propertyValue = generate(expr[2], env, ctx0);
return propertyName + ": " + propertyValue;
}
function generateObjectLiteral(expr, env, ctx) {
var list = expr[0];
var length = list.length;
var collect = [];
for (var i = 0; i < length; i++) {
collect[i] = generateProperty(list[i], env, ctx0);
}
var ret = "{\n" + collect.join(",\n") + "\n}";
return ctx(ret);
}
var transformVarName = function (name) {
return name;
}
function getVarName(expr) {
if (expr[1] === '{atomic}') {
return generateAtomic(expr, {}, ctx0);
} else if (expr[1] === '{.}') {
return getVarName (expr[0]);
}
}
function generateField(expr, env, ctx) {
if (expr[1] === '{atomic}') {
var ret = '.' + generateAtomic(expr, env, ctx0);
return ret;
} else if (expr[1] === '{index}') {
var ret = '[' + generate(expr[0], env, ctx0) + ']';
return ret;
}
}
function generateFieldAccess(expr, env, ctx) {
if (expr[1] === '{atomic}') {
var ret = generateAtomic(expr, env, ctx0);
return ctx(ret);
} else if (expr[1] === '{.}') {
var field = generateField(expr[2], env, ctx0);
var ret = generateFieldAccess(expr[0], env, ctx0) + field;
return ctx(ret);
}
}
function generateArray(expr, env, ctx) {
var ret = "[";
var arr = expr[0];
var length = arr.length;
for (var i = 0; i < length; i++) {
ret += generate(arr[i], env, ctx0) + ",";
}
ret += "]";
return ctx(ret);
}
function generateMethod(expr, env, ctx) {
var method = generateField(expr[0], env, ctx0);
var localVarName = '_var_' + getCount();
var localValueName = '_val_' + getCount();
method = localVarName + method;
ret = '(function (' + localVarName + ", " + localValueName + ") {\n" +
"if (typeof " + method + " === 'function') {\n" +
"arguments = Array.prototype.slice.call(arguments, 1, arguments.length);" +
"return " + method + ".apply(" + localVarName + ", arguments);\n" +
"} else if (" + method + " !== undefined) {\n" +
"if (" + localValueName + " === undefined) {\n" +
"return " + method + ';\n' +
"}\n" +
method +" = " + localValueName + ";\n" +
"}" +
"return " + localVarName + ';\n' +
'})';
return ctx(ret);
}
function generateWhile(expr, env, ctx) {
var cond = generate(expr[0], env, ctx0);
var seq = generate(expr[2], env, ctx0);
var ret = 'while (' + cond + ') {\n' +
seq +
"}\n" +
ctx('null');
return ret;
}
function generateBreak(expr, env, ctx) {
return 'break';
}
function generate (expr, env, ctx) {
if (expr[1] === '{start}') {
return generate(expr[0], env, ctx);
} else if (expr[1] === '{seq}') {
return generateSeq(expr, env, ctx);
} else if (expr[1] === '{mono}') {
return generate(expr[0], env, ctx);
} else if (expr[1] === '{atomic}') {
return generateAtomic(expr, env, ctx);
} else if (expr[1] === '{.}') {
return generateFieldAccess(expr, env, ctx);
} else if (expr[1] === '{func}') {
var funcEnv = env_new(env);
var args = generateFuncArgs(expr[0], funcEnv);
var body = generate(expr[2], funcEnv, function(v) {
return 'return ' + v;
});
var ret = '(function (' + args + ') {\n' + body + '\n})';
return ctx(ret);
} else if (expr[1] === '{array}') {
return generateArray(expr, env, ctx);
} else if (expr[1] === '{object}') {
return generateObjectLiteral(expr, env, ctx);
} else if (expr[1] === '{method}') {
return generateMethod(expr, env, ctx);
} else if (expr[1] === '{empty}') {
return ctx(0);
} else if (expr[1] === ':=') {
var varname = expr[0][0];
var ret = 'var ' + varname + ' = ' + generate(expr[2], env, ctx0) + ';\n' + ctx(varname);
env[varname] = true;
return ret;
} else if (expr[1] === '=') {
var varname = getVarName(expr[0]);
if (env[varname] == undefined) {
console.log(varname);
var_not_defined;
}
var fieldChain = generate(expr[0], env, ctx0);
var ret = "(" + fieldChain + " = " + generate(expr[2], env, ctx0) + ")";
return ctx(ret);
} else if (expr[1] === 'if') {
return generateIf(expr, env, ctx);
} else if (expr[1] === 'and') {
return generateAnd(expr, env, ctx);
} else if (expr[1] === 'or') {
return generateOr(expr, env, ctx);
} else if (expr[1] === 'while') {
return generateWhile(expr, env, ctx);
} else if (expr[1] === 'break') {
return generateBreak(expr, env, ctx);
}
var func = generate(expr[1], env, ctx0);
var args = generate(expr[0], env, ctx0);
for (var i = 2; i < expr.length; i++) {
args += ', ';
args += generate(expr[i], env, ctx0);
}
var ret = func + '(' + args + ')';
return ctx(ret);
}
function env_new(env) {
env = (env || {
print: true,
not: true,
_instance_: true,
__gt__: true,
__ge__: true,
__eq__: true,
__le__: true,
__lt__: true,
__add__: true,
__sub__: true,
__mul__: true,
__div__: true,
__mod__: true,
isFunction: true,
isArray: true,
foreach: true,
call: true,
apply: true,
exports: true,
});
var Env = function () {};
Env.prototype = env;
var env = new Env();
return env;
}
exports.compile = function (expr) {
var libs = [
"function print(x) {\n return console.log(x); \n}",
"function not(x) {\n return !x;\n}",
"function _instance_(x) {\n return new x(); \n}",
"function __add__(x, y) {\n return x + y;\n}",
"function __sub__(x, y) {\n return x - y;\n}",
"function __mul__(x, y) {\n return x * y;\n}",
"function __div__(x, y) {\n return x / y;\n}",
"function __mod__(x, y) {\n return x % y;\n}",
"function __gt__(x, y) {\n return x > y;\n}",
"function __ge__(x, y) {\n return x >= y;\n}",
"function __eq__(x, y) {\n return x === y;\n}",
"function __le__(x, y) {\n return x <= y;\n}",
"function __lt__(x, y) {\n return x < y;\n}",
"function isFunction(x) {\n return typeof x === 'function';\n}",
"function isArray(x) {\n return x instanceof Array;\n}",
"function foreach(arr, cb) {\n for (var i in arr) { cb(arr[i], i); };\n}",
"function call(fn) {\n var collect = []; for (var i in arguments) { collect.push(arguments[i]); }; collect.shift(); return fn.apply(fn, collect); \n}",
"function apply(fn) {\n return fn.apply(fn, arguments[1]); \n}",
].join("\n");
var env0 = env_new();
console.log(libs + "\n" + generate(expr, env0, ctx0));
}