Skip to content

Commit 4e483db

Browse files
committed
Import Lua AST as lua instead of tstl
1 parent 40e365a commit 4e483db

62 files changed

Lines changed: 1241 additions & 1256 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/LuaPrinter.ts

Lines changed: 168 additions & 168 deletions
Large diffs are not rendered by default.

src/transformation/builtins/array.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import * as tstl from "../../LuaAST";
2+
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { PropertyCallExpression, transformArguments } from "../transformers/call";
55
import { UnsupportedProperty } from "../utils/errors";
@@ -9,7 +9,7 @@ import { isExplicitArrayType } from "../utils/typescript";
99
export function transformArrayCall(
1010
context: TransformationContext,
1111
node: PropertyCallExpression
12-
): tstl.CallExpression | undefined {
12+
): lua.CallExpression | undefined {
1313
const expression = node.expression;
1414
const ownerType = context.checker.getTypeAtLocation(expression.expression);
1515
const signature = context.checker.getResolvedSignature(node);
@@ -31,8 +31,8 @@ export function transformArrayCall(
3131
case "sort":
3232
return transformLuaLibFunction(context, LuaLibFeature.ArraySort, node, caller, ...params);
3333
case "pop":
34-
return tstl.createCallExpression(
35-
tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("remove")),
34+
return lua.createCallExpression(
35+
lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("remove")),
3636
[caller],
3737
node
3838
);
@@ -61,16 +61,16 @@ export function transformArrayCall(
6161
case "splice":
6262
return transformLuaLibFunction(context, LuaLibFeature.ArraySplice, node, caller, ...params);
6363
case "join":
64-
const defaultSeparatorLiteral = tstl.createStringLiteral(",");
64+
const defaultSeparatorLiteral = lua.createStringLiteral(",");
6565
const parameters = [
6666
caller,
6767
node.arguments.length === 0
6868
? defaultSeparatorLiteral
69-
: tstl.createBinaryExpression(params[0], defaultSeparatorLiteral, tstl.SyntaxKind.OrOperator),
69+
: lua.createBinaryExpression(params[0], defaultSeparatorLiteral, lua.SyntaxKind.OrOperator),
7070
];
7171

72-
return tstl.createCallExpression(
73-
tstl.createTableIndexExpression(tstl.createIdentifier("table"), tstl.createStringLiteral("concat")),
72+
return lua.createCallExpression(
73+
lua.createTableIndexExpression(lua.createIdentifier("table"), lua.createStringLiteral("concat")),
7474
parameters,
7575
node
7676
);
@@ -88,14 +88,14 @@ export function transformArrayCall(
8888
export function transformArrayProperty(
8989
context: TransformationContext,
9090
node: ts.PropertyAccessExpression
91-
): tstl.UnaryExpression | undefined {
91+
): lua.UnaryExpression | undefined {
9292
switch (node.name.text) {
9393
case "length":
9494
let expression = context.transformExpression(node.expression);
95-
if (tstl.isTableExpression(expression)) {
96-
expression = tstl.createParenthesizedExpression(expression);
95+
if (lua.isTableExpression(expression)) {
96+
expression = lua.createParenthesizedExpression(expression);
9797
}
98-
return tstl.createUnaryExpression(expression, tstl.SyntaxKind.LengthOperator, node);
98+
return lua.createUnaryExpression(expression, lua.SyntaxKind.LengthOperator, node);
9999
default:
100100
return undefined;
101101
}

src/transformation/builtins/console.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import * as tstl from "../../LuaAST";
2+
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { PropertyCallExpression, transformArguments } from "../transformers/call";
55
import { UnsupportedProperty } from "../utils/errors";
@@ -10,7 +10,7 @@ const isStringFormatTemplate = (expression: ts.Expression) =>
1010
export function transformConsoleCall(
1111
context: TransformationContext,
1212
expression: PropertyCallExpression
13-
): tstl.Expression {
13+
): lua.Expression {
1414
const method = expression.expression;
1515
const methodName = method.name.text;
1616
const signature = context.checker.getResolvedSignature(expression);
@@ -20,56 +20,56 @@ export function transformConsoleCall(
2020
case "log":
2121
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
2222
// print(string.format([arguments]))
23-
const stringFormatCall = tstl.createCallExpression(
24-
tstl.createTableIndexExpression(
25-
tstl.createIdentifier("string"),
26-
tstl.createStringLiteral("format")
23+
const stringFormatCall = lua.createCallExpression(
24+
lua.createTableIndexExpression(
25+
lua.createIdentifier("string"),
26+
lua.createStringLiteral("format")
2727
),
2828
parameters
2929
);
30-
return tstl.createCallExpression(tstl.createIdentifier("print"), [stringFormatCall]);
30+
return lua.createCallExpression(lua.createIdentifier("print"), [stringFormatCall]);
3131
}
3232
// print([arguments])
33-
return tstl.createCallExpression(tstl.createIdentifier("print"), parameters);
33+
return lua.createCallExpression(lua.createIdentifier("print"), parameters);
3434
case "assert":
3535
if (expression.arguments.length > 1 && isStringFormatTemplate(expression.arguments[1])) {
3636
// assert([condition], string.format([arguments]))
37-
const stringFormatCall = tstl.createCallExpression(
38-
tstl.createTableIndexExpression(
39-
tstl.createIdentifier("string"),
40-
tstl.createStringLiteral("format")
37+
const stringFormatCall = lua.createCallExpression(
38+
lua.createTableIndexExpression(
39+
lua.createIdentifier("string"),
40+
lua.createStringLiteral("format")
4141
),
4242
parameters.slice(1)
4343
);
44-
return tstl.createCallExpression(tstl.createIdentifier("assert"), [parameters[0], stringFormatCall]);
44+
return lua.createCallExpression(lua.createIdentifier("assert"), [parameters[0], stringFormatCall]);
4545
}
4646
// assert()
47-
return tstl.createCallExpression(tstl.createIdentifier("assert"), parameters);
47+
return lua.createCallExpression(lua.createIdentifier("assert"), parameters);
4848
case "trace":
4949
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) {
5050
// print(debug.traceback(string.format([arguments])))
51-
const stringFormatCall = tstl.createCallExpression(
52-
tstl.createTableIndexExpression(
53-
tstl.createIdentifier("string"),
54-
tstl.createStringLiteral("format")
51+
const stringFormatCall = lua.createCallExpression(
52+
lua.createTableIndexExpression(
53+
lua.createIdentifier("string"),
54+
lua.createStringLiteral("format")
5555
),
5656
parameters
5757
);
58-
const debugTracebackCall = tstl.createCallExpression(
59-
tstl.createTableIndexExpression(
60-
tstl.createIdentifier("debug"),
61-
tstl.createStringLiteral("traceback")
58+
const debugTracebackCall = lua.createCallExpression(
59+
lua.createTableIndexExpression(
60+
lua.createIdentifier("debug"),
61+
lua.createStringLiteral("traceback")
6262
),
6363
[stringFormatCall]
6464
);
65-
return tstl.createCallExpression(tstl.createIdentifier("print"), [debugTracebackCall]);
65+
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
6666
}
6767
// print(debug.traceback([arguments])))
68-
const debugTracebackCall = tstl.createCallExpression(
69-
tstl.createTableIndexExpression(tstl.createIdentifier("debug"), tstl.createStringLiteral("traceback")),
68+
const debugTracebackCall = lua.createCallExpression(
69+
lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("traceback")),
7070
parameters
7171
);
72-
return tstl.createCallExpression(tstl.createIdentifier("print"), [debugTracebackCall]);
72+
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
7373
default:
7474
throw UnsupportedProperty("console", methodName, expression);
7575
}

src/transformation/builtins/function.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as tstl from "../../LuaAST";
1+
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
33
import { PropertyCallExpression, transformArguments } from "../transformers/call";
44
import { UnsupportedProperty, UnsupportedSelfFunctionConversion } from "../utils/errors";
@@ -8,7 +8,7 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
88
export function transformFunctionCall(
99
context: TransformationContext,
1010
node: PropertyCallExpression
11-
): tstl.CallExpression {
11+
): lua.CallExpression {
1212
const expression = node.expression;
1313
const callerType = context.checker.getTypeAtLocation(expression.expression);
1414
if (getFunctionContextType(context, callerType) === ContextType.Void) {

src/transformation/builtins/global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import * as tstl from "../../LuaAST";
2+
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { transformArguments } from "../transformers/call";
55
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
@@ -8,7 +8,7 @@ import { isNumberType } from "../utils/typescript";
88
export function transformGlobalFunctionCall(
99
context: TransformationContext,
1010
node: ts.CallExpression
11-
): tstl.Expression | undefined {
11+
): lua.Expression | undefined {
1212
const signature = context.checker.getResolvedSignature(node);
1313
const parameters = transformArguments(context, node.arguments, signature);
1414
const expressionType = context.checker.getTypeAtLocation(node.expression);

src/transformation/builtins/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import * as tstl from "../../LuaAST";
2+
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { PropertyCallExpression } from "../transformers/call";
55
import { checkForLuaLibType } from "../transformers/class/new";
@@ -19,7 +19,7 @@ import { transformSymbolConstructorCall } from "./symbol";
1919
export function transformBuiltinPropertyAccessExpression(
2020
context: TransformationContext,
2121
node: ts.PropertyAccessExpression
22-
): tstl.Expression | undefined {
22+
): lua.Expression | undefined {
2323
const type = context.checker.getTypeAtLocation(node.expression);
2424
if (isStringType(context, type)) {
2525
return transformStringProperty(context, node);
@@ -45,7 +45,7 @@ export function transformBuiltinPropertyAccessExpression(
4545
export function transformBuiltinCallExpression(
4646
context: TransformationContext,
4747
node: ts.CallExpression
48-
): tstl.Expression | undefined {
48+
): lua.Expression | undefined {
4949
const expressionType = context.checker.getTypeAtLocation(node.expression);
5050
if (ts.isIdentifier(node.expression) && isStandardLibraryType(context, expressionType, undefined)) {
5151
// TODO:
@@ -109,24 +109,24 @@ export function transformBuiltinCallExpression(
109109
export function transformBuiltinIdentifierExpression(
110110
context: TransformationContext,
111111
node: ts.Identifier
112-
): tstl.Expression | undefined {
112+
): lua.Expression | undefined {
113113
switch (node.text) {
114114
case "NaN":
115-
return tstl.createParenthesizedExpression(
116-
tstl.createBinaryExpression(
117-
tstl.createNumericLiteral(0),
118-
tstl.createNumericLiteral(0),
119-
tstl.SyntaxKind.DivisionOperator,
115+
return lua.createParenthesizedExpression(
116+
lua.createBinaryExpression(
117+
lua.createNumericLiteral(0),
118+
lua.createNumericLiteral(0),
119+
lua.SyntaxKind.DivisionOperator,
120120
node
121121
)
122122
);
123123

124124
case "Infinity":
125-
const math = tstl.createIdentifier("math");
126-
const huge = tstl.createStringLiteral("huge");
127-
return tstl.createTableIndexExpression(math, huge, node);
125+
const math = lua.createIdentifier("math");
126+
const huge = lua.createStringLiteral("huge");
127+
return lua.createTableIndexExpression(math, huge, node);
128128

129129
case "globalThis":
130-
return tstl.createIdentifier("_G", node, getIdentifierSymbolId(context, node), "globalThis");
130+
return lua.createIdentifier("_G", node, getIdentifierSymbolId(context, node), "globalThis");
131131
}
132132
}

src/transformation/builtins/math.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as ts from "typescript";
2-
import * as tstl from "../../LuaAST";
2+
import * as lua from "../../LuaAST";
33
import { TransformationContext } from "../context";
44
import { PropertyCallExpression, transformArguments } from "../transformers/call";
55
import { UnsupportedProperty } from "../utils/errors";
66

7-
export function transformMathProperty(node: ts.PropertyAccessExpression): tstl.Expression {
7+
export function transformMathProperty(node: ts.PropertyAccessExpression): lua.Expression {
88
const name = node.name.text;
99
switch (name) {
1010
case "PI":
11-
const property = tstl.createStringLiteral("pi", node.name);
12-
const math = tstl.createIdentifier("math", node.expression);
13-
return tstl.createTableIndexExpression(math, property, node);
11+
const property = lua.createStringLiteral("pi", node.name);
12+
const math = lua.createIdentifier("math", node.expression);
13+
return lua.createTableIndexExpression(math, property, node);
1414

1515
case "E":
1616
case "LN10":
@@ -19,14 +19,14 @@ export function transformMathProperty(node: ts.PropertyAccessExpression): tstl.E
1919
case "LOG2E":
2020
case "SQRT1_2":
2121
case "SQRT2":
22-
return tstl.createNumericLiteral(Math[name], node);
22+
return lua.createNumericLiteral(Math[name], node);
2323

2424
default:
2525
throw UnsupportedProperty("Math", name, node);
2626
}
2727
}
2828

29-
export function transformMathCall(context: TransformationContext, node: PropertyCallExpression): tstl.Expression {
29+
export function transformMathCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression {
3030
const expression = node.expression;
3131
const signature = context.checker.getResolvedSignature(node);
3232
const params = transformArguments(context, node.arguments, signature);
@@ -35,39 +35,39 @@ export function transformMathCall(context: TransformationContext, node: Property
3535
switch (expressionName) {
3636
// math.tan(x / y)
3737
case "atan2": {
38-
const math = tstl.createIdentifier("math");
39-
const atan = tstl.createStringLiteral("atan");
40-
const div = tstl.createBinaryExpression(params[0], params[1], tstl.SyntaxKind.DivisionOperator);
41-
return tstl.createCallExpression(tstl.createTableIndexExpression(math, atan), [div], node);
38+
const math = lua.createIdentifier("math");
39+
const atan = lua.createStringLiteral("atan");
40+
const div = lua.createBinaryExpression(params[0], params[1], lua.SyntaxKind.DivisionOperator);
41+
return lua.createCallExpression(lua.createTableIndexExpression(math, atan), [div], node);
4242
}
4343

4444
// (math.log(x) / Math.LNe)
4545
case "log10":
4646
case "log2": {
47-
const math = tstl.createIdentifier("math");
48-
const log1 = tstl.createTableIndexExpression(math, tstl.createStringLiteral("log"));
49-
const logCall1 = tstl.createCallExpression(log1, params);
50-
const e = tstl.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
51-
const div = tstl.createBinaryExpression(logCall1, e, tstl.SyntaxKind.DivisionOperator);
52-
return tstl.createParenthesizedExpression(div, node);
47+
const math = lua.createIdentifier("math");
48+
const log1 = lua.createTableIndexExpression(math, lua.createStringLiteral("log"));
49+
const logCall1 = lua.createCallExpression(log1, params);
50+
const e = lua.createNumericLiteral(expressionName === "log10" ? Math.LN10 : Math.LN2);
51+
const div = lua.createBinaryExpression(logCall1, e, lua.SyntaxKind.DivisionOperator);
52+
return lua.createParenthesizedExpression(div, node);
5353
}
5454

5555
// math.log(1 + x)
5656
case "log1p": {
57-
const math = tstl.createIdentifier("math");
58-
const log = tstl.createStringLiteral("log");
59-
const one = tstl.createNumericLiteral(1);
60-
const add = tstl.createBinaryExpression(one, params[0], tstl.SyntaxKind.AdditionOperator);
61-
return tstl.createCallExpression(tstl.createTableIndexExpression(math, log), [add], node);
57+
const math = lua.createIdentifier("math");
58+
const log = lua.createStringLiteral("log");
59+
const one = lua.createNumericLiteral(1);
60+
const add = lua.createBinaryExpression(one, params[0], lua.SyntaxKind.AdditionOperator);
61+
return lua.createCallExpression(lua.createTableIndexExpression(math, log), [add], node);
6262
}
6363

6464
// math.floor(x + 0.5)
6565
case "round": {
66-
const math = tstl.createIdentifier("math");
67-
const floor = tstl.createStringLiteral("floor");
68-
const half = tstl.createNumericLiteral(0.5);
69-
const add = tstl.createBinaryExpression(params[0], half, tstl.SyntaxKind.AdditionOperator);
70-
return tstl.createCallExpression(tstl.createTableIndexExpression(math, floor), [add], node);
66+
const math = lua.createIdentifier("math");
67+
const floor = lua.createStringLiteral("floor");
68+
const half = lua.createNumericLiteral(0.5);
69+
const add = lua.createBinaryExpression(params[0], half, lua.SyntaxKind.AdditionOperator);
70+
return lua.createCallExpression(lua.createTableIndexExpression(math, floor), [add], node);
7171
}
7272

7373
case "abs":
@@ -86,9 +86,9 @@ export function transformMathCall(context: TransformationContext, node: Property
8686
case "sin":
8787
case "sqrt":
8888
case "tan": {
89-
const math = tstl.createIdentifier("math");
90-
const method = tstl.createStringLiteral(expressionName);
91-
return tstl.createCallExpression(tstl.createTableIndexExpression(math, method), params, node);
89+
const math = lua.createIdentifier("math");
90+
const method = lua.createStringLiteral(expressionName);
91+
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
9292
}
9393

9494
default:

src/transformation/builtins/number.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as tstl from "../../LuaAST";
1+
import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
33
import { PropertyCallExpression, transformArguments } from "../transformers/call";
44
import { UnsupportedProperty } from "../utils/errors";
@@ -8,7 +8,7 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
88
export function transformNumberConstructorCall(
99
context: TransformationContext,
1010
expression: PropertyCallExpression
11-
): tstl.CallExpression {
11+
): lua.CallExpression {
1212
const method = expression.expression;
1313
const parameters = transformArguments(context, expression.arguments);
1414
const methodName = method.name.text;

0 commit comments

Comments
 (0)