@mojir/lits 2.2.3 → 2.3.0

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.
Files changed (65) hide show
  1. package/README.md +1 -8
  2. package/dist/cli/cli.js +1264 -909
  3. package/dist/cli/src/Lits/Lits.d.ts +8 -2
  4. package/dist/cli/src/builtin/bindingNode.d.ts +2 -1
  5. package/dist/cli/src/builtin/interface.d.ts +3 -2
  6. package/dist/cli/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  7. package/dist/cli/src/evaluator/functionExecutors.d.ts +2 -1
  8. package/dist/cli/src/evaluator/index.d.ts +3 -2
  9. package/dist/cli/src/evaluator/interface.d.ts +3 -2
  10. package/dist/cli/src/utils/maybePromise.d.ts +54 -0
  11. package/dist/full.esm.js +1 -1
  12. package/dist/full.esm.js.map +1 -1
  13. package/dist/full.js +1 -1
  14. package/dist/full.js.map +1 -1
  15. package/dist/index.esm.js +1 -1
  16. package/dist/index.esm.js.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/lits.iife.js +1 -1
  20. package/dist/lits.iife.js.map +1 -1
  21. package/dist/modules/assert.esm.js +1 -1
  22. package/dist/modules/assert.esm.js.map +1 -1
  23. package/dist/modules/assert.js +1 -1
  24. package/dist/modules/assert.js.map +1 -1
  25. package/dist/modules/collection.esm.js +1 -1
  26. package/dist/modules/collection.esm.js.map +1 -1
  27. package/dist/modules/collection.js +1 -1
  28. package/dist/modules/collection.js.map +1 -1
  29. package/dist/modules/grid.esm.js +1 -1
  30. package/dist/modules/grid.esm.js.map +1 -1
  31. package/dist/modules/grid.js +1 -1
  32. package/dist/modules/grid.js.map +1 -1
  33. package/dist/modules/number-theory.esm.js +1 -1
  34. package/dist/modules/number-theory.esm.js.map +1 -1
  35. package/dist/modules/number-theory.js +1 -1
  36. package/dist/modules/number-theory.js.map +1 -1
  37. package/dist/modules/sequence.esm.js +1 -1
  38. package/dist/modules/sequence.esm.js.map +1 -1
  39. package/dist/modules/sequence.js +1 -1
  40. package/dist/modules/sequence.js.map +1 -1
  41. package/dist/modules/src/Lits/Lits.d.ts +8 -2
  42. package/dist/modules/src/builtin/bindingNode.d.ts +2 -1
  43. package/dist/modules/src/builtin/interface.d.ts +3 -2
  44. package/dist/modules/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  45. package/dist/modules/src/evaluator/functionExecutors.d.ts +2 -1
  46. package/dist/modules/src/evaluator/index.d.ts +3 -2
  47. package/dist/modules/src/evaluator/interface.d.ts +3 -2
  48. package/dist/modules/src/utils/maybePromise.d.ts +54 -0
  49. package/dist/modules/vector.esm.js +1 -1
  50. package/dist/modules/vector.esm.js.map +1 -1
  51. package/dist/modules/vector.js +1 -1
  52. package/dist/modules/vector.js.map +1 -1
  53. package/dist/src/Lits/Lits.d.ts +8 -2
  54. package/dist/src/builtin/bindingNode.d.ts +2 -1
  55. package/dist/src/builtin/interface.d.ts +3 -2
  56. package/dist/src/builtin/modules/number-theory/sequences/index.d.ts +2 -1
  57. package/dist/src/evaluator/functionExecutors.d.ts +2 -1
  58. package/dist/src/evaluator/index.d.ts +3 -2
  59. package/dist/src/evaluator/interface.d.ts +3 -2
  60. package/dist/src/utils/maybePromise.d.ts +54 -0
  61. package/dist/testFramework.esm.js +1 -1
  62. package/dist/testFramework.esm.js.map +1 -1
  63. package/dist/testFramework.js +1 -1
  64. package/dist/testFramework.js.map +1 -1
  65. package/package.json +1 -1
