@mojir/lits 2.0.4 → 2.0.6

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 {};
@@ -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.4";
3126
+ var version = "2.0.6";
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();
@@ -7847,13 +7855,21 @@ function parsePolishToken(tokenStream, parseState) {
7847
7855
  return node;
7848
7856
  }
7849
7857
  case 'PolNotation': {
7850
- var node = void 0;
7858
+ var astNodes = [];
7851
7859
  parseState.position += 1;
7852
7860
  do {
7853
- node = parsePolishToken(tokenStream, parseState);
7861
+ astNodes.push(parsePolishToken(tokenStream, parseState));
7854
7862
  } while (!isEndNotationToken(asToken(tokenStream.tokens[parseState.position])));
7855
7863
  parseState.position += 1;
7856
- return node;
7864
+ if (astNodes.length === 1) {
7865
+ return astNodes[0];
7866
+ }
7867
+ return {
7868
+ t: AstNodeType.SpecialExpression,
7869
+ n: 'do',
7870
+ p: astNodes,
7871
+ token: astNodes[0].token,
7872
+ };
7857
7873
  }
7858
7874
  case 'P_CollectionAccessor':
7859
7875
  case 'P_Modifier':
@@ -8773,10 +8789,9 @@ var Lits = /** @class */ (function () {
8773
8789
  return analyze(ast, params);
8774
8790
  };
8775
8791
  Lits.prototype.tokenize = function (program, tokenizeParams) {
8776
- var _a, _b;
8777
8792
  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;
8793
+ var debug = this.debug;
8794
+ var algebraic = this.algebraic;
8780
8795
  return tokenize(program, __assign(__assign({}, tokenizeParams), { debug: debug, algebraic: algebraic }));
8781
8796
  };
8782
8797
  Lits.prototype.parse = function (tokenStream) {
@@ -8796,12 +8811,7 @@ var Lits = /** @class */ (function () {
8796
8811
  var _a;
8797
8812
  if (params === void 0) { params = {}; }
8798
8813
  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, ")");
8814
+ var program = this.generateApplyFunctionCall(fnName, fnParams);
8805
8815
  var ast = this.generateAst(program, params);
8806
8816
  var hostValues = fnParams.reduce(function (result, param, index) {
8807
8817
  result["".concat(fnName, "_").concat(index)] = param;
@@ -8810,20 +8820,26 @@ var Lits = /** @class */ (function () {
8810
8820
  params.values = __assign(__assign({}, params.values), hostValues);
8811
8821
  return this.evaluate(ast, params);
8812
8822
  };
8823
+ Lits.prototype.generateApplyFunctionCall = function (fnName, fnParams) {
8824
+ var paramsString = fnParams
8825
+ .map(function (_, index) {
8826
+ return "".concat(fnName, "_").concat(index);
8827
+ })
8828
+ .join(this.algebraic ? ', ' : ' ');
8829
+ return this.algebraic ? "".concat(fnName, "(").concat(paramsString, ")") : "(".concat(fnName, " ").concat(paramsString, ")");
8830
+ };
8813
8831
  Lits.prototype.generateAst = function (program, params) {
8814
- var _a, _b, _c;
8832
+ var _a;
8815
8833
  if (this.astCache) {
8816
8834
  var cachedAst = this.astCache.get(program);
8817
8835
  if (cachedAst)
8818
8836
  return cachedAst;
8819
8837
  }
8820
8838
  var tokenStream = this.tokenize(program, {
8821
- debug: (_a = params.debug) !== null && _a !== void 0 ? _a : this.debug,
8822
8839
  filePath: params.filePath,
8823
- algebraic: (_b = params.algebraic) !== null && _b !== void 0 ? _b : this.algebraic,
8824
8840
  });
8825
8841
  var ast = this.parse(tokenStream);
8826
- (_c = this.astCache) === null || _c === void 0 ? void 0 : _c.set(program, ast);
8842
+ (_a = this.astCache) === null || _a === void 0 ? void 0 : _a.set(program, ast);
8827
8843
  return ast;
8828
8844
  };
8829
8845
  return Lits;