UNPKG

mathjslab

Version:

MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.

264 lines (263 loc) 8.09 kB
/** * MATLAB®/Octave like syntax parser/interpreter/compiler. */ import type { NodeInput, NodeExpr, NodeIdentifier } from './AST'; import type { MathObject } from './MathOperation'; /** * aliasNameTable and AliasFunction type. */ type AliasNameTable = Record<string, RegExp>; type AliasFunction = (name: string) => string; /** * builtInFunctionTable type. */ type BuiltInFunctionTableEntry = { type: 'BUILTIN'; mapper: boolean; ev: boolean[]; func: Function; unparserMathML?: (tree: NodeInput) => string; }; type BuiltInFunctionTable = Record<string, BuiltInFunctionTableEntry>; /** * nameTable type. */ type NameTable = Record<string, NodeExpr>; /** * commandWordListTable type. */ type CommandWordListFunction = (...args: string[]) => any; type CommandWordListTableEntry = { func: CommandWordListFunction; }; type CommandWordListTable = Record<string, CommandWordListTableEntry>; /** * EvaluatorConfig type. */ type EvaluatorConfig = { aliasNameTable?: AliasNameTable; externalFunctionTable?: BuiltInFunctionTable; externalCmdWListTable?: CommandWordListTable; }; type IncDecOperator = (tree: NodeIdentifier) => MathObject; /** * Evaluator object. */ declare class Evaluator { /** * After run Evaluate method, the exitStatus property will contains * exit state of method. */ response: { EXTERNAL: number; WARNING: number; OK: number; LEX_ERROR: number; PARSER_ERROR: number; EVAL_ERROR: number; }; /** * Debug flag. */ debug: boolean; /** * Native name table. It's inserted in nameTable when Evaluator constructor executed. */ private nativeNameTable; nativeNameTableList: string[]; /** * Alias table. */ private aliasNameTable; /** * Name table. */ nameTable: NameTable; /** * Built-in function table. */ builtInFunctionTable: BuiltInFunctionTable; /** * Get a list of names of defined functions in builtInFunctionTable. */ get builtInFunctionList(): string[]; /** * Local table. */ localTable: Record<string, NodeInput>; /** * Command word list table. */ commandWordListTable: CommandWordListTable; /** * Evaluator exit status. */ exitStatus: number; /** * Increment and decrement operator * @param pre `true` if prefixed. `false` if postfixed. * @param operation Operation (`'plus'` or `'minus'`). * @returns Operator function with signature `(tree: NodeIdentifier) => MathObject`. */ private incDecOp; /** * Operator table. */ private readonly opTable; private static readonly precedence; /** * Operator precedence table. */ precedenceTable: { [key: string]: number; }; loadPrecedenceTable(): void; /** * Get tree node precedence. * @param tree Tree node. * @returns Node precedence. */ private nodePrecedence; /** * User functions. */ private readonly functions; /** * Special functions MathML unparser. */ private readonly unparseMathMLFunctions; /** * Alias name function. This property is set at Evaluator instantiation. * @param name Alias name. * @returns Canonical name. */ aliasName: AliasFunction; loadEvaluator(config?: EvaluatorConfig): void; /** * Evaluator object constructor */ constructor(config?: EvaluatorConfig); /** * Parse input string. * @param input String to parse. * @returns Abstract syntax tree of input. */ Parse(input: string): NodeInput; /** * Load native name table in the name table. */ private loadNativeNameTable; /** * Restart evaluator. */ Restart(): void; /** * Clear variables. If names is 0 lenght restart evaluator. * @param names Variable names to clear in nameTable and builtInFunctionTable. */ Clear(...names: string[]): void; /** * Throws error if left hand side length of multiple assignment greater * than maximum length (to be used in ReturnSelector functions). * @param maxLength Maximum length of return list. * @param currentLength Requested length of return list. */ static throwErrorIfGreaterThanReturnList(maxLength: number, currentLength: number): void; /** * Tests if it is a NodeReturnList and if so reduces it to its first * element. * @param value A node. * @returns Reduced node if `tree` is a NodeReturnList. */ reduceIfReturnList(tree: NodeInput): NodeInput; /** * Validate left side of assignment node. * @param tree Left side of assignment node. * @param shallow True if tree is a left root of assignment. * @returns An object with four properties: `left`, `id`, `args` and `field`. */ validateAssignment(tree: NodeExpr, shallow: boolean, local: boolean, fname: string): { id: string; index: NodeExpr[]; field: string[]; }[]; /** * Define function in builtInFunctionTable. * @param name Name of function. * @param func Function body. * @param map `true` if function is a mapper function. * @param ev A `boolean` array indicating which function argument should * be evaluated before executing the function. If array is zero-length all * arguments are evaluated. */ defineFunction(name: string, func: Function, mapper?: boolean, ev?: boolean[]): void; /** * Define unary operator function in builtInFunctionTable. * @param name Name of function. * @param func Function body. */ private defineUnaryOperatorFunction; /** * Define binary operator function in builtInFunctionTable. * @param name Name of function. * @param func Function body. */ private defineBinaryOperatorFunction; /** * Define define two-or-more operand function in builtInFunctionTable. * @param name * @param func */ private defineLeftAssociativeMultipleOperationFunction; /** * * @param tree * @returns */ toBoolean(tree: NodeExpr): boolean; /** * Expression tree recursive evaluator. * @param tree Expression to evaluate. * @param local Set `true` if evaluating function. * @param fname Function name if evaluating function. * @returns Expression `tree` evaluated. */ Evaluator(tree: NodeInput, local?: boolean, fname?: string): NodeInput; /** * Evaluate expression `tree`. * @param tree Expression to evaluate. * @returns Expression `tree` evaluated. */ Evaluate(tree: NodeInput): NodeInput; /** * Unparse expression `tree`. * @param tree Expression to unparse. * @returns Expression `tree` unparsed. */ Unparse(tree: NodeInput): string; /** * Unparse recursively expression tree generating MathML representation. * @param tree Expression tree. * @returns String of expression `tree` unparsed as MathML language. */ unparserMathML(tree: NodeInput, parentPrecedence?: number): string; /** * Unparse Expression tree in MathML. * @param tree Expression tree. * @returns String of expression unparsed as MathML language. */ UnparseMathML(tree: NodeInput, display?: 'inline' | 'block'): string; /** * Generates MathML representation of input without evaluation. * @param input Input to parse and generate MathML representation. * @param display `'inline'` or `'block'`. * @returns MathML representation of input. */ toMathML(input: string, display?: 'inline' | 'block'): string; } export type { AliasNameTable, AliasFunction, BuiltInFunctionTableEntry, BuiltInFunctionTable, NameTable, CommandWordListFunction, CommandWordListTableEntry, CommandWordListTable, EvaluatorConfig, IncDecOperator, }; export { Evaluator }; declare const _default: { Evaluator: typeof Evaluator; }; export default _default;