Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/transformation/utils/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ interface FunctionDefinitionInfo {
definition?: lua.VariableDeclarationStatement | lua.AssignmentStatement;
}

export enum LoopContinued {
WithGoto,
WithRepeatBreak,
}

export interface Scope {
type: ScopeType;
id: number;
Expand All @@ -30,7 +35,7 @@ export interface Scope {
variableDeclarations?: lua.VariableDeclarationStatement[];
functionDefinitions?: Map<lua.SymbolId, FunctionDefinitionInfo>;
importStatements?: lua.Statement[];
loopContinued?: boolean;
loopContinued?: LoopContinued;
functionReturned?: boolean;
}

Expand Down
28 changes: 18 additions & 10 deletions src/transformation/visitors/break-continue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import * as lua from "../../LuaAST";
import { FunctionVisitor } from "../context";
import { unsupportedForTarget } from "../utils/diagnostics";
import { findScope, ScopeType } from "../utils/scope";
import { findScope, LoopContinued, ScopeType } from "../utils/scope";

export const transformBreakStatement: FunctionVisitor<ts.BreakStatement> = (breakStatement, context) => {
void context;
return lua.createBreakStatement(breakStatement);
};

export const transformContinueStatement: FunctionVisitor<ts.ContinueStatement> = (statement, context) => {
if (
const scope = findScope(context, ScopeType.Loop);
const continuedWith =
context.luaTarget === LuaTarget.Universal ||
context.luaTarget === LuaTarget.Lua50 ||
context.luaTarget === LuaTarget.Lua51
) {
context.diagnostics.push(unsupportedForTarget(statement, "Continue statement", context.luaTarget));
}

const scope = findScope(context, ScopeType.Loop);
? LoopContinued.WithRepeatBreak
: LoopContinued.WithGoto;

if (scope) {
scope.loopContinued = true;
scope.loopContinued = continuedWith;
}

return lua.createGotoStatement(`__continue${scope?.id ?? ""}`, statement);
const label = `__continue${scope?.id ?? ""}`;

switch (continuedWith) {
case LoopContinued.WithGoto:
return lua.createGotoStatement(label, statement);

case LoopContinued.WithRepeatBreak:
return [
lua.createAssignmentStatement(lua.createIdentifier(label), lua.createBooleanLiteral(true), statement),
lua.createBreakStatement(statement),
];
}
};
32 changes: 24 additions & 8 deletions src/transformation/visitors/loops/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from "typescript";
import * as lua from "../../../LuaAST";
import { TransformationContext } from "../../context";
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
import { performHoisting, ScopeType } from "../../utils/scope";
import { LoopContinued, performHoisting, ScopeType } from "../../utils/scope";
import { isAssignmentPattern } from "../../utils/typescript";
import { transformAssignment } from "../binary-expression/assignments";
import { transformAssignmentPattern } from "../binary-expression/destructuring-assignments";
Expand All @@ -19,15 +19,31 @@ export function transformLoopBody(
const scope = context.popScope();
const scopeId = scope.id;

if (!scope.loopContinued) {
return body;
}
switch (scope.loopContinued) {
case undefined:
return body;

case LoopContinued.WithGoto:
return [lua.createDoStatement(body), lua.createLabelStatement(`__continue${scopeId}`)];

const baseResult: lua.Statement[] = [lua.createDoStatement(body)];
const continueLabel = lua.createLabelStatement(`__continue${scopeId}`);
baseResult.push(continueLabel);
case LoopContinued.WithRepeatBreak:
const identifier = lua.createIdentifier(`__continue${scopeId}`);
const literalTrue = lua.createBooleanLiteral(true);

return baseResult;
return [
lua.createDoStatement([
lua.createVariableDeclarationStatement(identifier),
lua.createRepeatStatement(
lua.createBlock([...body, lua.createAssignmentStatement(identifier, literalTrue)]),
literalTrue
),
lua.createIfStatement(
lua.createUnaryExpression(identifier, lua.SyntaxKind.NotOperator),
lua.createBlock([lua.createBreakStatement()])
),
]),
];
}
}

export function getVariableDeclarationBinding(
Expand Down
177 changes: 0 additions & 177 deletions test/unit/__snapshots__/loops.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,180 +11,3 @@ return ____exports"
`;

exports[`forin[Array]: diagnostics 1`] = `"main.ts(3,9): error TSTL: Iterating over arrays with 'for ... in' is not allowed."`;

exports[`loop continue (do { continue; } while (false)) [5.0]: code 1`] = `
"repeat
do
do
goto __continue2
end
::__continue2::
end
until not false"
`;

exports[`loop continue (do { continue; } while (false)) [5.0]: diagnostics 1`] = `"main.ts(1,6): error TSTL: Continue statement is/are not supported for target Lua 5.0."`;

exports[`loop continue (do { continue; } while (false)) [5.1]: code 1`] = `
"repeat
do
do
goto __continue2
end
::__continue2::
end
until not false"
`;

exports[`loop continue (do { continue; } while (false)) [5.1]: diagnostics 1`] = `"main.ts(1,6): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

exports[`loop continue (do { continue; } while (false)) [universal]: code 1`] = `
"repeat
do
do
goto __continue2
end
::__continue2::
end
until not false"
`;

exports[`loop continue (do { continue; } while (false)) [universal]: diagnostics 1`] = `"main.ts(1,6): error TSTL: Continue statement is/are not supported for target Lua universal."`;

exports[`loop continue (for (;;) { continue; }) [5.0]: code 1`] = `
"do
while true do
do
goto __continue2
end
::__continue2::
end
end"
`;

exports[`loop continue (for (;;) { continue; }) [5.0]: diagnostics 1`] = `"main.ts(1,12): error TSTL: Continue statement is/are not supported for target Lua 5.0."`;

exports[`loop continue (for (;;) { continue; }) [5.1]: code 1`] = `
"do
while true do
do
goto __continue2
end
::__continue2::
end
end"
`;

exports[`loop continue (for (;;) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,12): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

exports[`loop continue (for (;;) { continue; }) [universal]: code 1`] = `
"do
while true do
do
goto __continue2
end
::__continue2::
end
end"
`;

exports[`loop continue (for (;;) { continue; }) [universal]: diagnostics 1`] = `"main.ts(1,12): error TSTL: Continue statement is/are not supported for target Lua universal."`;

exports[`loop continue (for (const a in {}) { continue; }) [5.0]: code 1`] = `
"for a in pairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a in {}) { continue; }) [5.0]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.0."`;

exports[`loop continue (for (const a in {}) { continue; }) [5.1]: code 1`] = `
"for a in pairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a in {}) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

exports[`loop continue (for (const a in {}) { continue; }) [universal]: code 1`] = `
"for a in pairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a in {}) { continue; }) [universal]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua universal."`;

exports[`loop continue (for (const a of []) { continue; }) [5.0]: code 1`] = `
"for ____, a in ipairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a of []) { continue; }) [5.0]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.0."`;

exports[`loop continue (for (const a of []) { continue; }) [5.1]: code 1`] = `
"for ____, a in ipairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a of []) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

exports[`loop continue (for (const a of []) { continue; }) [universal]: code 1`] = `
"for ____, a in ipairs({}) do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (for (const a of []) { continue; }) [universal]: diagnostics 1`] = `"main.ts(1,23): error TSTL: Continue statement is/are not supported for target Lua universal."`;

exports[`loop continue (while (false) { continue; }) [5.0]: code 1`] = `
"while false do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (while (false) { continue; }) [5.0]: diagnostics 1`] = `"main.ts(1,17): error TSTL: Continue statement is/are not supported for target Lua 5.0."`;

exports[`loop continue (while (false) { continue; }) [5.1]: code 1`] = `
"while false do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (while (false) { continue; }) [5.1]: diagnostics 1`] = `"main.ts(1,17): error TSTL: Continue statement is/are not supported for target Lua 5.1."`;

exports[`loop continue (while (false) { continue; }) [universal]: code 1`] = `
"while false do
do
goto __continue2
end
::__continue2::
end"
`;

exports[`loop continue (while (false) { continue; }) [universal]: diagnostics 1`] = `"main.ts(1,17): error TSTL: Continue statement is/are not supported for target Lua universal."`;
Loading