Renaming internal ExprEval Expression class to ExprEvalExpression
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Adsooi 2024-09-23 18:37:57 +02:00
parent 45dff33bb5
commit 8c8964e75e
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
5 changed files with 19 additions and 14 deletions

View file

@ -146,7 +146,7 @@ function substitute(tokens, variable, expr) {
/**
* Evaluates the given instructions for a given Expression with given values.
* @param {Instruction[]} tokens
* @param {Expression} expr
* @param {ExprEvalExpression} expr
* @param {Record.<string, number>} values
* @return {number}
*/
@ -442,7 +442,7 @@ function getSymbols(tokens, symbols, options) {
}
}
export class Expression {
export class ExprEvalExpression {
/**
* @param {Instruction[]} tokens
* @param {Parser} parser
@ -458,26 +458,26 @@ export class Expression {
/**
* Simplifies the expression.
* @param {Object<string, number|Expression>|undefined} values
* @returns {Expression}
* @param {Object<string, number|ExprEvalExpression>|undefined} values
* @returns {ExprEvalExpression}
*/
simplify(values) {
values = values || {}
return new Expression(simplify(this.tokens, this.unaryOps, this.binaryOps, this.ternaryOps, values), this.parser)
return new ExprEvalExpression(simplify(this.tokens, this.unaryOps, this.binaryOps, this.ternaryOps, values), this.parser)
}
/**
* Creates a new expression where the variable is substituted by the given expression.
* @param {string} variable
* @param {string|Expression} expr
* @returns {Expression}
* @param {string|ExprEvalExpression} expr
* @returns {ExprEvalExpression}
*/
substitute(variable, expr) {
if(!(expr instanceof Expression)) {
if(!(expr instanceof ExprEvalExpression)) {
expr = this.parser.parse(String(expr))
}
return new Expression(substitute(this.tokens, variable, expr), this.parser)
return new ExprEvalExpression(substitute(this.tokens, variable, expr), this.parser)
}
/**
@ -527,7 +527,7 @@ export class Expression {
/**
* Converts the expression to a JS function.
* @param {string} param - Parsed variables for the function.
* @param {Object.<string, (Expression|string)>} variables - Default variables to provide.
* @param {Object.<string, (ExprEvalExpression|string)>} variables - Default variables to provide.
* @returns {function(...any)}
*/
toJSFunction(param, variables) {

View file

@ -25,7 +25,7 @@
import * as Polyfill from "./polyfill.mjs"
import { ParserState } from "./parserstate.mjs"
import { TEOF, TokenStream } from "./tokens.mjs"
import { Expression } from "./expression.mjs"
import { ExprEvalExpression } from "./expression.mjs"
const optionNameMap = {
"+": "add",
@ -156,7 +156,7 @@ export class Parser {
parserState.parseExpression(instr)
parserState.expect(TEOF, QT_TRANSLATE_NOOP("error", "EOF"))
return new Expression(instr, this)
return new ExprEvalExpression(instr, this)
}
evaluate(expr, variables) {

View file

@ -42,6 +42,10 @@ export class Expression {
this.latexMarkup = Latex.expression(this.calc.tokens)
}
variables() {
return this.calc.variables()
}
isConstant() {
let vars = this.calc.variables()
return !vars.includes("x") && !vars.includes("n")

View file

@ -79,6 +79,7 @@ export class ExprParserAPI extends Module {
/**
* @param {string} expression - Expression to parse
* @returns {ExprEvalExpression}
*/
parse(expression) {
return this._parser.parse(expression)

View file

@ -107,7 +107,7 @@ export class ExpressionSetting extends Setting {
* @param {Expression} value
*/
set(value) {
let vars = value.calc.variables()
let vars = value.variables()
if(vars.length === this.variables.length && vars.every(x => this.variables.includes(x)))
Helper.setSetting(this.nameInConfig, value)
else {