mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
361 lines (360 loc) • 10 kB
TypeScript
/**
* MATLAB®/Octave like syntax parser/interpreter/compiler.
*/
import type { NodeInput, NodeExpr, NodeIdentifier } from './AST';
import type { MathObject } from './MathOperation';
/**
* `aliasNameTable` type.
*/
type AliasNameTable = Record<string, RegExp>;
/**
* `aliasNameFunction` type.
*/
type AliasNameFunction = (name: string) => string;
/**
* `builtInFunctionTable` entry type.
*/
type BuiltInFunctionTableEntry = {
type: 'BUILTIN';
mapper: boolean;
ev: boolean[];
func: Function;
UnparserMathML?: (tree: NodeInput) => string;
};
/**
* `builtInFunctionTable` type.
*/
type BuiltInFunctionTable = Record<string, BuiltInFunctionTableEntry>;
/**
* `localTable` type.
*/
type LocalTable = Record<string, NodeInput>;
/**
* `nameTable` entry type.
*/
type NameTableEntry = {
undefined?: string;
node: NodeExpr;
};
/**
* `nameTable` type.
*/
type NameTable = Record<string, NameTableEntry>;
/**
* `commandWordListTable` function type.
*/
type CommandWordListFunction = (...args: string[]) => any;
/**
* `commandWordListTable` entry type.
*/
type CommandWordListTableEntry = {
func: CommandWordListFunction;
};
/**
* `commandWordListTable` type.
*/
type CommandWordListTable = Record<string, CommandWordListTableEntry>;
/**
* `response` type
*/
type ExitStatus = number;
type ExitStatusValues = Record<string, ExitStatus>;
/**
* EvaluatorConfig type.
*/
type EvaluatorConfig = {
aliasNameTable?: AliasNameTable;
externalFunctionTable?: BuiltInFunctionTable;
externalCmdWListTable?: CommandWordListTable;
};
/**
* Increment and decrement operator handler type.
*/
type IncDecOperator = (tree: NodeIdentifier) => MathObject;
/**
* Evaluator instance interface.
*/
interface EvaluatorInterface {
debug: boolean;
nativeNameTableList: string[];
nameTable: NameTable;
builtInFunctionList: string[];
localTable: LocalTable;
exitStatus: ExitStatus;
precedenceTable: {
[key: string]: number;
};
aliasNameFunction: AliasNameFunction;
Parse(input: string): NodeInput;
Restart(): void;
Clear(...names: string[]): void;
Evaluator(tree: NodeInput, local?: boolean, fname?: string): NodeInput;
Evaluate(tree: NodeInput): NodeInput;
Unparse(tree: NodeInput, parentPrecedence?: number): string;
UnparserMathML(tree: NodeInput, parentPrecedence: number): string;
UnparseMathML(tree: NodeInput, display: 'inline' | 'block'): string;
ToMathML(input: string, display: 'inline' | 'block'): string;
}
/**
* `Evaluator` object.
*/
declare class Evaluator implements EvaluatorInterface {
/**
* After run `Evaluate` method, the `exitStatus` property will contains
* exit state of evaluation.
*/
static readonly response: ExitStatusValues;
/**
* Private debug flag.
*/
private _debug;
/**
* `debug` getter.
*/
get debug(): boolean;
/**
* `debug` setter.
*/
set debug(value: boolean);
/**
* Native name table. It's inserted in nameTable when Evaluator constructor executed.
*/
private nativeNameTable;
/**
* Native name table list.
*/
private _nativeNameTableList;
/**
* Native name table list getter.
*/
get nativeNameTableList(): string[];
/**
* Alias name table.
*/
private aliasNameTable;
/**
* Name table.
*/
private _nameTable;
/**
* Name table getter.
*/
get nameTable(): NameTable;
/**
* Undefined Reference table.
*/
private undefinedReferenceTable;
/**
* Built-in function table.
*/
private builtInFunctionTable;
/**
* Get a list of names of defined functions in builtInFunctionTable.
*/
get builtInFunctionList(): string[];
/**
* Local table.
*/
private _localTable;
/**
* Local table getter.
*/
get localTable(): LocalTable;
/**
* Command word list table.
*/
private commandWordListTable;
/**
* Evaluator exit status.
*/
private _exitStatus;
/**
* Evaluator exit status getter.
*/
get exitStatus(): ExitStatus;
/**
* 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 incDecOpFactory;
/**
* Operator table.
*/
private readonly opTable;
private static readonly precedence;
/**
* Operator precedence table.
*/
private _precedenceTable;
/**
* Operator precedence table getter.
*/
get precedenceTable(): {
[key: string]: number;
};
/**
* Load precedence table.
*/
private loadPrecedenceTable;
/**
* 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.
*/
private _aliasNameFunction;
/**
* Alias name function setter.
*/
set aliasNameFunction(func: AliasNameFunction);
/**
* Alias name function getter.
*/
get aliasNameFunction(): AliasNameFunction;
/**
* Load the `Evaluator`.
* @param config
*/
private loadEvaluator;
/**
* `Evaluator` object private constructor
*/
private constructor();
/**
* Creates an instance of the `Evaluator` object.
* @param config
* @returns
*/
static readonly Create: (config?: EvaluatorConfig) => Evaluator;
/**
* 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;
/**
* 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`.
*/
private validateAssignment;
/**
* 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.
*/
private defineFunction;
/**
* 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
*/
private toBoolean;
/**
*
* @param id
*/
private solveUndefined;
/**
* 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;
/**
* Executes the `Parse` and `Evaluate` methods on an input string, returning the computed result.
* @param input String to parse and evaluate.
* @returns Computed result of input.
*/
Execute(input: string): NodeInput;
/**
* Unparse expression `tree`.
* @param tree Expression to unparse.
* @returns Expression `tree` unparsed.
*/
Unparse(tree: NodeInput, parentPrecedence?: number): 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, AliasNameFunction, BuiltInFunctionTableEntry, BuiltInFunctionTable, NameTable, CommandWordListFunction, CommandWordListTableEntry, CommandWordListTable, EvaluatorConfig, IncDecOperator, };
export { Evaluator };
declare const _default: {
Evaluator: typeof Evaluator;
};
export default _default;