Starting expr-eval's tests.
This commit is contained in:
parent
345458f453
commit
edf4518494
8 changed files with 203 additions and 22 deletions
|
@ -171,9 +171,6 @@ function evaluate(tokens, expr, values) {
|
|||
nstack.push(n1 ? !!evaluate(n2, expr, values) : false)
|
||||
} else if(item.value === "or") {
|
||||
nstack.push(n1 ? true : !!evaluate(n2, expr, values))
|
||||
} else if(item.value === "=") {
|
||||
f = expr.binaryOps[item.value]
|
||||
nstack.push(f(n1, evaluate(n2, expr, values), values))
|
||||
} else {
|
||||
f = expr.binaryOps[item.value]
|
||||
nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values)))
|
||||
|
|
|
@ -47,9 +47,7 @@ const optionNameMap = {
|
|||
"not": "logical",
|
||||
"?": "conditional",
|
||||
":": "conditional",
|
||||
//'=': 'assignment', // Disable assignment
|
||||
"[": "array"
|
||||
//'()=': 'fndef' // Diable function definition
|
||||
}
|
||||
|
||||
export class Parser {
|
||||
|
@ -109,7 +107,6 @@ export class Parser {
|
|||
and: Polyfill.andOperator,
|
||||
or: Polyfill.orOperator,
|
||||
"in": Polyfill.inOperator,
|
||||
"=": Polyfill.setVar,
|
||||
"[": Polyfill.arrayIndex
|
||||
}
|
||||
|
||||
|
|
|
@ -472,7 +472,7 @@ export class TokenStream {
|
|||
this.current = this.newToken(TOP, "==")
|
||||
this.pos++
|
||||
} else {
|
||||
this.current = this.newToken(TOP, c)
|
||||
return false
|
||||
}
|
||||
} else if(c === "!") {
|
||||
if(this.expression.charAt(this.pos + 1) === "=") {
|
||||
|
|
|
@ -84,11 +84,11 @@ class ExprParserAPI extends Module {
|
|||
return this.#parser.parse(expression)
|
||||
}
|
||||
|
||||
integral(a, b, ...args) {
|
||||
integral(a = null, b = null, ...args) {
|
||||
let usage1 = qsTranslate("usage", "integral(<from: number>, <to: number>, <f: ExecutableObject>)")
|
||||
let usage2 = qsTranslate("usage", "integral(<from: number>, <to: number>, <f: string>, <variable: string>)")
|
||||
let f = this.parseArgumentsForFunction(args, usage1, usage2)
|
||||
if(a == null || b == null)
|
||||
if(typeof a !== "number" || typeof b !== "number")
|
||||
throw EvalError(qsTranslate("usage", "Usage:\n%1\n%2").arg(usage1).arg(usage2))
|
||||
|
||||
// https://en.wikipedia.org/wiki/Simpson%27s_rule
|
||||
|
@ -101,10 +101,10 @@ class ExprParserAPI extends Module {
|
|||
let usage2 = qsTranslate("usage", "derivative(<f: string>, <variable: string>, <x: number>)")
|
||||
let x = args.pop()
|
||||
let f = this.parseArgumentsForFunction(args, usage1, usage2)
|
||||
if(x == null)
|
||||
if(typeof x !== "number")
|
||||
throw EvalError(qsTranslate("usage", "Usage:\n%1\n%2").arg(usage1).arg(usage2))
|
||||
|
||||
let derivative_precision = x / 10
|
||||
let derivative_precision = 1e-8
|
||||
return (f(x + derivative_precision / 2) - f(x - derivative_precision / 2)) / derivative_precision
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue