using 'yield' automatically turns functions into generators

This commit is contained in:
Andreas Lubbe
2013-11-15 22:19:31 +01:00
parent dafc7bdea5
commit 9941050120
2 changed files with 15 additions and 1 deletions

View File

@@ -1798,6 +1798,14 @@
this.params = params || [];
this.body = body || new Block;
this.bound = tag === 'boundfunc';
this.isGenerator = false;
this.body.traverseChildren(false, (function(_this) {
return function(child) {
if (child.operator === 'yield') {
return _this.isGenerator = true;
}
};
})(this));
}
Code.prototype.children = ['params', 'body'];
@@ -1910,6 +1918,9 @@
this.body.makeReturn();
}
code = 'function';
if (this.isGenerator) {
code += '*';
}
if (this.ctor) {
code += ' ' + this.name;
}

View File

@@ -456,7 +456,6 @@ exports.Return = class Return extends Base
answer.push @makeCode ";"
return answer
#### Value
# A value, variable or literal or parenthesized, indexed or dotted into,
@@ -1302,6 +1301,9 @@ exports.Code = class Code extends Base
@params = params or []
@body = body or new Block
@bound = tag is 'boundfunc'
@isGenerator = false
@body.traverseChildren false, (child) =>
@isGenerator = true if child.operator is 'yield'
children: ['params', 'body']
@@ -1369,6 +1371,7 @@ exports.Code = class Code extends Base
uniqs.push name
@body.makeReturn() unless wasEmpty or @noReturn
code = 'function'
code += '*' if @isGenerator
code += ' ' + @name if @ctor
code += '('
answer = [@makeCode(code)]