Comparing version 1.3.4 to 1.4.0
@@ -18,3 +18,3 @@ module.exports = { | ||
}, | ||
ignorePatterns: ['.eslintrc.js', 'lib/**', 'src/generated/**'], | ||
ignorePatterns: ['.eslintrc.js', 'lib/**', 'src/MathJSLabLexer.ts', 'src/MathJSLabParser.ts'], | ||
rules: { | ||
@@ -21,0 +21,0 @@ '@typescript-eslint/interface-name-prefix': 'off', |
@@ -5,4 +5,11 @@ # Release notes | ||
## 1.4.0 | ||
- Bug fix in parser (pre-increment and element-by-element operations). | ||
- Rules to functions definition and handlers in parser and AST implemented. Evaluation not yet implemented. | ||
- The string 'arguments' to define arguments block in functions is defined as keyword in lexer. | ||
- 'Parser.ts' file removed and Parser implemented as a method of `Evaluator`. The method `Evaluator.initialize` has been removed and initialization actions moved to `Evaluator` constructor. Now the `Evaluator` class can be instantiated more than one time. | ||
## 1.3.4 | ||
- More strong type definitions in 'MultiArray.ts', 'Evaluator.ts' and 'MathOperation.ts' files. | ||
- Global variable EvaluatorPointer removed. Evaluator instance reference passed in method parameters. | ||
@@ -14,4 +21,4 @@ ## 1.3.3 | ||
## 1.3.2 | ||
- Tests for types implemented as 'instanceof' in 'MathObject.ts' and 'Evaluator.ts'. Method 'isThis' removed from classes. | ||
- Some bug fix ins evaluator (not operation). | ||
- Tests for types implemented as `instanceof` in 'MathObject.ts' and 'Evaluator.ts'. Method 'isThis' removed from classes. | ||
- Some bug fix in evaluator (not operation). | ||
@@ -24,3 +31,3 @@ ## 1.3.1 | ||
## 1.3.0 | ||
- Parser implemented using ANTLR in files MathJSLabLexer.g4 and MathJSLabParser.g4. The wrapper class for lexer and parser has been created in file Parser.ts. Need to make extensive tests. | ||
- Parser implemented using ANTLR in files 'MathJSLabLexer.g4' and 'MathJSLabParser.g4'. The wrapper class for lexer and parser has been created in file Parser.ts. Need to make extensive tests. | ||
- File names converted to camel case. | ||
@@ -27,0 +34,0 @@ - The file AST.ts (Abstract Syntax Tree) has been created, and related types and interfaces defined in Evaluator.ts has been moved to AST.ts file. |
@@ -108,3 +108,3 @@ import { CharString } from './CharString'; | ||
type: 'LIST'; | ||
list: Array<NodeExpr>; | ||
list: NodeInput[]; | ||
} | ||
@@ -119,2 +119,38 @@ export type ReturnSelector = (length: number, index: number) => any; | ||
} | ||
export interface NodeFunctionHandle extends PrimaryNode { | ||
type: 'FCNHANDLE'; | ||
id: string | null; | ||
parameter: NodeInput[]; | ||
expression: NodeExpr; | ||
} | ||
export interface NodeFunction extends PrimaryNode { | ||
type: 'FCN'; | ||
id: string; | ||
return: NodeInput[]; | ||
parameter: NodeInput[]; | ||
arguments: NodeInput[]; | ||
statements: NodeInput[]; | ||
} | ||
export interface NodeArgumentValidation { | ||
name: NodeIdentifier; | ||
size: NodeInput[]; | ||
class: NodeIdentifier | null; | ||
functions: NodeInput[]; | ||
default: NodeExpr; | ||
} | ||
export interface NodeArguments extends PrimaryNode { | ||
type: 'ARGUMENTS'; | ||
attribute: NodeIdentifier | null; | ||
validation: { | ||
name: NodeIdentifier; | ||
size: NodeInput[]; | ||
class: NodeIdentifier | null; | ||
functions: NodeInput[]; | ||
default: NodeExpr; | ||
}[]; | ||
} | ||
export interface NodeDeclaration extends PrimaryNode { | ||
type: 'GLOBAL' | 'PERSISTENT'; | ||
list: NodeExpr[]; | ||
} | ||
export interface NodeIf extends PrimaryNode { | ||
@@ -176,9 +212,9 @@ type: 'IF'; | ||
/** | ||
* Create range node. If two arguments are passed then it is 'start' and | ||
* 'stop' of range. If three arguments are passed then it is 'start', | ||
* 'stride' and 'stop'. | ||
* @param args 'start' and 'stop' or 'start', 'stride' and 'stop'. | ||
* Create range node. | ||
* @param start | ||
* @param stop | ||
* @param stride | ||
* @returns NodeRange. | ||
*/ | ||
export declare const nodeRange: (...args: any) => NodeRange; | ||
export declare const nodeRange: (start: any, stop: any, stride?: any) => NodeRange; | ||
/** | ||
@@ -225,2 +261,9 @@ * Create operator node. | ||
export declare const nodeReturnList: (selector: ReturnSelector) => NodeReturnList; | ||
export declare const nodeFunctionHandle: (id?: NodeIdentifier | null, parameter_list?: NodeList | null, expression?: NodeExpr) => NodeFunctionHandle; | ||
export declare const nodeFunction: (id: NodeIdentifier, return_list: NodeList, parameter_list: NodeList, arguments_list: NodeList, statements_list: NodeList) => NodeFunction; | ||
export declare const nodeArgumentValidation: (name: NodeIdentifier, size: NodeList, cl: NodeIdentifier | null | undefined, functions: NodeList, dflt?: NodeExpr) => NodeArgumentValidation; | ||
export declare const nodeArguments: (attribute: NodeIdentifier | null, validationList: NodeList) => NodeArguments; | ||
export declare const nodeGlobal: () => NodeDeclaration; | ||
export declare const nodePersistent: () => NodeDeclaration; | ||
export declare const nodeAppendDeclaration: (node: NodeDeclaration, declaration: NodeExpr) => NodeDeclaration; | ||
export declare const nodeIfBegin: (expression: any, then: NodeList) => NodeIf; | ||
@@ -227,0 +270,0 @@ export declare const nodeIfAppendElse: (nodeIf: NodeIf, nodeElse: NodeElse) => NodeIf; |
/** | ||
* MATLAB®/Octave like syntax parser/interpreter/compiler. | ||
*/ | ||
import * as AST from './AST'; | ||
import { CharString } from './CharString'; | ||
@@ -8,4 +9,2 @@ import { ComplexDecimal } from './ComplexDecimal'; | ||
import { MathObject } from './MathOperation'; | ||
import * as AST from './AST'; | ||
import { Parser } from './Parser'; | ||
/** | ||
@@ -29,3 +28,3 @@ * aliasNameTable type. | ||
export type TNameTableEntry = { | ||
args: Array<AST.NodeIdentifier>; | ||
args: AST.NodeIdentifier[]; | ||
expr: AST.NodeExpr; | ||
@@ -52,16 +51,7 @@ }; | ||
/** | ||
* External parser declarations (defined in parser body) | ||
*/ | ||
declare global { | ||
var EvaluatorPointer: Evaluator; | ||
} | ||
/** | ||
* Evaluator object. | ||
* It is implemented as a class but cannot be instantiated more than one time | ||
* simultaneously. Instance is given by `Evaluator.initialize` static method | ||
* or global variable global.EvaluatorPointer. | ||
*/ | ||
export declare class Evaluator { | ||
/** | ||
* After run Parser or Evaluate method, the exitStatus property will contains | ||
* After run Evaluate method, the exitStatus property will contains | ||
* exit state of method. | ||
@@ -156,4 +146,2 @@ */ | ||
readonly toLogical: typeof MultiArray.toLogical; | ||
readonly parser: Parser; | ||
readonly Parse: (input: string) => any; | ||
/** | ||
@@ -164,17 +152,17 @@ * Special functions MathML unparser. | ||
/** | ||
* Evaluator object constructor | ||
* Alias name function. This property is set at Evaluator instantiation. | ||
* @param name Alias name. | ||
* @returns Canonical name. | ||
*/ | ||
private constructor(); | ||
aliasName: (name: string) => string; | ||
/** | ||
* Evaluator initialization. | ||
* @param config Evaluator configuration. | ||
* @returns Evaluator instance. | ||
* Evaluator object constructor | ||
*/ | ||
static initialize(config?: TEvaluatorConfig): Evaluator; | ||
constructor(config?: TEvaluatorConfig); | ||
/** | ||
* Alias name function. This property is set when running Evaluator.initialize. | ||
* @param name Alias name. | ||
* @returns Canonical name. | ||
* Parse input string. | ||
* @param input String to parse. | ||
* @returns Abstract syntax tree of input. | ||
*/ | ||
aliasName: (name: string) => string; | ||
Parse(input: string): AST.NodeInput; | ||
/** | ||
@@ -181,0 +169,0 @@ * Load native name table in name table. |
import { ErrorListener, RecognitionException, Recognizer } from 'antlr4'; | ||
export default class LexerErrorListener extends ErrorListener<number> { | ||
private errors; | ||
syntaxError(recognizer: Recognizer<number>, offendingSymbol: number, line: number, column: number, msg: string, e: RecognitionException | undefined): void; | ||
} |
@@ -0,12 +1,11 @@ | ||
export { Decimal } from 'decimal.js'; | ||
export { CharString } from './CharString'; | ||
export { Decimal } from 'decimal.js'; | ||
export { ComplexDecimal } from './ComplexDecimal'; | ||
export { MultiArray } from './MultiArray'; | ||
export { CoreFunctions } from './CoreFunctions'; | ||
export { LinearAlgebra } from './LinearAlgebra'; | ||
export { MathOperation } from './MathOperation'; | ||
export { CoreFunctions } from './CoreFunctions'; | ||
export { Configuration } from './Configuration'; | ||
export { SymbolTable } from './SymbolTable'; | ||
export * as AST from './AST'; | ||
export { Parser } from './Parser'; | ||
export * from './Evaluator'; |
@@ -46,81 +46,84 @@ import { ATN, CharStream, DFA, Lexer, RuleContext } from "antlr4"; | ||
static readonly STRING = 43; | ||
static readonly PLUS = 44; | ||
static readonly MINUS = 45; | ||
static readonly MUL = 46; | ||
static readonly DIV = 47; | ||
static readonly EQ = 48; | ||
static readonly COLON = 49; | ||
static readonly SEMICOLON = 50; | ||
static readonly COMMA = 51; | ||
static readonly TILDE = 52; | ||
static readonly EXCLAMATION = 53; | ||
static readonly COMMAT = 54; | ||
static readonly LPAREN = 55; | ||
static readonly RPAREN = 56; | ||
static readonly LBRACKET = 57; | ||
static readonly RBRACKET = 58; | ||
static readonly LCURLYBR = 59; | ||
static readonly RCURLYBR = 60; | ||
static readonly LEFTDIV = 61; | ||
static readonly ADD_EQ = 62; | ||
static readonly SUB_EQ = 63; | ||
static readonly MUL_EQ = 64; | ||
static readonly DIV_EQ = 65; | ||
static readonly LEFTDIV_EQ = 66; | ||
static readonly POW_EQ = 67; | ||
static readonly EMUL_EQ = 68; | ||
static readonly EDIV_EQ = 69; | ||
static readonly ELEFTDIV_EQ = 70; | ||
static readonly EPOW_EQ = 71; | ||
static readonly AND_EQ = 72; | ||
static readonly OR_EQ = 73; | ||
static readonly EXPR_AND_AND = 74; | ||
static readonly EXPR_OR_OR = 75; | ||
static readonly EXPR_AND = 76; | ||
static readonly EXPR_OR = 77; | ||
static readonly EXPR_LT = 78; | ||
static readonly EXPR_LE = 79; | ||
static readonly EXPR_EQ = 80; | ||
static readonly EXPR_NE = 81; | ||
static readonly EXPR_GE = 82; | ||
static readonly EXPR_GT = 83; | ||
static readonly EMUL = 84; | ||
static readonly EDIV = 85; | ||
static readonly ELEFTDIV = 86; | ||
static readonly PLUS_PLUS = 87; | ||
static readonly MINUS_MINUS = 88; | ||
static readonly POW = 89; | ||
static readonly EPOW = 90; | ||
static readonly TRANSPOSE = 91; | ||
static readonly HERMITIAN = 92; | ||
static readonly DQSTRING = 93; | ||
static readonly IDENTIFIER = 94; | ||
static readonly DECIMAL_NUMBER = 95; | ||
static readonly SPACE_OR_CONTINUATION = 96; | ||
static readonly NEWLINE = 97; | ||
static readonly BLOCK_COMMENT_START = 98; | ||
static readonly COMMENT_LINE = 99; | ||
static readonly INVALID = 100; | ||
static readonly SINGLEQ_STRING = 101; | ||
static readonly SINGLEQ_NL = 102; | ||
static readonly SINGLEQ_SINGLEQ = 103; | ||
static readonly SINGLEQ_END = 104; | ||
static readonly DOUBLEQ_STRING = 105; | ||
static readonly DOUBLEQ_NL = 106; | ||
static readonly DOUBLEQ_DOUBLEQ = 107; | ||
static readonly DOUBLEQ_ESCAPE = 108; | ||
static readonly DOUBLEQ_ESCAPE_OTHER = 109; | ||
static readonly DOUBLEQ_ESCAPE_OCT = 110; | ||
static readonly DOUBLEQ_ESCAPE_HEX = 111; | ||
static readonly DOUBLEQ_ESCAPE_UNICODE = 112; | ||
static readonly DOUBLEQ_END = 113; | ||
static readonly BLOCK_COMMENT_START_AGAIN = 114; | ||
static readonly BLOCK_COMMENT_END = 115; | ||
static readonly BLOCK_COMMENT_LINE = 116; | ||
static readonly BLOCK_COMMENT_EOF = 117; | ||
static readonly SKIP_SPACE = 118; | ||
static readonly SKIP_COMMENT_LINE = 119; | ||
static readonly EXIT_AT_NEWLINE = 120; | ||
static readonly EXIT_AT_EOF = 121; | ||
static readonly UNQUOTED_STRING = 122; | ||
static readonly ARGUMENTS = 44; | ||
static readonly PLUS = 45; | ||
static readonly MINUS = 46; | ||
static readonly MUL = 47; | ||
static readonly DIV = 48; | ||
static readonly EQ = 49; | ||
static readonly COLON = 50; | ||
static readonly SEMICOLON = 51; | ||
static readonly COMMA = 52; | ||
static readonly DOT = 53; | ||
static readonly TILDE = 54; | ||
static readonly EXCLAMATION = 55; | ||
static readonly COMMAT = 56; | ||
static readonly LPAREN = 57; | ||
static readonly RPAREN = 58; | ||
static readonly LBRACKET = 59; | ||
static readonly RBRACKET = 60; | ||
static readonly LCURLYBR = 61; | ||
static readonly RCURLYBR = 62; | ||
static readonly LEFTDIV = 63; | ||
static readonly ADD_EQ = 64; | ||
static readonly SUB_EQ = 65; | ||
static readonly MUL_EQ = 66; | ||
static readonly DIV_EQ = 67; | ||
static readonly LEFTDIV_EQ = 68; | ||
static readonly POW_EQ = 69; | ||
static readonly EMUL_EQ = 70; | ||
static readonly EDIV_EQ = 71; | ||
static readonly ELEFTDIV_EQ = 72; | ||
static readonly EPOW_EQ = 73; | ||
static readonly AND_EQ = 74; | ||
static readonly OR_EQ = 75; | ||
static readonly EXPR_AND_AND = 76; | ||
static readonly EXPR_OR_OR = 77; | ||
static readonly EXPR_AND = 78; | ||
static readonly EXPR_OR = 79; | ||
static readonly EXPR_LT = 80; | ||
static readonly EXPR_LE = 81; | ||
static readonly EXPR_EQ = 82; | ||
static readonly EXPR_NE = 83; | ||
static readonly EXPR_GE = 84; | ||
static readonly EXPR_GT = 85; | ||
static readonly EMUL = 86; | ||
static readonly EDIV = 87; | ||
static readonly ELEFTDIV = 88; | ||
static readonly PLUS_PLUS = 89; | ||
static readonly MINUS_MINUS = 90; | ||
static readonly POW = 91; | ||
static readonly EPOW = 92; | ||
static readonly TRANSPOSE = 93; | ||
static readonly HERMITIAN = 94; | ||
static readonly DQSTRING = 95; | ||
static readonly IDENTIFIER = 96; | ||
static readonly DECIMAL_NUMBER = 97; | ||
static readonly DECIMAL_DOT_OP = 98; | ||
static readonly SPACE_OR_CONTINUATION = 99; | ||
static readonly NEWLINE = 100; | ||
static readonly BLOCK_COMMENT_START = 101; | ||
static readonly COMMENT_LINE = 102; | ||
static readonly INVALID = 103; | ||
static readonly SINGLEQ_STRING = 104; | ||
static readonly SINGLEQ_NL = 105; | ||
static readonly SINGLEQ_SINGLEQ = 106; | ||
static readonly SINGLEQ_END = 107; | ||
static readonly DOUBLEQ_STRING = 108; | ||
static readonly DOUBLEQ_NL = 109; | ||
static readonly DOUBLEQ_DOUBLEQ = 110; | ||
static readonly DOUBLEQ_ESCAPE = 111; | ||
static readonly DOUBLEQ_ESCAPE_OTHER = 112; | ||
static readonly DOUBLEQ_ESCAPE_OCT = 113; | ||
static readonly DOUBLEQ_ESCAPE_HEX = 114; | ||
static readonly DOUBLEQ_ESCAPE_UNICODE = 115; | ||
static readonly DOUBLEQ_END = 116; | ||
static readonly BLOCK_COMMENT_START_AGAIN = 117; | ||
static readonly BLOCK_COMMENT_END = 118; | ||
static readonly BLOCK_COMMENT_LINE = 119; | ||
static readonly BLOCK_COMMENT_EOF = 120; | ||
static readonly SKIP_SPACE = 121; | ||
static readonly SKIP_COMMENT_LINE = 122; | ||
static readonly EXIT_AT_NEWLINE = 123; | ||
static readonly EXIT_AT_EOF = 124; | ||
static readonly UNQUOTED_STRING = 125; | ||
static readonly EOF: number; | ||
@@ -172,2 +175,3 @@ static readonly SQ_STRING = 1; | ||
private COMMA_action; | ||
private DOT_action; | ||
private TILDE_action; | ||
@@ -217,2 +221,3 @@ private EXCLAMATION_action; | ||
private DECIMAL_NUMBER_action; | ||
private DECIMAL_DOT_OP_action; | ||
private SPACE_OR_CONTINUATION_action; | ||
@@ -238,3 +243,2 @@ private NEWLINE_action; | ||
private BLOCK_COMMENT_EOF_action; | ||
private SKIP_COMMENT_LINE_action; | ||
private EXIT_AT_NEWLINE_action; | ||
@@ -241,0 +245,0 @@ private EXIT_AT_EOF_action; |
@@ -55,81 +55,84 @@ import { ATN, DFA, FailedPredicateException, Parser, RuleContext, ParserRuleContext, TerminalNode, Token, TokenStream } from 'antlr4'; | ||
static readonly STRING = 43; | ||
static readonly PLUS = 44; | ||
static readonly MINUS = 45; | ||
static readonly MUL = 46; | ||
static readonly DIV = 47; | ||
static readonly EQ = 48; | ||
static readonly COLON = 49; | ||
static readonly SEMICOLON = 50; | ||
static readonly COMMA = 51; | ||
static readonly TILDE = 52; | ||
static readonly EXCLAMATION = 53; | ||
static readonly COMMAT = 54; | ||
static readonly LPAREN = 55; | ||
static readonly RPAREN = 56; | ||
static readonly LBRACKET = 57; | ||
static readonly RBRACKET = 58; | ||
static readonly LCURLYBR = 59; | ||
static readonly RCURLYBR = 60; | ||
static readonly LEFTDIV = 61; | ||
static readonly ADD_EQ = 62; | ||
static readonly SUB_EQ = 63; | ||
static readonly MUL_EQ = 64; | ||
static readonly DIV_EQ = 65; | ||
static readonly LEFTDIV_EQ = 66; | ||
static readonly POW_EQ = 67; | ||
static readonly EMUL_EQ = 68; | ||
static readonly EDIV_EQ = 69; | ||
static readonly ELEFTDIV_EQ = 70; | ||
static readonly EPOW_EQ = 71; | ||
static readonly AND_EQ = 72; | ||
static readonly OR_EQ = 73; | ||
static readonly EXPR_AND_AND = 74; | ||
static readonly EXPR_OR_OR = 75; | ||
static readonly EXPR_AND = 76; | ||
static readonly EXPR_OR = 77; | ||
static readonly EXPR_LT = 78; | ||
static readonly EXPR_LE = 79; | ||
static readonly EXPR_EQ = 80; | ||
static readonly EXPR_NE = 81; | ||
static readonly EXPR_GE = 82; | ||
static readonly EXPR_GT = 83; | ||
static readonly EMUL = 84; | ||
static readonly EDIV = 85; | ||
static readonly ELEFTDIV = 86; | ||
static readonly PLUS_PLUS = 87; | ||
static readonly MINUS_MINUS = 88; | ||
static readonly POW = 89; | ||
static readonly EPOW = 90; | ||
static readonly TRANSPOSE = 91; | ||
static readonly HERMITIAN = 92; | ||
static readonly DQSTRING = 93; | ||
static readonly IDENTIFIER = 94; | ||
static readonly DECIMAL_NUMBER = 95; | ||
static readonly SPACE_OR_CONTINUATION = 96; | ||
static readonly NEWLINE = 97; | ||
static readonly BLOCK_COMMENT_START = 98; | ||
static readonly COMMENT_LINE = 99; | ||
static readonly INVALID = 100; | ||
static readonly SINGLEQ_STRING = 101; | ||
static readonly SINGLEQ_NL = 102; | ||
static readonly SINGLEQ_SINGLEQ = 103; | ||
static readonly SINGLEQ_END = 104; | ||
static readonly DOUBLEQ_STRING = 105; | ||
static readonly DOUBLEQ_NL = 106; | ||
static readonly DOUBLEQ_DOUBLEQ = 107; | ||
static readonly DOUBLEQ_ESCAPE = 108; | ||
static readonly DOUBLEQ_ESCAPE_OTHER = 109; | ||
static readonly DOUBLEQ_ESCAPE_OCT = 110; | ||
static readonly DOUBLEQ_ESCAPE_HEX = 111; | ||
static readonly DOUBLEQ_ESCAPE_UNICODE = 112; | ||
static readonly DOUBLEQ_END = 113; | ||
static readonly BLOCK_COMMENT_START_AGAIN = 114; | ||
static readonly BLOCK_COMMENT_END = 115; | ||
static readonly BLOCK_COMMENT_LINE = 116; | ||
static readonly BLOCK_COMMENT_EOF = 117; | ||
static readonly SKIP_SPACE = 118; | ||
static readonly SKIP_COMMENT_LINE = 119; | ||
static readonly EXIT_AT_NEWLINE = 120; | ||
static readonly EXIT_AT_EOF = 121; | ||
static readonly UNQUOTED_STRING = 122; | ||
static readonly ARGUMENTS = 44; | ||
static readonly PLUS = 45; | ||
static readonly MINUS = 46; | ||
static readonly MUL = 47; | ||
static readonly DIV = 48; | ||
static readonly EQ = 49; | ||
static readonly COLON = 50; | ||
static readonly SEMICOLON = 51; | ||
static readonly COMMA = 52; | ||
static readonly DOT = 53; | ||
static readonly TILDE = 54; | ||
static readonly EXCLAMATION = 55; | ||
static readonly COMMAT = 56; | ||
static readonly LPAREN = 57; | ||
static readonly RPAREN = 58; | ||
static readonly LBRACKET = 59; | ||
static readonly RBRACKET = 60; | ||
static readonly LCURLYBR = 61; | ||
static readonly RCURLYBR = 62; | ||
static readonly LEFTDIV = 63; | ||
static readonly ADD_EQ = 64; | ||
static readonly SUB_EQ = 65; | ||
static readonly MUL_EQ = 66; | ||
static readonly DIV_EQ = 67; | ||
static readonly LEFTDIV_EQ = 68; | ||
static readonly POW_EQ = 69; | ||
static readonly EMUL_EQ = 70; | ||
static readonly EDIV_EQ = 71; | ||
static readonly ELEFTDIV_EQ = 72; | ||
static readonly EPOW_EQ = 73; | ||
static readonly AND_EQ = 74; | ||
static readonly OR_EQ = 75; | ||
static readonly EXPR_AND_AND = 76; | ||
static readonly EXPR_OR_OR = 77; | ||
static readonly EXPR_AND = 78; | ||
static readonly EXPR_OR = 79; | ||
static readonly EXPR_LT = 80; | ||
static readonly EXPR_LE = 81; | ||
static readonly EXPR_EQ = 82; | ||
static readonly EXPR_NE = 83; | ||
static readonly EXPR_GE = 84; | ||
static readonly EXPR_GT = 85; | ||
static readonly EMUL = 86; | ||
static readonly EDIV = 87; | ||
static readonly ELEFTDIV = 88; | ||
static readonly PLUS_PLUS = 89; | ||
static readonly MINUS_MINUS = 90; | ||
static readonly POW = 91; | ||
static readonly EPOW = 92; | ||
static readonly TRANSPOSE = 93; | ||
static readonly HERMITIAN = 94; | ||
static readonly DQSTRING = 95; | ||
static readonly IDENTIFIER = 96; | ||
static readonly DECIMAL_NUMBER = 97; | ||
static readonly DECIMAL_DOT_OP = 98; | ||
static readonly SPACE_OR_CONTINUATION = 99; | ||
static readonly NEWLINE = 100; | ||
static readonly BLOCK_COMMENT_START = 101; | ||
static readonly COMMENT_LINE = 102; | ||
static readonly INVALID = 103; | ||
static readonly SINGLEQ_STRING = 104; | ||
static readonly SINGLEQ_NL = 105; | ||
static readonly SINGLEQ_SINGLEQ = 106; | ||
static readonly SINGLEQ_END = 107; | ||
static readonly DOUBLEQ_STRING = 108; | ||
static readonly DOUBLEQ_NL = 109; | ||
static readonly DOUBLEQ_DOUBLEQ = 110; | ||
static readonly DOUBLEQ_ESCAPE = 111; | ||
static readonly DOUBLEQ_ESCAPE_OTHER = 112; | ||
static readonly DOUBLEQ_ESCAPE_OCT = 113; | ||
static readonly DOUBLEQ_ESCAPE_HEX = 114; | ||
static readonly DOUBLEQ_ESCAPE_UNICODE = 115; | ||
static readonly DOUBLEQ_END = 116; | ||
static readonly BLOCK_COMMENT_START_AGAIN = 117; | ||
static readonly BLOCK_COMMENT_END = 118; | ||
static readonly BLOCK_COMMENT_LINE = 119; | ||
static readonly BLOCK_COMMENT_EOF = 120; | ||
static readonly SKIP_SPACE = 121; | ||
static readonly SKIP_COMMENT_LINE = 122; | ||
static readonly EXIT_AT_NEWLINE = 123; | ||
static readonly EXIT_AT_EOF = 124; | ||
static readonly UNQUOTED_STRING = 125; | ||
static readonly EOF: number; | ||
@@ -148,20 +151,32 @@ static readonly RULE_input = 0; | ||
static readonly RULE_matrix_row = 11; | ||
static readonly RULE_primary_expr = 12; | ||
static readonly RULE_magic_colon = 13; | ||
static readonly RULE_magic_tilde = 14; | ||
static readonly RULE_list_element = 15; | ||
static readonly RULE_arg_list = 16; | ||
static readonly RULE_oper_expr = 17; | ||
static readonly RULE_power_expr = 18; | ||
static readonly RULE_colon_expr = 19; | ||
static readonly RULE_simple_expr = 20; | ||
static readonly RULE_expression = 21; | ||
static readonly RULE_command = 22; | ||
static readonly RULE_select_command = 23; | ||
static readonly RULE_if_command = 24; | ||
static readonly RULE_elseif_clause = 25; | ||
static readonly RULE_else_clause = 26; | ||
static readonly RULE_sep_no_nl = 27; | ||
static readonly RULE_nl = 28; | ||
static readonly RULE_sep = 29; | ||
static readonly RULE_fcn_handle = 12; | ||
static readonly RULE_anon_fcn_handle = 13; | ||
static readonly RULE_primary_expr = 14; | ||
static readonly RULE_magic_colon = 15; | ||
static readonly RULE_magic_tilde = 16; | ||
static readonly RULE_list_element = 17; | ||
static readonly RULE_arg_list = 18; | ||
static readonly RULE_oper_expr = 19; | ||
static readonly RULE_power_expr = 20; | ||
static readonly RULE_colon_expr = 21; | ||
static readonly RULE_simple_expr = 22; | ||
static readonly RULE_expression = 23; | ||
static readonly RULE_command = 24; | ||
static readonly RULE_declaration = 25; | ||
static readonly RULE_decl_elt = 26; | ||
static readonly RULE_select_command = 27; | ||
static readonly RULE_if_command = 28; | ||
static readonly RULE_elseif_clause = 29; | ||
static readonly RULE_else_clause = 30; | ||
static readonly RULE_param_list = 31; | ||
static readonly RULE_param_list_elt = 32; | ||
static readonly RULE_return_list = 33; | ||
static readonly RULE_function = 34; | ||
static readonly RULE_arguments_block_list = 35; | ||
static readonly RULE_arguments_block = 36; | ||
static readonly RULE_args_validation_list = 37; | ||
static readonly RULE_arg_validation = 38; | ||
static readonly RULE_sep_no_nl = 39; | ||
static readonly RULE_nl = 40; | ||
static readonly RULE_sep = 41; | ||
static readonly literalNames: (string | null)[]; | ||
@@ -189,2 +204,4 @@ static readonly symbolicNames: (string | null)[]; | ||
matrix_row(): Matrix_rowContext; | ||
fcn_handle(): Fcn_handleContext; | ||
anon_fcn_handle(): Anon_fcn_handleContext; | ||
primary_expr(): Primary_exprContext; | ||
@@ -204,2 +221,4 @@ magic_colon(): Magic_colonContext; | ||
command(): CommandContext; | ||
declaration(): DeclarationContext; | ||
decl_elt(): Decl_eltContext; | ||
select_command(): Select_commandContext; | ||
@@ -209,2 +228,10 @@ if_command(): If_commandContext; | ||
else_clause(): Else_clauseContext; | ||
param_list(): Param_listContext; | ||
param_list_elt(): Param_list_eltContext; | ||
return_list(): Return_listContext; | ||
function_(): FunctionContext; | ||
arguments_block_list(): Arguments_block_listContext; | ||
arguments_block(): Arguments_blockContext; | ||
args_validation_list(): Args_validation_listContext; | ||
arg_validation(): Arg_validationContext; | ||
sep_no_nl(): Sep_no_nlContext; | ||
@@ -328,2 +355,17 @@ nl(): NlContext; | ||
} | ||
export declare class Fcn_handleContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
EXCLAMATION(): TerminalNode; | ||
identifier(): IdentifierContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Anon_fcn_handleContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
EXCLAMATION(): TerminalNode; | ||
param_list(): Param_listContext; | ||
expression(): ExpressionContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Primary_exprContext extends ParserRuleContext { | ||
@@ -334,2 +376,3 @@ node: AST.NodeInput; | ||
constant(): ConstantContext; | ||
fcn_handle(): Fcn_handleContext; | ||
matrix(): MatrixContext; | ||
@@ -378,2 +421,4 @@ LPAREN(): TerminalNode; | ||
oper_expr(i: number): Oper_exprContext; | ||
PLUS_PLUS(): TerminalNode; | ||
MINUS_MINUS(): TerminalNode; | ||
PLUS(): TerminalNode; | ||
@@ -389,7 +434,7 @@ MINUS(): TerminalNode; | ||
ELEFTDIV(): TerminalNode; | ||
PLUS_PLUS(): TerminalNode; | ||
MINUS_MINUS(): TerminalNode; | ||
LPAREN(): TerminalNode; | ||
RPAREN(): TerminalNode; | ||
arg_list(): Arg_listContext; | ||
LCURLYBR(): TerminalNode; | ||
RCURLYBR(): TerminalNode; | ||
TRANSPOSE(): TerminalNode; | ||
@@ -417,2 +462,4 @@ HERMITIAN(): TerminalNode; | ||
arg_list(): Arg_listContext; | ||
LCURLYBR(): TerminalNode; | ||
RCURLYBR(): TerminalNode; | ||
get ruleIndex(): number; | ||
@@ -468,2 +515,3 @@ } | ||
OR_EQ(): TerminalNode; | ||
anon_fcn_handle(): Anon_fcn_handleContext; | ||
get ruleIndex(): number; | ||
@@ -474,5 +522,25 @@ } | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
declaration(): DeclarationContext; | ||
select_command(): Select_commandContext; | ||
function_(): FunctionContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class DeclarationContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
i: number; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
GLOBAL(): TerminalNode; | ||
PERSISTENT(): TerminalNode; | ||
decl_elt_list(): Decl_eltContext[]; | ||
decl_elt(i: number): Decl_eltContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Decl_eltContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
identifier(): IdentifierContext; | ||
EQ(): TerminalNode; | ||
expression(): ExpressionContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Select_commandContext extends ParserRuleContext { | ||
@@ -517,2 +585,97 @@ node: AST.NodeInput; | ||
} | ||
export declare class Param_listContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
i: number; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
LPAREN(): TerminalNode; | ||
RPAREN(): TerminalNode; | ||
param_list_elt_list(): Param_list_eltContext[]; | ||
param_list_elt(i: number): Param_list_eltContext; | ||
COMMA_list(): TerminalNode[]; | ||
COMMA(i: number): TerminalNode; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Param_list_eltContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
decl_elt(): Decl_eltContext; | ||
magic_tilde(): Magic_tildeContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Return_listContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
i: number; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
identifier_list(): IdentifierContext[]; | ||
identifier(i: number): IdentifierContext; | ||
LBRACKET(): TerminalNode; | ||
RBRACKET(): TerminalNode; | ||
COMMA_list(): TerminalNode[]; | ||
COMMA(i: number): TerminalNode; | ||
get ruleIndex(): number; | ||
} | ||
export declare class FunctionContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
FUNCTION(): TerminalNode; | ||
identifier(): IdentifierContext; | ||
END(): TerminalNode; | ||
ENDFUNCTION(): TerminalNode; | ||
EOF(): TerminalNode; | ||
return_list(): Return_listContext; | ||
EQ(): TerminalNode; | ||
param_list(): Param_listContext; | ||
sep(): SepContext; | ||
arguments_block_list(): Arguments_block_listContext; | ||
list(): ListContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Arguments_block_listContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
i: number; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
arguments_block_list(): Arguments_blockContext[]; | ||
arguments_block(i: number): Arguments_blockContext; | ||
sep_list(): SepContext[]; | ||
sep(i: number): SepContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Arguments_blockContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
ARGUMENTS(): TerminalNode; | ||
args_validation_list(): Args_validation_listContext; | ||
END(): TerminalNode; | ||
sep_list(): SepContext[]; | ||
sep(i: number): SepContext; | ||
LPAREN(): TerminalNode; | ||
identifier(): IdentifierContext; | ||
RPAREN(): TerminalNode; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Args_validation_listContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
i: number; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
arg_validation_list(): Arg_validationContext[]; | ||
arg_validation(i: number): Arg_validationContext; | ||
sep_list(): SepContext[]; | ||
sep(i: number): SepContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Arg_validationContext extends ParserRuleContext { | ||
node: AST.NodeInput; | ||
constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); | ||
identifier_list(): IdentifierContext[]; | ||
identifier(i: number): IdentifierContext; | ||
LPAREN(): TerminalNode; | ||
arg_list_list(): Arg_listContext[]; | ||
arg_list(i: number): Arg_listContext; | ||
RPAREN(): TerminalNode; | ||
LCURLYBR(): TerminalNode; | ||
RCURLYBR(): TerminalNode; | ||
EQ(): TerminalNode; | ||
expression(): ExpressionContext; | ||
get ruleIndex(): number; | ||
} | ||
export declare class Sep_no_nlContext extends ParserRuleContext { | ||
@@ -519,0 +682,0 @@ constructor(parser?: MathJSLabParser, parent?: ParserRuleContext, invokingState?: number); |
@@ -181,3 +181,3 @@ import { CharString } from './CharString'; | ||
*/ | ||
static unparse(M: MultiArray): string; | ||
static unparse(M: MultiArray, evaluator: Evaluator): string; | ||
/** | ||
@@ -188,3 +188,3 @@ * Unparse MultiArray as MathML language. | ||
*/ | ||
static unparseMathML(M: MultiArray): string; | ||
static unparseMathML(M: MultiArray, evaluator: Evaluator): string; | ||
/** | ||
@@ -197,3 +197,3 @@ * Converts CharString to MultiArray. | ||
/** | ||
* Evaluate array. Calls `global.EvaluatorPointer.Evaluator` for each | ||
* Evaluate array. Calls `evaluator.Evaluator` for each | ||
* element of page (matrix row-ordered). Performs horizontal or vertical | ||
@@ -216,3 +216,3 @@ * concatenation if the evaluation of element results in an MultiArray. | ||
*/ | ||
static evaluate(M: MultiArray, local?: boolean, fname?: string): MultiArray; | ||
static evaluate(M: MultiArray, evaluator: Evaluator, local?: boolean, fname?: string): MultiArray; | ||
/** | ||
@@ -314,3 +314,3 @@ * Linearize MultiArray in an array of ElementType using column-major | ||
*/ | ||
static parseSubscript(dimension: number[], subscript: ComplexDecimal[], input?: string, that?: Evaluator): number; | ||
static parseSubscript(dimension: number[], subscript: ComplexDecimal[], input?: string, evaluator?: Evaluator): number; | ||
/** | ||
@@ -440,3 +440,3 @@ * Binary operation 'scalar `operation` array'. | ||
*/ | ||
static setElements(nameTable: TNameTable, id: string, indexList: (ComplexDecimal | MultiArray)[], right: MultiArray, input?: string, that?: Evaluator): void; | ||
static setElements(nameTable: TNameTable, id: string, indexList: (ComplexDecimal | MultiArray)[], right: MultiArray, input?: string, evaluator?: Evaluator): void; | ||
/** | ||
@@ -443,0 +443,0 @@ * Set selected items from MultiArray by logical indexing. |
import { ErrorListener, RecognitionException, Recognizer, Token } from 'antlr4'; | ||
export default class ParserErrorListener extends ErrorListener<Token> { | ||
private errors; | ||
syntaxError(recognizer: Recognizer<Token>, offendingSymbol: Token, line: number, column: number, msg: string, e: RecognitionException | undefined): void; | ||
} |
@@ -0,1 +1,2 @@ | ||
import * as AST from './AST'; | ||
export type EntryType = 'name' | 'function'; | ||
@@ -7,8 +8,8 @@ export interface PrimarySymbolTableEntry { | ||
type: 'name'; | ||
expr?: any; | ||
expr?: AST.NodeExpr; | ||
} | ||
export interface FunctionSymbolTableEntry extends PrimarySymbolTableEntry { | ||
type: 'function'; | ||
args?: any[]; | ||
expr?: any; | ||
args?: AST.NodeIdentifier[]; | ||
expr?: AST.NodeExpr; | ||
} | ||
@@ -15,0 +16,0 @@ export type SymbolTableEntry = ValueSymbolTableEntry | FunctionSymbolTableEntry; |
{ | ||
"name": "mathjslab", | ||
"version": "1.3.4", | ||
"version": "1.4.0", | ||
"description": "MathJSLab - An interpreter with language syntax like MATLAB®/Octave. ISBN 978-65-00-82338-7", | ||
@@ -59,6 +59,6 @@ "main": "lib/mathjslab.js", | ||
"@types/jest": "29.5.11", | ||
"@types/node": "^20.10.4", | ||
"@types/node": "^20.10.5", | ||
"@types/supertest": "^2.0.16", | ||
"@types/webpack": "^5.28.5", | ||
"@typescript-eslint/eslint-plugin": "^6.14.0", | ||
"@typescript-eslint/eslint-plugin": "^6.15.0", | ||
"eslint": "^8.56.0", | ||
@@ -65,0 +65,0 @@ "eslint-config-prettier": "^9.1.0", |
@@ -41,6 +41,6 @@ # MathJSLab | ||
- Comes with a large set of built-in functions and constants. | ||
- Is easily extensible. | ||
- Is easily extensible through configuration parameters passed to Evaluator constructor. | ||
- [Open source](https://en.wikipedia.org/wiki/Open-source_software) with fully documented code. | ||
- Test suites. | ||
- Improved [demo](https://mathjslab.netlify.app/) [Web application](https://en.wikipedia.org/wiki/Web_application). | ||
- Improved [demo Web application](https://mathjslab.netlify.app/). | ||
@@ -80,3 +80,3 @@ ## Browser support | ||
```typescript | ||
let evaluator = Evaluator.initialize(EvaluatorConfiguration); | ||
let evaluator = new Evaluator(EvaluatorConfiguration); | ||
``` | ||
@@ -114,6 +114,6 @@ | ||
The module will be loaded with `mathjslab` name. You can initialize evaluator with: | ||
The module will be loaded with `mathjslab` name. You can instantiate `Evaluator` with: | ||
```typescript | ||
let evaluator = mathjslab.Evaluator.initialize(EvaluatorConfiguration); | ||
let evaluator = new mathjslab.Evaluator(EvaluatorConfiguration); | ||
``` | ||
@@ -157,6 +157,3 @@ | ||
* There are only one a complex numeric type. Other implemented types is | ||
boolean and character string. Character strings are treated differently than | ||
in MATLAB®/Octave. An entire string can be placed as an element of an | ||
array and operations with strings are not yet supported, nor are they converted | ||
to numbers. | ||
boolean and character string. | ||
@@ -163,0 +160,0 @@ ## License |
139
src/AST.ts
@@ -167,3 +167,3 @@ import { CharString } from './CharString'; | ||
type: 'LIST'; | ||
list: Array<NodeExpr>; | ||
list: NodeInput[]; | ||
} | ||
@@ -181,2 +181,43 @@ | ||
export interface NodeFunctionHandle extends PrimaryNode { | ||
type: 'FCNHANDLE'; | ||
id: string | null; | ||
parameter: NodeInput[]; | ||
expression: NodeExpr; | ||
} | ||
export interface NodeFunction extends PrimaryNode { | ||
type: 'FCN'; | ||
id: string; | ||
return: NodeInput[]; | ||
parameter: NodeInput[]; | ||
arguments: NodeInput[]; | ||
statements: NodeInput[]; | ||
} | ||
export interface NodeArgumentValidation { | ||
name: NodeIdentifier; | ||
size: NodeInput[]; | ||
class: NodeIdentifier | null; | ||
functions: NodeInput[]; | ||
default: NodeExpr; // NodeExpr can be null. | ||
} | ||
export interface NodeArguments extends PrimaryNode { | ||
type: 'ARGUMENTS'; | ||
attribute: NodeIdentifier | null; | ||
validation: { | ||
name: NodeIdentifier; | ||
size: NodeInput[]; | ||
class: NodeIdentifier | null; | ||
functions: NodeInput[]; | ||
default: NodeExpr; // NodeExpr can be null. | ||
}[]; | ||
} | ||
export interface NodeDeclaration extends PrimaryNode { | ||
type: 'GLOBAL' | 'PERSISTENT'; | ||
list: NodeExpr[]; | ||
} | ||
export interface NodeIf extends PrimaryNode { | ||
@@ -270,26 +311,15 @@ type: 'IF'; | ||
/** | ||
* Create range node. If two arguments are passed then it is 'start' and | ||
* 'stop' of range. If three arguments are passed then it is 'start', | ||
* 'stride' and 'stop'. | ||
* @param args 'start' and 'stop' or 'start', 'stride' and 'stop'. | ||
* Create range node. | ||
* @param start | ||
* @param stop | ||
* @param stride | ||
* @returns NodeRange. | ||
*/ | ||
export const nodeRange = (...args: any): NodeRange => { | ||
if (args.length === 2) { | ||
return { | ||
type: 'RANGE', | ||
start: args[0], | ||
stop: args[1], | ||
stride: null, | ||
}; | ||
} else if (args.length === 3) { | ||
return { | ||
type: 'RANGE', | ||
start: args[0], | ||
stop: args[2], | ||
stride: args[1], | ||
}; | ||
} else { | ||
throw new SyntaxError('invalid range.'); | ||
} | ||
export const nodeRange = (start: any, stop: any, stride?: any): NodeRange => { | ||
return { | ||
type: 'RANGE', | ||
start, | ||
stop, | ||
stride: stride ?? null, | ||
}; | ||
}; | ||
@@ -446,2 +476,65 @@ | ||
export const nodeFunctionHandle = (id: NodeIdentifier | null = null, parameter_list: NodeList | null = null, expression: NodeExpr = null): NodeFunctionHandle => { | ||
return { | ||
type: 'FCNHANDLE', | ||
id: id ? id.id : null, | ||
parameter: parameter_list ? parameter_list.list : [], | ||
expression, | ||
}; | ||
}; | ||
export const nodeFunction = (id: NodeIdentifier, return_list: NodeList, parameter_list: NodeList, arguments_list: NodeList, statements_list: NodeList): NodeFunction => { | ||
return { | ||
type: 'FCN', | ||
id: id.id, | ||
return: return_list.list, | ||
parameter: parameter_list.list, | ||
arguments: arguments_list.list, | ||
statements: statements_list.list, | ||
}; | ||
}; | ||
export const nodeArgumentValidation = ( | ||
name: NodeIdentifier, | ||
size: NodeList, | ||
cl: NodeIdentifier | null = null, | ||
functions: NodeList, | ||
dflt: NodeExpr = null, | ||
): NodeArgumentValidation => { | ||
return { | ||
name, | ||
size: size.list, | ||
class: cl, | ||
functions: functions.list, | ||
default: dflt, | ||
}; | ||
}; | ||
export const nodeArguments = (attribute: NodeIdentifier | null, validationList: NodeList): NodeArguments => { | ||
return { | ||
type: 'ARGUMENTS', | ||
attribute, | ||
validation: validationList.list, | ||
}; | ||
}; | ||
export const nodeGlobal = (): NodeDeclaration => { | ||
return { | ||
type: 'GLOBAL', | ||
list: [], | ||
}; | ||
}; | ||
export const nodePersistent = (): NodeDeclaration => { | ||
return { | ||
type: 'PERSISTENT', | ||
list: [], | ||
}; | ||
}; | ||
export const nodeAppendDeclaration = (node: NodeDeclaration, declaration: NodeExpr): NodeDeclaration => { | ||
node.list.push(declaration); | ||
return node; | ||
}; | ||
export const nodeIfBegin = (expression: any, then: NodeList): NodeIf => { | ||
@@ -448,0 +541,0 @@ return { |
import { ErrorListener, RecognitionException, Recognizer } from 'antlr4'; | ||
export default class LexerErrorListener extends ErrorListener<number> { | ||
private errors: { line: number; column: number; msg: string }[] = []; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@@ -18,5 +16,4 @@ syntaxError(recognizer: Recognizer<number>, offendingSymbol: number, line: number, column: number, msg: string, e: RecognitionException | undefined): void { | ||
} | ||
this.errors.push({ line, column, msg }); | ||
throw new SyntaxError(`${msg} (${line}:${column})`); | ||
} | ||
} |
@@ -0,12 +1,11 @@ | ||
export { Decimal } from 'decimal.js'; | ||
export { CharString } from './CharString'; | ||
export { Decimal } from 'decimal.js'; | ||
export { ComplexDecimal } from './ComplexDecimal'; | ||
export { MultiArray } from './MultiArray'; | ||
export { CoreFunctions } from './CoreFunctions'; | ||
export { LinearAlgebra } from './LinearAlgebra'; | ||
export { MathOperation } from './MathOperation'; | ||
export { CoreFunctions } from './CoreFunctions'; | ||
export { Configuration } from './Configuration'; | ||
export { SymbolTable } from './SymbolTable'; | ||
export * as AST from './AST'; | ||
export { Parser } from './Parser'; | ||
export * from './Evaluator'; |
@@ -301,4 +301,4 @@ import { CharString } from './CharString'; | ||
*/ | ||
public static unparse(M: MultiArray): string { | ||
const unparseRows = (row: ElementType[]) => row.map((value) => global.EvaluatorPointer.Unparse(value)).join() + ';\n'; | ||
public static unparse(M: MultiArray, evaluator: Evaluator): string { | ||
const unparseRows = (row: ElementType[]) => row.map((value) => evaluator.Unparse(value)).join() + ';\n'; | ||
let arraystr: string = ''; | ||
@@ -316,3 +316,3 @@ if (M.dimension.reduce((p, c) => p * c, 1) === 0) { | ||
arraystr = arraystr.substring(0, arraystr.length - 2); | ||
result += `${M.isCell ? '{' : '['}\n${arraystr}\n${M.isCell ? '}' : ']'} (:,:,${MultiArray.linearIndexToSubscript(M.dimensionR, p).slice(1).join()})\n`; | ||
result += `${M.isCell ? '{' : '['}${arraystr}${M.isCell ? '}' : ']'} (:,:,${MultiArray.linearIndexToSubscript(M.dimensionR, p).slice(1).join()})\n`; | ||
} | ||
@@ -323,3 +323,3 @@ return result; | ||
arraystr = arraystr.substring(0, arraystr.length - 2); | ||
return `${M.isCell ? '{' : '['}\n${arraystr}\n${M.isCell ? '}' : ']'}`; | ||
return `${M.isCell ? '{' : '['}${arraystr}${M.isCell ? '}' : ']'}`; | ||
} | ||
@@ -333,4 +333,4 @@ } | ||
*/ | ||
public static unparseMathML(M: MultiArray): string { | ||
const unparseRows = (row: ElementType[]) => `<mtr>${row.map((value) => `<mtd>${global.EvaluatorPointer.unparserMathML(value)}</mtd>`).join('')}</mtr>`; | ||
public static unparseMathML(M: MultiArray, evaluator: Evaluator): string { | ||
const unparseRows = (row: ElementType[]) => `<mtr>${row.map((value) => `<mtd>${evaluator.unparserMathML(value)}</mtd>`).join('')}</mtr>`; | ||
const buildMrow = (rows: string) => `<mrow><mo>${M.isCell ? '{' : '['}</mo><mtable>${rows}</mtable><mo>${M.isCell ? '}' : ']'}</mo></mrow>`; | ||
@@ -376,3 +376,3 @@ if (M.dimension.reduce((p, c) => p * c, 1) === 0) { | ||
/** | ||
* Evaluate array. Calls `global.EvaluatorPointer.Evaluator` for each | ||
* Evaluate array. Calls `evaluator.Evaluator` for each | ||
* element of page (matrix row-ordered). Performs horizontal or vertical | ||
@@ -386,3 +386,3 @@ * concatenation if the evaluation of element results in an MultiArray. | ||
*/ | ||
private static evaluatePage(array: ElementType[][], local: boolean = false, fname: string = '', iscell: boolean = false, parent: any): ElementType[][] { | ||
private static evaluatePage(array: ElementType[][], evaluator: Evaluator, local: boolean = false, fname: string = '', iscell: boolean = false, parent: any): ElementType[][] { | ||
const result: ElementType[][] = []; | ||
@@ -394,3 +394,3 @@ for (let i = 0, k = 0; i < array.length; i++, k++) { | ||
array[i][j].parent = parent; | ||
const element = global.EvaluatorPointer.Evaluator(array[i][j], local, fname); | ||
const element = evaluator.Evaluator(array[i][j], local, fname); | ||
if (element instanceof MultiArray && !iscell) { | ||
@@ -430,7 +430,7 @@ if (j === 0) { | ||
*/ | ||
public static evaluate(M: MultiArray, local: boolean = false, fname: string = ''): MultiArray { | ||
public static evaluate(M: MultiArray, evaluator: Evaluator, local: boolean = false, fname: string = ''): MultiArray { | ||
const result: MultiArray = new MultiArray(); | ||
result.isCell = M.isCell; | ||
for (let p = 0; p < M.array.length; p += M.dimension[0]) { | ||
const page = MultiArray.evaluatePage(M.array.slice(p, p + M.dimension[0]), local, fname, M.isCell, result); | ||
const page = MultiArray.evaluatePage(M.array.slice(p, p + M.dimension[0]), evaluator, local, fname, M.isCell, result); | ||
if (p === 0) { | ||
@@ -710,5 +710,5 @@ result.dimension = [page.length, page[0].length, ...M.dimension.slice(2)]; | ||
*/ | ||
public static parseSubscript(dimension: number[], subscript: ComplexDecimal[], input?: string, that?: Evaluator): number { | ||
public static parseSubscript(dimension: number[], subscript: ComplexDecimal[], input?: string, evaluator?: Evaluator): number { | ||
// Converts ComplexDecimal[] subscript parameter to number[]. | ||
const index = subscript.map((i) => MultiArray.testIndex(i, `${input ? input : ''}${that ? '(' + subscript.map((i) => that.Unparse(i)).join() + ')' : ''}`)); | ||
const index = subscript.map((i) => MultiArray.testIndex(i, `${input ? input : ''}${evaluator ? '(' + subscript.map((i) => evaluator.Unparse(i)).join() + ')' : ''}`)); | ||
/** | ||
@@ -1166,3 +1166,3 @@ * Throws comprehensive out of bound error indicating subscript index and bound. | ||
*/ | ||
public static setElements(nameTable: TNameTable, id: string, indexList: (ComplexDecimal | MultiArray)[], right: MultiArray, input?: string, that?: Evaluator): void { | ||
public static setElements(nameTable: TNameTable, id: string, indexList: (ComplexDecimal | MultiArray)[], right: MultiArray, input?: string, evaluator?: Evaluator): void { | ||
if (indexList.length === 0) { | ||
@@ -1177,3 +1177,6 @@ throw new RangeError('invalid empty index list.'); | ||
arg.map((i) => | ||
MultiArray.testIndex(i as ComplexDecimal, `${input ? input : ''}${that ? '(' + args.map((arg) => arg.map((i) => that.Unparse(i))).join() + ')' : ''}`), | ||
MultiArray.testIndex( | ||
i as ComplexDecimal, | ||
`${input ? input : ''}${evaluator ? '(' + args.map((arg) => arg.map((i) => evaluator.Unparse(i))).join() + ')' : ''}`, | ||
), | ||
), | ||
@@ -1213,3 +1216,3 @@ ); | ||
args[r][s - 1] as ComplexDecimal, | ||
`${input ? input : ''}${that ? '(' + subscript.map((i) => that.Unparse(new ComplexDecimal(i))).join() + ')' : ''}`, | ||
`${input ? input : ''}${evaluator ? '(' + subscript.map((i) => evaluator.Unparse(new ComplexDecimal(i))).join() + ')' : ''}`, | ||
), | ||
@@ -1216,0 +1219,0 @@ ); |
import { ErrorListener, RecognitionException, Recognizer, Token } from 'antlr4'; | ||
export default class ParserErrorListener extends ErrorListener<Token> { | ||
private errors: { line: number; column: number; msg: string }[] = []; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@@ -18,5 +16,4 @@ syntaxError(recognizer: Recognizer<Token>, offendingSymbol: Token, line: number, column: number, msg: string, e: RecognitionException | undefined): void { | ||
} | ||
this.errors.push({ line, column, msg }); | ||
throw new SyntaxError(`${msg} (${line}:${column})`); | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
import * as AST from './AST'; | ||
export type EntryType = 'name' | 'function'; | ||
@@ -9,3 +11,3 @@ | ||
type: 'name'; | ||
expr?: any; | ||
expr?: AST.NodeExpr; | ||
} | ||
@@ -15,4 +17,4 @@ | ||
type: 'function'; | ||
args?: any[]; | ||
expr?: any; | ||
args?: AST.NodeIdentifier[]; | ||
expr?: AST.NodeExpr; | ||
} | ||
@@ -19,0 +21,0 @@ |
@@ -24,5 +24,2 @@ import path from 'path'; | ||
extensions: ['.ts', '.js'], | ||
alias: { | ||
parser: path.resolve(__dirname, 'src/'), | ||
}, | ||
}, | ||
@@ -32,4 +29,6 @@ output: { | ||
filename: 'mathjslab.js', | ||
library: 'mathjslab', | ||
libraryTarget: 'umd', | ||
library: { | ||
name: 'mathjslab', | ||
type: 'umd', | ||
}, | ||
clean: true, | ||
@@ -36,0 +35,0 @@ }, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
857097
10122
57
179