@sudoo/marked
Version:
JavaScript & TypeScript code runner in JavaScript, safe with marked territory, asynchronous
50 lines (49 loc) • 2.77 kB
JavaScript
;
/**
* @author WMXPY
* @namespace Evaluate
* @description Binary Expression
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.binaryExpressionEvaluator = exports.mountBinaryExpression = void 0;
const error_code_1 = require("../declare/error-code");
const in_1 = require("../operation/binary-expression/in");
const instance_of_1 = require("../operation/binary-expression/instance-of");
const operators_1 = require("../operation/binary-expression/operators");
const error_1 = require("../util/error/error");
const mountBinaryExpression = (sandbox) => {
sandbox.mount("BinaryExpression", exports.binaryExpressionEvaluator);
};
exports.mountBinaryExpression = mountBinaryExpression;
const binaryExpressionEvaluator = function (node, scope, trace) {
return __awaiter(this, void 0, void 0, function* () {
const nextTrace = trace.stack(node);
const evalLeft = () => __awaiter(this, void 0, void 0, function* () { return yield this.execute(node.left, scope, nextTrace); });
const evalRight = () => __awaiter(this, void 0, void 0, function* () { return yield this.execute(node.right, scope, nextTrace); });
if (node.operator === "in") {
const bindingExecuteInBinaryOperator = in_1.executeInBinaryOperator.bind(this);
return bindingExecuteInBinaryOperator(node.left, node.right, scope, nextTrace);
}
if (node.operator === "instanceof") {
const bindingExecuteInstanceOfBinaryOperator = instance_of_1.executeInstanceOfBinaryOperator.bind(this);
return bindingExecuteInstanceOfBinaryOperator(node.left, node.right, scope, nextTrace);
}
const operation = (0, operators_1.getBinaryOperation)(node.operator);
if (!operation) {
throw (0, error_1.error)(error_code_1.ERROR_CODE.BINARY_NOT_SUPPORT, node.operator, node, trace);
}
const left = yield evalLeft();
const right = yield evalRight();
return operation(left, right);
});
};
exports.binaryExpressionEvaluator = binaryExpressionEvaluator;