-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coder.js
48 lines (31 loc) · 981 Bytes
/
Coder.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
exports.Coder = function(pre, post, tabLevel) {
this._pre = pre;
this._post = post;
this._tabLevel = tabLevel;
this._blocks = [];
};
exports.Coder.prototype.addBlock = function(pre, post, increaseTabLevel = true) {
let block = new exports.Coder(pre, post, this._tabLevel + (increaseTabLevel ? 1 : 0));
this._blocks.push(block);
return block;
};
exports.Coder.prototype.addLine = function(line) {
this._blocks.push(line);
return this;
};
exports.Coder.prototype.generateCode = function(tabs) {
// create code
let code = tabs + this._pre + "\n";
// sub blocks
for(let i = 0; i < this._blocks.length; ++i) {
// get block
let block = this._blocks[i];
let t = " ".repeat(this._tabLevel);
if(block instanceof exports.Coder)
code += block.generateCode(t);
else
code += t + block + "\n";
}
code += tabs + this._post + "\n\n";
return code;
};