@mojir/lits 2.0.23 → 2.1.1
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.
- package/README.md +392 -109
- package/dist/cli/cli/src/cliDocumentation/getCliType.d.ts +1 -1
- package/dist/cli/cli.js +7212 -9389
- package/dist/cli/reference/api.d.ts +21 -18
- package/dist/cli/reference/categories/array.d.ts +2 -2
- package/dist/cli/reference/categories/assert.d.ts +2 -2
- package/dist/cli/reference/categories/bitwise.d.ts +2 -2
- package/dist/cli/reference/categories/collection.d.ts +2 -2
- package/dist/cli/reference/categories/functional.d.ts +2 -2
- package/dist/cli/reference/categories/math.d.ts +1 -1
- package/dist/cli/reference/categories/misc.d.ts +1 -1
- package/dist/cli/reference/categories/object.d.ts +1 -1
- package/dist/cli/reference/categories/regularExpression.d.ts +1 -1
- package/dist/cli/reference/categories/sequence.d.ts +1 -1
- package/dist/cli/reference/categories/specialExpressions.d.ts +3 -3
- package/dist/cli/reference/categories/string.d.ts +1 -1
- package/dist/cli/reference/index.d.ts +225 -16
- package/dist/cli/src/Lits/Cache.d.ts +1 -1
- package/dist/cli/src/Lits/Lits.d.ts +15 -15
- package/dist/cli/src/builtin/bindingNode.d.ts +4 -0
- package/dist/cli/src/builtin/index.d.ts +10 -30
- package/dist/cli/src/builtin/interface.d.ts +19 -26
- package/dist/cli/src/builtin/normalExpressions/index.d.ts +4 -1
- package/dist/cli/src/builtin/specialExpressionTypes.d.ts +27 -0
- package/dist/cli/src/builtin/specialExpressions/and.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/array.d.ts +6 -0
- package/dist/cli/src/builtin/specialExpressions/cond.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/def.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/defined.d.ts +5 -0
- package/dist/cli/src/builtin/specialExpressions/do.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/functions.d.ts +7 -10
- package/dist/cli/src/builtin/specialExpressions/if.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/let.d.ts +3 -4
- package/dist/cli/src/builtin/specialExpressions/loop.d.ts +3 -4
- package/dist/cli/src/builtin/specialExpressions/loops.d.ts +5 -14
- package/dist/cli/src/builtin/specialExpressions/object.d.ts +6 -0
- package/dist/cli/src/builtin/specialExpressions/or.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/qq.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/recur.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/switch.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/throw.d.ts +3 -3
- package/dist/cli/src/builtin/specialExpressions/try.d.ts +3 -5
- package/dist/cli/src/builtin/specialExpressions/unless.d.ts +3 -3
- package/dist/cli/src/builtin/utils.d.ts +3 -15
- package/dist/cli/src/cliDocumentation/getCliType.d.ts +1 -1
- package/dist/cli/src/constants/constants.d.ts +18 -29
- package/dist/cli/src/errors.d.ts +1 -1
- package/dist/cli/src/evaluator/ContextStack.d.ts +9 -9
- package/dist/cli/src/evaluator/functionExecutors.d.ts +4 -4
- package/dist/cli/src/evaluator/index.d.ts +2 -2
- package/dist/cli/src/evaluator/interface.d.ts +4 -4
- package/dist/cli/src/getUndefinedSymbols/index.d.ts +7 -0
- package/dist/cli/src/interface.d.ts +1 -2
- package/dist/cli/src/parser/Parser.d.ts +49 -0
- package/dist/cli/src/parser/types.d.ts +111 -0
- package/dist/cli/src/symbolPatterns.d.ts +2 -0
- package/dist/cli/src/tokenizer/minifyTokenStream.d.ts +1 -1
- package/dist/cli/src/tokenizer/operators.d.ts +12 -0
- package/dist/cli/src/tokenizer/reservedNames.d.ts +115 -0
- package/dist/cli/src/tokenizer/token.d.ts +85 -0
- package/dist/cli/src/tokenizer/tokenize.d.ts +8 -0
- package/dist/cli/src/tokenizer/tokenizers.d.ts +12 -0
- package/dist/cli/src/transformer/index.d.ts +2 -2
- package/dist/cli/src/typeGuards/array.d.ts +1 -1
- package/dist/cli/src/typeGuards/astNode.d.ts +25 -17
- package/dist/cli/src/typeGuards/index.d.ts +4 -3
- package/dist/cli/src/typeGuards/lits.d.ts +2 -2
- package/dist/cli/src/typeGuards/litsFunction.d.ts +3 -3
- package/dist/cli/src/typeGuards/number.d.ts +1 -1
- package/dist/cli/src/typeGuards/string.d.ts +1 -1
- package/dist/cli/src/untokenizer/index.d.ts +1 -1
- package/dist/cli/src/utils/debug/getCodeMarker.d.ts +1 -1
- package/dist/cli/src/utils/debug/getSourceCodeInfo.d.ts +2 -2
- package/dist/cli/src/utils/getAssertionError.d.ts +1 -1
- package/dist/cli/src/utils/index.d.ts +5 -4
- package/dist/index.esm.js +7428 -9599
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +7427 -9604
- package/dist/index.js.map +1 -1
- package/dist/lits.iife.js +7427 -9604
- package/dist/lits.iife.js.map +1 -1
- package/dist/reference/api.d.ts +21 -18
- package/dist/reference/categories/array.d.ts +2 -2
- package/dist/reference/categories/assert.d.ts +2 -2
- package/dist/reference/categories/bitwise.d.ts +2 -2
- package/dist/reference/categories/collection.d.ts +2 -2
- package/dist/reference/categories/functional.d.ts +2 -2
- package/dist/reference/categories/math.d.ts +1 -1
- package/dist/reference/categories/misc.d.ts +1 -1
- package/dist/reference/categories/object.d.ts +1 -1
- package/dist/reference/categories/regularExpression.d.ts +1 -1
- package/dist/reference/categories/sequence.d.ts +1 -1
- package/dist/reference/categories/specialExpressions.d.ts +3 -3
- package/dist/reference/categories/string.d.ts +1 -1
- package/dist/reference/index.d.ts +225 -16
- package/dist/src/Lits/Cache.d.ts +1 -1
- package/dist/src/Lits/Lits.d.ts +15 -15
- package/dist/src/builtin/bindingNode.d.ts +4 -0
- package/dist/src/builtin/index.d.ts +10 -30
- package/dist/src/builtin/interface.d.ts +19 -26
- package/dist/src/builtin/normalExpressions/index.d.ts +4 -1
- package/dist/src/builtin/specialExpressionTypes.d.ts +27 -0
- package/dist/src/builtin/specialExpressions/and.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/array.d.ts +6 -0
- package/dist/src/builtin/specialExpressions/cond.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/def.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/defined.d.ts +5 -0
- package/dist/src/builtin/specialExpressions/do.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/functions.d.ts +7 -10
- package/dist/src/builtin/specialExpressions/if.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/let.d.ts +3 -4
- package/dist/src/builtin/specialExpressions/loop.d.ts +3 -4
- package/dist/src/builtin/specialExpressions/loops.d.ts +5 -14
- package/dist/src/builtin/specialExpressions/object.d.ts +6 -0
- package/dist/src/builtin/specialExpressions/or.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/qq.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/recur.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/switch.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/throw.d.ts +3 -3
- package/dist/src/builtin/specialExpressions/try.d.ts +3 -5
- package/dist/src/builtin/specialExpressions/unless.d.ts +3 -3
- package/dist/src/builtin/utils.d.ts +3 -15
- package/dist/src/constants/constants.d.ts +18 -29
- package/dist/src/errors.d.ts +1 -1
- package/dist/src/evaluator/ContextStack.d.ts +9 -9
- package/dist/src/evaluator/functionExecutors.d.ts +4 -4
- package/dist/src/evaluator/index.d.ts +2 -2
- package/dist/src/evaluator/interface.d.ts +4 -4
- package/dist/src/getUndefinedSymbols/index.d.ts +7 -0
- package/dist/src/index.d.ts +6 -9
- package/dist/src/interface.d.ts +1 -2
- package/dist/src/parser/Parser.d.ts +49 -0
- package/dist/src/parser/types.d.ts +111 -0
- package/dist/src/symbolPatterns.d.ts +2 -0
- package/dist/src/tokenizer/minifyTokenStream.d.ts +1 -1
- package/dist/src/tokenizer/operators.d.ts +12 -0
- package/dist/src/tokenizer/reservedNames.d.ts +115 -0
- package/dist/src/tokenizer/token.d.ts +85 -0
- package/dist/src/tokenizer/tokenize.d.ts +8 -0
- package/dist/src/tokenizer/tokenizers.d.ts +12 -0
- package/dist/src/transformer/index.d.ts +2 -2
- package/dist/src/typeGuards/array.d.ts +1 -1
- package/dist/src/typeGuards/astNode.d.ts +25 -17
- package/dist/src/typeGuards/index.d.ts +4 -3
- package/dist/src/typeGuards/lits.d.ts +2 -2
- package/dist/src/typeGuards/litsFunction.d.ts +3 -3
- package/dist/src/typeGuards/number.d.ts +1 -1
- package/dist/src/typeGuards/string.d.ts +1 -1
- package/dist/src/untokenizer/index.d.ts +1 -1
- package/dist/src/utils/debug/getCodeMarker.d.ts +1 -1
- package/dist/src/utils/debug/getSourceCodeInfo.d.ts +2 -2
- package/dist/src/utils/getAssertionError.d.ts +1 -1
- package/dist/src/utils/index.d.ts +5 -4
- package/dist/testFramework.esm.js +3103 -4362
- package/dist/testFramework.esm.js.map +1 -1
- package/dist/testFramework.js +3103 -4362
- package/dist/testFramework.js.map +1 -1
- package/package.json +2 -2
- package/dist/cli/common/clojureDocs.d.ts +0 -1
- package/dist/cli/src/analyze/findUnresolvedIdentifiers.d.ts +0 -2
- package/dist/cli/src/analyze/index.d.ts +0 -15
- package/dist/cli/src/analyze/utils.d.ts +0 -4
- package/dist/cli/src/builtin/specialExpressions/comment.d.ts +0 -5
- package/dist/cli/src/builtin/specialExpressions/commonParser.d.ts +0 -4
- package/dist/cli/src/builtin/specialExpressions/declared.d.ts +0 -5
- package/dist/cli/src/identifier.d.ts +0 -4
- package/dist/cli/src/index.d.ts +0 -18
- package/dist/cli/src/parser/AlgebraicParser.d.ts +0 -44
- package/dist/cli/src/parser/PolishTokenParsers.d.ts +0 -3
- package/dist/cli/src/parser/commonTokenParsers.d.ts +0 -7
- package/dist/cli/src/parser/index.d.ts +0 -3
- package/dist/cli/src/parser/interface.d.ts +0 -160
- package/dist/cli/src/removeCommentNodes/index.d.ts +0 -11
- package/dist/cli/src/removeCommentNodes/removeCommentNodesFromSpecialExpression.d.ts +0 -3
- package/dist/cli/src/tokenizer/algebraic/algebraicReservedNames.d.ts +0 -107
- package/dist/cli/src/tokenizer/algebraic/algebraicTokenizers.d.ts +0 -11
- package/dist/cli/src/tokenizer/algebraic/algebraicTokens.d.ts +0 -66
- package/dist/cli/src/tokenizer/common/commonTokenizers.d.ts +0 -9
- package/dist/cli/src/tokenizer/common/commonTokens.d.ts +0 -43
- package/dist/cli/src/tokenizer/index.d.ts +0 -2
- package/dist/cli/src/tokenizer/interface.d.ts +0 -22
- package/dist/cli/src/tokenizer/polish/polishReservedNames.d.ts +0 -7
- package/dist/cli/src/tokenizer/polish/polishTokenizers.d.ts +0 -13
- package/dist/cli/src/tokenizer/polish/polishTokens.d.ts +0 -55
- package/dist/cli/src/tokenizer/sugar/applyCollectionAccessor.d.ts +0 -2
- package/dist/cli/src/tokenizer/sugar/index.d.ts +0 -3
- package/dist/cli/src/tokenizer/tokens.d.ts +0 -20
- package/dist/cli/src/tokenizer/utils.d.ts +0 -9
- package/dist/common/clojureDocs.d.ts +0 -1
- package/dist/src/analyze/findUnresolvedIdentifiers.d.ts +0 -2
- package/dist/src/analyze/index.d.ts +0 -15
- package/dist/src/analyze/utils.d.ts +0 -4
- package/dist/src/builtin/specialExpressions/comment.d.ts +0 -5
- package/dist/src/builtin/specialExpressions/commonParser.d.ts +0 -4
- package/dist/src/builtin/specialExpressions/declared.d.ts +0 -5
- package/dist/src/identifier.d.ts +0 -4
- package/dist/src/parser/AlgebraicParser.d.ts +0 -44
- package/dist/src/parser/PolishTokenParsers.d.ts +0 -3
- package/dist/src/parser/commonTokenParsers.d.ts +0 -7
- package/dist/src/parser/index.d.ts +0 -3
- package/dist/src/parser/interface.d.ts +0 -160
- package/dist/src/removeCommentNodes/index.d.ts +0 -11
- package/dist/src/removeCommentNodes/removeCommentNodesFromSpecialExpression.d.ts +0 -3
- package/dist/src/tokenizer/algebraic/algebraicReservedNames.d.ts +0 -107
- package/dist/src/tokenizer/algebraic/algebraicTokenizers.d.ts +0 -11
- package/dist/src/tokenizer/algebraic/algebraicTokens.d.ts +0 -66
- package/dist/src/tokenizer/common/commonTokenizers.d.ts +0 -9
- package/dist/src/tokenizer/common/commonTokens.d.ts +0 -43
- package/dist/src/tokenizer/index.d.ts +0 -2
- package/dist/src/tokenizer/interface.d.ts +0 -22
- package/dist/src/tokenizer/polish/polishReservedNames.d.ts +0 -7
- package/dist/src/tokenizer/polish/polishTokenizers.d.ts +0 -13
- package/dist/src/tokenizer/polish/polishTokens.d.ts +0 -55
- package/dist/src/tokenizer/sugar/applyCollectionAccessor.d.ts +0 -2
- package/dist/src/tokenizer/sugar/index.d.ts +0 -3
- package/dist/src/tokenizer/tokens.d.ts +0 -20
- package/dist/src/tokenizer/utils.d.ts +0 -9
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
|
-
|
|
5
|
+
Try it in the [Lits Playground](https://mojir.github.io/lits/).
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
## Features
|
|
6
8
|
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
+
*[Add installation instructions here]*
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
## Quick Start
|
|
21
23
|
|
|
22
|
-
|
|
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
|
-
|
|
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
|
-
|
|
44
|
+
|
|
45
|
+
## Syntax
|
|
46
|
+
|
|
47
|
+
### Basic Data Types
|
|
48
|
+
|
|
28
49
|
```
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
59
|
-
|
|
139
|
+
|
|
140
|
+
### Functions
|
|
141
|
+
|
|
60
142
|
```
|
|
143
|
+
// Standard function definition
|
|
144
|
+
function add(a, b)
|
|
145
|
+
a + b
|
|
146
|
+
end;
|
|
61
147
|
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
72
|
-
|
|
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
|
-
|
|
75
|
-
|
|
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
|
-
|
|
79
|
-
Lits provides two important functions for working with Lisp expressions: `lits.tokenize` and `lits.parse`.
|
|
203
|
+
### List Comprehension
|
|
80
204
|
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
###
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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]*
|