@mojir/lits 2.0.3 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,8 +23,6 @@ export interface LitsParams {
23
23
  lazyValues?: Record<string, LazyValue>;
24
24
  jsFunctions?: Record<string, JsFunction>;
25
25
  filePath?: string;
26
- debug?: boolean;
27
- algebraic?: boolean;
28
26
  }
29
27
  interface LitsConfig {
30
28
  initialCache?: Record<string, Ast>;
@@ -42,12 +40,13 @@ export declare class Lits {
42
40
  run(program: string, params?: LitsParams): unknown;
43
41
  context(program: string, params?: LitsParams): Context;
44
42
  analyze(program: string, params?: LitsParams): Analysis;
45
- tokenize(program: string, tokenizeParams?: TokenizeParams): TokenStream;
43
+ tokenize(program: string, tokenizeParams?: Pick<TokenizeParams, 'filePath'>): TokenStream;
46
44
  parse(tokenStream: TokenStream): Ast;
47
45
  evaluate(ast: Ast, params: LitsParams): Any;
48
46
  transform(tokenStream: TokenStream, transformer: (name: string) => string): TokenStream;
49
47
  untokenize(tokenStream: TokenStream): string;
50
48
  apply(fn: LitsFunction, fnParams: unknown[], params?: LitsParams): Any;
49
+ private generateApplyFunctionCall;
51
50
  private generateAst;
52
51
  }
53
52
  export {};
@@ -1,4 +1,4 @@
1
1
  export declare const polishIdentifierCharacterClass = "[\\w@%^?=!$<>+*/:-]";
2
2
  export declare const polishIdentifierFirstCharacterClass = "[a-zA-Z_@%^?=!$<>+*/-]";
3
- export declare const algebraicIdentifierCharacterClass = "[\\w$:]";
3
+ export declare const algebraicIdentifierCharacterClass = "[\\w$:!]";
4
4
  export declare const algebraicIdentifierFirstCharacterClass = "[a-zA-Z_$]";
@@ -16,7 +16,7 @@ export interface TokenStream {
16
16
  filePath?: string;
17
17
  }
18
18
  export interface TokenizeParams {
19
- debug?: boolean;
19
+ debug: boolean;
20
+ algebraic: boolean;
20
21
  filePath?: string;
21
- algebraic?: boolean;
22
22
  }
@@ -3123,7 +3123,7 @@ var mathNormalExpression = {
3123
3123
  },
3124
3124
  };
3125
3125
 
3126
- var version = "2.0.3";
3126
+ var version = "2.0.5";
3127
3127
 
3128
3128
  var uuidTemplate = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
3129
3129
  var xyRegexp = /[xy]/g;
