affinirum 1.0.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.
- package/LICENSE +661 -0
- package/README.md +301 -0
- package/dst/Affinirum.d.ts +73 -0
- package/dst/Affinirum.js +536 -0
- package/dst/Constant.d.ts +15 -0
- package/dst/Constant.js +26 -0
- package/dst/Constants.d.ts +2 -0
- package/dst/Constants.js +22 -0
- package/dst/Functions.d.ts +2 -0
- package/dst/Functions.js +85 -0
- package/dst/Keywords.d.ts +1 -0
- package/dst/Keywords.js +17 -0
- package/dst/Node.d.ts +13 -0
- package/dst/Node.js +22 -0
- package/dst/ParserFrame.d.ts +13 -0
- package/dst/ParserFrame.js +80 -0
- package/dst/ParserState.d.ts +54 -0
- package/dst/ParserState.js +562 -0
- package/dst/StaticScope.d.ts +13 -0
- package/dst/StaticScope.js +44 -0
- package/dst/Type.d.ts +72 -0
- package/dst/Type.js +196 -0
- package/dst/Value.d.ts +3 -0
- package/dst/Value.js +1 -0
- package/dst/Variable.d.ts +13 -0
- package/dst/Variable.js +25 -0
- package/dst/atom/ArrayAtom.d.ts +11 -0
- package/dst/atom/ArrayAtom.js +38 -0
- package/dst/atom/FunctionAtom.d.ts +17 -0
- package/dst/atom/FunctionAtom.js +54 -0
- package/dst/atom/ObjectAtom.d.ts +11 -0
- package/dst/atom/ObjectAtom.js +44 -0
- package/dst/atom/PrimitiveAtom.d.ts +8 -0
- package/dst/atom/PrimitiveAtom.js +26 -0
- package/dst/cjs/Affinirum.js +540 -0
- package/dst/cjs/Constant.js +30 -0
- package/dst/cjs/Constants.js +25 -0
- package/dst/cjs/Functions.js +88 -0
- package/dst/cjs/Keywords.js +20 -0
- package/dst/cjs/Node.js +26 -0
- package/dst/cjs/ParserFrame.js +84 -0
- package/dst/cjs/ParserState.js +566 -0
- package/dst/cjs/StaticScope.js +48 -0
- package/dst/cjs/Type.js +200 -0
- package/dst/cjs/Value.js +2 -0
- package/dst/cjs/Variable.js +29 -0
- package/dst/cjs/atom/ArrayAtom.js +42 -0
- package/dst/cjs/atom/FunctionAtom.js +58 -0
- package/dst/cjs/atom/ObjectAtom.js +48 -0
- package/dst/cjs/atom/PrimitiveAtom.js +30 -0
- package/dst/cjs/constant/Array.js +93 -0
- package/dst/cjs/constant/Boolean.js +25 -0
- package/dst/cjs/constant/Buffer.js +68 -0
- package/dst/cjs/constant/Enumerable.js +34 -0
- package/dst/cjs/constant/Float.js +85 -0
- package/dst/cjs/constant/Integer.js +104 -0
- package/dst/cjs/constant/Iterable.js +24 -0
- package/dst/cjs/constant/Number.js +59 -0
- package/dst/cjs/constant/Object.js +13 -0
- package/dst/cjs/constant/String.js +197 -0
- package/dst/cjs/constant/Timestamp.js +56 -0
- package/dst/cjs/constant/Unknown.js +101 -0
- package/dst/cjs/constant/notation/AN.js +46 -0
- package/dst/cjs/constant/notation/JSON.js +14 -0
- package/dst/cjs/index.js +19 -0
- package/dst/cjs/node/ArrayNode.js +34 -0
- package/dst/cjs/node/BlockNode.js +33 -0
- package/dst/cjs/node/CallNode.js +54 -0
- package/dst/cjs/node/ConstantNode.js +33 -0
- package/dst/cjs/node/LoopNode.js +34 -0
- package/dst/cjs/node/ObjectNode.js +42 -0
- package/dst/cjs/node/SwitchNode.js +45 -0
- package/dst/cjs/node/VariableNode.js +30 -0
- package/dst/cjs/package.json +3 -0
- package/dst/constant/Array.d.ts +22 -0
- package/dst/constant/Array.js +90 -0
- package/dst/constant/Boolean.d.ts +11 -0
- package/dst/constant/Boolean.js +22 -0
- package/dst/constant/Buffer.d.ts +10 -0
- package/dst/constant/Buffer.js +61 -0
- package/dst/constant/Enumerable.d.ts +5 -0
- package/dst/constant/Enumerable.js +31 -0
- package/dst/constant/Float.d.ts +22 -0
- package/dst/constant/Float.js +80 -0
- package/dst/constant/Integer.d.ts +10 -0
- package/dst/constant/Integer.js +100 -0
- package/dst/constant/Iterable.d.ts +3 -0
- package/dst/constant/Iterable.js +21 -0
- package/dst/constant/Number.d.ts +14 -0
- package/dst/constant/Number.js +56 -0
- package/dst/constant/Object.d.ts +7 -0
- package/dst/constant/Object.js +10 -0
- package/dst/constant/String.d.ts +34 -0
- package/dst/constant/String.js +179 -0
- package/dst/constant/Timestamp.d.ts +20 -0
- package/dst/constant/Timestamp.js +50 -0
- package/dst/constant/Unknown.d.ts +10 -0
- package/dst/constant/Unknown.js +95 -0
- package/dst/constant/notation/AN.d.ts +6 -0
- package/dst/constant/notation/AN.js +42 -0
- package/dst/constant/notation/JSON.d.ts +7 -0
- package/dst/constant/notation/JSON.js +10 -0
- package/dst/index.d.ts +3 -0
- package/dst/index.js +3 -0
- package/dst/node/ArrayNode.d.ts +12 -0
- package/dst/node/ArrayNode.js +30 -0
- package/dst/node/BlockNode.d.ts +12 -0
- package/dst/node/BlockNode.js +29 -0
- package/dst/node/CallNode.d.ts +14 -0
- package/dst/node/CallNode.js +50 -0
- package/dst/node/ConstantNode.d.ts +15 -0
- package/dst/node/ConstantNode.js +29 -0
- package/dst/node/LoopNode.d.ts +13 -0
- package/dst/node/LoopNode.js +30 -0
- package/dst/node/ObjectNode.d.ts +13 -0
- package/dst/node/ObjectNode.js +38 -0
- package/dst/node/SwitchNode.d.ts +14 -0
- package/dst/node/SwitchNode.js +41 -0
- package/dst/node/VariableNode.d.ts +14 -0
- package/dst/node/VariableNode.js +26 -0
- package/package.json +67 -0
package/README.md
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
# Affinirum
|
|
2
|
+
A fast, embeddable scripting language powered by a lightweight recursive descent parser,
|
|
3
|
+
designed to evaluate complex algorithmic logic with precision and flexibility.
|
|
4
|
+
Ideal for embedding in host applications, it offers clear syntax, dynamic typing,
|
|
5
|
+
and extensibility for custom operations—enabling rapid prototyping, advanced rule evaluation,
|
|
6
|
+
and safe script execution in constrained environments.
|
|
7
|
+
|
|
8
|
+
Supports algebraic and boolean expressions, variables, conditionals, loops, and a rich standard library for working with numbers, buffers, strings, arrays, and objects.
|
|
9
|
+
|
|
10
|
+
Runs in browser and NodeJS.
|
|
11
|
+
|
|
12
|
+
Target: ES2022 [browser+NodeJS][ESM+CJS].
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Efficient execution: Parse once, execute many times with different variable values
|
|
17
|
+
- Variable support: Input and statement-scoped variables
|
|
18
|
+
- Function support: Variadic and first-order function support for expressive scripting
|
|
19
|
+
- Optimized evaluation: Constant expression folding with type checks
|
|
20
|
+
- Comprehensive set of operators: Boolean, arithmetic, buffer, string, comparison, and indexing support
|
|
21
|
+
- Built-in functions: Rich set of mathematical and composition utilities
|
|
22
|
+
|
|
23
|
+
## Specifications
|
|
24
|
+
|
|
25
|
+
Script can contain multiple semicolon-separated expressions, and blocks of statements.
|
|
26
|
+
The value of a block is determined by the value of the last expression.
|
|
27
|
+
- Scientific notation is supported for floating point numbers, like *0.1281e+2*
|
|
28
|
+
- ISO Timestamps prefixed with **@**, like *@2025-05-11T19:09:21.320Z*
|
|
29
|
+
- Hexadecimal buffer values are prefixed with **#**, like *#10ab0901*
|
|
30
|
+
- String literals are enclosed in single (**'**), double (**"**), or backtick (**`**) quotes, like *'string value'*
|
|
31
|
+
|
|
32
|
+
Array is an ordered sequence of values of any type.
|
|
33
|
+
It is defined by comma-separated values enclosed in brackets (**[]**),
|
|
34
|
+
like *[0,1,2]*, *["a","b","c"]*.
|
|
35
|
+
<br>An empty array is represented as **[]**.
|
|
36
|
+
|
|
37
|
+
Array elements can be accessed using the dot (**.**) access operator with a index,
|
|
38
|
+
or using brackets with a zero-based numeric index, like *theArray.23*, *theArray[0]*, *theArray[10]*, *theArray[indexVar]*
|
|
39
|
+
|
|
40
|
+
Object is a container of named values of any type.
|
|
41
|
+
It is defined by comma-separated key-value pair enclosed in brackets (**[]**) where key is separated from value by colon (**:**),
|
|
42
|
+
like *["NumericProperty":100, "StringProperty":"abc"]*, *["a":0,"b":"str":"c":valueVar]*.
|
|
43
|
+
<br>An empty object is represented as **[\:]**.
|
|
44
|
+
|
|
45
|
+
Object properties can be accessed using the dot (**.**) access operator with a string literal,
|
|
46
|
+
or with brackets containing a string key or numeric index,
|
|
47
|
+
like *theObject."StringProperty"*, *theObject["StringProperty"]*, *theObject[indexVar]*
|
|
48
|
+
|
|
49
|
+
A function is a callable code unit that produces a value.
|
|
50
|
+
The set of built-in functions can be extended through configuration entries.
|
|
51
|
+
Additionally, subroutines (functions defined in code) can be created.
|
|
52
|
+
|
|
53
|
+
Valid variable and function names must start with a letter or underscore (**\_**)
|
|
54
|
+
and can be followed by any combination of alphanumeric characters or underscores,
|
|
55
|
+
like *x*, *\_a1*, *abc25*
|
|
56
|
+
|
|
57
|
+
Whitespace characters are ignored.
|
|
58
|
+
|
|
59
|
+
### Types
|
|
60
|
+
- **void** for value **null**
|
|
61
|
+
- **boolean** for values **true** and **false**
|
|
62
|
+
- **timestamp** for date-time values, millisecons since Unix epoch
|
|
63
|
+
- **float** IEEE 754 double-precision binary floating-point format: binary64
|
|
64
|
+
- **integer** for 64-bit integer values
|
|
65
|
+
- **buffer** for ordered sequences of bytes
|
|
66
|
+
- **string** for ordered sequences of characters, text strings
|
|
67
|
+
- **array** for ordered sequences of valuese
|
|
68
|
+
- **object** for sets of named values
|
|
69
|
+
- **function** for built-in, injected or script-defined subroutines
|
|
70
|
+
|
|
71
|
+
Type modifier **?** can be used to make any type optional (nullable).
|
|
72
|
+
<br>Examples: *float? optNumVar*, *array? optArrayVar*
|
|
73
|
+
|
|
74
|
+
Unknown or variant type is declared as **??**.
|
|
75
|
+
|
|
76
|
+
### Definitions
|
|
77
|
+
- Value grouping: **(...)**
|
|
78
|
+
- Unit grouping: **{...}**
|
|
79
|
+
- Value and unit separator: **,**
|
|
80
|
+
- Array element at numeric index, or object property by string key: **[]**
|
|
81
|
+
- Object property by string key or method function call: **.**
|
|
82
|
+
- Array definiton: **[item1, ...]**
|
|
83
|
+
- Object definition: **[propery1-key: property1-value, ...]**
|
|
84
|
+
- Subroutine definition: **~return-type(argument1-name: argument1-type, ...) {...}**
|
|
85
|
+
- Conditional switch definition, returns first or second value if prefix is true or false: **if condition {value1} else {value2}**,
|
|
86
|
+
and if **else** clause is ommited second value deemed null
|
|
87
|
+
- Loop definition, iteratively evaluates suffix while prefix is true, returns last evaluated value: **while condition {...}**
|
|
88
|
+
|
|
89
|
+
### Operators
|
|
90
|
+
- Boolean negation (Logical NOT): **!**
|
|
91
|
+
- Boolean disjunction (Logical OR): **|**
|
|
92
|
+
- Boolean conjunction (Logical AND): **&**
|
|
93
|
+
- Greater than: **>**
|
|
94
|
+
- Less than: **<**
|
|
95
|
+
- Greater than or equals to: **>=**
|
|
96
|
+
- Less than or equals to: **<=**
|
|
97
|
+
- Equals to: **==**
|
|
98
|
+
- Not equals to: **!=**
|
|
99
|
+
- Null coalescence: **?:**
|
|
100
|
+
- Arithmetic addition, or buffer, string, and array concatination: **+**
|
|
101
|
+
- Arithmetic subtraction or negation: **-**
|
|
102
|
+
- Arithmetic multiplication: **\***
|
|
103
|
+
- Arithmetic division: **/**
|
|
104
|
+
- Arithmetic remainder: **%**
|
|
105
|
+
- Exponentiation operator: **^**
|
|
106
|
+
- Assignment: **=**
|
|
107
|
+
- Boolean disjunction (Logical OR) assignment: **|=**
|
|
108
|
+
- Boolean conjunction (Logical AND) assignment: **&=**
|
|
109
|
+
- Arithmetic addition assignment: **+=**
|
|
110
|
+
- Arithmetic subtraction assignment: **-=**
|
|
111
|
+
- Arithmetic multiplication assignment: ***=**
|
|
112
|
+
- Arithmetic division assignment: **/=**
|
|
113
|
+
- Arithmetic remainder assignment: **%=**
|
|
114
|
+
|
|
115
|
+
### Constants
|
|
116
|
+
|
|
117
|
+
#### Boolean
|
|
118
|
+
- **boolean Boolean.Or(values:array...)** — Boolean disjunction
|
|
119
|
+
- **boolean Boolean.And(values: array...)** — Boolean conjunction
|
|
120
|
+
- **boolean Boolean.Not(value: boolean)** — Boolean negation
|
|
121
|
+
- **boolean? Boolean.Decode(value: buffer, offset?: integer)** — Decode boolean from buffer
|
|
122
|
+
- **boolean? Boolean.ParseBoolean()** — Parse boolean from string
|
|
123
|
+
|
|
124
|
+
#### Timestamp
|
|
125
|
+
- **timestamp Timestamp.Now()** — Current date and time
|
|
126
|
+
- **timestamp Timestamp.Epoch(value: float | integer, epoch?: timestamp)** — Create timestamp from milliseconds
|
|
127
|
+
- **timestamp? Boolean.Decode(value: buffer, offset?: integer)** — Decode timestamp from buffer
|
|
128
|
+
- **timestamp? Timestamp.Parse(value: string)** — Parse string to timestamp
|
|
129
|
+
|
|
130
|
+
#### Float
|
|
131
|
+
- **Float.NAN** — Not-a-number
|
|
132
|
+
- **Float.PositiveInfinity** — Positive infinity
|
|
133
|
+
- **Float.NegativeInfinity** — Negative infinity
|
|
134
|
+
- **Float.Epsilon** — Smallest positive float
|
|
135
|
+
- **float Float.Sum(values: array...)** — Numeric sum
|
|
136
|
+
- **float Float.Min(values: array...)** — Numeric minimum
|
|
137
|
+
- **float Float.Max(values: array...)** — Numeric maximum
|
|
138
|
+
- **float Float.Exponent()** — Exponent
|
|
139
|
+
- **float Float.Logarithm()** — Logarithm
|
|
140
|
+
- **float Float.Abs()** — Absolute value
|
|
141
|
+
- **float Float.Ceil()** — Ceil
|
|
142
|
+
- **float Float.Floor()** — Floor
|
|
143
|
+
- **float Float.Round()** — Rounded value
|
|
144
|
+
- **float Float.Truncate()** — Truncated value
|
|
145
|
+
- **float Float.Random(exclusiveTo: float)** — Random float up to value
|
|
146
|
+
- **float? Float.Decode(value: buffer, encoding: string, offset?: integer)** — Decode float from buffer
|
|
147
|
+
- **float? Float.Parse(value: string)** — Parse float from string
|
|
148
|
+
|
|
149
|
+
#### Integer
|
|
150
|
+
- **integer Integer.Sum(values: array...)** — Numeric sum
|
|
151
|
+
- **integer Integer.Min(values: array...)** — Numeric minimum
|
|
152
|
+
- **integer Integer.Max(values: array...)** — Numeric maximum
|
|
153
|
+
- **integer Integer.Random(exclusiveTo: integer)** — Random integer up to value
|
|
154
|
+
- **integer? Integer.Decode(value: buffer, encoding: string, offset?: integer)** — Decode integer from buffer
|
|
155
|
+
- **integer? Integer.Parse(value: string)** — Parse integer from string
|
|
156
|
+
|
|
157
|
+
#### String
|
|
158
|
+
- **string String.Random(length: integer)** — Random alphanumeric string
|
|
159
|
+
- **string? String.Decode(value: buffer, encoding?: string, offset?: integer, length?: integer)** — Decode buffer to string
|
|
160
|
+
|
|
161
|
+
#### Buffer
|
|
162
|
+
- **buffer Buffer.Random(length: integer)** — Buffer of given length filled with random bytes
|
|
163
|
+
- **buffer? Buffer.ParseBuffer(value: string)** — Parse buffer from hexadecimal string
|
|
164
|
+
|
|
165
|
+
#### Array
|
|
166
|
+
- **array Array.Join(values: array...)** — Join arrays of any depths into a single array
|
|
167
|
+
- **array Array.Range(inclusiveFrom: integer, exclusiveTo: integer)** — New array filled with integers in range
|
|
168
|
+
- **array Array.Unique()** — Array of unique values
|
|
169
|
+
- **array Array.Intersection(a1: array, a2: array)** — Common values from two arrays
|
|
170
|
+
- **array Array.Difference(a1: array, a2: array)** — Symmetrical difference between arrays
|
|
171
|
+
|
|
172
|
+
#### Object
|
|
173
|
+
- **object Object.Merge(values: array...)** — Merge multiple objects
|
|
174
|
+
|
|
175
|
+
#### AN
|
|
176
|
+
- **string AN.Format(value: string, whitespace?: string)** — Format string as AN
|
|
177
|
+
|
|
178
|
+
#### JSON
|
|
179
|
+
- **string JSON.Format(value: void | boolean | float | string | array | object, whitespace?: string)** — Format as JSON string
|
|
180
|
+
- **void | boolean | float? | string? | array? | object? JSON.Parse(value?: string)** — Parse JSON-formatted string
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
### Functions
|
|
184
|
+
|
|
185
|
+
#### General Functions
|
|
186
|
+
- **?? ??.Coalesce(otherwise: ??)** — Null coalescence
|
|
187
|
+
- **boolean ??.Equal(value: ??)** — Equals to
|
|
188
|
+
- **boolean ??.Unequal(value: ??)** — Not equals to
|
|
189
|
+
- **buffer float | integer | string.Encode(encoding?: string)** — Encode value to buffer
|
|
190
|
+
- **string boolean | float | integer | buffer | string | array | object.Format(radix?: integer, separator?: string)** — Format value to string
|
|
191
|
+
|
|
192
|
+
#### Array Functions
|
|
193
|
+
- **?? array.First(condition: function)** — First item satisfying condition
|
|
194
|
+
- **?? array.Last(condition: function)** — Last item satisfying condition
|
|
195
|
+
- **integer array.FirstIndex(condition: function)** — First index satisfying condition
|
|
196
|
+
- **integer array.LastIndex(condition: function)** — Last index satisfying condition
|
|
197
|
+
- **boolean array.Every(condition: function)** — All items satisfy condition
|
|
198
|
+
- **boolean array.Any(condition: function)** — Any item satisfies condition
|
|
199
|
+
- **array array.Reverse()** — Reversed array
|
|
200
|
+
- **array array.Flatten(depth: integer = 1)** — Flatten array
|
|
201
|
+
- **array array.Mutate(transformation: function)** — Transform items
|
|
202
|
+
- **array array.Filter(condition: function)** — Filtered array
|
|
203
|
+
- **?? array.Reduce(reducer: function)** — Reduced value
|
|
204
|
+
- **object array.Compose(generator: function)** — Compose object from array
|
|
205
|
+
- **array array.Prepend(items: array...)** — Prepend items
|
|
206
|
+
- **array array.Append(items: array...)** — Append items
|
|
207
|
+
|
|
208
|
+
#### Buffer Functions
|
|
209
|
+
- **buffer buffer.Byte(pos: integer)** — Byte at position
|
|
210
|
+
- **string Buffer.FormatBuffer(value: buffer)** — Hexadecimal string from buffer
|
|
211
|
+
|
|
212
|
+
#### Enumerable Functions
|
|
213
|
+
- **float | integer | buffer | string | array.Add(values:array...)** — Add or concatenate values
|
|
214
|
+
- **buffer | string | array.Slice(start?: integer, end?: integer)** — Slice section
|
|
215
|
+
- **buffer | string | array.Splice(start: integer, remove: integer, inject: array...)** — Splice section
|
|
216
|
+
- **buffer | string | array.Inject(start: integer, inject: array...)** — Inject section
|
|
217
|
+
|
|
218
|
+
#### Iterable Functions
|
|
219
|
+
- **integer buffer | string | array | object.Length()** — Get length
|
|
220
|
+
- **?? array? | object?.At(index: integer | string)** — Access by index or key
|
|
221
|
+
|
|
222
|
+
#### Number Functions
|
|
223
|
+
- **boolean float | integer.GreaterThan(value)** — Greater than
|
|
224
|
+
- **boolean float | integer.LessThan(value)** — Less than
|
|
225
|
+
- **boolean float | integer.GreaterOrEqual(value)** — Greater or equal
|
|
226
|
+
- **boolean float | integer.LessOrEqual(value)** — Less or equal
|
|
227
|
+
- **float | integer float | integer.Subtract(subtrahend)** — Subtract value
|
|
228
|
+
- **float | integer float | integer.Negate()** — Negate number
|
|
229
|
+
- **float | integer float | integer.Multiply(values:array...)** — Multiply values
|
|
230
|
+
- **float | integer float | integer.Divide(divisor)** — Divide value
|
|
231
|
+
- **float | integer float | integer.Remainder(divisor)** — Remainder
|
|
232
|
+
- **float | integer float | integer.Modulo(divisor)** — Modulo
|
|
233
|
+
- **float | integer float | integer.Power(exponent)** — Power
|
|
234
|
+
- **float | integer float | integer.Root(index)** — Root
|
|
235
|
+
- **float | integer float | integer.Cast()** — Cast to number
|
|
236
|
+
|
|
237
|
+
#### Object Functions
|
|
238
|
+
- **array object.Entries()** — Key-value pairs
|
|
239
|
+
- **array object.Keys()** — Object keys
|
|
240
|
+
- **array object.Values()** — Object values
|
|
241
|
+
|
|
242
|
+
#### String Functions
|
|
243
|
+
- **boolean string.Like(value: string)** — Alphanumeric equality
|
|
244
|
+
- **boolean string.Unlike(value: string)** — Alphanumeric inequality
|
|
245
|
+
- **boolean string.Contains(search: string, startPos?: integer, ignoreCaseSpaceEtc?: boolean)** — Substring check
|
|
246
|
+
- **boolean string.StartsWith(search: string, startPos?: integer, ignoreCaseSpaceEtc?: boolean)** — Prefix check
|
|
247
|
+
- **boolean string.EndsWith(search: string, endPos?: integer, ignoreCaseSpaceEtc?: boolean)** — Suffix check
|
|
248
|
+
- **string string.Char(pos: integer)** — Character at position
|
|
249
|
+
- **integer string.CharCode(pos: integer)** — Character code
|
|
250
|
+
- **string string.Alphanum()** — Alphanumeric digest
|
|
251
|
+
- **string string.Trim()** — Trim whitespace
|
|
252
|
+
- **string string.TrimStart()** — Trim leading whitespace
|
|
253
|
+
- **string string.TrimEnd()** — Trim trailing whitespace
|
|
254
|
+
- **string string.LowerCase()** — To lowercase
|
|
255
|
+
- **string string.UpperCase()** — To uppercase
|
|
256
|
+
- **array string.Split(separator: string = ' ')** — Split into array
|
|
257
|
+
|
|
258
|
+
#### Timestamp Functions
|
|
259
|
+
- **integer timestamp.Year(utc?: boolean)** — Get year
|
|
260
|
+
- **integer timestamp.Month(utc?: boolean)** — Get month
|
|
261
|
+
- **integer timestamp.MonthIndex(utc?: boolean)** — Month index
|
|
262
|
+
- **integer timestamp.WeekdayIndex(utc?: boolean)** — Weekday index
|
|
263
|
+
- **integer timestamp.Day(utc?: boolean)** — Day of month
|
|
264
|
+
- **integer timestamp.Hour(utc?: boolean)** — Hour
|
|
265
|
+
- **integer timestamp.Minute(utc?: boolean)** — Minute
|
|
266
|
+
- **integer timestamp.Second(utc?: boolean)** — Second
|
|
267
|
+
- **integer timestamp.Millisecond(utc?: boolean)** — Millisecond
|
|
268
|
+
- **integer timestamp.EpochTime(epoch?: timestamp)** — Epoch milliseconds
|
|
269
|
+
|
|
270
|
+
## Reference
|
|
271
|
+
|
|
272
|
+
Create instance of Affinirum class with a string containing script and optional compilation configuration.
|
|
273
|
+
During the parsing any alphanumeric sequence not identified as
|
|
274
|
+
number value, string value, operator, or a function name is assumed to be variable.
|
|
275
|
+
Evaluate the expression by providing variable values.
|
|
276
|
+
|
|
277
|
+
Sample code:
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
...
|
|
281
|
+
const expr = new Affinirum( 'x * (y + abc / 5) > 10' );
|
|
282
|
+
const value1 = expr.evaluate( { x: 10, y: 20, abc: 10 } ); // true
|
|
283
|
+
const value2 = expr.evaluate( { x: 1, y: 4, abc: 5 } ); // false
|
|
284
|
+
...
|
|
285
|
+
const arrExpr = new Affinirum( 'Integer.Sum([ 1, 2, 3, a, b, c ])' );
|
|
286
|
+
const valueSum = arrExpr.evaluate( { a: 10, b: 20, c: 30 } ); // 66
|
|
287
|
+
...
|
|
288
|
+
const objExpr = new Affinirum( '[`prop1`:a,`prop2`:`abc`].prop1+10' );
|
|
289
|
+
const oValue = objExpr.evaluate( { a: 50 } ); // 60
|
|
290
|
+
...
|
|
291
|
+
const iteratorExpr = new Affinirum(
|
|
292
|
+
'Float.Sum(arr1.Mutate(float(a:float)->a*2).Filter(boolean(a:float)->a>3))'
|
|
293
|
+
);
|
|
294
|
+
const iValue = iteratorExpr.evaluate( { arr1: [ 1, 2, 3 ] } ); // 10
|
|
295
|
+
...
|
|
296
|
+
const complexExpr = new Affinirum(
|
|
297
|
+
'var a=myvar1/10, const b=myvar2-100, a/b + b*a + 600'
|
|
298
|
+
);
|
|
299
|
+
const value = complexExpr.evaluate( { myvar1: 40, myvar2: 104 } ); // 4761
|
|
300
|
+
...
|
|
301
|
+
```
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Constant } from './Constant.js';
|
|
2
|
+
import { Variable } from './Variable.js';
|
|
3
|
+
import { Value } from './Value.js';
|
|
4
|
+
import { Type } from './Type.js';
|
|
5
|
+
import { Node } from './Node.js';
|
|
6
|
+
import { LoopNode } from './node/LoopNode.js';
|
|
7
|
+
import { SwitchNode } from './node/SwitchNode.js';
|
|
8
|
+
import { ParserFrame } from './ParserFrame.js';
|
|
9
|
+
import { ParserState } from './ParserState.js';
|
|
10
|
+
import { StaticScope } from './StaticScope.js';
|
|
11
|
+
export declare class Affinirum {
|
|
12
|
+
static readonly keywords: string[];
|
|
13
|
+
protected readonly _script: string;
|
|
14
|
+
protected readonly _strict: boolean;
|
|
15
|
+
protected readonly _root: Node;
|
|
16
|
+
protected readonly _vframes: Map<string, ParserFrame>;
|
|
17
|
+
protected readonly _variables: Map<string, Variable>;
|
|
18
|
+
protected readonly _constants: Map<string, Record<string, Constant>>;
|
|
19
|
+
protected readonly _functions: Map<string, Constant>;
|
|
20
|
+
protected readonly _scope: StaticScope;
|
|
21
|
+
/**
|
|
22
|
+
Creates compiled expression. Any parsed token not recognized as a constant or a function will be compiled as a variable.
|
|
23
|
+
@param script Math expression to compile.
|
|
24
|
+
@param config Optional expected type, strict mode, variable types, constant values and functions to add for the compilation.
|
|
25
|
+
If expected type is provided then expression return type is matched against it.
|
|
26
|
+
If strict mode is set then undeclared variables will not be allowed in expression.
|
|
27
|
+
*/
|
|
28
|
+
constructor(script: string, config?: {
|
|
29
|
+
type?: Type;
|
|
30
|
+
strict?: boolean;
|
|
31
|
+
variables?: Record<string, Type>;
|
|
32
|
+
});
|
|
33
|
+
/**
|
|
34
|
+
Returns original script text.
|
|
35
|
+
*/
|
|
36
|
+
get script(): String;
|
|
37
|
+
/**
|
|
38
|
+
Returns compiled expression return value type.
|
|
39
|
+
*/
|
|
40
|
+
get type(): Type;
|
|
41
|
+
/**
|
|
42
|
+
Returns string representing parsed node tree structure.
|
|
43
|
+
@returns Parsed expression string.
|
|
44
|
+
*/
|
|
45
|
+
toString(): string;
|
|
46
|
+
/**
|
|
47
|
+
Returns record with compiled variable names and expected types.
|
|
48
|
+
@returns Record with variable names and types.
|
|
49
|
+
*/
|
|
50
|
+
variables(): Record<string, Type>;
|
|
51
|
+
/**
|
|
52
|
+
Evaluates compiled expression using provided variable values.
|
|
53
|
+
@param values Record with variable names and values.
|
|
54
|
+
@returns Calculated value.
|
|
55
|
+
*/
|
|
56
|
+
evaluate(values?: Record<string, Value>): Value;
|
|
57
|
+
protected _block(state: ParserState, scope: StaticScope): Node;
|
|
58
|
+
protected _unit(state: ParserState, scope: StaticScope): Node;
|
|
59
|
+
protected _disjunction(state: ParserState, scope: StaticScope): Node;
|
|
60
|
+
protected _conjunction(state: ParserState, scope: StaticScope): Node;
|
|
61
|
+
protected _comparison(state: ParserState, scope: StaticScope): Node;
|
|
62
|
+
protected _aggregate(state: ParserState, scope: StaticScope): Node;
|
|
63
|
+
protected _product(state: ParserState, scope: StaticScope): Node;
|
|
64
|
+
protected _factor(state: ParserState, scope: StaticScope): Node;
|
|
65
|
+
protected _coalescence(state: ParserState, scope: StaticScope): Node;
|
|
66
|
+
protected _accessor(state: ParserState, scope: StaticScope): Node;
|
|
67
|
+
protected _term(state: ParserState, scope: StaticScope): Node;
|
|
68
|
+
protected _function(state: ParserState, scope: StaticScope): Node;
|
|
69
|
+
protected _loop(state: ParserState, scope: StaticScope): LoopNode;
|
|
70
|
+
protected _switch(state: ParserState, scope: StaticScope): SwitchNode;
|
|
71
|
+
protected _call(frame: ParserFrame, func: Constant, subnodes: Node[]): Node;
|
|
72
|
+
protected _type(state: ParserState, scope: StaticScope): Type;
|
|
73
|
+
}
|