@@ -5,6 +5,7 @@ import type { TokenStream } from '../tokenizer/tokenize';
5
5
  import { AutoCompleter } from '../AutoCompleter/AutoCompleter';
6
6
  import type { Arity } from '../builtin/interface';
7
7
  import type { LitsModule } from '../builtin/modules/interface';
8
+ import type { MaybePromise } from '../utils/maybePromise';
8
9
  import { Cache } from './Cache';
9
10
  export interface LitsRuntimeInfo {
10
11
  astCache: Cache | null;
@@ -42,15 +43,20 @@ export declare class Lits {
42
43
  private modules;
43
44
  constructor(config?: LitsConfig);
44
45
  getRuntimeInfo(): LitsRuntimeInfo;
46
+ readonly async: {
47
+ run: (program: string, params?: ContextParams & FilePathParams) => Promise<unknown>;
48
+ context: (programOrAst: string | Ast, params?: ContextParams & FilePathParams) => Promise<Context>;
49
+ apply: (fn: LitsFunction, fnParams: unknown[], params?: ContextParams) => Promise<unknown>;
50
+ };
45
51
  run(program: string, params?: ContextParams & FilePathParams): unknown;
46
52
  context(programOrAst: string | Ast, params?: ContextParams & FilePathParams): Context;
47
53
  getUndefinedSymbols(programOrAst: string | Ast, params?: ContextParams): Set<string>;
48
54
  tokenize(program: string, tokenizeParams?: FilePathParams & MinifyParams): TokenStream;
49
55
  parse(tokenStream: TokenStream): Ast;
50
- evaluate(ast: Ast, params: ContextParams): Any;
56
+ evaluate(ast: Ast, params: ContextParams): MaybePromise<Any>;
51
57
  transformSymbols(tokenStream: TokenStream, transformer: (symbol: string) => string): TokenStream;
52
58
  untokenize(tokenStream: TokenStream): string;
53
- apply(fn: LitsFunction, fnParams: unknown[], params?: ContextParams): Any;
59
+ apply(fn: LitsFunction, fnParams: unknown[], params?: ContextParams): MaybePromise<Any>;
54
60
  private generateApplyFunctionCall;
55
61
  private generateAst;
56
62
  getAutoCompleter(program: string, position: number, params?: ContextParams): AutoCompleter;
@@ -1,5 +1,6 @@
1
1
  import type { Any } from '../interface';
2
2
  import { type AstNode, type BindingTarget } from '../parser/types';
3
+ import type { MaybePromise } from '../utils/maybePromise';
3
4
  export declare function walkDefaults(bindingTarget: BindingTarget, onDefault: (Node: AstNode) => void): void;
4
- export declare function evaluateBindingNodeValues(target: BindingTarget, value: Any, evaluate: (Node: AstNode) => Any): Record<string, Any>;
5
+ export declare function evaluateBindingNodeValues(target: BindingTarget, value: Any, evaluate: (Node: AstNode) => MaybePromise<Any>): MaybePromise<Record<string, Any>>;
5
6
  export declare function getAllBindingTargetNames(bindingTarget: BindingTarget): Record<string, true>;
@@ -4,6 +4,7 @@ import type { GetUndefinedSymbols, UndefinedSymbols } from '../getUndefinedSymbo
4
4
  import type { Any, Arr } from '../interface';
5
5
  import type { SpecialExpressionNode } from '../parser/types';
6
6
  import type { SourceCodeInfo } from '../tokenizer/token';
7
+ import type { MaybePromise } from '../utils/maybePromise';
7
8
  import type { SpecialExpressions } from '.';
8
9
  export type Arity = {
9
10
  min?: number;
@@ -76,7 +77,7 @@ export type SpecialExpressionDocs = FunctionDocs | CustomDocs;
76
77
  export declare function isFunctionDocs(docs: SpecialExpressionDocs): docs is FunctionDocs;
77
78
  type NormalExpressionEvaluator<T> = (params: Arr, sourceCodeInfo: SourceCodeInfo | undefined, contextStack: ContextStack, { executeFunction }: {
78
79
  executeFunction: ExecuteFunction;
79
- }) => T;
80
+ }) => MaybePromise<T>;
80
81
  export interface BuiltinNormalExpression<T> {
81
82
  evaluate: NormalExpressionEvaluator<T>;
82
83
  name?: string;
@@ -91,7 +92,7 @@ interface EvaluateHelpers {
91
92
  getUndefinedSymbols: GetUndefinedSymbols;
92
93
  }
93
94
  export interface BuiltinSpecialExpression<T, N extends SpecialExpressionNode> {
94
- evaluate: (node: N, contextStack: ContextStack, helpers: EvaluateHelpers) => T;
95
+ evaluate: (node: N, contextStack: ContextStack, helpers: EvaluateHelpers) => MaybePromise<T>;
95
96
  evaluateAsNormalExpression?: NormalExpressionEvaluator<T>;
96
97
  arity: Arity;
97
98
  docs?: SpecialExpressionDocs;
@@ -1,11 +1,12 @@
1
1
  import type { SourceCodeInfo } from '../../../../tokenizer/token';
2
+ import type { MaybePromise } from '../../../../utils/maybePromise';
2
3
  import type { BuiltinNormalExpression, BuiltinNormalExpressions } from '../../../../builtin/interface';
3
4
  type SeqKey<T extends string> = `${T}-seq`;
4
5
  type TakeWhileKey<T extends string> = `${T}-take-while`;
5
6
  type NthKey<T extends string> = `${T}-nth`;
6
7
  type PredKey<T extends string> = `${T}?`;
7
8
  type SeqFunction<Type extends number | string> = (length: number, sourceCodeInfo: SourceCodeInfo | undefined) => Type[];
8
- type TakeWhileFunction<Type extends number | string> = (pred: (value: Type, index: number) => boolean, sourceCodeInfo: SourceCodeInfo | undefined) => Type[];
9
+ type TakeWhileFunction<Type extends number | string> = (pred: (value: Type, index: number) => MaybePromise<boolean>, sourceCodeInfo: SourceCodeInfo | undefined) => MaybePromise<Type[]>;
9
10
  type PredFunction<Type extends number | string> = (n: Type, sourceCodeInfo: SourceCodeInfo | undefined) => boolean;
10
11
  export type SequenceKeys<T extends string> = SeqKey<T> | TakeWhileKey<T> | NthKey<T> | PredKey<T>;
11
12
  export type SequenceDefinition<T extends string, Type extends number | string = number> = {
@@ -1,11 +1,12 @@
1
1
  import type { Any, Arr } from '../interface';
2
2
  import type { LitsFunctionType } from '../parser/types';
3
3
  import type { SourceCodeInfo } from '../tokenizer/token';
4
+ import type { MaybePromise } from '../utils/maybePromise';
4
5
  import type { ContextStack } from './ContextStack';
5
6
  import type { EvaluateNode, ExecuteFunction } from './interface';
6
7
  type FunctionExecutors = Record<LitsFunctionType, (fn: any, params: Arr, sourceCodeInfo: SourceCodeInfo | undefined, contextStack: ContextStack, helpers: {
7
8
  evaluateNode: EvaluateNode;
8
9
  executeFunction: ExecuteFunction;
9
- }) => Any>;
10
+ }) => MaybePromise<Any>>;
10
11
  export declare const functionExecutors: FunctionExecutors;
11
12
  export {};
@@ -1,5 +1,6 @@
1
1
  import type { Any } from '../interface';
2
2
  import type { Ast, AstNode } from '../parser/types';
3
+ import type { MaybePromise } from '../utils/maybePromise';
3
4
  import type { ContextStack } from './ContextStack';
4
- export declare function evaluate(ast: Ast, contextStack: ContextStack): Any;
5
- export declare function evaluateNode(node: AstNode, contextStack: ContextStack): Any;
5
+ export declare function evaluate(ast: Ast, contextStack: ContextStack): MaybePromise<Any>;
6
+ export declare function evaluateNode(node: AstNode, contextStack: ContextStack): MaybePromise<Any>;
@@ -1,13 +1,14 @@
1
1
  import type { Any, Arr } from '../interface';
2
2
  import type { AstNode, FunctionLike } from '../parser/types';
3
3
  import type { SourceCodeInfo } from '../tokenizer/token';
4
+ import type { MaybePromise } from '../utils/maybePromise';
4
5
  import type { ContextStack } from './ContextStack';
5
6
  interface ContextEntry {
6
7
  value: Any;
7
8
  }
8
9
  export type Context = Record<string, ContextEntry>;
9
- export type EvaluateNode = (node: AstNode, contextStack: ContextStack) => Any;
10
- export type ExecuteFunction = (fn: FunctionLike, params: Arr, contextStack: ContextStack, sourceCodeInfo?: SourceCodeInfo) => Any;
10
+ export type EvaluateNode = (node: AstNode, contextStack: ContextStack) => MaybePromise<Any>;
11
+ export type ExecuteFunction = (fn: FunctionLike, params: Arr, contextStack: ContextStack, sourceCodeInfo?: SourceCodeInfo) => MaybePromise<Any>;
11
12
  export type LookUpResult = ContextEntry | null;
12
13
  export declare function isContextEntry(value: unknown): value is ContextEntry;
13
14
  export {};
@@ -0,0 +1,54 @@
1
+ /**
2
+ * MaybePromise utilities for transparent async support.
3
+ *
4
+ * The sync path stays zero-overhead — when no async JS functions are involved,
5
+ * everything runs synchronously with only `instanceof Promise` checks as overhead.
6
+ * When an async value is detected, the evaluation chain switches to Promise-based
7
+ * execution for the remainder.
8
+ */
9
+ export type MaybePromise<T> = T | Promise<T>;
10
+ /**
11
+ * Chain a value that might be a Promise. If the value is sync, calls fn synchronously.
12
+ * If it's a Promise, chains with .then().
13
+ */
14
+ export declare function chain<T, U>(value: MaybePromise<T>, fn: (v: T) => MaybePromise<U>): MaybePromise<U>;
15
+ /**
16
+ * Like Array.map but handles MaybePromise callbacks sequentially.
17
+ * In the sync case, runs as a simple loop. Switches to async only when needed.
18
+ */
19
+ export declare function mapSequential<T, U>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<U>): MaybePromise<U[]>;
20
+ /**
21
+ * Like Array.reduce but handles MaybePromise callbacks sequentially.
22
+ * In the sync case, runs as a simple loop. Switches to async only when needed.
23
+ */
24
+ export declare function reduceSequential<T, U>(arr: readonly T[], fn: (acc: U, elem: T, index: number) => MaybePromise<U>, initial: U): MaybePromise<U>;
25
+ /**
26
+ * Like Array.forEach but handles MaybePromise callbacks sequentially.
27
+ * In the sync case, runs as a simple loop. Switches to async only when needed.
28
+ */
29
+ export declare function forEachSequential<T>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<void>): MaybePromise<void>;
30
+ /**
31
+ * Try/catch that handles MaybePromise values correctly.
32
+ * If tryFn returns a Promise, catches both sync throws and Promise rejections.
33
+ */
34
+ export declare function tryCatch<T>(tryFn: () => MaybePromise<T>, catchFn: (error: unknown) => MaybePromise<T>): MaybePromise<T>;
35
+ /**
36
+ * Like Array.some but handles MaybePromise callbacks sequentially.
37
+ * Returns the first truthy MaybePromise result, or false.
38
+ */
39
+ export declare function someSequential<T>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<unknown>): MaybePromise<boolean>;
40
+ /**
41
+ * Like Array.every but handles MaybePromise callbacks sequentially.
42
+ * Returns false as soon as a falsy result is found.
43
+ */
44
+ export declare function everySequential<T>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<unknown>): MaybePromise<boolean>;
45
+ /**
46
+ * Like Array.filter but handles MaybePromise callbacks sequentially.
47
+ * Returns the filtered array.
48
+ */
49
+ export declare function filterSequential<T>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<unknown>): MaybePromise<T[]>;
50
+ /**
51
+ * Like Array.findIndex but handles MaybePromise callbacks sequentially.
52
+ * Returns -1 if no element matches.
53
+ */
54
+ export declare function findIndexSequential<T>(arr: readonly T[], fn: (elem: T, index: number) => MaybePromise<unknown>): MaybePromise<number>;