@mojir/lits 2.1.0 → 2.1.2

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 (93) hide show
  1. package/README.md +392 -109
  2. package/dist/cli/cli.js +879 -946
  3. package/dist/cli/reference/api.d.ts +0 -1
  4. package/dist/cli/reference/index.d.ts +208 -208
  5. package/dist/cli/src/builtin/bindingNode.d.ts +3 -2
  6. package/dist/cli/src/builtin/index.d.ts +10 -50
  7. package/dist/cli/src/builtin/interface.d.ts +10 -17
  8. package/dist/cli/src/builtin/normalExpressions/index.d.ts +4 -1
  9. package/dist/cli/src/builtin/specialExpressionTypes.d.ts +25 -0
  10. package/dist/cli/src/builtin/specialExpressions/and.d.ts +3 -3
  11. package/dist/cli/src/builtin/specialExpressions/array.d.ts +3 -3
  12. package/dist/cli/src/builtin/specialExpressions/cond.d.ts +3 -3
  13. package/dist/cli/src/builtin/specialExpressions/def.d.ts +3 -6
  14. package/dist/cli/src/builtin/specialExpressions/defined.d.ts +5 -0
  15. package/dist/cli/src/builtin/specialExpressions/do.d.ts +3 -3
  16. package/dist/cli/src/builtin/specialExpressions/functions.d.ts +5 -13
  17. package/dist/cli/src/builtin/specialExpressions/if.d.ts +3 -3
  18. package/dist/cli/src/builtin/specialExpressions/let.d.ts +3 -6
  19. package/dist/cli/src/builtin/specialExpressions/loop.d.ts +3 -4
  20. package/dist/cli/src/builtin/specialExpressions/loops.d.ts +5 -14
  21. package/dist/cli/src/builtin/specialExpressions/object.d.ts +3 -3
  22. package/dist/cli/src/builtin/specialExpressions/or.d.ts +3 -3
  23. package/dist/cli/src/builtin/specialExpressions/qq.d.ts +3 -3
  24. package/dist/cli/src/builtin/specialExpressions/recur.d.ts +3 -3
  25. package/dist/cli/src/builtin/specialExpressions/switch.d.ts +3 -3
  26. package/dist/cli/src/builtin/specialExpressions/throw.d.ts +3 -3
  27. package/dist/cli/src/builtin/specialExpressions/try.d.ts +3 -5
  28. package/dist/cli/src/builtin/specialExpressions/unless.d.ts +3 -3
  29. package/dist/cli/src/builtin/utils.d.ts +2 -5
  30. package/dist/cli/src/constants/constants.d.ts +15 -3
  31. package/dist/cli/src/evaluator/ContextStack.d.ts +3 -3
  32. package/dist/cli/src/evaluator/functionExecutors.d.ts +3 -3
  33. package/dist/cli/src/evaluator/index.d.ts +2 -2
  34. package/dist/cli/src/evaluator/interface.d.ts +3 -3
  35. package/dist/cli/src/getUndefinedSymbols/index.d.ts +3 -3
  36. package/dist/cli/src/parser/Parser.d.ts +7 -5
  37. package/dist/cli/src/parser/types.d.ts +33 -96
  38. package/dist/cli/src/typeGuards/astNode.d.ts +19 -16
  39. package/dist/cli/src/typeGuards/index.d.ts +2 -5
  40. package/dist/cli/src/typeGuards/litsFunction.d.ts +2 -2
  41. package/dist/cli/src/utils/index.d.ts +0 -1
  42. package/dist/index.esm.js +849 -913
  43. package/dist/index.esm.js.map +1 -1
  44. package/dist/index.js +849 -913
  45. package/dist/index.js.map +1 -1
  46. package/dist/lits.iife.js +849 -913
  47. package/dist/lits.iife.js.map +1 -1
  48. package/dist/reference/api.d.ts +0 -1
  49. package/dist/reference/index.d.ts +208 -208
  50. package/dist/src/builtin/bindingNode.d.ts +3 -2
  51. package/dist/src/builtin/index.d.ts +10 -50
  52. package/dist/src/builtin/interface.d.ts +10 -17
  53. package/dist/src/builtin/normalExpressions/index.d.ts +4 -1
  54. package/dist/src/builtin/specialExpressionTypes.d.ts +25 -0
  55. package/dist/src/builtin/specialExpressions/and.d.ts +3 -3
  56. package/dist/src/builtin/specialExpressions/array.d.ts +3 -3
  57. package/dist/src/builtin/specialExpressions/cond.d.ts +3 -3
  58. package/dist/src/builtin/specialExpressions/def.d.ts +3 -6
  59. package/dist/src/builtin/specialExpressions/defined.d.ts +5 -0
  60. package/dist/src/builtin/specialExpressions/do.d.ts +3 -3
  61. package/dist/src/builtin/specialExpressions/functions.d.ts +5 -13
  62. package/dist/src/builtin/specialExpressions/if.d.ts +3 -3
  63. package/dist/src/builtin/specialExpressions/let.d.ts +3 -6
  64. package/dist/src/builtin/specialExpressions/loop.d.ts +3 -4
  65. package/dist/src/builtin/specialExpressions/loops.d.ts +5 -14
  66. package/dist/src/builtin/specialExpressions/object.d.ts +3 -3
  67. package/dist/src/builtin/specialExpressions/or.d.ts +3 -3
  68. package/dist/src/builtin/specialExpressions/qq.d.ts +3 -3
  69. package/dist/src/builtin/specialExpressions/recur.d.ts +3 -3
  70. package/dist/src/builtin/specialExpressions/switch.d.ts +3 -3
  71. package/dist/src/builtin/specialExpressions/throw.d.ts +3 -3
  72. package/dist/src/builtin/specialExpressions/try.d.ts +3 -5
  73. package/dist/src/builtin/specialExpressions/unless.d.ts +3 -3
  74. package/dist/src/builtin/utils.d.ts +2 -5
  75. package/dist/src/constants/constants.d.ts +15 -3
  76. package/dist/src/evaluator/ContextStack.d.ts +3 -3
  77. package/dist/src/evaluator/functionExecutors.d.ts +3 -3
  78. package/dist/src/evaluator/index.d.ts +2 -2
  79. package/dist/src/evaluator/interface.d.ts +3 -3
  80. package/dist/src/getUndefinedSymbols/index.d.ts +3 -3
  81. package/dist/src/parser/Parser.d.ts +7 -5
  82. package/dist/src/parser/types.d.ts +33 -96
  83. package/dist/src/typeGuards/astNode.d.ts +19 -16
  84. package/dist/src/typeGuards/index.d.ts +2 -5
  85. package/dist/src/typeGuards/litsFunction.d.ts +2 -2
  86. package/dist/src/utils/index.d.ts +0 -1
  87. package/dist/testFramework.esm.js +865 -909
  88. package/dist/testFramework.esm.js.map +1 -1
  89. package/dist/testFramework.js +865 -909
  90. package/dist/testFramework.js.map +1 -1
  91. package/package.json +1 -1
  92. package/dist/cli/src/builtin/specialExpressions/declared.d.ts +0 -5
  93. package/dist/src/builtin/specialExpressions/declared.d.ts +0 -5
