ts-budgie
Version:
Converts TypeScript code to Budgie.
91 lines • 4.85 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var budgie_1 = require("budgie");
var ts = require("typescript");
var budgieLine_1 = require("../../output/budgieLine");
var transformation_1 = require("../../output/transformation");
var unsupported_1 = require("../../output/unsupported");
var numerics_1 = require("../../parsing/numerics");
var visitor_1 = require("../visitor");
var forLoopsMustBeAdditiveComplaint = "For loops over numbers must change the iterator with addition.";
var irregularComplaint = "Could not parse irregular for loop.";
var ForStatementVisitor = /** @class */ (function (_super) {
__extends(ForStatementVisitor, _super);
function ForStatementVisitor() {
return _super !== null && _super.apply(this, arguments) || this;
}
ForStatementVisitor.prototype.visit = function (node) {
return [transformation_1.Transformation.fromNode(node, this.sourceFile, this.getTransformationContents(node))];
};
ForStatementVisitor.prototype.getTransformationContents = function (node) {
var condition = node.condition, incrementor = node.incrementor, initializer = node.initializer;
if (condition === undefined ||
incrementor === undefined ||
initializer === undefined ||
!ts.isVariableDeclarationList(initializer) ||
initializer.declarations.length !== 1) {
return [unsupported_1.createUnsupportedBudgieLine(irregularComplaint)];
}
var declaration = initializer.declarations[0];
if (declaration.initializer === undefined) {
return [unsupported_1.createUnsupportedBudgieLine(irregularComplaint)];
}
var name = declaration.name.text;
var incrementorText = this.getIncrementorIfNotOne(name, incrementor);
var end = this.getConditionEnd(name, condition);
var start = declaration.initializer.getText(this.sourceFile);
var realType = typeof end === "string" ? numerics_1.getNumericTypeNameFromUsages([start, end]) : "float";
var bodyNodes = this.router.recurseIntoNode(node.statement);
var parameters = [name, realType, start, end];
if (incrementorText !== undefined) {
parameters.push(incrementorText);
}
return __spreadArrays([new (budgieLine_1.BudgieLine.bind.apply(budgieLine_1.BudgieLine, __spreadArrays([void 0, budgie_1.CommandNames.ForNumbersStart], parameters)))()], bodyNodes, [new budgieLine_1.BudgieLine(budgie_1.CommandNames.ForNumbersEnd)]);
};
ForStatementVisitor.prototype.getIncrementorIfNotOne = function (target, incrementor) {
if (ts.isPostfixUnaryExpression(incrementor) || ts.isPrefixUnaryExpression(incrementor)) {
return undefined;
}
if (ts.isBinaryExpression(incrementor) &&
incrementor.operatorToken.kind === ts.SyntaxKind.PlusEqualsToken &&
incrementor.left.text === target) {
var right = incrementor.right;
return right.text === "1" ? undefined : right.text;
}
return unsupported_1.createUnsupportedBudgieLine(forLoopsMustBeAdditiveComplaint);
};
ForStatementVisitor.prototype.getConditionEnd = function (target, condition) {
if (!ts.isBinaryExpression(condition) ||
condition.left.text !== target ||
condition.operatorToken.kind !== ts.SyntaxKind.LessThanToken) {
return unsupported_1.createUnsupportedBudgieLine(irregularComplaint);
}
if (ts.isNumericLiteral(condition.right)) {
return condition.right.text;
}
return this.router.recurseIntoValue(condition.right);
};
return ForStatementVisitor;
}(visitor_1.NodeVisitor));
exports.ForStatementVisitor = ForStatementVisitor;
//# sourceMappingURL=forStatementVisitor.js.map