From 75bae2065b1fbd588251186565e4e750fb84fe24 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 17 Mar 2018 13:53:20 +0100 Subject: [PATCH] Added do-while loop --- src/Transpiler.ts | 15 +++++++++++++++ test/src/util.ts | 14 ++++++++++++++ test/unit/while.spec.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 test/unit/while.spec.ts diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 2cfff1e04..ac5cb41ae 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -160,6 +160,8 @@ export class LuaTranspiler { return this.transpileIf(node); case ts.SyntaxKind.WhileStatement: return this.transpileWhile(node); + case ts.SyntaxKind.DoStatement: + return this.transpileDoStatement(node); case ts.SyntaxKind.ForStatement: return this.transpileFor(node); case ts.SyntaxKind.ForOfStatement: @@ -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 = (node.initializer).declarations[0]; diff --git a/test/src/util.ts b/test/src/util.ts index 53f50d9aa..71ddd40b1 100644 --- a/test/src/util.ts +++ b/test/src/util.ts @@ -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"; @@ -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"; diff --git a/test/unit/while.spec.ts b/test/unit/while.spec.ts new file mode 100644 index 000000000..29f9e3295 --- /dev/null +++ b/test/unit/while.spec.ts @@ -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); + } +}