Skip to content

Commit 046a28c

Browse files
committed
Replace numerical fors with while loops
1 parent f1c2d70 commit 046a28c

File tree

6 files changed

+38
-53
lines changed

6 files changed

+38
-53
lines changed

src/CommandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class CLIError extends Error {
1919

2020
const optionDeclarations: { [key: string]: yargs.Options } = {
2121
addHeader: {
22-
alias: "ah",
22+
alias: ["ah", "header"],
2323
default: true,
2424
describe: "Specify if a header will be added to compiled files.",
2525
type: "boolean",
@@ -154,9 +154,9 @@ function runDiagnostics(commandLine: ts.ParsedCommandLine) {
154154
}
155155

156156
/** Find configFile, function from ts api seems to be broken? */
157-
function findConfigFile(commandLine: ts.ParsedCommandLine) {
157+
export function findConfigFile(commandLine: ts.ParsedCommandLine) {
158158
if (!commandLine.options.project) {
159-
return;
159+
throw new CLIError(`error no base path provided, could not find config.`);
160160
}
161161
let configPath;
162162
if (path.isAbsolute(commandLine.options.project)) {

src/Transpiler.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as ts from "typescript";
22

33
import { CompilerOptions } from "./CommandLineParser";
4-
import { ForHelper } from "./ForHelper";
54
import { TSHelper as tsEx } from "./TSHelper";
65

76
import * as path from "path";
@@ -363,23 +362,25 @@ export class LuaTranspiler {
363362

364363
public transpileFor(node: ts.ForStatement): string {
365364
// Get iterator variable
366-
const variable = (node.initializer as ts.VariableDeclarationList).declarations[0];
367-
const identifier = variable.name as ts.Identifier;
368-
369-
// Populate three components of lua numeric for loop:
370-
const start = this.transpileExpression(variable.initializer);
371-
const end = ForHelper.GetForEnd(node.condition, this);
372-
const step = ForHelper.GetForStep(node.incrementor, this);
365+
const variableDeclaration = (node.initializer as ts.VariableDeclarationList).declarations[0];
366+
const variable = this.transpileVariableDeclaration(variableDeclaration);
367+
const condition = this.transpileExpression(node.condition);
368+
const incrementor = this.transpileExpression(node.incrementor);
373369

374370
// Add header
375-
let result = this.indent + `for ${identifier.escapedText}=${start},${end},${step} do\n`;
371+
let result = `--for(${variable.trim()}; ${condition}; ${incrementor};)\n`;
372+
result += this.indent + variable;
373+
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;
376374

377375
// Add body
378376
this.pushIndent();
379377
result += this.transpileStatement(node.statement);
378+
result += this.indent + incrementor + "\n";
380379
this.popIndent();
381380

382-
return result + this.indent + "end\n";
381+
result += this.indent + "end\n";
382+
383+
return result;
383384
}
384385

385386
public transpileForOf(node: ts.ForOfStatement): string {

test/compiler/project.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class CompilerProjectTests {
3232
"typescript_lualib.lua",
3333
"test_src/test_lib/file.lua",
3434
"test_src/main.lua")
35+
@TestCase(".",
36+
"typescript_lualib.lua",
37+
"test_src/test_lib/file.lua",
38+
"test_src/main.lua")
3539
@TestCase("test_src/main.ts",
3640
"typescript_lualib.lua",
3741
"test_src/test_lib/file.lua",

test/unit/cli.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Expect, Test, TestCase, Teardown } from "alsatian";
22

3-
import { CompilerOptions, parseCommandLine, ParsedCommandLine } from "../../src/CommandLineParser";
3+
import { CompilerOptions, findConfigFile, parseCommandLine, ParsedCommandLine } from "../../src/CommandLineParser";
44

55

66
export class CLITests {
@@ -23,7 +23,7 @@ export class CLITests {
2323
Expect(() => parseCommandLine(['--luaTarget', '42'])).toThrow();
2424
}
2525

26-
@Test("InvalidArgument")
26+
@Test("InvalidArgumentTSTL")
2727
public invalidArgument() {
2828
// Don't check error message because the yargs library messes the message up.
2929
Expect(() => parseCommandLine(['--invalidTarget', 'test'])).toThrow();
@@ -52,4 +52,10 @@ export class CLITests {
5252
Expect(parsedCommandLine.options['rootDir']).toBe('./testRoot');
5353
}
5454

55+
@Test("Find config no path")
56+
public findConfigNoPath() {
57+
Expect(() => findConfigFile({options: {}, fileNames: [], errors: []})).toThrow();
58+
59+
}
60+
5561
}

test/unit/loops.spec.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export class LuaLoopTests {
102102
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length > i; i++")
103103
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = 0; arrTest.length - 1 >= i; i++")
104104
@TestCase([0, 1, 2, 3], [1, 1, 3, 3], "let i = 0; i < arrTest.length; i += 2")
105-
@TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = arrTest.length - 1; i <= 0; i--")
106-
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i <= 0; i -= 2")
105+
// @TestCase([0, 1, 2, 3], [1, 2, 3, 4], "let i = arrTest.length - 1; i <= 0; i--")
106+
// @TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i <= 0; i -= 2")
107107
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i >= 0; i -= 2")
108108
@TestCase([0, 1, 2, 3], [0, 2, 2, 4], "let i = arrTest.length - 1; i > 0; i -= 2")
109109
@Test("forheader")
@@ -124,42 +124,6 @@ export class LuaLoopTests {
124124
Expect(result).toBe(JSON.stringify(expected));
125125
}
126126

127-
@Test("forstepThrow")
128-
public forstepThrow(inp: number[], expected: number[], header: string) {
129-
// Transpile & Assert
130-
Expect(() => {
131-
let lua = util.transpileString(
132-
`for (let i = 0; i < 30; i = i + 10) {
133-
}`
134-
);
135-
136-
// Execute
137-
let result = util.executeLua(lua);
138-
}).toThrowError(Error, "Unsupported for-loop increment step: BinaryExpression")
139-
}
140-
141-
@TestCase("let i = 0; i + 3; i++")
142-
@TestCase("let i = 0; 3 + i; i++")
143-
@TestCase("let i = 0; i - 3; i++")
144-
@TestCase("let i = 0; i * 3; i++")
145-
@TestCase("let i = 0; i / 3; i++")
146-
@TestCase("let i = 0; i &= 3; i++")
147-
@TestCase("let i = 0; i < 3; !i")
148-
@TestCase("let i = 0; i < 3; i as string")
149-
@Test("forconditionThrow")
150-
public forconditionThrow(header: string) {
151-
// Transpile & Assert
152-
Expect(() => {
153-
let lua = util.transpileString(
154-
`for (${header}) {
155-
}`
156-
);
157-
158-
// Execute
159-
let result = util.executeLua(lua);
160-
}).toThrow();
161-
}
162-
163127
@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })
164128
@Test("forin[Object]")
165129
public forinObject(inp: any, expected: any) {

test/unit/string.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import * as util from "../src/util"
33

44
export class StringTests {
55

6+
@Test("Unsuported string function")
7+
public stringUnsuportedFunction() {
8+
// Assert
9+
Expect(() => {
10+
util.transpileString(
11+
`return "test".testThisIsNoMember()`
12+
);
13+
}).toThrowError(Error, "Unsupported string function: testThisIsNoMember");
14+
}
15+
616
@TestCase([])
717
@TestCase([65])
818
@TestCase([65, 66])

0 commit comments

Comments
 (0)