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
15 changes: 15 additions & 0 deletions src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export class LuaTranspiler {
return this.transpileIf(<ts.IfStatement>node);
case ts.SyntaxKind.WhileStatement:
return this.transpileWhile(<ts.WhileStatement>node);
case ts.SyntaxKind.DoStatement:
return this.transpileDoStatement(<ts.DoStatement>node);
case ts.SyntaxKind.ForStatement:
return this.transpileFor(<ts.ForStatement>node);
case ts.SyntaxKind.ForOfStatement:
Expand Down Expand Up @@ -312,6 +314,19 @@ export class LuaTranspiler {
return result + this.indent + "end\n";
}

transpileDoStatement(node: ts.DoStatement): string {
let result = this.indent + `repeat\n`;

this.pushIndent();
result += this.transpileStatement(node.statement);
this.popIndent();

// Negate the expression because we translate from do-while to repeat-until (repeat-while-not)
result += this.indent + `until not ${this.transpileExpression(node.expression, true)}\n`;

return result;
}

transpileFor(node: ts.ForStatement): string {
// Get iterator variable
const variable = (<ts.VariableDeclarationList>node.initializer).declarations[0];
Expand Down
14 changes: 14 additions & 0 deletions test/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as ts from "typescript";
import * as path from "path";

import { Expect } from "alsatian";

import { LuaTranspiler, TranspileError } from "../../src/Transpiler";
import { CompilerOptions } from "../../src/CommandLineParser";

Expand Down Expand Up @@ -60,6 +62,18 @@ export function executeLua(lua: string, withLib = true): any {
return luavm.execute(lua)[0];
}

export function expectCodeEqual(code1: string, code2: string) {
// Trim leading/trailing whitespace
let c1 = code1.trim();
let c2 = code2.trim();

// Unify indentation
c1 = c1.replace(/\s+/g, " ");
c2 = c2.replace(/\s+/g, " ");

Expect(c1).toBe(c2);
}

const lualib = fs.readFileSync("dist/lualib/typescript.lua") + "\n";

const jsonlib = fs.readFileSync("test/src/json.lua") + "\n";
Expand Down
33 changes: 33 additions & 0 deletions test/unit/while.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Expect, Test, TestCase, FocusTest } from "alsatian";
import * as util from "../src/util";

export class WhileTests {

@Test("While loop")
public defaultWhile() {
const input = `declare var a: number;
while (a < 10) {
a++;
}`;

const expected = `while a<10 do
a=a+1
end`;

util.expectCodeEqual(util.transpileString(input), expected);
}

@Test("Do While")
public doWhile() {
const input = `declare var a: number;
do {
a++;
} while (a < 10);`;

const expected = `repeat
a=a+1
until not (a<10)`;

util.expectCodeEqual(util.transpileString(input), expected);
}
}