-
Notifications
You must be signed in to change notification settings - Fork 4
/
generator.js
55 lines (52 loc) · 1.49 KB
/
generator.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
module.exports = Generator;
var util = require('util'), stream = require('stream');
util.inherits(module.exports, stream.Transform);
function Generator(options) {
stream.Transform.call(this, options);
this._writableState.objectMode = true;
this._readableState.objectMode = false;
this.tabs = 0;
}
Generator.prototype._transform = function(chunk, encoding, cb) {
this.statement(chunk);
cb();
};
Generator.prototype.statement = function(statement) {
var type = statement['type'];
var func = this[type];
if (func) {
this.leadTabs();
func.call(this, statement);
} else throw type;
};
Generator.prototype.leadTabs = function() {
this.push(new Array(this.tabs+1).join(' '));
};
Generator.prototype.PRINT = function(stmt) {
this.push('console.log(');
this.push(JSON.stringify(stmt.expr.data));
this.push(');\r\n');
};
Generator.prototype.END = function(stmt) {
this.push('process.exit(0);\r\n');
};
Generator.prototype.FOR = function(stmt) {
this.push('for(var ');
this.push(stmt.varName);
this.push(' = ');
this.push(stmt.init.toString());
this.push('; ');
this.push(stmt.varName);
this.push(' <= ');
this.push(stmt.limit.toString());
this.push('; ');
this.push(stmt.varName);
this.push(' += ');
this.push(stmt.step.toString());
this.push(') {\r\n');
this.tabs++;
stmt.body.forEach(this.statement.bind(this));
this.tabs--;
this.leadTabs();
this.push('}\r\n');
};