UNPKG

expression-evaluation

Version:
77 lines (76 loc) 3.6 kB
import { FunctionDefinition } from './FunctionDefinition.js'; import { StaticScope } from './StaticScope.js'; import { Variable } from './Variable.js'; import { Type, Value } from './Type.js'; import { ParserFrame } from './ParserFrame.js'; import { ParserState } from './ParserState.js'; import { Node } from './Node.js'; export declare class Expression { static readonly keywords: string[]; protected readonly _expression: string; protected readonly _strict: boolean; protected readonly _root: Node; protected readonly _variables: Map<string, Variable>; protected readonly _constants: Map<string, Value>; protected readonly _gfunctions: Map<string, FunctionDefinition>; protected readonly _mfunctions: Map<string, FunctionDefinition>; protected readonly _scope: StaticScope; /** Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable. @param expr Math expression to compile. @param config Optional expected type, strict mode, variable types, constant values and functions to add for the compilation. If expected type is provided then expression return type is matched against it. If strict mode is set then undeclared variables will not be allowed in expression. */ constructor(expr: string, config?: { type?: Type; strict?: boolean; variables?: Record<string, Type>; constants?: Record<string, Value>; functions?: Record<string, { func: (...values: any[]) => Value; type: Type; argTypes: Type[]; minArity?: number; maxArity?: number; typeInference?: number; pure?: boolean; }>; }); /** Returns compiled expression return value type. */ get type(): Type; /** Returns string representing parsed node tree structure. @returns Parsed expression string. */ toString(): string; /** Returns record with compiled variable names and expected types. @returns Record with variable names and types. */ variables(): Record<string, Type>; /** Evaluates compiled expression using provided variable values. @param values Record with variable names and values. @returns Calculated value. */ evaluate(values?: Record<string, Value>): Value; protected program(state: ParserState, scope: StaticScope): Node; protected unit(state: ParserState, scope: StaticScope): Node; protected loop(state: ParserState, scope: StaticScope): Node; protected condition(state: ParserState, scope: StaticScope): Node; protected disjunction(state: ParserState, scope: StaticScope): Node; protected conjunction(state: ParserState, scope: StaticScope): Node; protected comparison(state: ParserState, scope: StaticScope): Node; protected aggregate(state: ParserState, scope: StaticScope): Node; protected product(state: ParserState, scope: StaticScope): Node; protected factor(state: ParserState, scope: StaticScope): Node; protected coalescence(state: ParserState, scope: StaticScope): Node; protected accessor(state: ParserState, scope: StaticScope): Node; protected term(state: ParserState, scope: StaticScope): Node; protected variable(state: ParserState, scope: StaticScope, type?: Type): Node; protected subroutine(state: ParserState, scope: StaticScope, type?: Type): Node; protected call(frame: ParserFrame, func: FunctionDefinition, subnodes: Node[]): Node; }