UNPKG

onelang

Version:

OneLang transpiler framework core

57 lines 2.3 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "../AstVisitor", "../AstHelper"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const AstVisitor_1 = require("../AstVisitor"); const AstHelper_1 = require("../AstHelper"); /** * Fills out `isMutable` and `isUnused` properties of variables. * * It sets `isMutable` to `false` and `isUnused` to `true` by default, and if a variable * has a reference then it sets `isUnused` to `false` and if the reference is used * for modification, then it sets `isMutable` to `true` */ class FillVariableMutability extends AstVisitor_1.AstVisitor { constructor(lang) { super(); this.lang = lang; } visitBinaryExpression(expr, isMutable) { this.visitExpression(expr.left, AstHelper_1.AstHelper.isBinaryOpModifies(expr)); this.visitExpression(expr.right, false); } visitCallExpression(callExpr, isMutable) { const method = AstHelper_1.AstHelper.getMethodFromRef(this.lang, callExpr.method); const mutates = method && method.mutates; this.visitExpression(callExpr.method, mutates); for (const arg of callExpr.arguments) this.visitExpression(arg, false); } visitVariable(stmt) { stmt.isMutable = false; stmt.isUnused = true; } visitVariableRef(expr, isMutable) { if (expr.thisExpr) this.visitExpression(expr.thisExpr, false); if (isMutable) expr.varRef.isMutable = true; expr.varRef.isUnused = false; } visitUnaryExpression(expr) { this.visitExpression(expr.operand, true); } process(schema) { this.visitSchema(schema, false); } } exports.FillVariableMutability = FillVariableMutability; }); //# sourceMappingURL=FillVariableMutability.js.map