package/README.md CHANGED
@@ -1,147 +1,430 @@
1
+ # Lits
1
2
 
3
+ Lits is a lexically scoped pure functional language with algebraic notation. It combines the power of functional programming with an intuitive, readable syntax.
2
4
 
3
- # Lits
5
+ Try it in the [Lits Playground](https://mojir.github.io/lits/).
4
6
 
5
- Lits is a programming language and REPL (Read-Eval-Print Loop) that allows you to evaluate Lisp expressions. It provides a playground for experimenting with Lits and an API for integrating Lits into your own projects.
7
+ ## Features
6
8
 
7
- Lits is a Lisp dialect implemented in TypeScript, drawing heavy inspiration from Clojure. Most core functions have been ported to Lits, ensuring a robust and familiar experience for Clojure users.
9
+ - **Pure functional language** - Variables cannot be changed, ensuring predictable behavior and easier reasoning about code
10
+ - **JavaScript interoperability** - JavaScript values and functions can easily be exposed in Lits
11
+ - **First-class functions** - Functions are treated as values that can be passed to other functions
12
+ - **Algebraic notation** - All operators can be used as functions, and functions that take two parameters can be used as operators
13
+ - **Clojure-inspired functions** - Most core functions are inspired by Clojure
14
+ - **Comprehensive standard library** - Rich set of functions for collections, math, strings, and more
15
+ - **Structural equality** - Objects are compared by value, not by reference
16
+ - **Destructuring** - Extract values from complex data structures with ease
8
17
 
9
- * **Dependencies**: No third party dependencies.
10
- * **Immutability**: All datatypes in Lits are immutable.
11
- * **Pure Functions**: Functions are [pure](https://en.wikipedia.org/wiki/Pure_function) by default. Functions with side effects have names ending in an exclamation mark (!), such as `write!` or `rand!`
12
- * **Type Mapping**: All datatypes in Lits map directly to JavaScript types.
13
- * **Evaluation**: Lits does not support lazy evaluation.
14
- * **Macros**:Macros are not supported in Lits.
15
- * **Keyword Symbols**: There are no keyword symbols. The notation `:foo` is simply shorthand for the string `"foo"`
16
- * **Scoping**: Lits uses [dynamic scoping](https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scope) not [lexical scoping](https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scope)
18
+ ## Installation
17
19
 
18
- ## Documentation
20
+ *[Add installation instructions here]*
19
21
 
20
- You can find the Lits playground [here](https://mojir.github.io/lits/#index). The playground allows you to interactively write and evaluate Lits expressions.
22
+ ## Quick Start
21
23
 
22
- ## Installation
24
+ Here's a simple example to get you started:
25
+
26
+ ```
27
+ // Defining a function
28
+ function square(x)
29
+ x * x
30
+ end;
31
+
32
+ // Using the function
33
+ let result := square(5);
34
+ // => 25
23
35
 
24
- To install Lits globally, run the following command:
36
+ // Using function as an operator
37
+ let squares := [1, 2, 3, 4, 5] map square;
38
+ // => [1, 4, 9, 16, 25]
25
39
 
40
+ // Using operator as a function
41
+ let sum := +([1, 2, 3, 4, 5]);
42
+ // => 15
26
43
  ```
27
- npm i -g @mojir/lits
44
+
45
+ ## Syntax
46
+
47
+ ### Basic Data Types
48
+
28
49
  ```
29
- ## Repl usage
30
- Initiate the Lits REPL in a terminal by typing `lits`. If Lits hasn't been installed globally, you can use `npx lits` instead.
50
+ // Numbers
51
+ 42 // integer
52
+ 3.14 // float
53
+ 0xFFFF // hexadecimal
54
+ 0b1100 // binary
55
+ 0o77 // octal
56
+ -2.3e-2 // scientific notation
57
+
58
+ // Strings
59
+ "Hello, world!"
31
60
 
32
- * Tab completion
33
- * History stored on file
61
+ // Booleans
62
+ true
63
+ false
34
64
 
65
+ // Null
66
+ null
67
+
68
+ // Arrays
69
+ [1, 2, 3, 4]
70
+
71
+ // Objects
72
+ { name := "John", age := 30 }
35
73
  ```
36
- $ lits
37
- Type "`help" for more information.
38
- > (+ 7 4)
39
- 11
40
- > (let ((day (* 24 60 60 1000))) (* 7 day)) ; Ever wondered how many milliseconds there are in a week?
41
- 604800000
74
+
75
+ ### Builtin Number Symbols
76
+
77
+ Lits provides a set of predefined mathematical constants that can be used directly in your code:
78
+
42
79
  ```
80
+ // Mathematical constants
81
+ PI // => 3.141592653589793
82
+ π // => 3.141592653589793 (Unicode alternative)
83
+ -PI // => -3.141592653589793
84
+ -π // => -3.141592653589793
85
+
86
+ E // => 2.718281828459045 (Euler's number)
87
+ ε // => 2.718281828459045 (Unicode alternative)
88
+ -E // => -2.718281828459045
89
+ -ε // => -2.718281828459045
90
+
91
+ PHI // => 1.618033988749895 (Golden ratio)
92
+ φ // => 1.618033988749895 (Unicode alternative)
93
+ -PHI // => -1.618033988749895
94
+ -φ // => -1.618033988749895
95
+
96
+ // Epsilon/Delta (smallest representable number)
97
+ DELTA // => 2.220446049250313e-16
98
+ δ // => 2.220446049250313e-16 (Unicode alternative)
99
+ -DELTA // => -2.220446049250313e-16
100
+ -δ // => -2.220446049250313e-16
101
+
102
+ // Infinity values
103
+ POSITIVE_INFINITY // => Infinity
104
+ ∞ // => Infinity (Unicode alternative)
105
+ NEGATIVE_INFINITY // => -Infinity
106
+ -∞ // => -Infinity (Unicode alternative)
107
+
108
+ // Integer limits
109
+ MAX_SAFE_INTEGER // => 9007199254740991
110
+ MIN_SAFE_INTEGER // => -9007199254740991
111
+
112
+ // Floating point limits
113
+ MAX_VALUE // => 1.7976931348623157e+308
114
+ MIN_VALUE // => 5e-324
115
+
116
+ // Not a Number
117
+ NaN // => NaN
43
118
  ```
44
- $ lits --help
45
- Usage: lits [options]
46
119
 
47
- Options:
48
- -c, --context=... Context as a JSON string
49
- -C, --context-file=... Context file (.json file)
50
- -e, --eval=... Evaluate Lits expression
51
- -f, --file=... Evaluate .lits file
52
- -p, --test-pattern=... Test name pattern, used together with --test
53
- -t, --test=... Test .test.lits file
54
- --help Show this help
55
- --version Print lits version
120
+ These constants can be used anywhere a number value is expected and help make mathematical code more readable and elegant.
121
+
122
+ ### Variable Binding
123
+
56
124
  ```
125
+ // Let expression
126
+ let x := 10;
127
+ // => 10
128
+
129
+ // Variables are immutable
130
+ let x := 20; // Error: x is already defined
131
+
132
+ // But can be shadowed in inner scopes
133
+ let y := do
134
+ let x := 20;
135
+ x
136
+ end;
137
+ // => 20, outer x is still 10
57
138
  ```
58
- $ lits -e "(/ 81 9)"
59
- 9
139
+
140
+ ### Functions
141
+
60
142
  ```
143
+ // Standard function definition
144
+ function add(a, b)
145
+ a + b
146
+ end;
61
147
 
62
- # API
63
- ## Install api
148
+ // Lambda functions
149
+ let add := (a, b) -> a + b;
64
150
 
151
+ // Short form with positional arguments
152
+ let add := -> $1 + $2;
153
+
154
+ // Single argument short form
155
+ let cube := x -> x ** 3;
156
+ let fourth := -> $ ** 4;
65
157
  ```
66
- npm i @mojir/lits
158
+
159
+ ### Control Flow
160
+
67
161
  ```
162
+ // If expression
163
+ if x > 10 then
164
+ "large"
165
+ else
166
+ "small"
167
+ end;
168
+ // => "large" (if x > 10) or "small" (if x <= 10)
169
+
170
+ // Unless expression (reversed if)
171
+ unless x > 10 then
172
+ "small"
173
+ else
174
+ "large"
175
+ end;
176
+ // => "small" (if x <= 10) or "large" (if x > 10)
68
177
 
69
- ## How to use?
178
+ // Switch expression
179
+ switch x
180
+ case 0 then "zero"
181
+ case 1 then "one"
182
+ case 2 then "two"
183
+ end;
184
+ // => "zero" (if x = 0), "one" (if x = 1), "two" (if x = 2), or null (otherwise)
70
185
 
71
- ```ts
72
- import { Lits } from '@mojir/lits'
186
+ // Cond expression
187
+ cond
188
+ case val < 5 then "S"
189
+ case val < 10 then "M"
190
+ case val < 15 then "L"
191
+ end ?? "No match";
192
+ // => "S" (if val < 5), "M" (if 5 <= val < 10), "L" (if 10 <= val < 15), or "No match" (otherwise)
73
193
 
74
- const lits = new Lits()
75
- lits.run("(+ 1 2 3 4)"); // returns 10
194
+ // Try/catch
195
+ try
196
+ riskyOperation()
197
+ catch (error)
198
+ "Error: " ++ error.message
199
+ end;
200
+ // => result of riskyOperation() or error message if an exception occurs
76
201
  ```
77
202
 
78
- ## Tokenization and Parsing
79
- Lits provides two important functions for working with Lisp expressions: `lits.tokenize` and `lits.parse`.
203
+ ### List Comprehension
80
204
 
81
- ### lits.tokenize
82
- The `lits.tokenize` function takes a string as input and returns a `TokenStream`. Tokens are the individual components of a Lisp expression, such as parentheses, symbols, numbers, and strings. Here's an example usage:
205
+ ```
206
+ // Simple for comprehension
207
+ for
208
+ each x of [0, 1, 2, 3, 4, 5], let y := x * 3, while even?(y)
209
+ do
210
+ y
211
+ end;
212
+ // => [0, 6, 12]
83
213
 
214
+ // Multiple generators
215
+ for (
216
+ each x of [1, 2, 3]
217
+ each y of [1, 2, 3], when x <= y
218
+ z of [1, 2, 3]
219
+ )
220
+ [x, y, z]
221
+ end;
222
+ // => [[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1], [1, 3, 2], [1, 3, 3],
223
+ // [2, 2, 1], [2, 2, 2], [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3],
224
+ // [3, 3, 1], [3, 3, 2], [3, 3, 3]]
84
225
  ```
85
- const lits = new Lits()
86
- const expression = "(+ 1 2)";
87
- const tokens = lits.tokenize(expression);
88
- console.log(tokens);
89
- // Output: {
90
- "tokens": [
91
- {
92
- "t": 101,
93
- "v": "("
94
- },
95
- {
96
- "t": 103,
97
- "v": "+"
98
- },
99
- {
100
- "t": 102,
101
- "v": "1"
102
- },
103
- {
104
- "t": 102,
105
- "v": "2"
106
- },
107
- {
108
- "t": 101,
109
- "v": ")"
110
- }
111
- ]
112
- }
226
+
227
+ ### Destructuring
228
+
229
+ ```
230
+ // Object destructuring
231
+ let { name, age } := { name := "John", age := 30 };
232
+ // name => "John"
233
+ // age => 30
234
+
235
+ // Array destructuring
236
+ let [nbr1, nbr2] := [1, 2, 3, 4];
237
+ // nbr1 => 1
238
+ // nbr2 => 2
239
+
240
+ // Destructuring in function parameters
241
+ function displayPerson({name, age})
242
+ name ++ " is " ++ str(age) ++ " years old"
243
+ end;
244
+
245
+ displayPerson({ name := "John", age := 30 });
246
+ // => "John is 30 years old"
247
+ ```
248
+
249
+ ## Operators and Functions
250
+
251
+ ### Function Usage vs Operator Usage
252
+
253
+ All functions that take two parameters can be used as operators:
254
+
255
+ ```
256
+ // As a function
257
+ max(5, 10);
258
+ // => 10
259
+
260
+ // As an operator
261
+ 5 max 10;
262
+ // => 10
263
+ ```
264
+
265
+ All operators can be used as functions:
266
+
267
+ ```
268
+ // As an operator
269
+ 5 + 3;
270
+ // => 8
271
+
272
+ // As a function
273
+ +(5, 3);
274
+ // => 8
113
275
  ```
114
276
 
115
- ### Lits.parse
116
- The `lits.parse` function accepts a `TokenStream` as an argument and returns an Abstract Syntax Tree (AST). By compiling Lits expressions into an AST, you can significantly enhance the evaluation speed, achieving approximately a tenfold improvement.
117
-
118
- The AST is represented as a JSON document, which facilitates straightforward serialization.
119
-
120
- To evaluate an AST, utilize the `lits.evaluate` function.
121
-
122
- ```
123
- const lits = new Lits()
124
- const expression = "(+ 1 2)";
125
- const tokens = lits.tokenize(expression);
126
- const ast = lits.parse(tokens)
127
- console.log(tokens);
128
- // Output: {
129
- "b": [
130
- {
131
- "t": 203,
132
- "n": "+",
133
- "p": [
134
- {
135
- "t": 201,
136
- "v": 1
137
- },
138
- {
139
- "t": 201,
140
- "v": 2
141
- }
142
- ]
143
- }
144
- ]
277
+ ### Parameter Order
278
+
279
+ Unlike Clojure, Lits favors subject-first parameter order:
280
+
281
+ ```
282
+ // Lits
283
+ filter([1, 2, 3, 4], odd?);
284
+ // => [1, 3]
285
+
286
+ // Equivalent Clojure
287
+ // (filter odd? [1 2 3 4])
288
+ ```
289
+
290
+ This makes operator usage more readable:
291
+
292
+ ```
293
+ [1, 2, 3, 4] filter odd?;
294
+ // => [1, 3]
295
+ ```
296
+
297
+ ## Built-in Functions
298
+
299
+ Lits comes with a comprehensive standard library of functions organized into categories:
300
+
301
+ ### Collection Functions
302
+ `count`, `get`, `get-in`, `contains?`, `assoc`, `assoc-in`, `++`, `not-empty`, `every?`, `not-every?`, `any?`, `not-any?`, `update`, `update-in`
303
+
304
+ ### Array Functions
305
+ `array`, `range`, `repeat`, `flatten`, `mapcat`
306
+
307
+ ### Sequence Functions
308
+ `nth`, `push`, `pop`, `unshift`, `shift`, `slice`, `splice`, `reductions`, `reduce`, `reduce-right`, `map`, `filter`, `position`, `index-of`, `last-index-of`, `some`, `reverse`, `first`, `second`, `last`, `rest`, `next`, `take`, `take-last`, `take-while`, `drop`, `drop-last`, `drop-while`, `sort`, `sort-by`, `distinct`, `remove`, `remove-at`, `split-at`, `split-with`, `frequencies`, `group-by`, `partition`, `partition-all`, `partition-by`, `starts-with?`, `ends-with?`, `interleave`, `interpose`
309
+
310
+ ### Math Functions
311
+ `+`, `-`, `*`, `/`, `mod`, `%`, `quot`, `inc`, `dec`, `√`, `∛`, `**`, `round`, `trunc`, `floor`, `ceil`, `min`, `max`, `abs`, `sign`, `log`, `log2`, `log10`, `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`
312
+
313
+ ### Functional Programming
314
+ `apply`, `identity`, `partial`, `comp`, `constantly`, `juxt`, `complement`, `every-pred`, `some-pred`, `fnull`
315
+
316
+ ### Object Functions
317
+ `dissoc`, `object`, `keys`, `vals`, `entries`, `find`, `merge`, `merge-with`, `zipmap`, `select-keys`
318
+
319
+ ### String Functions
320
+ `string-repeat`, `str`, `number`, `lower-case`, `upper-case`, `trim`, `trim-left`, `trim-right`, `pad-left`, `pad-right`, `split`, `split-lines`, `template`, `to-char-code`, `from-char-code`, `encode-base64`, `decode-base64`, `encode-uri-component`, `decode-uri-component`, `join`, `capitalize`, `blank?`
321
+
322
+ ### Predicates
323
+ `boolean?`, `null?`, `number?`, `string?`, `function?`, `integer?`, `array?`, `object?`, `coll?`, `seq?`, `regexp?`, `zero?`, `pos?`, `neg?`, `even?`, `odd?`, `finite?`, `nan?`, `negative-infinity?`, `positive-infinity?`, `false?`, `true?`, `empty?`, `not-empty?`
324
+
325
+ ### Regular Expressions
326
+ `regexp`, `match`, `replace`, `replace-all`
327
+
328
+ ### Bitwise Operations
329
+ `<<`, `>>`, `>>>`, `~`, `&`, `bit-and-not`, `|`, `^`, `bit-flip`, `bit-clear`, `bit-set`, `bit-test`
330
+
331
+ ### Assertions
332
+ `assert`, `assert=`, `assert!=`, `assert-gt`, `assert-lt`, `assert-gte`, `assert-lte`, `assert-true`, `assert-false`, `assert-truthy`, `assert-falsy`, `assert-null`, `assert-throws`, `assert-throws-error`, `assert-not-throws`
333
+
334
+ ## Modules and Exports
335
+
336
+ You can export definitions to make them available to other modules:
337
+
338
+ ```
339
+ // Exporting variables
340
+ export let magic-number := 42;
341
+ // => 42
342
+
343
+ // Exporting functions
344
+ export function square(x)
345
+ x * x
346
+ end;
347
+ ```
348
+
349
+ ## API
350
+
351
+ Lits provides a JavaScript API for embedding:
352
+
353
+ ```javascript
354
+ interface Lits {
355
+ getRuntimeInfo: () => LitsRuntimeInfo
356
+ run: (program: string, params?: ContextParams & FilePathParams) => unknown
357
+ context: (programOrAst: string | Ast, params?: ContextParams & FilePathParams) => Context
358
+ getUndefinedSymbols: (programOrAst: string | Ast, params: ContextParams) => Set<string>
359
+ tokenize: (program: string, tokenizeParams: FilePathParams & MinifyParams) => TokenStream
360
+ parse: (tokenStream: TokenStream) => Ast
361
+ evaluate: (ast: Ast, params: ContextParams) => unknown
362
+ transformSymbols: (tokenStream: TokenStream, transformer: (symbol: string) => string) => TokenStream
363
+ untokenize: (tokenStream: TokenStream) => string
364
+ apply: (fn: LitsFunction, fnParams: unknown[], params: ContextParams) => unknown
145
365
  }
146
366
  ```
147
367
 
368
+ ## Examples
369
+
370
+ ### Factorial Function
371
+
372
+ ```
373
+ function factorial(n)
374
+ if n <= 1 then
375
+ 1
376
+ else
377
+ n * factorial(n - 1)
378
+ end
379
+ end;
380
+
381
+ factorial(5);
382
+ // => 120
383
+ ```
384
+
385
+ ### Fibonacci Sequence
386
+
387
+ ```
388
+ function fib(n)
389
+ if n < 2 then
390
+ n
391
+ else
392
+ fib(n - 1) + fib(n - 2)
393
+ end
394
+ end;
395
+
396
+ // Generate the first 10 Fibonacci numbers
397
+ range(10) map fib;
398
+ // => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
399
+ ```
400
+
401
+ ### Working with Collections
402
+
403
+ ```
404
+ let people := [
405
+ { name := "Alice", age := 25 },
406
+ { name := "Bob", age := 30 },
407
+ { name := "Charlie", age := 35 },
408
+ { name := "Diana", age := 40 }
409
+ ];
410
+
411
+ // Get all names
412
+ people map (p -> p.name);
413
+ // => ["Alice", "Bob", "Charlie", "Diana"]
414
+
415
+ // Get people older than 30
416
+ people filter (p -> p.age > 30);
417
+ // => [{ name := "Charlie", age := 35 }, { name := "Diana", age := 40 }]
418
+
419
+ // Calculate average age
420
+ (people map (p -> p.age) reduce +) / count(people);
421
+ // => 32.5
422
+ ```
423
+
424
+ ## Contributing
425
+
426
+ *[Add contribution guidelines here]*
427
+
428
+ ## License
429
+
430
+ *[Add license information here]*