Comparing version 0.5.3 to 0.5.4
{ | ||
"name": "jsdc", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "compiler ecmascript6 to ecmascript5", | ||
@@ -55,3 +55,3 @@ "maintainers": [ | ||
"dependencies": { | ||
"homunculus": "^0.7.5" | ||
"homunculus": "^0.7.6" | ||
}, | ||
@@ -58,0 +58,0 @@ "devDependencies": { |
@@ -21,9 +21,27 @@ var homunculus = require('homunculus'); | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
//of只会出现在3或4位 | ||
var index = 0; | ||
if(node.leaf(3).name() == JsNode.TOKEN | ||
&& node.leaf(3).token().content() == 'of') { | ||
index = 3; | ||
} | ||
else if(node.leaf(4).name() == JsNode.TOKEN | ||
&& node.leaf(4).token().content() == 'of') { | ||
index = 4; | ||
} | ||
if(index == 0) { | ||
return; | ||
} | ||
var of = node.leaf(index); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 3; | ||
//存放临时id供block首改写引用 | ||
var leaf = node.leaf(2); | ||
if(leaf.name() == JsNode.PRMREXPR | ||
var leaf = node.leaf(index - 1); | ||
//如果是var解构 | ||
if([JsNode.OBJBINDPAT, JsNode.ARRBINDPAT].indexOf(leaf.name()) > -1) { | ||
this.destruct[node.nid()] = leaf; | ||
this.hash[node.nid()] = this.getLast(leaf); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
//prmrexpr解构 | ||
else if(leaf.name() == JsNode.PRMREXPR | ||
&& leaf.size() == 1 | ||
@@ -37,18 +55,7 @@ && [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.hash[node.nid()] = true; | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
} | ||
this.pos[node.nid()] = index; | ||
this.jsdc.ignore(of, 'forof2'); | ||
} | ||
//for(var forbind of中为4 | ||
else { | ||
of = node.leaf(4); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 4; | ||
this.destruct[node.nid()] = node.leaf(3); | ||
//存放临时id供block首改写引用 | ||
this.hash[node.nid()] = this.getLast(node.leaf(3)); | ||
this.jsdc.ignore(of, 'forof3'); | ||
this.jsdc.ignore(node.leaf(3), 'forof4'); | ||
} | ||
} | ||
} | ||
@@ -80,3 +87,5 @@ } | ||
this.jsdc.append('.next();!'); | ||
var index = this.pos[parent.nid()]; | ||
var k; | ||
//解构 | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
@@ -86,12 +95,7 @@ k = this.hash[parent.nid()]; | ||
else { | ||
k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = join(k); | ||
} | ||
k = parent.leaf(index - 1); | ||
//forof的var后只能是bindid或者解构 | ||
k = k.first().token().content(); | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
var v = join(parent.leaf(index + 1)); | ||
this.jsdc.append(k + '.done;'); | ||
@@ -132,6 +136,7 @@ this.jsdc.append(k + '=' + v + '.next()'); | ||
else { | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
var index = this.pos[node.nid()]; | ||
k = node.leaf(index - 1); | ||
//只会是bindid或解构 | ||
if(k.name() == JsNode.BINDID) { | ||
k = k.first().token().content(); | ||
} | ||
@@ -138,0 +143,0 @@ else { |
@@ -108,3 +108,2 @@ var homunculus = require('homunculus'); | ||
self.jsdc.ignore(node.first(), 'gen7'); | ||
var parent = node.parent(); | ||
//赋值语句需要添加上参数,先默认undefined,并记录在变量中为下次添加做标记 | ||
@@ -448,90 +447,32 @@ var parent = node.parent(); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
//特殊的for(var forbind of语句 | ||
if(itstmt.leaf(4).name() == JsNode.TOKEN | ||
&& itstmt.leaf(4).token().content() == 'of') { | ||
self.jsdc.ignore(itstmt.leaf(2), 'gen36'); | ||
var ids = self.jsdc.destruct.getIds(itstmt.leaf(3), { arr: [] }); | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
}); | ||
} | ||
}); | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(3).name()) > -1 ? node.leaf(3) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(3)); | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen37'); | ||
self.jsdc.ignore(node.leaf(3), 'gen38'); | ||
self.jsdc.ignore(node.leaf(4), 'gen39'); | ||
eventbus.on(itstmt.leaf(5).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 4, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
//of和in只会出现在3或4位 | ||
var index = 0; | ||
if(node.leaf(3).name() == JsNode.TOKEN | ||
&& ['of', 'in'].indexOf(node.leaf(3).token().content()) > -1) { | ||
index = 3; | ||
} | ||
else { | ||
switch(itstmt.leaf(3).token().content()) { | ||
else if(node.leaf(4).name() == JsNode.TOKEN | ||
&& ['of', 'in'].indexOf(node.leaf(4).token().content()) > -1) { | ||
index = 4; | ||
} | ||
if(index > 0) { | ||
switch(node.leaf(index).token().content()) { | ||
case 'in': | ||
var keys = self.jsdc.uid(); | ||
var len = self.jsdc.uid(); | ||
var index = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
var index2 = self.jsdc.uid(); | ||
var id = join(node.leaf(index - 1)); | ||
//插入var变量 | ||
if(index == 4) { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
} | ||
}); | ||
self.jsdc.ignore(node.leaf(2), 'gen27'); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen28'); | ||
self.jsdc.ignore(node.leaf(3), 'gen29'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
self.jsdc.ignore(node.leaf(index - 1), 'gen28'); | ||
self.jsdc.ignore(node.leaf(index), 'gen29'); | ||
eventbus.on(itstmt.leaf(index + 1).nid(), function(node, start) { | ||
if(start) { | ||
@@ -546,7 +487,7 @@ self.jsdc.append('var ' + obj + '='); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
self.jsdc.appendBefore(index2 + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
self.jsdc.appendBefore(index2 + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
@@ -556,3 +497,3 @@ itEndTemp = ++top.index2; | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
itstmt.done = index2 + '<' + len; | ||
} | ||
@@ -565,3 +506,3 @@ }); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
self.jsdc.append(index2 + '];'); | ||
} | ||
@@ -579,21 +520,17 @@ else { | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(index - 1).name()) > -1 ? node.leaf(index - 1) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(index - 1)); | ||
var obj = self.jsdc.uid(); | ||
//插入var | ||
if(index == 4) { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
} | ||
}); | ||
self.jsdc.ignore(node.leaf(index - 2), 'gen37'); | ||
} | ||
else { | ||
if(node.leaf(2).name() == JsNode.PRMREXPR | ||
&& [JsNode.ARRLTR, JsNode.OBJLTR].indexOf(node.leaf(2).first().name()) > -1) { | ||
isDestruct = node.leaf(2).first(); | ||
id = self.jsdc.forof.getLast(isDestruct); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen30'); | ||
self.jsdc.ignore(node.leaf(3), 'gen31'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
self.jsdc.ignore(node.leaf(index - 1), 'gen38'); | ||
self.jsdc.ignore(node.leaf(index), 'gen39'); | ||
eventbus.on(itstmt.leaf(index + 1).nid(), function(node, start) { | ||
if(start) { | ||
@@ -629,3 +566,3 @@ self.jsdc.append('var ' + obj + '='); | ||
s: '', | ||
pos: 3, | ||
pos: 4, | ||
append: function(s) { | ||
@@ -648,47 +585,94 @@ this.s += s; | ||
break; | ||
default: | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
loopTemp = ++top.index2; | ||
self.jsdc.append('case ' + loopTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
//防止优先级错误 | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.append('('); | ||
} | ||
itTemp = ++top.index2; | ||
} | ||
else { | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
} | ||
itEndTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break'); | ||
//供yield判断 | ||
itstmt.done = join(itstmt.leaf(4)); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(6).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp); | ||
self.jsdc.appendBefore(';break'); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
} | ||
//普通for语句 | ||
else { | ||
loopTemp = self.jsdc.uid(); | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen32'); | ||
self.jsdc.ignore(block.first().last(), 'gen33'); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
} | ||
}); | ||
var expr1 = 0; | ||
//for(;expr1 | ||
if(itstmt.leaf(2).name() == JsNode.TOKEN | ||
&& itstmt.leaf(2).token().content() == ';') { | ||
if(itstmt.leaf(3).name() != JsNode.TOKEN | ||
|| itstmt.leaf(3).token().content() != ';') { | ||
expr1 = 3; | ||
} | ||
} | ||
//for(expr0;expr1 | ||
else if(itstmt.leaf(4).name() != JsNode.TOKEN | ||
|| itstmt.leaf(4).token().content() != ';') { | ||
expr1 = 4; | ||
} | ||
if(expr1) { | ||
eventbus.on(itstmt.leaf(expr1).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
self.jsdc.append('(' + loopTemp + '='); | ||
} | ||
else { | ||
self.jsdc.appendBefore(')?' + itTemp + ':' + itEndTemp); | ||
} | ||
}); | ||
} | ||
else { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
}); | ||
} | ||
var expr2 = 0; | ||
if(expr1 | ||
&& (itstmt.leaf(expr1 + 2).name() != JsNode.TOKEN | ||
|| itstmt.leaf(expr1 + 2).token().content() != ')')) { | ||
expr2 = expr1 + 2; | ||
} | ||
else if( | ||
!expr1 | ||
&& (itstmt.leaf(4).name() != JsNode.TOKEN | ||
|| itstmt.leaf(4).token().content() != ')')) { | ||
expr2 = 4; | ||
} | ||
if(expr2) { | ||
eventbus.on(itstmt.leaf(expr2).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
}); | ||
} | ||
else { | ||
eventbus.on(expr1 ? itstmt.leaf(expr1).nid() : itstmt.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
}); | ||
} | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
//供yield判断 | ||
itstmt.done = '!' + loopTemp; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
break; | ||
@@ -705,6 +689,5 @@ case 'while': | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
endTemp = top.index2; | ||
self.jsdc.append(top.state + '='); | ||
@@ -725,3 +708,3 @@ } | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
itstmt.done = '!' + loopTemp; | ||
} | ||
@@ -737,3 +720,3 @@ else { | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
itstmt.done = '!' + loopTemp; | ||
self.jsdc.ignore(itstmt.first()); | ||
@@ -748,3 +731,3 @@ self.jsdc.ignore(itstmt.leaf(2)); | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
} | ||
@@ -751,0 +734,0 @@ else { |
@@ -21,9 +21,27 @@ define(function(require, exports, module){var homunculus = require('homunculus'); | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
//of只会出现在3或4位 | ||
var index = 0; | ||
if(node.leaf(3).name() == JsNode.TOKEN | ||
&& node.leaf(3).token().content() == 'of') { | ||
index = 3; | ||
} | ||
else if(node.leaf(4).name() == JsNode.TOKEN | ||
&& node.leaf(4).token().content() == 'of') { | ||
index = 4; | ||
} | ||
if(index == 0) { | ||
return; | ||
} | ||
var of = node.leaf(index); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 3; | ||
//存放临时id供block首改写引用 | ||
var leaf = node.leaf(2); | ||
if(leaf.name() == JsNode.PRMREXPR | ||
var leaf = node.leaf(index - 1); | ||
//如果是var解构 | ||
if([JsNode.OBJBINDPAT, JsNode.ARRBINDPAT].indexOf(leaf.name()) > -1) { | ||
this.destruct[node.nid()] = leaf; | ||
this.hash[node.nid()] = this.getLast(leaf); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
//prmrexpr解构 | ||
else if(leaf.name() == JsNode.PRMREXPR | ||
&& leaf.size() == 1 | ||
@@ -37,18 +55,7 @@ && [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.hash[node.nid()] = true; | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
} | ||
this.pos[node.nid()] = index; | ||
this.jsdc.ignore(of, 'forof2'); | ||
} | ||
//for(var forbind of中为4 | ||
else { | ||
of = node.leaf(4); | ||
if(of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
this.pos[node.nid()] = 4; | ||
this.destruct[node.nid()] = node.leaf(3); | ||
//存放临时id供block首改写引用 | ||
this.hash[node.nid()] = this.getLast(node.leaf(3)); | ||
this.jsdc.ignore(of, 'forof3'); | ||
this.jsdc.ignore(node.leaf(3), 'forof4'); | ||
} | ||
} | ||
} | ||
@@ -80,3 +87,5 @@ } | ||
this.jsdc.append('.next();!'); | ||
var index = this.pos[parent.nid()]; | ||
var k; | ||
//解构 | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
@@ -86,12 +95,7 @@ k = this.hash[parent.nid()]; | ||
else { | ||
k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = join(k); | ||
} | ||
k = parent.leaf(index - 1); | ||
//forof的var后只能是bindid或者解构 | ||
k = k.first().token().content(); | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
var v = join(parent.leaf(index + 1)); | ||
this.jsdc.append(k + '.done;'); | ||
@@ -132,6 +136,7 @@ this.jsdc.append(k + '=' + v + '.next()'); | ||
else { | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
var index = this.pos[node.nid()]; | ||
k = node.leaf(index - 1); | ||
//只会是bindid或解构 | ||
if(k.name() == JsNode.BINDID) { | ||
k = k.first().token().content(); | ||
} | ||
@@ -138,0 +143,0 @@ else { |
@@ -108,3 +108,2 @@ define(function(require, exports, module){var homunculus = require('homunculus'); | ||
self.jsdc.ignore(node.first(), 'gen7'); | ||
var parent = node.parent(); | ||
//赋值语句需要添加上参数,先默认undefined,并记录在变量中为下次添加做标记 | ||
@@ -448,90 +447,32 @@ var parent = node.parent(); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
//特殊的for(var forbind of语句 | ||
if(itstmt.leaf(4).name() == JsNode.TOKEN | ||
&& itstmt.leaf(4).token().content() == 'of') { | ||
self.jsdc.ignore(itstmt.leaf(2), 'gen36'); | ||
var ids = self.jsdc.destruct.getIds(itstmt.leaf(3), { arr: [] }); | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
ids.forEach(function(id) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
}); | ||
} | ||
}); | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(3).name()) > -1 ? node.leaf(3) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(3)); | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen37'); | ||
self.jsdc.ignore(node.leaf(3), 'gen38'); | ||
self.jsdc.ignore(node.leaf(4), 'gen39'); | ||
eventbus.on(itstmt.leaf(5).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + next); | ||
self.jsdc.appendBefore('=' + obj + '.next();'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(next + '.done' + '?'); | ||
iterator = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = next + '.done'; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + iterator + ':'); | ||
self.jsdc.append(next + '=' + obj + '.next();'); | ||
self.jsdc.append(top.state + '=' + endTemp); | ||
self.jsdc.append(';break;case ' + itTemp + ':'); | ||
self.jsdc.append(id + '=' + next + '.value;'); | ||
if(isDestruct) { | ||
var ret = { | ||
o: id, | ||
s: '', | ||
pos: 4, | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
self.jsdc.forof.assign(isDestruct, ret); | ||
self.jsdc.appendBefore(ret.s); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
//of和in只会出现在3或4位 | ||
var index = 0; | ||
if(node.leaf(3).name() == JsNode.TOKEN | ||
&& ['of', 'in'].indexOf(node.leaf(3).token().content()) > -1) { | ||
index = 3; | ||
} | ||
else { | ||
switch(itstmt.leaf(3).token().content()) { | ||
else if(node.leaf(4).name() == JsNode.TOKEN | ||
&& ['of', 'in'].indexOf(node.leaf(4).token().content()) > -1) { | ||
index = 4; | ||
} | ||
if(index > 0) { | ||
switch(node.leaf(index).token().content()) { | ||
case 'in': | ||
var keys = self.jsdc.uid(); | ||
var len = self.jsdc.uid(); | ||
var index = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
var index2 = self.jsdc.uid(); | ||
var id = join(node.leaf(index - 1)); | ||
//插入var变量 | ||
if(index == 4) { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
} | ||
}); | ||
self.jsdc.ignore(node.leaf(2), 'gen27'); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen28'); | ||
self.jsdc.ignore(node.leaf(3), 'gen29'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
self.jsdc.ignore(node.leaf(index - 1), 'gen28'); | ||
self.jsdc.ignore(node.leaf(index), 'gen29'); | ||
eventbus.on(itstmt.leaf(index + 1).nid(), function(node, start) { | ||
if(start) { | ||
@@ -546,7 +487,7 @@ self.jsdc.append('var ' + obj + '='); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
self.jsdc.appendBefore(index2 + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
self.jsdc.appendBefore(index2 + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
@@ -556,3 +497,3 @@ itEndTemp = ++top.index2; | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
itstmt.done = index2 + '<' + len; | ||
} | ||
@@ -565,3 +506,3 @@ }); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
self.jsdc.append(index2 + '];'); | ||
} | ||
@@ -579,21 +520,17 @@ else { | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
var isDestruct = [JsNode.ARRBINDPAT, JsNode.OBJBINDPAT].indexOf(node.leaf(index - 1).name()) > -1 ? node.leaf(index - 1) : false; | ||
var id = isDestruct ? self.jsdc.forof.getLast(isDestruct) : join(node.leaf(index - 1)); | ||
var obj = self.jsdc.uid(); | ||
//插入var | ||
if(index == 4) { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + id + ';', self.hash[nid].pos); | ||
} | ||
}); | ||
self.jsdc.ignore(node.leaf(index - 2), 'gen37'); | ||
} | ||
else { | ||
if(node.leaf(2).name() == JsNode.PRMREXPR | ||
&& [JsNode.ARRLTR, JsNode.OBJLTR].indexOf(node.leaf(2).first().name()) > -1) { | ||
isDestruct = node.leaf(2).first(); | ||
id = self.jsdc.forof.getLast(isDestruct); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
} | ||
var obj = self.jsdc.uid(); | ||
self.jsdc.ignore(node.leaf(2), 'gen30'); | ||
self.jsdc.ignore(node.leaf(3), 'gen31'); | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
self.jsdc.ignore(node.leaf(index - 1), 'gen38'); | ||
self.jsdc.ignore(node.leaf(index), 'gen39'); | ||
eventbus.on(itstmt.leaf(index + 1).nid(), function(node, start) { | ||
if(start) { | ||
@@ -629,3 +566,3 @@ self.jsdc.append('var ' + obj + '='); | ||
s: '', | ||
pos: 3, | ||
pos: 4, | ||
append: function(s) { | ||
@@ -648,47 +585,94 @@ this.s += s; | ||
break; | ||
default: | ||
eventbus.on(itstmt.leaf(4).nid(), function(node, start) { | ||
if(start) { | ||
top = self.hash[nid]; | ||
loopTemp = ++top.index2; | ||
self.jsdc.append('case ' + loopTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
//防止优先级错误 | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.append('('); | ||
} | ||
itTemp = ++top.index2; | ||
} | ||
else { | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
} | ||
itEndTemp = ++top.index2; | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp); | ||
self.jsdc.appendBefore(';break'); | ||
//供yield判断 | ||
itstmt.done = join(itstmt.leaf(4)); | ||
} | ||
}); | ||
eventbus.on(itstmt.leaf(6).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp); | ||
self.jsdc.appendBefore(';break'); | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
} | ||
//普通for语句 | ||
else { | ||
loopTemp = self.jsdc.uid(); | ||
var block = itstmt.last(); | ||
if(block.name() == JsNode.BLOCKSTMT) { | ||
self.jsdc.ignore(block.first().first(), 'gen32'); | ||
self.jsdc.ignore(block.first().last(), 'gen33'); | ||
} | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
} | ||
}); | ||
var expr1 = 0; | ||
//for(;expr1 | ||
if(itstmt.leaf(2).name() == JsNode.TOKEN | ||
&& itstmt.leaf(2).token().content() == ';') { | ||
if(itstmt.leaf(3).name() != JsNode.TOKEN | ||
|| itstmt.leaf(3).token().content() != ';') { | ||
expr1 = 3; | ||
} | ||
} | ||
//for(expr0;expr1 | ||
else if(itstmt.leaf(4).name() != JsNode.TOKEN | ||
|| itstmt.leaf(4).token().content() != ';') { | ||
expr1 = 4; | ||
} | ||
if(expr1) { | ||
eventbus.on(itstmt.leaf(expr1).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
self.jsdc.append(top.state + '='); | ||
self.jsdc.append('(' + loopTemp + '='); | ||
} | ||
else { | ||
self.jsdc.appendBefore(')?' + itTemp + ':' + itEndTemp); | ||
} | ||
}); | ||
} | ||
else { | ||
eventbus.on(itstmt.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
} | ||
}); | ||
} | ||
var expr2 = 0; | ||
if(expr1 | ||
&& (itstmt.leaf(expr1 + 2).name() != JsNode.TOKEN | ||
|| itstmt.leaf(expr1 + 2).token().content() != ')')) { | ||
expr2 = expr1 + 2; | ||
} | ||
else if( | ||
!expr1 | ||
&& (itstmt.leaf(4).name() != JsNode.TOKEN | ||
|| itstmt.leaf(4).token().content() != ')')) { | ||
expr2 = 4; | ||
} | ||
if(expr2) { | ||
eventbus.on(itstmt.leaf(expr2).nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
} | ||
}); | ||
} | ||
else { | ||
eventbus.on(expr1 ? itstmt.leaf(expr1).nid() : itstmt.nid(), function(node, start) { | ||
if(!start) { | ||
self.jsdc.append(';case ' + itTemp + ':'); | ||
} | ||
}); | ||
} | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
//供yield判断 | ||
itstmt.done = '!' + loopTemp; | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
} | ||
break; | ||
@@ -705,6 +689,5 @@ case 'while': | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
top = self.hash[nid]; | ||
endTemp = ++top.index2; | ||
self.jsdc.append('case ' + endTemp + ':'); | ||
endTemp = top.index2; | ||
self.jsdc.append(top.state + '='); | ||
@@ -725,3 +708,3 @@ } | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
itstmt.done = '!' + loopTemp; | ||
} | ||
@@ -737,3 +720,3 @@ else { | ||
//供yield判断 | ||
itstmt.done = loopTemp; | ||
itstmt.done = '!' + loopTemp; | ||
self.jsdc.ignore(itstmt.first()); | ||
@@ -748,3 +731,3 @@ self.jsdc.ignore(itstmt.leaf(2)); | ||
if(start) { | ||
self.jsdc.append('var ' + loopTemp + ';'); | ||
self.jsdc.insert('var ' + loopTemp + ';', self.hash[nid].pos); | ||
} | ||
@@ -751,0 +734,0 @@ else { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
335569
7821
Updatedhomunculus@^0.7.6