mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
298 lines (297 loc) • 10.1 kB
TypeScript
/**
* MATLAB®/Octave like syntax parser/interpreter/compiler.
*/
import * as AST from './AST';
import { CharString } from './CharString';
import { ComplexDecimal } from './ComplexDecimal';
import { MultiArray } from './MultiArray';
import type { MathObject } from './MathOperation';
import { Structure } from './Structure';
import { FunctionHandle } from './FunctionHandle';
/**
* 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: AST.NodeInput) => string;
};
type BuiltInFunctionTable = Record<string, BuiltInFunctionTableEntry>;
/**
* nameTable type.
*/
type NameTable = Record<string, AST.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: AST.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 readonly nativeNameTable;
readonly 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, AST.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: AST.NodeIdentifier) => MathObject`.
*/
private incDecOp;
/**
* Operator table.
*/
private readonly opTable;
private static readonly precedence;
/**
* Operator precedence table.
*/
readonly precedenceTable: {
[key: string]: number;
};
/**
* Get tree node precedence.
* @param tree Tree node.
* @returns Node precedence.
*/
private nodePrecedence;
/**
* User functions.
*/
private readonly functions;
/**
* Local methods.
*/
readonly unparseString: typeof CharString.unparse;
readonly unparseStringMathML: typeof CharString.unparseMathML;
readonly newNumber: typeof ComplexDecimal.newThis;
readonly unparseNumber: typeof ComplexDecimal.unparse;
readonly unparseNumberMathML: typeof ComplexDecimal.unparseMathML;
readonly isRowVector: typeof MultiArray.isRowVector;
readonly unparseArray: typeof MultiArray.unparse;
readonly unparseStructure: typeof Structure.unparse;
readonly unparseStructureMathML: typeof Structure.unparseMathML;
readonly newFunctionHandle: typeof FunctionHandle.newThis;
readonly unparseFunctionHandle: typeof FunctionHandle.unparse;
readonly unparseFunctionHandleMathML: typeof FunctionHandle.unparseMathML;
readonly unparseArrayMathML: typeof MultiArray.unparseMathML;
readonly evaluateArray: typeof MultiArray.evaluate;
readonly mapArray: typeof MultiArray.rawMap;
readonly getElements: typeof MultiArray.getElements;
readonly getElementsLogical: typeof MultiArray.getElementsLogical;
readonly setElements: typeof MultiArray.setElements;
readonly setElementsLogical: typeof MultiArray.setElementsLogical;
readonly expandRange: typeof MultiArray.expandRange;
readonly expandColon: typeof MultiArray.expandColon;
readonly array0x0: typeof MultiArray.emptyArray;
readonly linearize: typeof MultiArray.linearize;
readonly scalarToArray: typeof MultiArray.scalarToMultiArray;
readonly scalarOrCellToArray: typeof MultiArray.scalarOrCellToMultiArray;
readonly arrayToScalar: typeof MultiArray.MultiArrayToScalar;
readonly linearLength: typeof MultiArray.linearLength;
readonly getDimension: typeof MultiArray.getDimension;
readonly toLogical: typeof MultiArray.toLogical;
readonly getFields: typeof Structure.getFields;
readonly setNewField: typeof Structure.setNewField;
/**
* 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;
/**
* Evaluator object constructor
*/
constructor(config?: EvaluatorConfig);
/**
* Parse input string.
* @param input String to parse.
* @returns Abstract syntax tree of input.
*/
Parse(input: string): AST.NodeInput;
/**
* Load native name table in name table.
*/
private loadNativeTable;
/**
* 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: AST.NodeInput): AST.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: AST.NodeExpr, shallow: boolean, local: boolean, fname: string): {
id: string;
index: AST.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.
*/
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
*/
toBoolean(tree: AST.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: AST.NodeInput, local?: boolean, fname?: string): AST.NodeInput;
/**
* Evaluate expression `tree`.
* @param tree Expression to evaluate.
* @returns Expression `tree` evaluated.
*/
Evaluate(tree: AST.NodeInput): AST.NodeInput;
/**
* Unparse expression `tree`.
* @param tree Expression to unparse.
* @returns Expression `tree` unparsed.
*/
Unparse(tree: AST.NodeInput): string;
/**
* Unparse recursively expression tree generating MathML representation.
* @param tree Expression tree.
* @returns String of expression `tree` unparsed as MathML language.
*/
unparserMathML(tree: AST.NodeInput, parentPrecedence?: number): string;
/**
* Unparse Expression tree in MathML.
* @param tree Expression tree.
* @returns String of expression unparsed as MathML language.
*/
UnparseMathML(tree: AST.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 };
export default Evaluator;