cluaupp 0.2.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -79
- package/README.md +42 -88
- package/docs/README.md +13 -21
- package/docs/architecture.md +13 -76
- package/docs/cli.md +12 -64
- package/docs/comparison.md +6 -24
- package/docs/config.md +6 -7
- package/docs/examples/combat.md +19 -227
- package/docs/examples/hud.md +13 -81
- package/docs/examples/index.md +9 -16
- package/docs/getting-started.md +20 -137
- package/docs/intellisense.md +5 -41
- package/docs/intro.md +16 -15
- package/docs/migration.md +38 -0
- package/docs/oop/file-tags.md +12 -36
- package/docs/oop/index.md +8 -14
- package/docs/syntax.md +54 -236
- package/editors/vscode/extension.js +1 -1
- package/editors/vscode/package.json +4 -4
- package/examples/README.md +5 -7
- package/examples/game/.vscode/extensions.json +0 -3
- package/examples/game/.vscode/settings.json +6 -14
- package/examples/game/src/client/hud.client.clpp +54 -0
- package/examples/game/src/client/init.client.clpp +6 -0
- package/examples/game/src/server/combat.server.clpp +85 -0
- package/examples/game/src/server/leaderstats.server.clpp +28 -0
- package/examples/game/src/shared/PlayerData.clh +20 -0
- package/examples/game/src/shared/config.clp +6 -0
- package/examples/game/src/shared/features.clp +88 -0
- package/generated/cli.js +40 -24
- package/generated/clpp/contract.js +5 -0
- package/generated/clpp/index.js +27 -0
- package/generated/clpp/paths.js +121 -0
- package/generated/clpp/postprocess.js +78 -0
- package/generated/clpp/runner.js +102 -0
- package/generated/compile.js +26 -138
- package/generated/intellisense.js +38 -157
- package/generated/libs.js +1 -1
- package/generated/lsp.js +1 -8
- package/generated/preprocess.js +8 -179
- package/generated/system-understander.js +1 -1
- package/generated/transpile.js +3 -50
- package/generated/utils/project.js +13 -28
- package/include/{cluaupp/datatypes.hpp → clpp/datatypes.clh} +1 -1
- package/include/{cluaupp/generated/instances.hpp → clpp/generated/instances.clh} +1 -1
- package/include/{cluaupp/libs/chrono.hpp → clpp/libs/chrono.clh} +9 -9
- package/include/{cluaupp/libs/cmdr.hpp → clpp/libs/cmdr.clh} +4 -4
- package/include/{cluaupp/libs/dataservice.hpp → clpp/libs/dataservice.clh} +8 -15
- package/include/{cluaupp/libs/display.hpp → clpp/libs/display.clh} +1 -2
- package/include/{cluaupp/libs/ezvisual.hpp → clpp/libs/ezvisual.clh} +1 -3
- package/include/{cluaupp/libs/formatnumber.hpp → clpp/libs/formatnumber.clh} +2 -3
- package/include/{cluaupp/libs/fusion.hpp → clpp/libs/fusion.clh} +5 -5
- package/include/{cluaupp/libs/iris.hpp → clpp/libs/iris.clh} +2 -2
- package/include/{cluaupp/libs/janitor.hpp → clpp/libs/janitor.clh} +1 -2
- package/include/{cluaupp/libs/module3d.hpp → clpp/libs/module3d.clh} +2 -4
- package/include/{cluaupp/libs/net.hpp → clpp/libs/net.clh} +2 -4
- package/include/{cluaupp/libs/promise.hpp → clpp/libs/promise.clh} +1 -4
- package/include/{cluaupp/libs/spring.hpp → clpp/libs/spring.clh} +1 -2
- package/include/{cluaupp/libs/statemachine.hpp → clpp/libs/statemachine.clh} +2 -3
- package/include/{cluaupp/libs/stickybillboard.hpp → clpp/libs/stickybillboard.clh} +1 -2
- package/include/{cluaupp/libs/topbarplus.hpp → clpp/libs/topbarplus.clh} +3 -4
- package/include/clpp/libs.clh +24 -0
- package/include/clpp/roblox.clh +31 -0
- package/package.json +7 -14
- package/src/cli.ts +39 -25
- package/src/clpp/contract.ts +39 -0
- package/src/clpp/index.ts +18 -0
- package/src/clpp/paths.ts +117 -0
- package/src/clpp/postprocess.ts +93 -0
- package/src/clpp/runner.ts +102 -0
- package/src/compile.ts +26 -149
- package/src/intellisense.ts +36 -165
- package/src/libs.ts +1 -1
- package/src/lsp.ts +1 -5
- package/src/preprocess.ts +8 -180
- package/src/system-understander.ts +1 -1
- package/src/transpile.ts +9 -52
- package/src/utils/project.ts +12 -28
- package/templates/game/.vscode/extensions.json +0 -3
- package/templates/game/.vscode/settings.json +6 -14
- package/templates/game/src/client/init.client.clpp +6 -0
- package/templates/game/src/server/leaderstats.server.clpp +29 -0
- package/templates/game/src/shared/config.clp +6 -0
- package/examples/game/.clangd +0 -25
- package/examples/game/compile_flags.txt +0 -6
- package/examples/game/src/client/hud.client.cpp +0 -12
- package/examples/game/src/client/init.client.cpp +0 -6
- package/examples/game/src/server/combat.server.cpp +0 -23
- package/examples/game/src/server/leaderstats.server.cpp +0 -29
- package/examples/game/src/shared/config.cpp +0 -5
- package/examples/game/src/shared/config.h +0 -5
- package/include/cluaupp/libs.hpp +0 -25
- package/include/cluaupp/roblox.hpp +0 -47
- package/src/architecture.ts +0 -500
- package/src/ast.ts +0 -37
- package/src/emit.ts +0 -609
- package/src/emitter/luau-codegen.ts +0 -187
- package/src/emitter/translators.ts +0 -168
- package/src/headers.ts +0 -233
- package/src/layout.ts +0 -114
- package/src/lex.ts +0 -158
- package/src/parse.ts +0 -729
- package/src/parser/collector.ts +0 -148
- package/src/parser/index.ts +0 -27
- package/src/tree-sitter-cpp.d.ts +0 -4
- package/src/understand.ts +0 -322
- package/templates/game/src/client/init.client.cpp +0 -6
- package/templates/game/src/server/leaderstats.server.cpp +0 -29
- package/templates/game/src/shared/config.cpp +0 -5
- package/templates/game/src/shared/config.h +0 -5
- /package/include/{cluaupp/generated/enums.hpp → clpp/generated/enums.clh} +0 -0
- /package/include/{cluaupp/libs/math.hpp → clpp/libs/math.clh} +0 -0
- /package/include/{cluaupp/libs/twinkle.hpp → clpp/libs/twinkle.clh} +0 -0
- /package/include/{cluaupp/libs/vfx.hpp → clpp/libs/vfx.clh} +0 -0
package/src/emit.ts
DELETED
|
@@ -1,609 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { isInstanceType, isMethod, isDatatype, luauType } from "./api.js";
|
|
3
|
-
import { isLibraryType, isLibraryMethod, MODULE_COLON } from "./libs.js";
|
|
4
|
-
|
|
5
|
-
function emit(ast, options = {}) {
|
|
6
|
-
const strict = options.strict === true;
|
|
7
|
-
const lines = [];
|
|
8
|
-
if (!options.skipHeader) {
|
|
9
|
-
if (strict) {
|
|
10
|
-
lines.push("--!strict");
|
|
11
|
-
}
|
|
12
|
-
lines.push("-- Compiled by Cluaupp — C++ × Luau");
|
|
13
|
-
lines.push("");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const indentOf = (n) => "\t".repeat(n);
|
|
17
|
-
let switchId = 0;
|
|
18
|
-
|
|
19
|
-
const classOwners = new Set();
|
|
20
|
-
const classFields = new Set();
|
|
21
|
-
const classMethods = new Set();
|
|
22
|
-
for (const decl of ast.body || []) {
|
|
23
|
-
if (decl.owner) {
|
|
24
|
-
classOwners.add(decl.owner);
|
|
25
|
-
}
|
|
26
|
-
if (decl.type === "decl" && decl.owner) {
|
|
27
|
-
classFields.add(decl.name);
|
|
28
|
-
}
|
|
29
|
-
if ((decl.type === "function" || decl.type === "proto") && decl.owner && decl.name) {
|
|
30
|
-
classMethods.add(decl.name);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
const ownedFns = (ast.body || []).filter((decl) => decl.type === "function");
|
|
34
|
-
const hasOwnedMethods = ownedFns.some((decl) => Boolean(decl.owner));
|
|
35
|
-
const classModule = ownedFns.length > 0 && ownedFns.every((decl) => Boolean(decl.owner));
|
|
36
|
-
const nestedTypes = new Set();
|
|
37
|
-
for (const decl of ast.body || []) {
|
|
38
|
-
if (decl.type === "decl" && decl.owner && decl.valueType && classOwners.has(decl.valueType) && decl.valueType !== decl.owner) {
|
|
39
|
-
nestedTypes.add(decl.valueType);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const structRoots = [...classOwners].filter((name) => !nestedTypes.has(name));
|
|
43
|
-
const structModule = !classModule && ownedFns.length === 0 && structRoots.length > 0;
|
|
44
|
-
|
|
45
|
-
const localNames = new Set();
|
|
46
|
-
let selfOwner = null;
|
|
47
|
-
|
|
48
|
-
const isBareGlobal = (name) => {
|
|
49
|
-
return (
|
|
50
|
-
name === "print" ||
|
|
51
|
-
name === "warn" ||
|
|
52
|
-
name === "error" ||
|
|
53
|
-
name === "game" ||
|
|
54
|
-
name === "workspace" ||
|
|
55
|
-
name === "script" ||
|
|
56
|
-
name === "cout" ||
|
|
57
|
-
name === "cerr" ||
|
|
58
|
-
name === "endl" ||
|
|
59
|
-
isLibraryType(name) ||
|
|
60
|
-
isInstanceType(name) ||
|
|
61
|
-
isDatatype(name)
|
|
62
|
-
);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const emitSelfIdent = (name) => {
|
|
66
|
-
if (name === "this") {
|
|
67
|
-
return "self";
|
|
68
|
-
}
|
|
69
|
-
if (!selfOwner || localNames.has(name) || isBareGlobal(name)) {
|
|
70
|
-
return name;
|
|
71
|
-
}
|
|
72
|
-
if (classFields.has(name)) {
|
|
73
|
-
return `self.${name}`;
|
|
74
|
-
}
|
|
75
|
-
return name;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const emitMethodArg = (arg) => {
|
|
79
|
-
if (arg && arg.type === "ident" && selfOwner && classMethods.has(arg.name) && !localNames.has(arg.name)) {
|
|
80
|
-
return `function(...) self:${arg.name}(...) end`;
|
|
81
|
-
}
|
|
82
|
-
return emitExpr(arg);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const flattenShift = (node) => {
|
|
86
|
-
const parts = [];
|
|
87
|
-
let current = node;
|
|
88
|
-
while (current && current.type === "binary" && current.op === "<<") {
|
|
89
|
-
parts.unshift(current.right);
|
|
90
|
-
current = current.left;
|
|
91
|
-
}
|
|
92
|
-
return { stream: current, args: parts };
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const isEndl = (node) => node && node.type === "ident" && node.name === "endl";
|
|
96
|
-
|
|
97
|
-
const streamPrint = (stream) => {
|
|
98
|
-
if (!stream) {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
if (stream.type === "ident") {
|
|
102
|
-
if (stream.name === "cout") {
|
|
103
|
-
return "print";
|
|
104
|
-
}
|
|
105
|
-
if (stream.name === "cerr") {
|
|
106
|
-
return "warn";
|
|
107
|
-
}
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
if (stream.type === "member" && stream.object && stream.object.type === "ident" && stream.object.name === "cout") {
|
|
111
|
-
const mapped = { print: "print", warn: "warn", error: "error", ping: "print", endl: "print" };
|
|
112
|
-
return mapped[stream.name] || "print";
|
|
113
|
-
}
|
|
114
|
-
return null;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
const emitCout = (node) => {
|
|
118
|
-
const { stream, args } = flattenShift(node);
|
|
119
|
-
const fn = streamPrint(stream);
|
|
120
|
-
if (!fn) {
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
const linesOut = [];
|
|
124
|
-
let current = [];
|
|
125
|
-
const flush = () => {
|
|
126
|
-
if (current.length === 0) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
linesOut.push(`${fn}(${current.map(emitExpr).join(", ")})`);
|
|
130
|
-
current = [];
|
|
131
|
-
};
|
|
132
|
-
for (const arg of args) {
|
|
133
|
-
if (isEndl(arg)) {
|
|
134
|
-
flush();
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
current.push(arg);
|
|
138
|
-
}
|
|
139
|
-
flush();
|
|
140
|
-
if (linesOut.length === 0) {
|
|
141
|
-
linesOut.push(`${fn}()`);
|
|
142
|
-
}
|
|
143
|
-
return linesOut;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const isStringConcatCall = (node) => node && node.type === "call" && !node.object && node.name === "string_concat";
|
|
147
|
-
|
|
148
|
-
const emitStringConcat = (args) => {
|
|
149
|
-
const parts = (args || []).map(emitExpr);
|
|
150
|
-
if (parts.length === 0) {
|
|
151
|
-
return `""`;
|
|
152
|
-
}
|
|
153
|
-
if (parts.length === 1) {
|
|
154
|
-
return parts[0];
|
|
155
|
-
}
|
|
156
|
-
return `(${parts.join(" .. ")})`;
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
const isStringExpr = (node) => {
|
|
160
|
-
if (!node) {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
if (node.type === "string") {
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
if (isStringConcatCall(node)) {
|
|
167
|
-
return true;
|
|
168
|
-
}
|
|
169
|
-
if (node.type === "binary" && node.op === "+" && (isStringExpr(node.left) || isStringExpr(node.right))) {
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
if (node.type === "member" && (node.name === "Name" || node.name === "Text" || node.name === "DisplayName")) {
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
return false;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const emitExpr = (node) => {
|
|
179
|
-
if (!node) {
|
|
180
|
-
return "nil";
|
|
181
|
-
}
|
|
182
|
-
switch (node.type) {
|
|
183
|
-
case "null":
|
|
184
|
-
return "nil";
|
|
185
|
-
case "bool":
|
|
186
|
-
return node.value ? "true" : "false";
|
|
187
|
-
case "number":
|
|
188
|
-
return node.value;
|
|
189
|
-
case "string":
|
|
190
|
-
return `"${node.value}"`;
|
|
191
|
-
case "ident":
|
|
192
|
-
return emitSelfIdent(node.name);
|
|
193
|
-
case "initlist": {
|
|
194
|
-
const entries = (node.fields || []).map((field) => `${field.name} = ${emitExpr(field.value)}`);
|
|
195
|
-
if (entries.length === 0) {
|
|
196
|
-
return "{}";
|
|
197
|
-
}
|
|
198
|
-
return `{ ${entries.join(", ")} }`;
|
|
199
|
-
}
|
|
200
|
-
case "cast":
|
|
201
|
-
if (node.valueType === "void") {
|
|
202
|
-
return emitExpr(node.argument);
|
|
203
|
-
}
|
|
204
|
-
return emitExpr(node.argument);
|
|
205
|
-
case "lambda": {
|
|
206
|
-
const params = (node.params || [])
|
|
207
|
-
.map((param) => {
|
|
208
|
-
if (typeof param === "string") {
|
|
209
|
-
return param;
|
|
210
|
-
}
|
|
211
|
-
const typeAnn = luauType(param.valueType);
|
|
212
|
-
return typeAnn ? `${param.name}: ${typeAnn}` : param.name;
|
|
213
|
-
})
|
|
214
|
-
.join(", ");
|
|
215
|
-
const inner = [];
|
|
216
|
-
for (const stmt of node.body || []) {
|
|
217
|
-
inner.push(...emitStmt(stmt, 1));
|
|
218
|
-
}
|
|
219
|
-
const body = inner.length > 0 ? `\n${inner.join("\n")}\n` : "\n";
|
|
220
|
-
return `function(${params})${body}end`;
|
|
221
|
-
}
|
|
222
|
-
case "unary": {
|
|
223
|
-
const inner = emitExpr(node.argument);
|
|
224
|
-
if (node.op === "!") {
|
|
225
|
-
return `not ${inner}`;
|
|
226
|
-
}
|
|
227
|
-
return `-${inner}`;
|
|
228
|
-
}
|
|
229
|
-
case "getService":
|
|
230
|
-
return `game:GetService("${node.service}")`;
|
|
231
|
-
case "new": {
|
|
232
|
-
if (isLibraryType(node.className) || !isInstanceType(node.className)) {
|
|
233
|
-
const args = node.args.map(emitExpr).join(", ");
|
|
234
|
-
return `${node.className}.new(${args})`;
|
|
235
|
-
}
|
|
236
|
-
return `Instance.new("${node.className}")`;
|
|
237
|
-
}
|
|
238
|
-
case "member":
|
|
239
|
-
return `${emitExpr(node.object)}.${node.name}`;
|
|
240
|
-
case "call": {
|
|
241
|
-
if (isStringConcatCall(node)) {
|
|
242
|
-
return emitStringConcat(node.args);
|
|
243
|
-
}
|
|
244
|
-
const args = node.args.map(emitMethodArg).join(", ");
|
|
245
|
-
if (!node.object) {
|
|
246
|
-
if (isDatatype(node.name)) {
|
|
247
|
-
return `${node.name}.new(${args})`;
|
|
248
|
-
}
|
|
249
|
-
if (selfOwner && classMethods.has(node.name) && !localNames.has(node.name)) {
|
|
250
|
-
return `self:${node.name}(${args})`;
|
|
251
|
-
}
|
|
252
|
-
return `${node.name}(${args})`;
|
|
253
|
-
}
|
|
254
|
-
const obj = emitExpr(node.object);
|
|
255
|
-
if (classMethods.has(node.name) && !localNames.has(node.name)) {
|
|
256
|
-
return `${obj}:${node.name}(${args})`;
|
|
257
|
-
}
|
|
258
|
-
if (node.access === "::" && node.object.type === "ident" && node.object.name === "cout") {
|
|
259
|
-
if (node.name === "endl") {
|
|
260
|
-
return "print()";
|
|
261
|
-
}
|
|
262
|
-
const mapped = { print: "print", warn: "warn", error: "error", ping: "print" };
|
|
263
|
-
return `${mapped[node.name] || "print"}(${args})`;
|
|
264
|
-
}
|
|
265
|
-
if (node.access === "::") {
|
|
266
|
-
if (MODULE_COLON.has(node.name)) {
|
|
267
|
-
return `${obj}:${node.name}(${args})`;
|
|
268
|
-
}
|
|
269
|
-
const staticNs =
|
|
270
|
-
node.object.type === "ident" &&
|
|
271
|
-
(isLibraryType(node.object.name) ||
|
|
272
|
-
isDatatype(node.object.name) ||
|
|
273
|
-
node.object.name === "DataService" ||
|
|
274
|
-
node.object.name === "FormatNumber" ||
|
|
275
|
-
node.object.name === "Enum");
|
|
276
|
-
if (staticNs) {
|
|
277
|
-
return `${obj}.${node.name}(${args})`;
|
|
278
|
-
}
|
|
279
|
-
return `${obj}:${node.name}(${args})`;
|
|
280
|
-
}
|
|
281
|
-
if (isDatatype(obj) || (node.object.type === "ident" && isDatatype(node.object.name))) {
|
|
282
|
-
return `${obj}.${node.name}(${args})`;
|
|
283
|
-
}
|
|
284
|
-
const colon = isMethod(node.name) || isLibraryMethod(node.name);
|
|
285
|
-
return `${obj}${colon ? ":" : "."}${node.name}(${args})`;
|
|
286
|
-
}
|
|
287
|
-
case "assign":
|
|
288
|
-
return `${emitExpr(node.left)} = ${emitExpr(node.right)}`;
|
|
289
|
-
case "binary": {
|
|
290
|
-
if (node.op === "<<") {
|
|
291
|
-
const printed = emitCout(node);
|
|
292
|
-
if (printed) {
|
|
293
|
-
return printed.join("; ");
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
if (node.op === "+" && (isStringExpr(node.left) || isStringExpr(node.right))) {
|
|
297
|
-
return `${emitExpr(node.left)} .. ${emitExpr(node.right)}`;
|
|
298
|
-
}
|
|
299
|
-
const ops = { "!=": "~=", "&&": "and", "||": "or" };
|
|
300
|
-
const op = ops[node.op] || node.op;
|
|
301
|
-
return `${emitExpr(node.left)} ${op} ${emitExpr(node.right)}`;
|
|
302
|
-
}
|
|
303
|
-
default:
|
|
304
|
-
return "nil";
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
const inferredType = (node) => {
|
|
309
|
-
const created = node.value;
|
|
310
|
-
if (created && created.type === "new" && (isInstanceType(created.className) || isLibraryType(created.className) || isDatatype(created.className))) {
|
|
311
|
-
return created.className;
|
|
312
|
-
}
|
|
313
|
-
if (created && created.type === "call" && !created.object && isDatatype(created.name)) {
|
|
314
|
-
return created.name;
|
|
315
|
-
}
|
|
316
|
-
if (created && created.type === "getService") {
|
|
317
|
-
return created.service;
|
|
318
|
-
}
|
|
319
|
-
const fromCpp = luauType(node.valueType);
|
|
320
|
-
if (fromCpp && fromCpp !== "auto") {
|
|
321
|
-
return fromCpp;
|
|
322
|
-
}
|
|
323
|
-
return null;
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
const emitNewDecl = (node, indent) => {
|
|
327
|
-
const prefix = indentOf(indent);
|
|
328
|
-
const typeAnn = inferredType(node);
|
|
329
|
-
const created = node.value;
|
|
330
|
-
if (node.owner && indent === 0 && classModule) {
|
|
331
|
-
const value = node.value ? emitExpr(node.value) : "nil";
|
|
332
|
-
if (created && created.type === "new" && isInstanceType(created.className) && !isLibraryType(created.className)) {
|
|
333
|
-
const linesOut = [`${prefix}${node.owner}.${node.name} = Instance.new("${created.className}")`];
|
|
334
|
-
if (created.args[0]) {
|
|
335
|
-
linesOut.push(`${prefix}${node.owner}.${node.name}.Parent = ${emitExpr(created.args[0])}`);
|
|
336
|
-
}
|
|
337
|
-
return linesOut;
|
|
338
|
-
}
|
|
339
|
-
if (created && created.type === "new" && (isDatatype(created.className) || isLibraryType(created.className))) {
|
|
340
|
-
const args = created.args.map(emitExpr).join(", ");
|
|
341
|
-
return [`${prefix}${node.owner}.${node.name} = ${created.className}.new(${args})`];
|
|
342
|
-
}
|
|
343
|
-
return [`${prefix}${node.owner}.${node.name} = ${value}`];
|
|
344
|
-
}
|
|
345
|
-
const kind = node.isConst ? "const" : "local";
|
|
346
|
-
const typed = typeAnn ? `: ${typeAnn}` : "";
|
|
347
|
-
if (created && created.type === "new" && isInstanceType(created.className) && !isLibraryType(created.className)) {
|
|
348
|
-
const linesOut = [`${prefix}${kind} ${node.name}${typed} = Instance.new("${created.className}")`];
|
|
349
|
-
if (created.args[0]) {
|
|
350
|
-
linesOut.push(`${prefix}${node.name}.Parent = ${emitExpr(created.args[0])}`);
|
|
351
|
-
}
|
|
352
|
-
return linesOut;
|
|
353
|
-
}
|
|
354
|
-
if (created && created.type === "new" && (isDatatype(created.className) || isLibraryType(created.className))) {
|
|
355
|
-
const args = created.args.map(emitExpr).join(", ");
|
|
356
|
-
return [`${prefix}${kind} ${node.name}${typed} = ${created.className}.new(${args})`];
|
|
357
|
-
}
|
|
358
|
-
const value = node.value ? emitExpr(node.value) : "nil";
|
|
359
|
-
if (!node.value && node.valueType && classOwners.has(node.valueType)) {
|
|
360
|
-
return [`${prefix}${kind} ${node.name}${typed} = ${node.valueType}`];
|
|
361
|
-
}
|
|
362
|
-
return [`${prefix}${kind} ${node.name}${typed} = ${value}`];
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
const emitStmt = (node, indent) => {
|
|
366
|
-
const prefix = indentOf(indent);
|
|
367
|
-
switch (node.type) {
|
|
368
|
-
case "decl":
|
|
369
|
-
if (node.name) {
|
|
370
|
-
localNames.add(node.name);
|
|
371
|
-
}
|
|
372
|
-
return emitNewDecl(node, indent);
|
|
373
|
-
case "expr": {
|
|
374
|
-
const expr = node.expr;
|
|
375
|
-
if (expr && expr.type === "cast" && expr.valueType === "void") {
|
|
376
|
-
return [];
|
|
377
|
-
}
|
|
378
|
-
if (expr && expr.type === "binary" && expr.op === "<<") {
|
|
379
|
-
const printed = emitCout(expr);
|
|
380
|
-
if (printed) {
|
|
381
|
-
return printed.map((line) => `${prefix}${line}`);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
if (
|
|
385
|
-
expr &&
|
|
386
|
-
expr.type === "assign" &&
|
|
387
|
-
expr.right &&
|
|
388
|
-
expr.right.type === "new" &&
|
|
389
|
-
isInstanceType(expr.right.className) &&
|
|
390
|
-
!isLibraryType(expr.right.className)
|
|
391
|
-
) {
|
|
392
|
-
const left = emitExpr(expr.left);
|
|
393
|
-
const linesOut = [`${prefix}${left} = Instance.new("${expr.right.className}")`];
|
|
394
|
-
if (expr.right.args[0]) {
|
|
395
|
-
linesOut.push(`${prefix}${left}.Parent = ${emitExpr(expr.right.args[0])}`);
|
|
396
|
-
}
|
|
397
|
-
return linesOut;
|
|
398
|
-
}
|
|
399
|
-
return [`${prefix}${emitExpr(expr)}`];
|
|
400
|
-
}
|
|
401
|
-
case "return":
|
|
402
|
-
return node.value ? [`${prefix}return ${emitExpr(node.value)}`] : [`${prefix}return`];
|
|
403
|
-
case "if": {
|
|
404
|
-
const out = [`${prefix}if ${emitExpr(node.test)} then`];
|
|
405
|
-
for (const stmt of node.consequent) {
|
|
406
|
-
out.push(...emitStmt(stmt, indent + 1));
|
|
407
|
-
}
|
|
408
|
-
if (node.alternate) {
|
|
409
|
-
out.push(`${prefix}else`);
|
|
410
|
-
for (const stmt of node.alternate) {
|
|
411
|
-
out.push(...emitStmt(stmt, indent + 1));
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
out.push(`${prefix}end`);
|
|
415
|
-
return out;
|
|
416
|
-
}
|
|
417
|
-
case "while": {
|
|
418
|
-
const out = [`${prefix}while ${emitExpr(node.test)} do`];
|
|
419
|
-
for (const stmt of node.body) {
|
|
420
|
-
out.push(...emitStmt(stmt, indent + 1));
|
|
421
|
-
}
|
|
422
|
-
out.push(`${prefix}end`);
|
|
423
|
-
return out;
|
|
424
|
-
}
|
|
425
|
-
case "foreach": {
|
|
426
|
-
const out = [`${prefix}for _, ${node.name} in ${emitExpr(node.iter)} do`];
|
|
427
|
-
for (const stmt of node.body) {
|
|
428
|
-
out.push(...emitStmt(stmt, indent + 1));
|
|
429
|
-
}
|
|
430
|
-
out.push(`${prefix}end`);
|
|
431
|
-
return out;
|
|
432
|
-
}
|
|
433
|
-
case "break":
|
|
434
|
-
return [`${prefix}break`];
|
|
435
|
-
case "switch": {
|
|
436
|
-
switchId += 1;
|
|
437
|
-
const id = `__switch${switchId}`;
|
|
438
|
-
const disc = node.discriminant;
|
|
439
|
-
const simple =
|
|
440
|
-
disc &&
|
|
441
|
-
(disc.type === "ident" || disc.type === "number" || disc.type === "string" || disc.type === "bool");
|
|
442
|
-
const subject = simple ? emitExpr(disc) : id;
|
|
443
|
-
const out = [`${prefix}repeat`];
|
|
444
|
-
const inner = indent + 1;
|
|
445
|
-
const innerP = indentOf(inner);
|
|
446
|
-
if (!simple) {
|
|
447
|
-
out.push(`${innerP}local ${id} = ${emitExpr(disc)}`);
|
|
448
|
-
}
|
|
449
|
-
const regular = (node.cases || []).filter((item) => !item.isDefault);
|
|
450
|
-
const fallback = (node.cases || []).find((item) => item.isDefault);
|
|
451
|
-
const testOf = (item) =>
|
|
452
|
-
(item.values || []).map((value) => `${subject} == ${emitExpr(value)}`).join(" or ");
|
|
453
|
-
for (let index = 0; index < regular.length; index += 1) {
|
|
454
|
-
const item = regular[index];
|
|
455
|
-
const keyword = index === 0 ? "if" : "elseif";
|
|
456
|
-
out.push(`${innerP}${keyword} ${testOf(item)} then`);
|
|
457
|
-
for (const stmt of item.body || []) {
|
|
458
|
-
out.push(...emitStmt(stmt, inner + 1));
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
if (fallback) {
|
|
462
|
-
if (regular.length === 0) {
|
|
463
|
-
for (const stmt of fallback.body || []) {
|
|
464
|
-
out.push(...emitStmt(stmt, inner));
|
|
465
|
-
}
|
|
466
|
-
} else {
|
|
467
|
-
out.push(`${innerP}else`);
|
|
468
|
-
for (const stmt of fallback.body || []) {
|
|
469
|
-
out.push(...emitStmt(stmt, inner + 1));
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
if (regular.length > 0) {
|
|
474
|
-
out.push(`${innerP}end`);
|
|
475
|
-
}
|
|
476
|
-
out.push(`${prefix}until true`);
|
|
477
|
-
return out;
|
|
478
|
-
}
|
|
479
|
-
default:
|
|
480
|
-
return [];
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
const paramList = (fn) =>
|
|
485
|
-
fn.params
|
|
486
|
-
.map((param) => {
|
|
487
|
-
if (typeof param === "string") {
|
|
488
|
-
return param;
|
|
489
|
-
}
|
|
490
|
-
const typeAnn = luauType(param.valueType);
|
|
491
|
-
return typeAnn ? `${param.name}: ${typeAnn}` : param.name;
|
|
492
|
-
})
|
|
493
|
-
.join(", ");
|
|
494
|
-
|
|
495
|
-
const returnAnn = (fn) => {
|
|
496
|
-
const typeAnn = luauType(fn.returnType);
|
|
497
|
-
if (!typeAnn || typeAnn === "()") {
|
|
498
|
-
return "";
|
|
499
|
-
}
|
|
500
|
-
return `: ${typeAnn}`;
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
const emitFieldLiteral = (decl) => {
|
|
504
|
-
if (decl.value && decl.value.type === "initlist") {
|
|
505
|
-
const inner = (decl.value.fields || [])
|
|
506
|
-
.map((field) => `${field.name} = ${emitExpr(field.value)}`)
|
|
507
|
-
.join(", ");
|
|
508
|
-
return `{ ${inner} }`;
|
|
509
|
-
}
|
|
510
|
-
if (decl.valueType && nestedTypes.has(decl.valueType)) {
|
|
511
|
-
const nested = (ast.body || []).filter((item) => item.type === "decl" && item.owner === decl.valueType);
|
|
512
|
-
const inner = nested.map((item) => `${item.name} = ${emitFieldLiteral(item)}`).join(", ");
|
|
513
|
-
return `{ ${inner} }`;
|
|
514
|
-
}
|
|
515
|
-
return decl.value ? emitExpr(decl.value) : "nil";
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const emitStructConstructor = (root) => {
|
|
519
|
-
const fields = (ast.body || []).filter((decl) => decl.type === "decl" && decl.owner === root);
|
|
520
|
-
const inner = fields.map((field) => `\t\t${field.name} = ${emitFieldLiteral(field)},`).join("\n");
|
|
521
|
-
lines.push(`const function ${root}()`);
|
|
522
|
-
lines.push(" return {");
|
|
523
|
-
if (inner) {
|
|
524
|
-
lines.push(inner);
|
|
525
|
-
}
|
|
526
|
-
lines.push(" }");
|
|
527
|
-
lines.push("end");
|
|
528
|
-
lines.push("");
|
|
529
|
-
};
|
|
530
|
-
|
|
531
|
-
if (hasOwnedMethods) {
|
|
532
|
-
for (const owner of classOwners) {
|
|
533
|
-
lines.push(`local ${owner} = {}`);
|
|
534
|
-
lines.push("");
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
if (structModule) {
|
|
539
|
-
for (const root of structRoots) {
|
|
540
|
-
emitStructConstructor(root);
|
|
541
|
-
}
|
|
542
|
-
if (structRoots.length === 1) {
|
|
543
|
-
lines.push(`return ${structRoots[0]}`);
|
|
544
|
-
} else {
|
|
545
|
-
lines.push("return {");
|
|
546
|
-
for (const root of structRoots) {
|
|
547
|
-
lines.push(` ${root} = ${root},`);
|
|
548
|
-
}
|
|
549
|
-
lines.push("}");
|
|
550
|
-
}
|
|
551
|
-
lines.push("");
|
|
552
|
-
return lines.join("\n");
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
for (const decl of ast.body) {
|
|
556
|
-
if (decl.type === "decl") {
|
|
557
|
-
lines.push(...emitNewDecl(decl, 0));
|
|
558
|
-
lines.push("");
|
|
559
|
-
continue;
|
|
560
|
-
}
|
|
561
|
-
if (decl.type === "expr") {
|
|
562
|
-
lines.push(emitExpr(decl.expr));
|
|
563
|
-
lines.push("");
|
|
564
|
-
continue;
|
|
565
|
-
}
|
|
566
|
-
if (decl.type !== "function") {
|
|
567
|
-
continue;
|
|
568
|
-
}
|
|
569
|
-
selfOwner = decl.owner || null;
|
|
570
|
-
localNames.clear();
|
|
571
|
-
for (const param of decl.params || []) {
|
|
572
|
-
const paramName = typeof param === "string" ? param : param.name;
|
|
573
|
-
if (paramName) {
|
|
574
|
-
localNames.add(paramName);
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
if (decl.owner) {
|
|
578
|
-
lines.push(`function ${decl.owner}:${decl.name}(${paramList(decl)})${returnAnn(decl)}`);
|
|
579
|
-
} else {
|
|
580
|
-
lines.push(`const function ${decl.name}(${paramList(decl)})${returnAnn(decl)}`);
|
|
581
|
-
}
|
|
582
|
-
for (const stmt of decl.body) {
|
|
583
|
-
lines.push(...emitStmt(stmt, 1));
|
|
584
|
-
}
|
|
585
|
-
lines.push("end");
|
|
586
|
-
lines.push("");
|
|
587
|
-
selfOwner = null;
|
|
588
|
-
localNames.clear();
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
const hasInit = ast.body.some((decl) => decl.type === "function" && decl.name === "init");
|
|
592
|
-
if (classModule) {
|
|
593
|
-
for (const owner of classOwners) {
|
|
594
|
-
if (hasInit) {
|
|
595
|
-
lines.push(`${owner}.Start = ${owner}.init`);
|
|
596
|
-
lines.push(`${owner}.Init = ${owner}.init`);
|
|
597
|
-
}
|
|
598
|
-
lines.push(`return ${owner}`);
|
|
599
|
-
lines.push("");
|
|
600
|
-
}
|
|
601
|
-
} else if (hasInit && !options.skipInit) {
|
|
602
|
-
lines.push("init()");
|
|
603
|
-
lines.push("");
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
return lines.join("\n");
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
export { emit };
|