@scalenc/nc-format
Version:
Library for handling TRUMPF NC file format.
142 lines • 5.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.evaluate = exports.asString = exports.asInt = exports.asNumber = void 0;
const models_1 = require("../models");
const Errors_1 = require("./Errors");
const ProcessorException_1 = require("./ProcessorException");
function asNumber(value) {
if (typeof value === 'number') {
return value;
}
throw new Error(`Expected value '${value}' to be a number`);
}
exports.asNumber = asNumber;
function asInt(value) {
return Math.trunc(asNumber(value));
}
exports.asInt = asInt;
function asString(value) {
if (typeof value === 'string') {
return value;
}
throw new Error(`Expected value '${value}' to be a string`);
}
exports.asString = asString;
class ExpressionEvaluator {
variables;
value;
constructor(variables) {
this.variables = variables;
}
onBinaryOperation(o) {
o.leftExpression.visit(this);
const left = this.value;
this.value = undefined;
o.rightExpression.visit(this);
const right = this.value;
switch (o.operator) {
case models_1.BinaryOperator.MULTIPLY:
this.value = asNumber(left) * asNumber(right);
break;
case models_1.BinaryOperator.DIVIDE:
this.value = asNumber(left) / asNumber(right);
break;
case models_1.BinaryOperator.DIV_DIVIDE:
this.value = asInt(left) / asInt(right);
break;
case models_1.BinaryOperator.MODULO:
this.value = asNumber(left) % asNumber(right);
break;
case models_1.BinaryOperator.ADD:
this.value = asNumber(left) + asNumber(right);
break;
case models_1.BinaryOperator.SUBTRACT:
this.value = asNumber(left) - asNumber(right);
break;
case models_1.BinaryOperator.B_AND:
this.value = asInt(left) & asInt(right);
break;
case models_1.BinaryOperator.B_XOR:
this.value = asInt(left) ^ asInt(right);
break;
case models_1.BinaryOperator.B_OR:
this.value = asInt(left) | asInt(right);
break;
case models_1.BinaryOperator.AND:
this.value = asNumber(left) && asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.XOR:
this.value = !asNumber(left) !== !asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.OR:
this.value = asNumber(left) || asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.CONCAT:
this.value = asString(left) + asString(right);
break;
case models_1.BinaryOperator.EQUAL:
this.value = left === right ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.INEQUAL:
this.value = left !== right ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.GREATER:
this.value = asNumber(left) > asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.LESS:
this.value = asNumber(left) < asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.GREATER_EQUAL:
this.value = asNumber(left) >= asNumber(right) ? 1.0 : 0.0;
break;
case models_1.BinaryOperator.LESS_EQUAL:
this.value = asNumber(left) <= asNumber(right) ? 1.0 : 0.0;
break;
default:
throw new Error(`Unknown binary operation ${o.operator}`);
}
}
onBracket(b) {
b.innerExpression.visit(this);
}
onFunction() {
throw new Error('Not yet implemented');
}
// eslint-disable-next-line @typescript-eslint/ban-types
onNumber(n) {
this.value = n.value;
}
// eslint-disable-next-line @typescript-eslint/ban-types
onString(s) {
this.value = s.value;
}
onUnaryOperation(o) {
o.expression.visit(this);
switch (o.operator) {
case models_1.UnaryOperator.NOT:
this.value = asNumber(this.value) ? 0.0 : 1.0;
break;
case models_1.UnaryOperator.B_NOT:
this.value = ~asInt(this.value);
break;
case models_1.UnaryOperator.NEGATE:
this.value = -asNumber(this.value);
break;
default:
throw new Error(`Unknown unitary operation ${o.operator}`);
}
}
onVariable(v) {
this.value = this.variables.tryGetNumber(v.name);
if (this.value === undefined) {
throw new ProcessorException_1.ProcessorException(Errors_1.Errors.UNKNOWN_VARIABLE.replace(/\{0\}/, v.name));
}
}
}
function evaluate(expression, variables) {
const evaluator = new ExpressionEvaluator(variables);
expression.visit(evaluator);
return evaluator.value;
}
exports.evaluate = evaluate;
//# sourceMappingURL=ExpressionEvaluator.js.map