@@ -7345,14 +7345,22 @@ var AlgebraicParser = /** @class */ (function () {
7345
7345
  return parseReservedSymbol(this.tokenStream, this.parseState);
7346
7346
  case 'PolNotation': {
7347
7347
  this.parseState.algebraic = false;
7348
- var astNode = void 0;
7348
+ var astNodes = [];
7349
7349
  this.advance();
7350
7350
  do {
7351
- astNode = this.parseState.parseToken(this.tokenStream, this.parseState);
7351
+ astNodes.push(this.parseState.parseToken(this.tokenStream, this.parseState));
7352
7352
  } while (!isEndNotationToken(this.peek()));
7353
7353
  this.advance();
7354
7354
  this.parseState.algebraic = true;
7355
- return astNode;
7355
+ if (astNodes.length === 1) {
7356
+ return astNodes[0];
7357
+ }
7358
+ return {
7359
+ t: AstNodeType.SpecialExpression,
7360
+ n: 'do',
7361
+ p: astNodes,
7362
+ token: getTokenDebugData(token) && token,
7363
+ };
7356
7364
  }
7357
7365
  case 'AlgNotation': {
7358
7366
  this.advance();
@@ -7905,7 +7913,7 @@ function parseToken(tokenStream, parseState) {
7905
7913
  }
7906
7914
 
7907
7915
  var polishIdentifierCharacterClass = '[\\w@%^?=!$<>+*/:-]';
7908
- var algebraicIdentifierCharacterClass = '[\\w$:]';
7916
+ var algebraicIdentifierCharacterClass = '[\\w$:!]';
7909
7917
  var algebraicIdentifierFirstCharacterClass = '[a-zA-Z_$]';
7910
7918
 
7911
7919
  var NO_MATCH = [0];
@@ -8773,10 +8781,9 @@ var Lits = /** @class */ (function () {
8773
8781
  return analyze(ast, params);
8774
8782
  };
8775
8783
  Lits.prototype.tokenize = function (program, tokenizeParams) {
8776
- var _a, _b;
8777
8784
  if (tokenizeParams === void 0) { tokenizeParams = {}; }
8778
- var debug = (_a = tokenizeParams.debug) !== null && _a !== void 0 ? _a : this.debug;
8779
- var algebraic = (_b = tokenizeParams.algebraic) !== null && _b !== void 0 ? _b : this.algebraic;
8785
+ var debug = this.debug;
8786
+ var algebraic = this.algebraic;
8780
8787
  return tokenize(program, __assign(__assign({}, tokenizeParams), { debug: debug, algebraic: algebraic }));
8781
8788
  };
8782
8789
  Lits.prototype.parse = function (tokenStream) {
@@ -8796,12 +8803,7 @@ var Lits = /** @class */ (function () {
8796
8803
  var _a;
8797
8804
  if (params === void 0) { params = {}; }
8798
8805
  var fnName = 'FN_2eb7b316-471c-5bfa-90cb-d3dfd9164a59';
8799
- var paramsString = fnParams
8800
- .map(function (_, index) {
8801
- return "".concat(fnName, "_").concat(index);
8802
- })
8803
- .join(' ');
8804
- var program = "(".concat(fnName, " ").concat(paramsString, ")");
8806
+ var program = this.generateApplyFunctionCall(fnName, fnParams);
8805
8807
  var ast = this.generateAst(program, params);
8806
8808
  var hostValues = fnParams.reduce(function (result, param, index) {
8807
8809
  result["".concat(fnName, "_").concat(index)] = param;
@@ -8810,20 +8812,26 @@ var Lits = /** @class */ (function () {
8810
8812
  params.values = __assign(__assign({}, params.values), hostValues);
8811
8813
  return this.evaluate(ast, params);
8812
8814
  };
8815
+ Lits.prototype.generateApplyFunctionCall = function (fnName, fnParams) {
8816
+ var paramsString = fnParams
8817
+ .map(function (_, index) {
8818
+ return "".concat(fnName, "_").concat(index);
8819
+ })
8820
+ .join(this.algebraic ? ', ' : ' ');
8821
+ return this.algebraic ? "".concat(fnName, "(").concat(paramsString, ")") : "(".concat(fnName, " ").concat(paramsString, ")");
8822
+ };
8813
8823
  Lits.prototype.generateAst = function (program, params) {
8814
- var _a, _b, _c;
8824
+ var _a;
8815
8825
  if (this.astCache) {
8816
8826
  var cachedAst = this.astCache.get(program);
8817
8827
  if (cachedAst)
8818
8828
  return cachedAst;
8819
8829
  }
8820
8830
  var tokenStream = this.tokenize(program, {
8821
- debug: (_a = params.debug) !== null && _a !== void 0 ? _a : this.debug,
8822
8831
  filePath: params.filePath,
8823
- algebraic: (_b = params.algebraic) !== null && _b !== void 0 ? _b : this.algebraic,
8824
8832
  });
8825
8833
  var ast = this.parse(tokenStream);
8826
- (_c = this.astCache) === null || _c === void 0 ? void 0 : _c.set(program, ast);
8834
+ (_a = this.astCache) === null || _a === void 0 ? void 0 : _a.set(program, ast);
8827
8835
  return ast;
8828
8836
  };
8829
8837
  return Lits;