Comparing version 0.4.1 to 0.4.2
{ | ||
"name": "jsdc", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "compiler ecmascript6 to ecmascript5", | ||
@@ -55,3 +55,3 @@ "maintainers": [ | ||
"dependencies": { | ||
"homunculus": "^0.3.4" | ||
"homunculus": "^0.3.7" | ||
}, | ||
@@ -58,0 +58,0 @@ "devDependencies": { |
@@ -226,3 +226,3 @@ # Javascript Downcast | ||
```js | ||
function method(a, b ) {if(b ===void 0)b=1; | ||
function method(a, b) {if(b ===void 0)b=1; | ||
} | ||
@@ -389,3 +389,3 @@ ``` | ||
```js | ||
!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
!function(){var _0_=Object.create(B.prototype);_0_.constructor=A;A.prototype=_0_;}(); | ||
function A(){} | ||
@@ -405,3 +405,3 @@ Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
```js | ||
!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
!function(){var _0_=Object.create(B.prototype);_0_.constructor=A;A.prototype=_0_;}(); | ||
function A(){B.call(this)} | ||
@@ -417,3 +417,3 @@ Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
```js | ||
!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_;}(); | ||
!function(){var _0_=Object.create(B.prototype);_0_.constructor=A;A.prototype=_0_;}(); | ||
function A(){B.prototype.a.call(this)} | ||
@@ -428,3 +428,3 @@ Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
```js | ||
function A(){B.call(this)}!function(){var _=Object.create(B.prototype);_.constructor=A;A.prototype=_}(); | ||
function A(){B.call(this)}!function(){var _0_=Object.create(B.prototype);_0_.constructor=A;A.prototype=_0_}(); | ||
Object.keys(B).forEach(function(k){A[k]=B[k]}); | ||
@@ -802,3 +802,3 @@ ``` | ||
```js | ||
(!function(){var _0_= {};a=_0_["a"];if(a===void 0)a=1;return _0_}()) | ||
(!function(){var _0_= {};a=_0_["a"];if(!_0_.hasOwnProperty('a'))a=1;return _0_}()) | ||
``` | ||
@@ -805,0 +805,0 @@ 数组解构最后允许`rest`运算符: |
@@ -20,3 +20,3 @@ var homunculus = require('homunculus'); | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init, 'default1'); | ||
self.jsdc.ignore(init, 'default1', true); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
@@ -23,0 +23,0 @@ } |
@@ -14,3 +14,3 @@ var homunculus = require('homunculus'); | ||
}).methods({ | ||
getIds: function(node) { | ||
getIds: function(node, res) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
@@ -20,3 +20,3 @@ return this.idCache[node.nid()]; | ||
this.jsdc.ignore(node, 'destruct1'); | ||
var res = { arr: [] }; | ||
res = res || { arr: [] }; | ||
this.recursion(node, res); | ||
@@ -86,20 +86,34 @@ this.idCache[node.nid()] = res.arr; | ||
}, | ||
parse: function(node, start) { | ||
parse: function(node, start, ret) { | ||
var self = this; | ||
var first = node.first(); | ||
var first; | ||
if(ret) { | ||
first = node; | ||
} | ||
else { | ||
first = node.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRBINDPAT: | ||
if(start) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
var temp = self.jsdc.uid(); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -112,9 +126,20 @@ var target = self.getArray(first.leaves()); | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -124,17 +149,20 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}); | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
}); | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
if(!ret) { | ||
(ret || self.jsdc).appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
(ret || self.jsdc).appendBefore(';var '); | ||
} | ||
} | ||
@@ -146,9 +174,22 @@ } | ||
if(start) { | ||
self.jsdc.append('!function(){var '); | ||
var temp = self.jsdc.uid(); | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -162,9 +203,20 @@ var target = self.getName(first.leaves()); | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -177,9 +229,20 @@ case JsNode.PROPTNAME: | ||
var id = last.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -189,5 +252,6 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -198,3 +262,13 @@ } | ||
}); | ||
self.jsdc.appendBefore('}()'); | ||
if(!ret) { | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
} | ||
} | ||
} | ||
} | ||
@@ -204,27 +278,38 @@ break; | ||
}, | ||
destruct: function(node, data) { | ||
destruct: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -234,8 +319,9 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}); | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
@@ -245,13 +331,13 @@ }); | ||
case JsNode.OBJBINDPAT: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -261,9 +347,20 @@ switch(leaf.name()) { | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -276,9 +373,20 @@ case JsNode.PROPTNAME: | ||
var id = last.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -288,5 +396,6 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -300,13 +409,19 @@ } | ||
}, | ||
expr: function(assignexpr, start) { | ||
expr: function(assignexpr, start, ret) { | ||
var self = this; | ||
var first = assignexpr.first(); | ||
first = first.first(); | ||
var first; | ||
if(ret) { | ||
first = assignexpr; | ||
} | ||
else { | ||
first = assignexpr.first(); | ||
first = first.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRLTR: | ||
if(start) { | ||
this.jsdc.ignore(first, 'destruct2'); | ||
!ret && this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
} | ||
@@ -318,21 +433,33 @@ else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.inAssign[first.nid()] = true; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
var temp = self.jsdc.uid(); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append('function(){var '); | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append('function(){var '); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -350,7 +477,7 @@ var target = self.getArray(first.leaves()); | ||
var id = leaf.next().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
@@ -360,5 +487,15 @@ break; | ||
var id = leaf.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + '=' + join(leaf.next().next()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
@@ -371,3 +508,3 @@ case JsNode.ARRLTR: | ||
index: i | ||
}); | ||
}, ret); | ||
} | ||
@@ -380,4 +517,4 @@ }); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
ret || (self.jsdc.appendBefore(';return ' + temp)); | ||
ret || (self.jsdc.appendBefore('}()')); | ||
} | ||
@@ -401,18 +538,30 @@ break; | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.inAssign[first.nid()] = true; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -426,8 +575,19 @@ var target = self.getName(first.leaves()); | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
if(leaf.next()) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -440,18 +600,39 @@ case JsNode.PROPTNAME: | ||
var id = last.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(join(last.parent()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -465,3 +646,3 @@ case JsNode.ARRLTR: | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -477,4 +658,4 @@ } | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
!ret && self.jsdc.appendBefore(';return ' + temp); | ||
!ret && self.jsdc.appendBefore('}()'); | ||
} | ||
@@ -484,13 +665,13 @@ break; | ||
}, | ||
destructExpr: function(node, data) { | ||
destructExpr: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRLTR: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
@@ -508,7 +689,7 @@ var target = self.getArray(node.leaves()); | ||
var id = leaf.next().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
@@ -518,5 +699,5 @@ break; | ||
var id = leaf.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
break; | ||
@@ -529,3 +710,3 @@ case JsNode.ARRLTR: | ||
index: i | ||
}); | ||
}, ret); | ||
} | ||
@@ -535,9 +716,9 @@ }); | ||
case JsNode.OBJLTR: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
@@ -551,7 +732,7 @@ var target = self.getArray(node.leaves()); | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -565,8 +746,8 @@ break; | ||
var id = last.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -576,7 +757,7 @@ break; | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -591,3 +772,3 @@ break; | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -594,0 +775,0 @@ } |
195
src/Forof.js
@@ -11,5 +11,7 @@ var homunculus = require('homunculus'); | ||
this.hash = {}; | ||
this.pos = {}; | ||
this.destruct = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
//有可能被Generator中该写过 | ||
//有可能被Generator中改写过 | ||
if(node.gen) { | ||
@@ -19,9 +21,34 @@ return; | ||
if(start) { | ||
var of = node.leaf(3); | ||
if(node.first().token().content() == 'for' | ||
&& of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
//存放临时li供block首尾改写引用 | ||
this.hash[node.nid()] = true; | ||
this.jsdc.ignore(of, 'forof1'); | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
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 | ||
&& leaf.size() == 1 | ||
&& [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.destruct[node.nid()] = leaf.first(); | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
else { | ||
this.hash[node.nid()] = true; | ||
} | ||
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'); | ||
} | ||
} | ||
} | ||
@@ -41,2 +68,5 @@ } | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
this.jsdc.append(this.hash[parent.nid()]); | ||
} | ||
this.jsdc.append('='); | ||
@@ -51,11 +81,17 @@ } | ||
this.jsdc.append('.next();!'); | ||
var k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
var v = join(parent.leaf(4)); | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
var k; | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
k = this.hash[parent.nid()]; | ||
} | ||
else { | ||
k = join(k); | ||
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); | ||
} | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
this.jsdc.append(k + '.done;'); | ||
@@ -87,12 +123,129 @@ this.jsdc.append(k + '=' + v + '.next()'); | ||
}, | ||
assign: function(node) { | ||
var k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
assign: function(node, ret) { | ||
var k; | ||
if(ret) { | ||
k = ret.o; | ||
} | ||
else if(typeof this.hash[node.nid()] == 'string') { | ||
k = this.hash[node.nid()]; | ||
} | ||
else { | ||
k = join(k); | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = join(k); | ||
} | ||
} | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(ret) { | ||
if(ret.pos == 4) { | ||
this.jsdc.destruct.parse(node, true, ret); | ||
this.jsdc.destruct.parse(node, false, ret); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(node, true, ret); | ||
this.jsdc.destruct.expr(node, false, ret); | ||
} | ||
return ret.s; | ||
} | ||
else { | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(this.destruct.hasOwnProperty(node.nid())) { | ||
var ret2 = { | ||
o: k, | ||
s: '', | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
if(this.pos[node.nid()] == 4) { | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], false, ret2); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], false, ret2); | ||
} | ||
this.jsdc.append(ret2.s); | ||
} | ||
} | ||
}, | ||
getLast: function(node) { | ||
if(node.name() == JsNode.ARRLTR | ||
|| node.name() == JsNode.ARRBINDPAT) { | ||
return this.getArrLast(node); | ||
} | ||
else { | ||
return this.getObjLast(node); | ||
} | ||
}, | ||
getArrLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.SINGLENAME | ||
|| s == JsNode.ASSIGNEXPR) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
temp = temp.first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else { | ||
return this.getLast(temp); | ||
} | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
}, | ||
getObjLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.BINDPROPT) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
s = leaves[0].name(); | ||
return leaves[0].first().first().token().content(); | ||
} | ||
else { | ||
temp = leaves[2]; | ||
s = temp.name(); | ||
if(s == JsNode.SINGLENAME) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
} | ||
else if(s == JsNode.PROPTDEF) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
return leaves[0].token().content(); | ||
} | ||
else { | ||
temp = leaves[2].first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
return temp.first().token().content(); | ||
} | ||
else { | ||
return this.getLast(temp); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -99,0 +252,0 @@ }); |
@@ -133,2 +133,6 @@ var homunculus = require('homunculus'); | ||
self.jsdc.append('return{value:'); | ||
//无yield返回,则附上undefined | ||
if(node.size() == 1) { | ||
self.jsdc.append('void 0'); | ||
} | ||
} | ||
@@ -267,3 +271,3 @@ } | ||
if(!start) { | ||
self.jsdc.append('{value:'); | ||
self.jsdc.append('{value:void 0'); | ||
} | ||
@@ -419,144 +423,240 @@ }); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
switch(itstmt.leaf(3).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()); | ||
//特殊的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 { | ||
id = join(node.leaf(2)); | ||
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'; | ||
} | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
}); | ||
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(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
}); | ||
} | ||
else { | ||
switch(itstmt.leaf(3).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()); | ||
} | ||
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'; | ||
id = join(node.leaf(2)); | ||
} | ||
}); | ||
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;'); | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
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('('); | ||
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); | ||
} | ||
itTemp = ++top.index2; | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
} | ||
else { | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
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 + ':'); | ||
} | ||
}); | ||
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: 3, | ||
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 + ':'); | ||
} | ||
}); | ||
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 + ':'); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -563,0 +663,0 @@ break; |
@@ -206,3 +206,5 @@ var homunculus = require('homunculus'); | ||
if(!ignore || ig.type() != Token.BLANK) { | ||
this.res += ig.content(); | ||
if(!ig.ignore) { | ||
this.res += ig.content(); | ||
} | ||
ignore && (ig.ignore = true); | ||
@@ -374,13 +376,25 @@ ignore = false; | ||
}, | ||
ignore: function(node, msg) { | ||
ignore: function(node, msg, prev) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = msg || true; | ||
//忽略前置空格 | ||
if(prev) { | ||
prev = node; | ||
while(prev = prev.prev()) { | ||
if(prev.type() == Token.BLANK) { | ||
prev.ignore = msg || true; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
else if(node.name() == JsNode.TOKEN) { | ||
this.ignore(node.token()); | ||
this.ignore(node.token(), msg, prev); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf, msg); | ||
self.ignore(leaf, msg, prev); | ||
}); | ||
@@ -387,0 +401,0 @@ } |
@@ -31,6 +31,7 @@ var homunculus = require('homunculus'); | ||
this.body(node.last().prev(), o.name, o.extend); | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var _=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append('_.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=_'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
@@ -79,6 +80,7 @@ } | ||
if(o.extend) { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var _=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append('_.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=_'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
@@ -85,0 +87,0 @@ } |
@@ -21,3 +21,3 @@ define(function(require, exports, module) { | ||
if(init.name() == JsNode.INITLZ) { | ||
self.jsdc.ignore(init, 'default1'); | ||
self.jsdc.ignore(init, 'default1', true); | ||
(self.hash[fnbody] = self.hash[fnbody] || []).push(sgname); | ||
@@ -24,0 +24,0 @@ } |
@@ -15,3 +15,3 @@ define(function(require, exports, module) { | ||
}).methods({ | ||
getIds: function(node) { | ||
getIds: function(node, res) { | ||
if(this.idCache.hasOwnProperty(node.nid())) { | ||
@@ -21,3 +21,3 @@ return this.idCache[node.nid()]; | ||
this.jsdc.ignore(node, 'destruct1'); | ||
var res = { arr: [] }; | ||
res = res || { arr: [] }; | ||
this.recursion(node, res); | ||
@@ -87,20 +87,34 @@ this.idCache[node.nid()] = res.arr; | ||
}, | ||
parse: function(node, start) { | ||
parse: function(node, start, ret) { | ||
var self = this; | ||
var first = node.first(); | ||
var first; | ||
if(ret) { | ||
first = node; | ||
} | ||
else { | ||
first = node.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRBINDPAT: | ||
if(start) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
var temp = self.jsdc.uid(); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -113,9 +127,20 @@ var target = self.getArray(first.leaves()); | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -125,17 +150,20 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}); | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
}); | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
if(!ret) { | ||
(ret || self.jsdc).appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
(ret || self.jsdc).appendBefore(';var '); | ||
} | ||
} | ||
@@ -147,9 +175,22 @@ } | ||
if(start) { | ||
self.jsdc.append('!function(){var '); | ||
var temp = self.jsdc.uid(); | ||
if(!ret) { | ||
//忽略前面的,改为; | ||
var prev = node.prev(); | ||
if(prev.token().content() == ',') { | ||
self.jsdc.append(';'); | ||
} | ||
self.jsdc.append('!function(){var '); | ||
} | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -163,9 +204,20 @@ var target = self.getName(first.leaves()); | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -178,9 +230,20 @@ case JsNode.PROPTNAME: | ||
var id = last.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -190,5 +253,6 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -199,3 +263,13 @@ } | ||
}); | ||
self.jsdc.appendBefore('}()'); | ||
if(!ret) { | ||
self.jsdc.appendBefore('}()'); | ||
//忽略后面的,改为; | ||
var next = node.next(); | ||
if(next.token().content() == ',') { | ||
next = next.next(); | ||
if(next.first().name() == JsNode.BINDID) { | ||
self.jsdc.appendBefore(';var '); | ||
} | ||
} | ||
} | ||
} | ||
@@ -205,27 +279,38 @@ break; | ||
}, | ||
destruct: function(node, data) { | ||
destruct: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRBINDPAT: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
switch(leaf.name()) { | ||
case JsNode.SINGLENAME: | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -235,8 +320,9 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
index: i | ||
}); | ||
}, ret); | ||
break; | ||
case JsNode.BINDREST: | ||
var id = leaf.last().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
@@ -246,13 +332,13 @@ }); | ||
case JsNode.OBJBINDPAT: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
var target = self.getArray(node.leaves()); | ||
target.forEach(function(leaf, i) { | ||
var end = i == target.length - 1; | ||
var end = i == target.length - 1 && data.end; | ||
leaf = leaf.first(); | ||
@@ -262,9 +348,20 @@ switch(leaf.name()) { | ||
var id = leaf.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(leaf.size() == 2) { | ||
var init = leaf.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -277,9 +374,20 @@ case JsNode.PROPTNAME: | ||
var id = last.first().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = this.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + ';'); | ||
ret.appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.appendBefore('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.appendBefore(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -289,5 +397,6 @@ case JsNode.BINDELEM: | ||
temp: temp, | ||
end: end, | ||
name: name, | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -301,13 +410,19 @@ } | ||
}, | ||
expr: function(assignexpr, start) { | ||
expr: function(assignexpr, start, ret) { | ||
var self = this; | ||
var first = assignexpr.first(); | ||
first = first.first(); | ||
var first; | ||
if(ret) { | ||
first = assignexpr; | ||
} | ||
else { | ||
first = assignexpr.first(); | ||
first = first.first(); | ||
} | ||
switch(first.name()) { | ||
case JsNode.ARRLTR: | ||
if(start) { | ||
this.jsdc.ignore(first, 'destruct2'); | ||
!ret && this.jsdc.ignore(first, 'destruct2'); | ||
if(assignexpr.parent().name() == JsNode.ASSIGNEXPR) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().first().first().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct3'); | ||
} | ||
@@ -319,21 +434,33 @@ else if(assignexpr.parent().name() == JsNode.INITLZ) { | ||
self.hash[first.nid()] = self.hash[assignexpr.parent().prev().nid()]; | ||
this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
!ret && this.jsdc.ignore(assignexpr.leaf(1), 'destruct4'); | ||
} | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.inAssign[first.nid()] = true; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
var temp = self.jsdc.uid(); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append('function(){var '); | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append('function(){var '); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -351,7 +478,7 @@ var target = self.getArray(first.leaves()); | ||
var id = leaf.next().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
@@ -361,5 +488,15 @@ break; | ||
var id = leaf.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '[' + i + '];'); | ||
ret.append('if(' + temp2 + '.indexOf(' + id + ')!=' + i + ')'); | ||
ret.append(id + '=' + join(leaf.next().next()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
} | ||
break; | ||
@@ -372,3 +509,3 @@ case JsNode.ARRLTR: | ||
index: i | ||
}); | ||
}, ret); | ||
} | ||
@@ -381,4 +518,4 @@ }); | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
ret || (self.jsdc.appendBefore(';return ' + temp)); | ||
ret || (self.jsdc.appendBefore('}()')); | ||
} | ||
@@ -402,18 +539,30 @@ break; | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
self.inAssign[first.nid()] = true; | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.inAssign[first.nid()] = true; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.append('function(){var '); | ||
var temp = self.jsdc.uid(); | ||
!ret && self.jsdc.append('function(){var '); | ||
var temp; | ||
if(ret) { | ||
temp = ret.o; | ||
} | ||
else { | ||
temp = self.jsdc.uid(); | ||
} | ||
self.hash[first.nid()] = temp; | ||
self.jsdc.append(temp); | ||
!ret && self.jsdc.append(temp); | ||
} | ||
} | ||
else { | ||
self.jsdc.appendBefore(';'); | ||
!ret && self.jsdc.appendBefore(';'); | ||
var temp = self.hash[first.nid()]; | ||
@@ -427,8 +576,19 @@ var target = self.getName(first.leaves()); | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
if(leaf.next()) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && leaf.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + id + '"];'); | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + id + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -441,18 +601,39 @@ case JsNode.PROPTNAME: | ||
var id = last.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.size() == 2) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(id + join(init) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
case JsNode.PRMREXPR: | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
//在forof中由于最后一个默认值变量冲突需特殊对待 | ||
if(ret && end && last.next()) { | ||
var temp2 = self.jsdc.uid(); | ||
ret.append('!function(){var ' + temp2 + '=' + id + ';'); | ||
ret.append(id + '=' + temp + '["' + name + '"];'); | ||
ret.append('if(!' + temp2 + '.hasOwnProperty("' + name + '"))'); | ||
ret.append(join(last.parent()) + '}();'); | ||
} | ||
else { | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
} | ||
break; | ||
@@ -466,3 +647,3 @@ case JsNode.ARRLTR: | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -478,4 +659,4 @@ } | ||
} | ||
self.jsdc.appendBefore(';return ' + temp); | ||
self.jsdc.appendBefore('}()'); | ||
!ret && self.jsdc.appendBefore(';return ' + temp); | ||
!ret && self.jsdc.appendBefore('}()'); | ||
} | ||
@@ -485,13 +666,13 @@ break; | ||
}, | ||
destructExpr: function(node, data) { | ||
destructExpr: function(node, data, ret) { | ||
var self = this; | ||
switch(node.name()) { | ||
case JsNode.ARRLTR: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
@@ -509,7 +690,7 @@ var target = self.getArray(node.leaves()); | ||
var id = leaf.next().first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '.slice(' + i + ')'); | ||
} | ||
else { | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + ']' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + ']' + (!ret && end ? '' : ';')); | ||
} | ||
@@ -519,5 +700,5 @@ break; | ||
var id = leaf.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '[' + i + '];'); | ||
self.jsdc.appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
self.jsdc.appendBefore(id + '=' + join(leaf.next().next()) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '[' + i + '];'); | ||
(ret || self.jsdc).appendBefore('if(' + temp + '.indexOf(' + id + ')!=' + i + ')'); | ||
(ret || self.jsdc).appendBefore(id + '=' + join(leaf.next().next()) + (!ret && end ? '' : ';')); | ||
break; | ||
@@ -530,3 +711,3 @@ case JsNode.ARRLTR: | ||
index: i | ||
}); | ||
}, ret); | ||
} | ||
@@ -536,9 +717,9 @@ }); | ||
case JsNode.OBJLTR: | ||
self.jsdc.appendBefore('var '); | ||
(ret || self.jsdc).appendBefore('var '); | ||
var temp = self.jsdc.uid(); | ||
if(data.name) { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '["' + data.name + '"];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
(ret || self.jsdc).appendBefore(temp + '=' + data.temp + '[' + data.index + '];'); | ||
} | ||
@@ -552,7 +733,7 @@ var target = self.getArray(node.leaves()); | ||
var id = leaf.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + id + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + id + '"]' + (!ret && end ? '' : ';')); | ||
if(leaf.next()) { | ||
var init = leaf.next(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + id + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -566,8 +747,8 @@ break; | ||
var id = last.token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.size() == 2) { | ||
var init = last.last(); | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(id + join(init) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(id + join(init) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -577,7 +758,7 @@ break; | ||
var id = last.first().token().content(); | ||
self.jsdc.appendBefore(id + '=' + temp + '["' + name + '"]' + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore(id + '=' + temp + '["' + name + '"]' + (!ret && end ? '' : ';')); | ||
//初始化赋值 | ||
if(last.next()) { | ||
self.jsdc.appendBefore((end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
self.jsdc.appendBefore(join(last.parent()) + (end ? '' : ';')); | ||
(ret || self.jsdc).appendBefore((!ret && end ? ';' : '') + 'if(!' + temp + '.hasOwnProperty("' + name + '"))'); | ||
(ret || self.jsdc).appendBefore(join(last.parent()) + (!ret && end ? '' : ';')); | ||
} | ||
@@ -592,3 +773,3 @@ break; | ||
index: 0 | ||
}); | ||
}, ret); | ||
break; | ||
@@ -595,0 +776,0 @@ } |
195
web/Forof.js
@@ -12,5 +12,7 @@ define(function(require, exports, module) { | ||
this.hash = {}; | ||
this.pos = {}; | ||
this.destruct = {}; | ||
}).methods({ | ||
parse: function(node, start) { | ||
//有可能被Generator中该写过 | ||
//有可能被Generator中改写过 | ||
if(node.gen) { | ||
@@ -20,9 +22,34 @@ return; | ||
if(start) { | ||
var of = node.leaf(3); | ||
if(node.first().token().content() == 'for' | ||
&& of.name() == JsNode.TOKEN | ||
&& of.token().content() == 'of') { | ||
//存放临时li供block首尾改写引用 | ||
this.hash[node.nid()] = true; | ||
this.jsdc.ignore(of, 'forof1'); | ||
if(node.first().token().content() == 'for') { | ||
var of = node.leaf(3); | ||
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 | ||
&& leaf.size() == 1 | ||
&& [JsNode.OBJLTR, JsNode.ARRLTR].indexOf(leaf.first().name()) > -1) { | ||
this.destruct[node.nid()] = leaf.first(); | ||
this.hash[node.nid()] = this.getLast(leaf.first()); | ||
this.jsdc.ignore(leaf, 'forof1'); | ||
} | ||
else { | ||
this.hash[node.nid()] = true; | ||
} | ||
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'); | ||
} | ||
} | ||
} | ||
@@ -42,2 +69,5 @@ } | ||
&& this.hash.hasOwnProperty(parent.nid())) { | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
this.jsdc.append(this.hash[parent.nid()]); | ||
} | ||
this.jsdc.append('='); | ||
@@ -52,11 +82,17 @@ } | ||
this.jsdc.append('.next();!'); | ||
var k = parent.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
var v = join(parent.leaf(4)); | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
var k; | ||
if(typeof this.hash[parent.nid()] == 'string') { | ||
k = this.hash[parent.nid()]; | ||
} | ||
else { | ||
k = join(k); | ||
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); | ||
} | ||
} | ||
var v = join(parent.leaf(this.pos[parent.nid()]+1)); | ||
this.jsdc.append(k + '.done;'); | ||
@@ -88,12 +124,129 @@ this.jsdc.append(k + '=' + v + '.next()'); | ||
}, | ||
assign: function(node) { | ||
var k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
assign: function(node, ret) { | ||
var k; | ||
if(ret) { | ||
k = ret.o; | ||
} | ||
else if(typeof this.hash[node.nid()] == 'string') { | ||
k = this.hash[node.nid()]; | ||
} | ||
else { | ||
k = join(k); | ||
k = node.leaf(2); | ||
//forof的varstmt只能有一个id,其它为mmbexpr或destruct | ||
if(k.name() == JsNode.VARSTMT) { | ||
k = k.last().first().first().token().content(); | ||
} | ||
else { | ||
k = join(k); | ||
} | ||
} | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(ret) { | ||
if(ret.pos == 4) { | ||
this.jsdc.destruct.parse(node, true, ret); | ||
this.jsdc.destruct.parse(node, false, ret); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(node, true, ret); | ||
this.jsdc.destruct.expr(node, false, ret); | ||
} | ||
return ret.s; | ||
} | ||
else { | ||
this.jsdc.append(k + '=' + k + '.value;'); | ||
if(this.destruct.hasOwnProperty(node.nid())) { | ||
var ret2 = { | ||
o: k, | ||
s: '', | ||
append: function(s) { | ||
this.s += s; | ||
}, | ||
appendBefore: function(s) { | ||
this.s += s; | ||
} | ||
}; | ||
if(this.pos[node.nid()] == 4) { | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.parse(this.destruct[node.nid()], false, ret2); | ||
} | ||
else { | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], true, ret2); | ||
this.jsdc.destruct.expr(this.destruct[node.nid()], false, ret2); | ||
} | ||
this.jsdc.append(ret2.s); | ||
} | ||
} | ||
}, | ||
getLast: function(node) { | ||
if(node.name() == JsNode.ARRLTR | ||
|| node.name() == JsNode.ARRBINDPAT) { | ||
return this.getArrLast(node); | ||
} | ||
else { | ||
return this.getObjLast(node); | ||
} | ||
}, | ||
getArrLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.SINGLENAME | ||
|| s == JsNode.ASSIGNEXPR) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
temp = temp.first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else { | ||
return this.getLast(temp); | ||
} | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
}, | ||
getObjLast: function(node) { | ||
for(var leaves = node.leaves(), i = leaves.length - 2; i > 0; i--) { | ||
var temp = leaves[i]; | ||
var s = temp.name(); | ||
if(s == JsNode.BINDPROPT) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
s = leaves[0].name(); | ||
return leaves[0].first().first().token().content(); | ||
} | ||
else { | ||
temp = leaves[2]; | ||
s = temp.name(); | ||
if(s == JsNode.SINGLENAME) { | ||
return temp.first().first().token().content(); | ||
} | ||
else if(s == JsNode.BINDELEM) { | ||
return this.getLast(temp.first()); | ||
} | ||
} | ||
} | ||
else if(s == JsNode.PROPTDEF) { | ||
leaves = temp.leaves(); | ||
if(leaves.length < 3) { | ||
return leaves[0].token().content(); | ||
} | ||
else { | ||
temp = leaves[2].first(); | ||
s = temp.name(); | ||
if(s == JsNode.TOKEN) { | ||
return temp.token().content(); | ||
} | ||
else if(s == JsNode.PRMREXPR) { | ||
return temp.first().token().content(); | ||
} | ||
else { | ||
return this.getLast(temp); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -100,0 +253,0 @@ }); |
@@ -134,2 +134,6 @@ define(function(require, exports, module) { | ||
self.jsdc.append('return{value:'); | ||
//无yield返回,则附上undefined | ||
if(node.size() == 1) { | ||
self.jsdc.append('void 0'); | ||
} | ||
} | ||
@@ -268,3 +272,3 @@ } | ||
if(!start) { | ||
self.jsdc.append('{value:'); | ||
self.jsdc.append('{value:void 0'); | ||
} | ||
@@ -420,144 +424,240 @@ }); | ||
self.jsdc.ignore(block.prev(), 'gen27'); | ||
switch(itstmt.leaf(3).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()); | ||
//特殊的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 { | ||
id = join(node.leaf(2)); | ||
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'; | ||
} | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
}); | ||
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(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
else { | ||
id = join(node.leaf(2)); | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
}); | ||
} | ||
else { | ||
switch(itstmt.leaf(3).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()); | ||
} | ||
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'; | ||
id = join(node.leaf(2)); | ||
} | ||
}); | ||
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;'); | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
else { | ||
self.jsdc.appendBefore(',' + keys + '=Object.keys('); | ||
self.jsdc.appendBefore(obj); | ||
self.jsdc.appendBefore('),' + len); | ||
self.jsdc.appendBefore('=' + keys + '.length,'); | ||
self.jsdc.appendBefore(index + '=0;'); | ||
endTemp = ++top.index2; | ||
self.jsdc.appendBefore('case ' + endTemp + ':'); | ||
self.jsdc.appendBefore(top.state + '='); | ||
self.jsdc.appendBefore(index + '++<' + len + '?'); | ||
itTemp = ++top.index2; | ||
itEndTemp = ++top.index2; | ||
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;'); | ||
//供yield判断 | ||
itstmt.done = index + '<' + len; | ||
} | ||
}); | ||
eventbus.on(block.nid(), function(node, start) { | ||
if(start) { | ||
self.jsdc.append('case ' + itTemp + ':'); | ||
self.jsdc.append(id + '='); | ||
self.jsdc.append(keys + '['); | ||
self.jsdc.append(index + '];'); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
break; | ||
case 'of': | ||
var iterator; | ||
//标记使Forof类处理失效 | ||
itstmt.gen = true; | ||
var next = self.jsdc.uid(); | ||
var id; | ||
var isDestruct; | ||
if(node.leaf(2).name() == JsNode.VARSTMT) { | ||
id = join(node.leaf(2).last()); | ||
} | ||
else { | ||
self.jsdc.appendBefore(top.state + '=' + endTemp); | ||
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':'); | ||
} | ||
}); | ||
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('('); | ||
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); | ||
} | ||
itTemp = ++top.index2; | ||
else { | ||
id = join(node.leaf(2)); | ||
} | ||
} | ||
else { | ||
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) { | ||
self.jsdc.appendBefore(')'); | ||
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) { | ||
if(start) { | ||
self.jsdc.append('var ' + obj + '='); | ||
top = self.hash[nid]; | ||
} | ||
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 + ':'); | ||
} | ||
}); | ||
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: 3, | ||
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 + ':'); | ||
} | ||
}); | ||
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 + ':'); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -564,0 +664,0 @@ break; |
@@ -207,3 +207,5 @@ define(function(require, exports, module) { | ||
if(!ignore || ig.type() != Token.BLANK) { | ||
this.res += ig.content(); | ||
if(!ig.ignore) { | ||
this.res += ig.content(); | ||
} | ||
ignore && (ig.ignore = true); | ||
@@ -375,13 +377,25 @@ ignore = false; | ||
}, | ||
ignore: function(node, msg) { | ||
ignore: function(node, msg, prev) { | ||
var self = this; | ||
if(node instanceof Token) { | ||
node.ignore = msg || true; | ||
//忽略前置空格 | ||
if(prev) { | ||
prev = node; | ||
while(prev = prev.prev()) { | ||
if(prev.type() == Token.BLANK) { | ||
prev.ignore = msg || true; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
else if(node.name() == JsNode.TOKEN) { | ||
this.ignore(node.token()); | ||
this.ignore(node.token(), msg, prev); | ||
} | ||
else { | ||
node.leaves().forEach(function(leaf) { | ||
self.ignore(leaf, msg); | ||
self.ignore(leaf, msg, prev); | ||
}); | ||
@@ -388,0 +402,0 @@ } |
@@ -32,6 +32,7 @@ define(function(require, exports, module) { | ||
this.body(node.last().prev(), o.name, o.extend); | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var _=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append('_.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=_'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
@@ -80,6 +81,7 @@ } | ||
if(o.extend) { | ||
var temp = this.jsdc.uid(); | ||
this.jsdc.append('!function(){'); | ||
this.jsdc.append('var _=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append('_.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=_'); | ||
this.jsdc.append('var ' + temp + '=Object.create(' + o.extend + '.prototype);'); | ||
this.jsdc.append(temp + '.constructor=' + o.name + ';'); | ||
this.jsdc.append(o.name + '.prototype=' + temp); | ||
this.jsdc.append('}();'); | ||
@@ -86,0 +88,0 @@ } |
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
300916
6975
Updatedhomunculus@^0.3.7