UNPKG

mathjslab

Version:

MathJSLab - Interpreter with language syntax like MATLAB/Octave

159 lines (158 loc) 5.05 kB
/** * MATLAB®/Octave like syntax parser/interpreter/compiler. */ import { CharString } from './char-string'; import { ComplexDecimal } from './complex-decimal'; import { MultiArray } from './multi-array'; /** * baseFunctionTable type */ export type TBaseFunctionTableEntry = { mapper?: boolean; ev?: Array<boolean>; func: Function; }; export type TBaseFunctionTable = { [k: string]: TBaseFunctionTableEntry; }; /** * TEvaluatorConfig type */ type TAliasNameTable = Record<string, RegExp>; export type TEvaluatorConfig = { aliasTable?: TAliasNameTable; externalFuctionTable?: TBaseFunctionTable; }; /** * Abstract Syntax Tree nodes */ export type NodeExpr = NodeName | NodeArgExpr | NodeOperation | NodeList; interface NodeRange { start: NodeExpr | null; stop: NodeExpr | null; stride: NodeExpr | null; } interface PrimaryNode { type: string | number; } interface NodeReserved extends PrimaryNode { } export interface NodeName extends PrimaryNode { type: 'NAME'; id: string; } interface NodeCmdWList extends PrimaryNode { type: 'CmdWList'; id: string; args: Array<CharString>; } export interface NodeArgExpr extends PrimaryNode { type: 'ARG'; expr: NodeExpr; args: Array<NodeExpr>; } export type NodeOperation = UnaryOperation | BinaryOperation; type UnaryOperation = UnaryOperationL | UnaryOperationR; interface UnaryOperationR extends PrimaryNode { right: NodeExpr; } interface UnaryOperationL extends PrimaryNode { left: NodeExpr; } interface BinaryOperation extends PrimaryNode { left: NodeExpr; right: NodeExpr; } export interface NodeList { list: Array<NodeExpr>; } /** * External parser declarations (defined in parser body) */ declare global { var EvaluatorPointer: any; const commandsTable: string[]; } /** * Evaluator object * It is implemented as a class but cannot be instantiated more than one time * simultaneously. */ export declare class Evaluator { /** * After run Parser or Evaluate method, the exitStatus property will contains * exit state of method. */ static response: { INFO: number; WARNING: number; OK: number; LEX_ERROR: number; PARSER_ERROR: number; EVAL_ERROR: number; }; debug: boolean; private readonly nativeNameTable; private nameTable; private aliasTable; baseFunctionTable: TBaseFunctionTable; localTable: { [k: string]: any; }; private readonly parser; exitStatus: number; exitMessage: string; private readonly opTable; readonly nodeString: typeof CharString.parse; readonly isString: typeof CharString.isThis; readonly unparseString: typeof CharString.unparse; readonly unparseStringML: typeof CharString.unparseML; readonly removeQuotes: typeof CharString.removeQuotes; readonly nodeNumber: typeof ComplexDecimal.parse; readonly newNumber: typeof ComplexDecimal.newThis; readonly isNumber: typeof ComplexDecimal.isThis; readonly unparseNumber: typeof ComplexDecimal.unparse; readonly unparseNumberML: typeof ComplexDecimal.unparseML; readonly isTensor: typeof MultiArray.isThis; readonly isRange: typeof MultiArray.isRange; readonly unparseTensor: typeof MultiArray.unparse; readonly unparseTensorML: typeof MultiArray.unparseML; readonly evaluateTensor: typeof MultiArray.evaluate; readonly mapTensor: typeof MultiArray.map; readonly subTensor: typeof MultiArray.subMatrix; readonly expandRange: typeof MultiArray.expandRange; readonly firstRow: typeof MultiArray.firstRow; readonly appendRow: typeof MultiArray.appendRow; readonly tensor0x0: typeof MultiArray.mat_0x0; /** * Evaluator object constructor * @param config Evaluator configuration */ constructor(config?: TEvaluatorConfig); set(config?: TEvaluatorConfig): void; aliasName: (name: string) => string; ReloadNativeTable(): void; Restart(): void; Parse(input: string): any; nodeReserved(nodeid: string): NodeReserved; nodeName(nodeid: string): NodeName; nodeCmdWList(nodename: NodeName, nodelist: NodeList): NodeCmdWList; nodeArgExpr(nodeexpr: any, nodelist?: any): NodeArgExpr; nodeRange(left: any, ...right: any): NodeRange; nodeOp(op: string, data1: any, data2: any): NodeOperation; nodeListFirst(node?: any): NodeList; nodeList(lnode: any, node: any): NodeList; nodeFirstRow(row: any): MultiArray; nodeAppendRow(matrix: any, row: any): MultiArray; validateAssignment(tree: any): any; DefUnOpFunction(name: string, func: Function): void; DefBinOpFunction(name: string, func: Function): void; DefBinMoreOpFunction(name: string, func: Function): void; defFunction(name: string, func: Function, map?: boolean): void; Unparse(tree: any): string; Evaluator(tree: any, local: boolean, fname: string): any; Evaluate(tree: any): any; unparserML(tree: any): string; UnparseML(tree: any): string; } export {};