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
111 lines • 5.72 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 utility_1 = require("../utility");
function isUnaryExpressionNonStatement(parent, node) {
return !(ts.TypeGuards.isExpressionStatement(parent) ||
(ts.TypeGuards.isForStatement(parent) && parent.getCondition() !== node));
}
function getIncrementString(opKind, expStr, node, varName) {
const op = opKind === ts.SyntaxKind.PlusPlusToken
? " + "
: opKind === ts.SyntaxKind.MinusMinusToken
? " - "
: (() => {
throw new CompilerError_1.CompilerError(`Unexpected UnaryExpression ( ${opKind} ) in getIncrementString`, node, CompilerError_1.CompilerErrorType.BadPrefixUnaryExpression, true);
})();
return `${varName ? `${varName} = ` : ""}${expStr}${op}1`;
}
function compilePrefixUnaryExpression(state, node) {
const operand = utility_1.skipNodesDownwards(node.getOperand(), true);
const opKind = node.getOperatorToken();
if (opKind === ts.SyntaxKind.PlusPlusToken || opKind === ts.SyntaxKind.MinusMinusToken) {
const parent = utility_1.skipNodesUpwards(node.getParentOrThrow());
const isNonStatement = isUnaryExpressionNonStatement(parent, node);
const expData = _1.getWritableOperandName(state, operand);
const { expStr } = expData;
if (isNonStatement) {
if (!ts.TypeGuards.isIdentifier(operand) || _1.isIdentifierDefinedInExportLet(operand)) {
const id = state.pushToDeclarationOrNewId(node, getIncrementString(opKind, expStr, node, ""), declaration => declaration.isIdentifier);
const context = state.getCurrentPrecedingStatementContext(node);
const isPushed = context.isPushed;
state.pushPrecedingStatements(node, state.indent + `${expStr} = ${id};\n`);
context.isPushed = isPushed;
return id;
}
else {
const incrStr = getIncrementString(opKind, expStr, node, expStr);
state.pushPrecedingStatements(node, state.indent + incrStr + ";\n");
return expStr;
}
}
else {
return getIncrementString(opKind, expStr, node, expStr);
}
}
else {
const expStr = _1.compileExpression(state, operand);
const tokenKind = node.getOperatorToken();
if (tokenKind === ts.SyntaxKind.ExclamationToken) {
return `not ${expStr}`;
}
else if (tokenKind === ts.SyntaxKind.MinusToken) {
return `-${expStr}`;
}
else if (tokenKind === ts.SyntaxKind.TildeToken) {
state.usesTSLibrary = true;
return `TS.bit_not(${expStr})`;
}
/* istanbul ignore next */
throw new CompilerError_1.CompilerError(`Unexpected prefix UnaryExpression ( ${tokenKind} ) in compilePrefixUnaryExpression`, node, CompilerError_1.CompilerErrorType.BadPrefixUnaryExpression, true);
}
}
exports.compilePrefixUnaryExpression = compilePrefixUnaryExpression;
function compilePostfixUnaryExpression(state, node) {
const operand = utility_1.skipNodesDownwards(node.getOperand());
const opKind = node.getOperatorToken();
if (opKind === ts.SyntaxKind.PlusPlusToken || opKind === ts.SyntaxKind.MinusMinusToken) {
const parent = utility_1.skipNodesUpwards(node.getParentOrThrow());
const isNonStatement = isUnaryExpressionNonStatement(parent, node);
const expData = _1.getWritableOperandName(state, operand);
const { expStr } = expData;
if (isNonStatement) {
const declaration = state.declarationContext.get(node);
let id;
if (declaration &&
(declaration.isIdentifier || expData.isIdentifier) &&
declaration.set !== "return" &&
declaration.set !== expStr) {
state.declarationContext.delete(node);
state.pushPrecedingStatements(node, state.indent + `${declaration.needsLocalizing ? "local " : ""}${declaration.set} = ${expStr};\n`);
// due to this optimization here, this shouldn't be shortened with `state.pushToDeclarationOrNewId`
id = expData.isIdentifier ? expStr : declaration.set;
const incrStr = getIncrementString(opKind, id, node, expStr);
state.pushPrecedingStatements(node, state.indent + incrStr + ";\n");
}
else {
id = state.pushPrecedingStatementToReuseableId(node, expStr);
const incrStr = getIncrementString(opKind, id, node, expStr);
state.pushPrecedingStatements(node, state.indent + incrStr + ";\n");
state.getCurrentPrecedingStatementContext(node).isPushed = true;
}
return id;
}
else {
return getIncrementString(opKind, expStr, node, expStr);
}
}
/* istanbul ignore next */
throw new CompilerError_1.CompilerError(`Unexpected postfix UnaryExpression! ( ${opKind} ) in compilePostfixUnaryExpression`, node, CompilerError_1.CompilerErrorType.BadPostfixUnaryExpression, true);
}
exports.compilePostfixUnaryExpression = compilePostfixUnaryExpression;
//# sourceMappingURL=unary.js.map