File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed
Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,8 @@ export class LuaTranspiler {
160160 return this . transpileIf ( < ts . IfStatement > node ) ;
161161 case ts . SyntaxKind . WhileStatement :
162162 return this . transpileWhile ( < ts . WhileStatement > node ) ;
163+ case ts . SyntaxKind . DoStatement :
164+ return this . transpileDoStatement ( < ts . DoStatement > node ) ;
163165 case ts . SyntaxKind . ForStatement :
164166 return this . transpileFor ( < ts . ForStatement > node ) ;
165167 case ts . SyntaxKind . ForOfStatement :
@@ -312,6 +314,19 @@ export class LuaTranspiler {
312314 return result + this . indent + "end\n" ;
313315 }
314316
317+ transpileDoStatement ( node : ts . DoStatement ) : string {
318+ let result = this . indent + `repeat\n` ;
319+
320+ this . pushIndent ( ) ;
321+ result += this . transpileStatement ( node . statement ) ;
322+ this . popIndent ( ) ;
323+
324+ // Negate the expression because we translate from do-while to repeat-until (repeat-while-not)
325+ result += this . indent + `until not ${ this . transpileExpression ( node . expression , true ) } \n` ;
326+
327+ return result ;
328+ }
329+
315330 transpileFor ( node : ts . ForStatement ) : string {
316331 // Get iterator variable
317332 const variable = ( < ts . VariableDeclarationList > node . initializer ) . declarations [ 0 ] ;
Original file line number Diff line number Diff line change 11import * as ts from "typescript" ;
22import * as path from "path" ;
33
4+ import { Expect } from "alsatian" ;
5+
46import { LuaTranspiler , TranspileError } from "../../src/Transpiler" ;
57import { CompilerOptions } from "../../src/CommandLineParser" ;
68
@@ -60,6 +62,18 @@ export function executeLua(lua: string, withLib = true): any {
6062 return luavm . execute ( lua ) [ 0 ] ;
6163}
6264
65+ export function expectCodeEqual ( code1 : string , code2 : string ) {
66+ // Trim leading/trailing whitespace
67+ let c1 = code1 . trim ( ) ;
68+ let c2 = code2 . trim ( ) ;
69+
70+ // Unify indentation
71+ c1 = c1 . replace ( / \s + / g, " " ) ;
72+ c2 = c2 . replace ( / \s + / g, " " ) ;
73+
74+ Expect ( c1 ) . toBe ( c2 ) ;
75+ }
76+
6377const lualib = fs . readFileSync ( "dist/lualib/typescript.lua" ) + "\n" ;
6478
6579const jsonlib = fs . readFileSync ( "test/src/json.lua" ) + "\n" ;
Original file line number Diff line number Diff line change 1+ import { Expect , Test , TestCase , FocusTest } from "alsatian" ;
2+ import * as util from "../src/util" ;
3+
4+ export class WhileTests {
5+
6+ @Test ( "While loop" )
7+ public defaultWhile ( ) {
8+ const input = `declare var a: number;
9+ while (a < 10) {
10+ a++;
11+ }` ;
12+
13+ const expected = `while a<10 do
14+ a=a+1
15+ end` ;
16+
17+ util . expectCodeEqual ( util . transpileString ( input ) , expected ) ;
18+ }
19+
20+ @Test ( "Do While" )
21+ public doWhile ( ) {
22+ const input = `declare var a: number;
23+ do {
24+ a++;
25+ } while (a < 10);` ;
26+
27+ const expected = `repeat
28+ a=a+1
29+ until not (a<10)` ;
30+
31+ util . expectCodeEqual ( util . transpileString ( input ) , expected ) ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments