roblox-ts
Version:
<div align="center"><img width=25% src="https://i.imgur.com/yCjHmng.png"></div> <h1 align="center"><a href="https://roblox-ts.github.io/">roblox-ts</a></h1> <div align="center">A TypeScript-to-Lua Compiler for Roblox</div> <br> <div align="center"> <a hr
70 lines • 2.94 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("ts-morph"));
const _1 = require(".");
const CompilerError_1 = require("../errors/CompilerError");
const typeUtilities_1 = require("../typeUtilities");
const utility_1 = require("../utility");
function assertNonLuaTuple(exp) {
if (typeUtilities_1.isTupleType(typeUtilities_1.getType(exp))) {
throw new CompilerError_1.CompilerError(`Cannot check a LuaTuple in a conditional! Change this to:\n\t${exp.getText()}[0]`, exp, CompilerError_1.CompilerErrorType.LuaTupleInConditional);
}
return exp;
}
exports.assertNonLuaTuple = assertNonLuaTuple;
function compileIfStatement(state, node) {
let result = "";
state.enterPrecedingStatementContext();
const expStr = _1.compileExpression(state, utility_1.skipNodesDownwards(assertNonLuaTuple(node.getExpression())));
result += state.exitPrecedingStatementContextAndJoin();
result += state.indent + `if ${expStr} then\n`;
state.pushIndent();
result += _1.compileStatement(state, node.getThenStatement());
state.popIndent();
let elseStatement = node.getElseStatement();
let numBlocks = 1;
while (elseStatement && ts.TypeGuards.isIfStatement(elseStatement)) {
state.enterPrecedingStatementContext();
state.pushIndent();
const exp = utility_1.skipNodesDownwards(assertNonLuaTuple(elseStatement.getExpression()));
const elseIfExpression = _1.compileExpression(state, exp);
const context = state.exitPrecedingStatementContext();
if (context.length > 0) {
state.popIndent();
result += state.indent + `else\n`;
state.pushIndent();
numBlocks++;
result += utility_1.joinIndentedLines(context);
result += state.indent + `if ${elseIfExpression} then\n`;
}
else {
state.popIndent();
result += state.indent + `elseif ${elseIfExpression} then\n`;
}
state.pushIndent();
result += _1.compileStatement(state, elseStatement.getThenStatement());
state.popIndent();
elseStatement = elseStatement.getElseStatement();
}
if (elseStatement) {
result += state.indent + "else\n";
state.pushIndent();
result += _1.compileStatement(state, elseStatement);
state.popIndent();
}
result += state.indent + `end;\n`;
for (let i = 1; i < numBlocks; i++) {
state.popIndent();
result += state.indent + `end;\n`;
}
return result;
}
exports.compileIfStatement = compileIfStatement;
//# sourceMappingURL=if.js.map