evaluateExpression?
In the Readme.md under the heading Custom Language it's described how to evaluate an expression (with new ExpressionParser(arithmeticLanguage).evaluateExpression(expr)).
However I can't get this to work. Looking in the code I can't even see any evaluateExpression.
Have I misunderstood something, or is the documentation faulty?
it's called expressionToValue, docs seem to be faulty
yes it's expressionToValue, but then it complains about types are not explicitly written (Parameter 'a' implicitly has an 'any' type), and when I fix that it says that 'ExpressionThunk' is not assignable to type 'number'
theres no way of specifying the thunk/term types don't think you can do much about it other than cast, ts-ignore or runtime checks
const arithmeticLanguage: ExpressionParserOptions = {
INFIX_OPS: {
"+": function (a, b) {
return (a() as number) + (b() as number);
},
Thankyou
Fixed expressionToValue in README.md
I also exported the helpers used in the demo language:
import {
array,
char,
evalArray,
evalBool,
evalString,
iterable,
num,
obj,
string,
unpackArgs,
}
which can help with typing, and error handling on incorrect types
e.g. in formula.ts
ADD: (a, b) => num(a()) + num(b()),
SUB: (a, b) => num(a()) - num(b()),
MUL: (a, b) => num(a()) * num(b()),
DIV: (a, b) => num(a()) / num(b()),
...
SUM: (arg) =>
evalArray(arg(), num).reduce((prev: number, curr) => prev + num(curr), 0),
(Note these go through unpackArgs)