mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
403 lines (402 loc) • 11.7 kB
TypeScript
import { CharString, StringQuoteCharacter } from './CharString';
import { ComplexType } from './Complex';
import { FunctionHandle } from './FunctionHandle';
import { type ElementType, MultiArray } from './MultiArray';
/**
* AST (Abstract Syntax Tree).
*/
/**
* Operator type.
*/
type OperatorType = '+' | '-' | '.*' | '*' | './' | '/' | '.\\' | '\\' | '.^' | '^' | '.**' | '**' | '<' | '<=' | '==' | '>=' | '>' | '!=' | '~=' | '&' | '|' | '&&' | '||' | '=' | '+=' | '-=' | '*=' | '/=' | '\\=' | '^=' | '**=' | '.*=' | './=' | '.\\=' | '.^=' | '.**=' | '&=' | '|=' | '()' | '!' | '~' | '+_' | '-_' | '++_' | '--_' | ".'" | "'" | '_++' | '_--';
/**
* NodeType
*/
type NodeType = 'IDENT' | 'CMDWLIST' | 'IDX' | 'RANGE' | 'ENDRANGE' | 'LIST' | '.' | ':' | '<~>' | 'RETLIST' | 'FCN' | 'GLOBAL' | 'PERSIST' | 'IF' | OperatorType;
/**
* Node base.
*/
interface NodeBase {
type: NodeType | number;
parent?: any;
index?: number;
omitOutput?: boolean;
omitAnswer?: boolean;
start?: {
line: number;
column: number;
};
stop?: {
line: number;
column: number;
};
}
type NodeInput = NodeExpr | NodeList | NodeDeclaration | NodeIf;
/**
* Expression node.
*/
type NodeExpr = ElementType | NodeIdentifier | NodeIndexExpr | NodeOperation | NodeRange | NodeIndirectRef | NodeReturnList | any;
/**
* Reserved node.
*/
interface NodeReserved extends NodeBase {
}
/**
* Literal node.
*/
interface NodeLiteral extends NodeBase {
}
/**
* Name node.
*/
interface NodeIdentifier extends NodeBase {
type: 'IDENT';
id: string;
}
/**
* Command word list node.
*/
interface NodeCmdWList extends NodeBase {
type: 'CMDWLIST';
id: string;
args: CharString[];
}
/**
* Expression and arguments node.
*/
interface NodeIndexExpr extends NodeBase {
type: 'IDX';
expr: NodeExpr;
args: NodeExpr[];
delim: '()' | '{}';
}
/**
* Range node.
*/
interface NodeRange extends NodeBase {
type: 'RANGE';
start_: NodeExpr | null;
stop_: NodeExpr | null;
stride_: NodeExpr | null;
}
/**
* Operation node.
*/
type NodeOperation = UnaryOperation | BinaryOperation;
/**
* Unary operation node.
*/
type UnaryOperation = UnaryOperationL | UnaryOperationR;
/**
* Right unary operation node.
*/
interface UnaryOperationR extends NodeBase {
right: NodeExpr;
}
/**
* Left unary operation node.
*/
interface UnaryOperationL extends NodeBase {
left: NodeExpr;
}
/**
* Binary operation.
*/
interface BinaryOperation extends NodeBase {
left: NodeExpr;
right: NodeExpr;
}
/**
* List node
*/
interface NodeList extends NodeBase {
type: 'LIST';
list: NodeInput[];
}
interface NodeIndirectRef extends NodeBase {
type: '.';
obj: NodeExpr;
field: (string | NodeExpr)[];
}
type ReturnHandlerResult = {
length: number;
} & Record<string, NodeExpr>;
type ReturnSelector = (evaluated: ReturnHandlerResult, index: number) => NodeExpr;
type ReturnHandler = (length: number) => ReturnHandlerResult;
/**
* Return list node
*/
interface NodeReturnList extends NodeBase {
type: 'RETLIST';
selector: ReturnSelector;
handler: ReturnHandler;
}
interface NodeFunction extends NodeBase {
type: 'FCN';
id: string;
return: NodeInput[];
parameter: NodeInput[];
arguments: NodeInput[];
statements: NodeInput[];
}
interface NodeArgumentValidation {
name: NodeIdentifier;
size: NodeInput[];
class: NodeIdentifier | null;
functions: NodeInput[];
default: NodeExpr;
}
interface NodeArguments {
attribute: NodeIdentifier | null;
validation: NodeArgumentValidation[];
}
interface NodeDeclaration extends NodeBase {
type: 'GLOBAL' | 'PERSIST';
list: NodeExpr[];
}
interface NodeIf extends NodeBase {
type: 'IF';
expression: NodeExpr[];
then: NodeList[];
else: NodeList | null;
}
interface NodeElseIf {
expression: NodeExpr;
then: NodeList;
}
interface NodeElse {
else: NodeList;
}
/**
* AST (Abstract Syntax Tree) node factory methods.
*/
declare abstract class AST {
/**
* External node factory methods.
*/
static nodeString: (str: string, quote?: StringQuoteCharacter) => CharString;
static nodeNumber: (value: string) => ComplexType;
static firstRow: (row: ElementType[], iscell?: boolean) => MultiArray;
static appendRow: (M: MultiArray, row: ElementType[]) => MultiArray;
static emptyArray: (iscell?: boolean | undefined) => MultiArray;
/**
* Reload external node factory methods.
*/
static readonly reload: () => void;
/**
* Node types that, by definition, should omit writing to the `ans`
* variable.
*/
static readonly omitAnswerTable: (NodeType | number)[];
/**
* Tests whether the node should omit writing to the `ans` variable.
* @param node
* @returns
*/
static readonly omitAnswer: (node: NodeInput) => boolean;
/**
* Create literal node.
* @param type
* @returns
*/
static readonly nodeLiteral: (type: NodeType) => NodeLiteral;
/**
* Create name node.
* @param nodeid
* @returns
*/
static readonly nodeIdentifier: (id: string) => NodeIdentifier;
/**
* Create command word list node.
* @param nodename
* @param nodelist
* @returns
*/
static readonly nodeCmdWList: (nodename: NodeIdentifier, nodelist: NodeList) => NodeCmdWList;
/**
* Create expression and arguments node.
* @param nodeexpr
* @param nodelist
* @returns
*/
static readonly nodeIndexExpr: (nodeexpr: NodeExpr, nodelist?: NodeList | null, delimiter?: "()" | "{}") => NodeIndexExpr;
/**
* Create range node.
* @param start_
* @param stop_
* @param stride_
* @returns NodeRange.
*/
static readonly nodeRange: (start_: NodeExpr, stop_: NodeExpr, stride_?: NodeExpr) => NodeRange;
/**
* Create operator node.
* @param op
* @param data1
* @param data2
* @returns
*/
static readonly nodeOp: (op: OperatorType, data1: NodeExpr, data2?: NodeExpr) => NodeOperation;
/**
* Create first element of list node.
* @param node First element of list node.
* @returns A NodeList.
*/
static readonly nodeListFirst: (node?: NodeInput) => NodeList;
/**
* Append node to list node.
* @param lnode NodeList.
* @param node Element to append to list.
* @returns NodeList with element appended.
*/
static readonly appendNodeList: (lnode: NodeList, node: NodeInput) => NodeList;
/**
*
* @param list
* @returns
*/
static readonly nodeList: (list: NodeInput[]) => NodeList;
/**
* Create first row of a MultiArray.
* @param row
* @returns
*/
static readonly nodeFirstRow: (row?: NodeList | null, iscell?: boolean) => MultiArray;
/**
* Append row to MultiArray.
* @param M
* @param row
* @returns
*/
static readonly nodeAppendRow: (M: MultiArray, row?: NodeList | null) => MultiArray;
/**
*
* @param left
* @param right
* @returns
*/
static readonly nodeIndirectRef: (left: NodeExpr, right: string | NodeExpr) => NodeIndirectRef;
/**
* Creates NodeReturnList (multiple assignment)
* @param selector Left side selector function.
* @returns Return list node.
*/
static readonly nodeReturnList: (selector: ReturnSelector, handler?: ReturnHandler) => NodeReturnList;
/**
* 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 readonly throwErrorIfGreaterThanReturnList: (maxLength: number, currentLength: number) => void | never;
/**
* 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.
*/
static readonly reduceToFirstIfReturnList: (tree: NodeInput) => NodeInput;
/**
* Throw invalid call error if (optional) test is true.
* @param name
*/
static readonly throwInvalidCallError: (name: string, test?: boolean) => void;
/**
*
* @param id
* @param parameter_list
* @param expression
* @returns
*/
static readonly nodeFunctionHandle: (id?: NodeIdentifier | null, parameter_list?: NodeList | null, expression?: NodeExpr) => FunctionHandle;
/**
*
* @param id
* @param return_list
* @param parameter_list
* @param arguments_list
* @param statements_list
* @returns
*/
static readonly nodeFunction: (id: NodeIdentifier, return_list: NodeList, parameter_list: NodeList, arguments_list: NodeList, statements_list: NodeList) => NodeFunction;
/**
*
* @param name
* @param size
* @param cl
* @param functions
* @param dflt
* @returns
*/
static readonly nodeArgumentValidation: (name: NodeIdentifier, size: NodeList, cl: (NodeIdentifier | null) | undefined, functions: NodeList, dflt?: NodeExpr) => NodeArgumentValidation;
/**
*
* @param attribute
* @param validationList
* @returns
*/
static readonly nodeArguments: (attribute: NodeIdentifier | null, validationList: NodeList) => NodeArguments;
/**
*
* @param type
* @returns
*/
static readonly nodeDeclarationFirst: (type: "GLOBAL" | "PERSIST") => NodeDeclaration;
/**
*
* @param node
* @param declaration
* @returns
*/
static readonly nodeAppendDeclaration: (node: NodeDeclaration, declaration: NodeExpr) => NodeDeclaration;
/**
*
* @param expression
* @param then
* @returns
*/
static readonly nodeIfBegin: (expression: NodeExpr, then: NodeList) => NodeIf;
/**
*
* @param nodeIf
* @param nodeElse
* @returns
*/
static readonly nodeIfAppendElse: (nodeIf: NodeIf, nodeElse: NodeElse) => NodeIf;
/**
*
* @param nodeIf
* @param nodeElseIf
* @returns
*/
static readonly nodeIfAppendElseIf: (nodeIf: NodeIf, nodeElseIf: NodeElseIf) => NodeIf;
/**
*
* @param expression
* @param then
* @returns
*/
static readonly nodeElseIf: (expression: NodeExpr, then: NodeList) => NodeElseIf;
/**
*
* @param elseStmt
* @returns
*/
static readonly nodeElse: (elseStmt: NodeList) => NodeElse;
}
export type { OperatorType, NodeBase, NodeInput, NodeExpr, NodeReserved, NodeLiteral, NodeIdentifier, NodeCmdWList, NodeIndexExpr, NodeRange, NodeOperation, UnaryOperation, UnaryOperationR, UnaryOperationL, BinaryOperation, NodeList, NodeIndirectRef, ReturnHandlerResult, ReturnHandler, ReturnSelector, NodeReturnList, NodeFunction, NodeArgumentValidation, NodeArguments, NodeDeclaration, NodeIf, NodeElseIf, NodeElse, };
export { AST };
declare const _default: {
AST: typeof AST;
};
export default _default;
/**
* External exports.
*/
export type { SingleQuoteCharacter, DoubleQuoteCharacter, StringQuoteCharacter } from './CharString';
export { singleQuoteCharacter, doubleQuoteCharacter, stringClass, CharString } from './CharString';
export type { Decimal, RealType, NumberObjectType, RealTypeDescriptor, ComplexType } from './Complex';
export { Complex } from './Complex';
export type { ElementType } from './MultiArray';
export { MultiArray } from './MultiArray';
export { Structure } from './Structure';
export { FunctionHandle } from './FunctionHandle';