-
-
Notifications
You must be signed in to change notification settings - Fork 185
Generator functions #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Generator functions #384
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fbda8cf
-added generateAnnonymousIdentifier
76fcf9e
-added support to transpile generator functions and yield calls
ad78338
-added tests
d5bcb86
Merge remote-tracking branch 'origin/master' into yeld_statement
431caba
-fixed hoisting for generator functions
38ea839
-cleaned up formatting and moved the return logic in the generator it…
63299b2
-added error check
fba7761
-removed superfluous check
872002a
-underscodes++
d17cb35
-removed unused function
5ed80e2
-missing local
e26e15b
-added generator for...of test
261e3b0
-removed unused function
b18eee1
-cleaned up tests
39ef866
Merge remote-tracking branch 'origin/master' into yeld_statement
7813ca8
-spacing
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,6 @@ import {TSTLErrors} from "./TSTLErrors"; | |
|
|
||
| export type StatementVisitResult = tstl.Statement | tstl.Statement[] | undefined; | ||
| export type ExpressionVisitResult = tstl.Expression | undefined; | ||
|
|
||
| export enum ScopeType { | ||
| File = 0x1, | ||
| Function = 0x2, | ||
|
|
@@ -1031,6 +1030,144 @@ export class LuaTransformer { | |
| }); | ||
| } | ||
|
|
||
| private transformGeneratorFunction( | ||
| parameters: ts.NodeArray<ts.ParameterDeclaration>, | ||
| body: ts.Block, | ||
| transformedParameters: tstl.Identifier[], | ||
| dotsLiteral: tstl.DotsLiteral, | ||
| spreadIdentifier?: tstl.Identifier | ||
| ): [tstl.Statement[], Scope] | ||
| { | ||
| this.importLuaLibFeature(LuaLibFeature.Symbol); | ||
| const [functionBody, functionScope] = this.transformFunctionBody( | ||
| parameters, | ||
| body, | ||
| spreadIdentifier | ||
| ); | ||
|
|
||
| const coroutineIdentifier = tstl.createIdentifier("____co"); | ||
| const valueIdentifier = tstl.createIdentifier("____value"); | ||
| const errIdentifier = tstl.createIdentifier("____err"); | ||
| const itIdentifier = tstl.createIdentifier("____it"); | ||
|
|
||
| //local ____co = coroutine.create(originalFunction) | ||
| const coroutine = | ||
| tstl.createVariableDeclarationStatement(coroutineIdentifier, | ||
| tstl.createCallExpression( | ||
| tstl.createTableIndexExpression(tstl.createIdentifier("coroutine"), | ||
| tstl.createStringLiteral("create") | ||
| ), | ||
| [tstl.createFunctionExpression( | ||
| tstl.createBlock(functionBody), | ||
| transformedParameters, | ||
| dotsLiteral, | ||
| spreadIdentifier), | ||
| ] | ||
| ) | ||
| ); | ||
|
|
||
| const nextBody = []; | ||
| // coroutine.resume(__co, ...) | ||
| const resumeCall = tstl.createCallExpression( | ||
| tstl.createTableIndexExpression( | ||
| tstl.createIdentifier("coroutine"), | ||
| tstl.createStringLiteral("resume") | ||
| ), | ||
| [coroutineIdentifier, tstl.createDotsLiteral()] | ||
| ); | ||
|
|
||
| // ____err, ____value = coroutine.resume(____co, ...) | ||
| nextBody.push(tstl.createVariableDeclarationStatement( | ||
| [errIdentifier, valueIdentifier], | ||
| resumeCall) | ||
| ); | ||
|
|
||
| //coroutine.status(____co) ~= "dead"; | ||
| const coStatus = tstl.createCallExpression( | ||
| tstl.createTableIndexExpression( | ||
| tstl.createIdentifier("coroutine"), | ||
| tstl.createStringLiteral("status") | ||
| ), | ||
| [coroutineIdentifier] | ||
| ); | ||
| const status = tstl.createBinaryExpression( | ||
| coStatus, | ||
| tstl.createStringLiteral("dead"), | ||
| tstl.SyntaxKind.EqualityOperator | ||
| ); | ||
| nextBody.push(status); | ||
| //if(not ____err){error(____value)} | ||
| const errorCheck = tstl.createIfStatement( | ||
| tstl.createUnaryExpression( | ||
| errIdentifier, | ||
| tstl.SyntaxKind.NotOperator | ||
| ), | ||
| tstl.createBlock([ | ||
| tstl.createExpressionStatement( | ||
| tstl.createCallExpression( | ||
| tstl.createIdentifier("error"), | ||
| [valueIdentifier] | ||
| ) | ||
| ), | ||
| ]) | ||
| ); | ||
| nextBody.push(errorCheck); | ||
| //{done = coroutine.status(____co) ~= "dead"; value = ____value} | ||
| const iteratorResult = tstl.createTableExpression([ | ||
| tstl.createTableFieldExpression( | ||
| status, | ||
| tstl.createStringLiteral("done") | ||
| ), | ||
| tstl.createTableFieldExpression( | ||
| valueIdentifier, | ||
| tstl.createStringLiteral("value") | ||
| ), | ||
| ]); | ||
| nextBody.push(tstl.createReturnStatement([iteratorResult])); | ||
|
|
||
| //function(____, ...) | ||
| const nextFunctionDeclaration = tstl.createFunctionExpression( | ||
| tstl.createBlock(nextBody), | ||
| [tstl.createAnnonymousIdentifier()], | ||
| tstl.createDotsLiteral()); | ||
|
|
||
| //____it = {next = function(____, ...)} | ||
| const iterator = tstl.createVariableDeclarationStatement( | ||
| itIdentifier, | ||
| tstl.createTableExpression([ | ||
| tstl.createTableFieldExpression( | ||
| nextFunctionDeclaration, | ||
| tstl.createStringLiteral("next") | ||
| ), | ||
| ]) | ||
| ); | ||
|
|
||
| const symbolIterator = tstl.createTableIndexExpression( | ||
| tstl.createIdentifier("Symbol"), | ||
| tstl.createStringLiteral("iterator") | ||
| ); | ||
|
|
||
| const block = [ | ||
| coroutine, | ||
| iterator, | ||
| //____it[Symbol.iterator] = {return ____it} | ||
| tstl.createAssignmentStatement( | ||
| tstl.createTableIndexExpression( | ||
| itIdentifier, | ||
| symbolIterator | ||
| ), | ||
| tstl.createFunctionExpression( | ||
| tstl.createBlock( | ||
| [tstl.createReturnStatement([itIdentifier])] | ||
| ) | ||
| ) | ||
| ), | ||
| //return ____it | ||
| tstl.createReturnStatement([itIdentifier]), | ||
| ]; | ||
| return [block, functionScope]; | ||
| } | ||
|
|
||
| public transformFunctionDeclaration(functionDeclaration: ts.FunctionDeclaration): StatementVisitResult { | ||
| // Don't transform functions without body (overload declarations) | ||
| if (!functionDeclaration.body) { | ||
|
|
@@ -1044,22 +1181,28 @@ export class LuaTransformer { | |
| const [params, dotsLiteral, restParamName] = this.transformParameters(functionDeclaration.parameters, context); | ||
|
|
||
| const name = this.transformIdentifier(functionDeclaration.name); | ||
| const [body, functionScope] = this.transformFunctionBody( | ||
| functionDeclaration.parameters, | ||
| functionDeclaration.body, | ||
| restParamName | ||
| ); | ||
| const [body, functionScope] = functionDeclaration.asteriskToken | ||
| ? this.transformGeneratorFunction( | ||
| functionDeclaration.parameters, | ||
| functionDeclaration.body, | ||
| params, | ||
| dotsLiteral, | ||
| restParamName | ||
| ) | ||
| : this.transformFunctionBody( | ||
| functionDeclaration.parameters, | ||
| functionDeclaration.body, | ||
| restParamName | ||
| ); | ||
| const block = tstl.createBlock(body); | ||
| const functionExpression = tstl.createFunctionExpression(block, params, dotsLiteral, restParamName); | ||
|
|
||
| // Remember symbols referenced in this function for hoisting later | ||
| if (!this.options.noHoisting && name.symbolId !== undefined) { | ||
| const scope = this.peekScope(); | ||
| if (!scope.functionDefinitions) { scope.functionDefinitions = new Map(); } | ||
| const functionInfo = {referencedSymbols: functionScope.referencedSymbols || new Set()}; | ||
| scope.functionDefinitions.set(name.symbolId, functionInfo); | ||
| } | ||
|
|
||
| return this.createLocalOrExportedOrGlobalDeclaration(name, functionExpression, functionDeclaration); | ||
| } | ||
|
|
||
|
|
@@ -1219,6 +1362,12 @@ export class LuaTransformer { | |
| return tstl.createExpressionStatement(this.transformExpression(expression)); | ||
| } | ||
|
|
||
| public transformYield(expression: ts.YieldExpression): tstl.Expression { | ||
| return tstl.createCallExpression( | ||
| tstl.createTableIndexExpression(tstl.createIdentifier("coroutine"), tstl.createStringLiteral("yield")), | ||
| expression.expression?[this.transformExpression(expression.expression)]:[], expression); | ||
| } | ||
|
|
||
| public transformReturn(statement: ts.ReturnStatement): tstl.Statement { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| if (statement.expression) { | ||
| const returnType = tsHelper.getContainingFunctionReturnType(statement, this.checker); | ||
|
|
@@ -1731,6 +1880,8 @@ export class LuaTransformer { | |
| return this.transformSpreadElement(expression as ts.SpreadElement); | ||
| case ts.SyntaxKind.NonNullExpression: | ||
| return this.transformExpression((expression as ts.NonNullExpression).expression); | ||
| case ts.SyntaxKind.YieldExpression: | ||
| return this.transformYield(expression as ts.YieldExpression); | ||
| case ts.SyntaxKind.EmptyStatement: | ||
| return undefined; | ||
| case ts.SyntaxKind.NotEmittedStatement: | ||
|
|
@@ -1871,7 +2022,6 @@ export class LuaTransformer { | |
| throw TSTLErrors.UnsupportedUnionAccessor(lhs); | ||
| } | ||
| } | ||
|
|
||
| return tstl.createAssignmentStatement( | ||
| this.transformExpression(lhs) as tstl.IdentifierOrTableIndexExpression, | ||
| right, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.