formulang-ts 0.0.0-stage → 1.1.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/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/README.md +135 -2
- package/dist/abstractions/index.cjs +44 -0
- package/dist/abstractions/index.cjs.map +1 -0
- package/dist/abstractions/index.d.cts +48 -0
- package/dist/abstractions/index.d.ts +48 -0
- package/dist/abstractions/index.js +3 -0
- package/dist/abstractions/index.js.map +1 -0
- package/dist/chunk-BAFJXDWP.cjs +99 -0
- package/dist/chunk-BAFJXDWP.cjs.map +1 -0
- package/dist/chunk-LBF7G2HJ.js +89 -0
- package/dist/chunk-LBF7G2HJ.js.map +1 -0
- package/dist/chunk-N62Q5DFP.js +4310 -0
- package/dist/chunk-N62Q5DFP.js.map +1 -0
- package/dist/chunk-QWOQGFUA.cjs +4387 -0
- package/dist/chunk-QWOQGFUA.cjs.map +1 -0
- package/dist/environment-B6GN2zSd.d.ts +490 -0
- package/dist/environment-DPblIy7_.d.cts +490 -0
- package/dist/index.cjs +533 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +700 -0
- package/dist/index.d.ts +700 -0
- package/dist/index.js +187 -0
- package/dist/index.js.map +1 -0
- package/dist/processing-CnZQCacu.d.cts +398 -0
- package/dist/processing-CnZQCacu.d.ts +398 -0
- package/dist/stock/index.cjs +786 -0
- package/dist/stock/index.cjs.map +1 -0
- package/dist/stock/index.d.cts +329 -0
- package/dist/stock/index.d.ts +329 -0
- package/dist/stock/index.js +757 -0
- package/dist/stock/index.js.map +1 -0
- package/package.json +111 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<img src="./raw/images/icons/monogram.svg" height="50" alt="FormuLang-TS monogram">
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Changelog
|
|
7
|
+
|
|
8
|
+
</div>
|
|
9
|
+
|
|
10
|
+
All notable changes of **FormuLang-TS**. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); the library uses [Semantic Versioning](https://semver.org).
|
|
11
|
+
|
|
12
|
+
## [1.1.0] - 2026-11-25
|
|
13
|
+
|
|
14
|
+
**Supported standards:** `core-1.1`, `stock-1.1` (backward compatible with `core-1.0` and `stock-1.0`). Faster execution of formulas run for every candle or element (Iteration Context); the results of `1.0` formulas are the same.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- `core-1.1`: delegate calls `$f(x)`, `f(x)` for a lambda parameter, `$o.f(x)`, `$a[0](x)`, `((x) => x)(1)`, translated by the parser into the new function `CALL`; the invocation depth is limited to 256 (`MAX_INVOCATION_DEPTH`), and a too deep invocation (or a stack overflow) is an `OPERATION_ERROR` instead of a thrown `RangeError`.
|
|
18
|
+
- `core-1.1` functions: `FIRST`, `LAST`, `IFNULL`, `IFERR`, `ISINT`, `ISEVEN`, `ISODD`, `MEDIAN`, `VAR_P`, `VAR_S`, `STDEV_P`, `STDEV_S`, `MERGE`; the `precision` of `ROUND`, `FLOOR`, `CEIL` and `TRUNC` is optional.
|
|
19
|
+
- `stock-1.1` functions: `LAG(series, offset?)` and `WINDOW(series, length, handler?)` (`createLagFunction`, `createWindowFunction`).
|
|
20
|
+
- `STANDARDS` lists the earlier compatible versions too (`["core-1.0", "core-1.1"]`, then `"stock-1.0"`, `"stock-1.1"`), so `REQUIRE "core-1.0"` keeps working (`CoreStandard.compatibleIdentifiers`, `StockStandard.compatibleIdentifiers`, `STOCK_COMPATIBLE_IDENTIFIERS`).
|
|
21
|
+
- `FunctionOptions.sideEffectFree` (`new FlFunction(name, overloads, type, { sideEffectFree })`, `hostFunction(name, implementation, { sideEffectFree })`): a function that only computes its result. All Core and Stock functions are side-effect free; functions of the host application are not, unless they set it.
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
- The evaluator skips the branch of `IF` that is not selected when every function of the sequence (or of the value of an assignment, e.g. `$x = IF(...)`) is side-effect free and the condition is a **Boolean**. The selected value and the errors are the same as with both branches computed; a function with side effects, a procedure or a delegate from a variable keeps the computation of both branches. An indicator such as RSI or EMA with a first value computed by `IF(ISNULL(PREV), ...)` runs about 3-6 times faster.
|
|
25
|
+
- The expression of a Number created by `Value.number(value)` is formatted when it is first read, not when the value is created.
|
|
26
|
+
- `IterationContext.beginIteration` runs the pre-execution once instead of twice, keeps the copies of the environment variables that the formula did not change, and reuses the `PREV` constant.
|
|
27
|
+
- `LOAD_INDICATOR` checks the elements of a source once for every array (never for `CLOSE`) instead of in every call, and compares the elements of a source as numbers instead of serializing them to text, to find a series that is already computed.
|
|
28
|
+
|
|
29
|
+
## [1.0.0] - 2026-11-23
|
|
30
|
+
|
|
31
|
+
First release of the library.
|
|
32
|
+
|
|
33
|
+
**Supported standards:** `core-1.0`, `stock-1.0` ([`Adisoreq/FormuLang@c40e08a`](https://github.com/Adisoreq/FormuLang/tree/c40e08a)); same results as FormuLang.NET 1.0.0.
|
|
34
|
+
|
|
35
|
+
### Added
|
|
36
|
+
- Execution pipeline of the standard: pre-execution, validation, clean-up, tokenization, parsing (Pratt parser), evaluation and finalization, with replaceable stages (`Executor.with*`).
|
|
37
|
+
- Core standard `core-1.0`: all functions and procedures (including `FORMAT` and the time functions), operators, lambdas, objects, scoped comments `<# ... #>`, Unicode texts (characters are code points), serialization variables, `NOW` and `time_offset`, the Default, Iteration and Time Contexts (`Executor.createFrameRunner`, `executeFrames`).
|
|
38
|
+
- Stock standard `stock-1.0` (`formulang-ts/stock`): `Stock.Candle`, `CANDLE`, `IS_BULLISH`, `IS_BEARISH`, `LOAD_INDICATOR` and `LOAD_PATTERN` with the formula registry (`FormulaRegistry`), the Stock, Indicator, Pattern and Strategy contexts and `StockExecutor`.
|
|
39
|
+
- JavaScript facade: `evaluate`, `createEngine` / `Engine`, conversion between FormuLang and JavaScript values, host functions.
|
|
40
|
+
- Interfaces of every module in `formulang-ts/abstractions`.
|
|
41
|
+
- Conformance tests ported from FormuLang.NET and the JSON conformance tests of the standard (`scripts/sync-conformance.mjs`), samples (calculator CLI, web preview), documentation, CI and the npm release workflow.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Adrian Piznal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,136 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<img src="./raw/images/icons/logotype.svg" alt="FormuLang logo" width="500" />
|
|
4
|
+
|
|
5
|
+
**FormuLang-TS** is a multi-platform, modular TypeScript library implementing the [**FormuLang**](https://github.com/Adisoreq/FormuLang) standard.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
[](https://www.npmjs.com/package/formulang-ts)
|
|
9
|
+
[](docs/conformance.md)
|
|
10
|
+
|
|
11
|
+
[**Documentation**](docs/README.md) · [**Getting started**](docs/getting-started.md) · [**Changelog**](CHANGELOG.md)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
**Web preview** available here:
|
|
16
|
+
|
|
17
|
+
<a href="https://adisoreq.github.io/formulang-ts/">
|
|
18
|
+
<img src="https://img.shields.io/badge/%E2%96%B6-Run%20Web%20Preview-3178c6?style=for-the-badge&labelColor=1e4f8a" alt="Run Web Preview button" />
|
|
19
|
+
</a>
|
|
20
|
+
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
## Overview
|
|
24
|
+
|
|
25
|
+
[**FormuLang**](https://github.com/Adisoreq/FormuLang) is a formula-based scripting language standard: a host application embeds an interpreter and lets its users write text formulas such as below:
|
|
26
|
+
|
|
27
|
+
```php
|
|
28
|
+
$principal := 1000
|
|
29
|
+
$rate := 0.05
|
|
30
|
+
$principal * (1 + $rate) ^ 10
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**FormuLang-TS** is the implementation of that standard for TypeScript and JavaScript, in Node.js and in the browser. It turns a formula into a single result value by running the execution pipeline defined by the standard:
|
|
34
|
+
|
|
35
|
+
**Validation** → **Clean-up** → **Tokenization** → **Parsing** → **Evaluation**
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { evaluate } from "formulang-ts";
|
|
39
|
+
|
|
40
|
+
const result = evaluate("$principal * (1 + $rate) ^ 10", { variables: { principal: 1000, rate: 0.05 } });
|
|
41
|
+
|
|
42
|
+
if (result.ok) console.log(result.value); // 1628.894626777442
|
|
43
|
+
else console.log(result.error.toString()); // e.g. [UNKNOWN_SYMBOL_ERROR (5)] Variable "rate" is not defined.
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **Core 1.1** and **Stock 1.1** standards (backward compatible with 1.0): all Core functions and procedures, lambdas and delegate calls (`$f(x)`), objects, scoped comments, Unicode texts, `FORMAT`, the time and statistical functions and `NOW`, the Iteration and Time Contexts; the Stock context types, the formula registry of indicators and patterns, `CANDLE`, `IS_BULLISH`, `IS_BEARISH`, `LOAD_INDICATOR`, `LOAD_PATTERN`, `LAG` and `WINDOW`.
|
|
49
|
+
- **Modular**: one package with separate entry points – `formulang-ts`, `formulang-ts/abstractions` and `formulang-ts/stock` – each as an ES module and as CommonJS, with type declarations and no runtime dependencies.
|
|
50
|
+
- **Two levels of API**: a facade working with plain JavaScript values (`evaluate`, `createEngine`), and the pipeline API following the architecture of [FormuLang.NET](https://github.com/Adisoreq/formulang-dotnet) (environments, contexts, replaceable stages, typed function overloads).
|
|
51
|
+
- **Errors are values**: a formula never throws; every problem is a FormuLang Error with a code from the standard, and validation reports every problem at once.
|
|
52
|
+
- **Same results as the reference implementation**: the test cases of FormuLang.NET and the language-independent conformance tests of the standard are the tests of this library, and both implementations give identical outputs (see [Conformance](docs/conformance.md)).
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
The library is one package on [npm](https://www.npmjs.com/package/formulang-ts), with three entry points and no runtime dependencies:
|
|
57
|
+
|
|
58
|
+
| Import | Content |
|
|
59
|
+
| :----- | :------ |
|
|
60
|
+
| `formulang-ts` | Core standard, execution pipeline, JavaScript facade |
|
|
61
|
+
| `formulang-ts/stock` | Stock extension standard: candles, indicators, patterns, strategies |
|
|
62
|
+
| `formulang-ts/abstractions` | Interfaces shared by every module |
|
|
63
|
+
|
|
64
|
+
### 1. Check the requirements
|
|
65
|
+
|
|
66
|
+
Node.js 20 or newer, or any modern browser through a bundler (Vite, webpack, esbuild, ...). Check the installed version of Node.js:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
node --version
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 2. Add the package
|
|
73
|
+
|
|
74
|
+
In the directory of the project (the one with `package.json`):
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install formulang-ts
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
With other package managers: `yarn add formulang-ts`, `pnpm add formulang-ts` or `bun add formulang-ts`. The package contains type declarations, so TypeScript needs no extra `@types` package.
|
|
81
|
+
|
|
82
|
+
### 3. Run your first formula
|
|
83
|
+
|
|
84
|
+
The package works as an ES module and as CommonJS:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { evaluate } from "formulang-ts"; // ES module (TypeScript, bundlers, "type": "module")
|
|
88
|
+
// const { evaluate } = require("formulang-ts"); // CommonJS
|
|
89
|
+
|
|
90
|
+
const result = evaluate("$double = (x) => x * 2
|
|
91
|
+
$double(21)");
|
|
92
|
+
console.log(result.ok ? result.value : result.error.toString()); // 42
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### 4. Add the Stock standard (optional)
|
|
96
|
+
|
|
97
|
+
The Stock standard is a separate entry point of the same package, so there is nothing more to install:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { Environment } from "formulang-ts";
|
|
101
|
+
import { registerStock } from "formulang-ts/stock";
|
|
102
|
+
|
|
103
|
+
const environment = Environment.create();
|
|
104
|
+
const registry = registerStock(environment).registry; // LOAD_INDICATOR, LAG, WINDOW, Stock.Candle, stock-1.1
|
|
105
|
+
registry.indicators.add("SMA", "WINDOW(SOURCE, PARAMETERS.period, AVG)", { period: 20 });
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The next steps (contexts, iterations over candles, your own functions) are in [Getting started](docs/getting-started.md).
|
|
109
|
+
|
|
110
|
+
## Project status
|
|
111
|
+
|
|
112
|
+
The library implements the standards **Core 1.1** and **Stock 1.1**, backward compatible with their first stable versions, **Core 1.0** and **Stock 1.0**. From version `1.0.0` on it follows [Semantic Versioning](https://semver.org): changes within the `1.x` versions are backward compatible. See [Conformance](docs/conformance.md) for the details, the implementation choices and the proposals for the standard, and [Development](docs/development.md#releasing) for the releases.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Related projects
|
|
116
|
+
|
|
117
|
+
- [**FormuLang**](https://github.com/Adisoreq/FormuLang)
|
|
118
|
+
<br> Language standard, reference point for syntax and semantics rules.
|
|
119
|
+
|
|
120
|
+
- [**FormuLang.NET**](https://github.com/Adisoreq/formulang-dotnet)
|
|
121
|
+
<br> Implementation for C# (.NET 8.0), reference implementation of this library.
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
## Authors
|
|
125
|
+
|
|
126
|
+
Adrian Piznal
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
The library is licensed under the [MIT License](LICENSE).
|
|
132
|
+
The FormuLang standard documentation is licensed separately, under [CC BY 4.0](https://github.com/Adisoreq/FormuLang/blob/main/LICENSE.txt).
|
|
133
|
+
|
|
134
|
+
<div align="center">
|
|
135
|
+
<img src="raw/images/icons/monogram.svg" width="50" alt="Monogram" />
|
|
136
|
+
</div>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkBAFJXDWP_cjs = require('../chunk-BAFJXDWP.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "Associativity", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkBAFJXDWP_cjs.Associativity; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "ContainerType", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkBAFJXDWP_cjs.ContainerType; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "ContextType", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return chunkBAFJXDWP_cjs.ContextType; }
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "ExpressionKind", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return chunkBAFJXDWP_cjs.ExpressionKind; }
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports, "FunctionType", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function () { return chunkBAFJXDWP_cjs.FunctionType; }
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(exports, "OperatorType", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return chunkBAFJXDWP_cjs.OperatorType; }
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "PrimitiveType", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return chunkBAFJXDWP_cjs.PrimitiveType; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, "TokenType", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return chunkBAFJXDWP_cjs.TokenType; }
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, "VariableScope", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () { return chunkBAFJXDWP_cjs.VariableScope; }
|
|
42
|
+
});
|
|
43
|
+
//# sourceMappingURL=index.cjs.map
|
|
44
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export { j as ContainerType, C as ContextType, N as ExpressionKind, F as FunctionType, g as IAbstractTree, D as IAbstractTreeNode, n as IArgumentTemplate, E as ICallExpression, o as ICleanUpResult, y as ICleaner, G as IConstantExpression, i as IContainer, b as IContext, c as IEnvironment, d as IError, q as IEvaluationResult, B as IEvaluator, p as IExecutionResult, t as IExpressionNode, h as IFunction, l as IFunctionOverload, O as IInvokable, M as ILambda, H as ILambdaExpression, J as ILiteralExpression, I as IObject, P as IOperationResult, K as IParameterExpression, A as IParser, r as IParsingResult, Q as IPrimitive, m as IProcedure, s as ISequence, e as IStandard, f as IStandardVersion, v as IToken, u as ITokenizationResult, z as ITokenizer, k as IType, w as IValidationResult, x as IValidator, a as IValue, L as IVariableExpression, R as PrimitiveType, T as TokenType, V as VariableScope } from '../processing-CnZQCacu.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Contracts of operators.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/operator-parsing.md
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** Grouping of operators with equal binding power. */
|
|
10
|
+
declare const Associativity: {
|
|
11
|
+
/** `a - b - c` = `(a - b) - c` */
|
|
12
|
+
readonly Left: "Left";
|
|
13
|
+
/** `a ^ b ^ c` = `a ^ (b ^ c)` */
|
|
14
|
+
readonly Right: "Right";
|
|
15
|
+
/** Operators cannot be chained: `a < b < c` is a syntax error. */
|
|
16
|
+
readonly None: "None";
|
|
17
|
+
};
|
|
18
|
+
type Associativity = (typeof Associativity)[keyof typeof Associativity];
|
|
19
|
+
/** Roles of an operator token. The values are bit flags and can be combined. */
|
|
20
|
+
declare const OperatorType: {
|
|
21
|
+
readonly None: 0;
|
|
22
|
+
/** Separates operands: `A [operator] B` */
|
|
23
|
+
readonly Separator: 1;
|
|
24
|
+
/** Unary operator before its operand: `[operator] A` */
|
|
25
|
+
readonly Prefix: 2;
|
|
26
|
+
/** Unary operator after its operand: `A [operator]` */
|
|
27
|
+
readonly Suffix: 4;
|
|
28
|
+
/** Opening bracket. */
|
|
29
|
+
readonly OpenBracket: 8;
|
|
30
|
+
/** Closing bracket. */
|
|
31
|
+
readonly CloseBracket: 16;
|
|
32
|
+
/** Sequence boundary (new line, semicolon). Not an expression operator. */
|
|
33
|
+
readonly Boundary: 32;
|
|
34
|
+
/** Structural separator inside a delimiter (comma, colon). */
|
|
35
|
+
readonly ItemSeparator: 64;
|
|
36
|
+
};
|
|
37
|
+
/** Combination of {@link OperatorType} flags. */
|
|
38
|
+
type OperatorType = number;
|
|
39
|
+
interface IOperator {
|
|
40
|
+
readonly symbol: string;
|
|
41
|
+
readonly type: OperatorType;
|
|
42
|
+
/** Binding power. A higher binding power binds more tightly; 0 for tokens that do not compete for it. */
|
|
43
|
+
readonly priority: number;
|
|
44
|
+
readonly associativity: Associativity;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { Associativity, OperatorType };
|
|
48
|
+
export type { IOperator };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export { j as ContainerType, C as ContextType, N as ExpressionKind, F as FunctionType, g as IAbstractTree, D as IAbstractTreeNode, n as IArgumentTemplate, E as ICallExpression, o as ICleanUpResult, y as ICleaner, G as IConstantExpression, i as IContainer, b as IContext, c as IEnvironment, d as IError, q as IEvaluationResult, B as IEvaluator, p as IExecutionResult, t as IExpressionNode, h as IFunction, l as IFunctionOverload, O as IInvokable, M as ILambda, H as ILambdaExpression, J as ILiteralExpression, I as IObject, P as IOperationResult, K as IParameterExpression, A as IParser, r as IParsingResult, Q as IPrimitive, m as IProcedure, s as ISequence, e as IStandard, f as IStandardVersion, v as IToken, u as ITokenizationResult, z as ITokenizer, k as IType, w as IValidationResult, x as IValidator, a as IValue, L as IVariableExpression, R as PrimitiveType, T as TokenType, V as VariableScope } from '../processing-CnZQCacu.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Contracts of operators.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/operator-parsing.md
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** Grouping of operators with equal binding power. */
|
|
10
|
+
declare const Associativity: {
|
|
11
|
+
/** `a - b - c` = `(a - b) - c` */
|
|
12
|
+
readonly Left: "Left";
|
|
13
|
+
/** `a ^ b ^ c` = `a ^ (b ^ c)` */
|
|
14
|
+
readonly Right: "Right";
|
|
15
|
+
/** Operators cannot be chained: `a < b < c` is a syntax error. */
|
|
16
|
+
readonly None: "None";
|
|
17
|
+
};
|
|
18
|
+
type Associativity = (typeof Associativity)[keyof typeof Associativity];
|
|
19
|
+
/** Roles of an operator token. The values are bit flags and can be combined. */
|
|
20
|
+
declare const OperatorType: {
|
|
21
|
+
readonly None: 0;
|
|
22
|
+
/** Separates operands: `A [operator] B` */
|
|
23
|
+
readonly Separator: 1;
|
|
24
|
+
/** Unary operator before its operand: `[operator] A` */
|
|
25
|
+
readonly Prefix: 2;
|
|
26
|
+
/** Unary operator after its operand: `A [operator]` */
|
|
27
|
+
readonly Suffix: 4;
|
|
28
|
+
/** Opening bracket. */
|
|
29
|
+
readonly OpenBracket: 8;
|
|
30
|
+
/** Closing bracket. */
|
|
31
|
+
readonly CloseBracket: 16;
|
|
32
|
+
/** Sequence boundary (new line, semicolon). Not an expression operator. */
|
|
33
|
+
readonly Boundary: 32;
|
|
34
|
+
/** Structural separator inside a delimiter (comma, colon). */
|
|
35
|
+
readonly ItemSeparator: 64;
|
|
36
|
+
};
|
|
37
|
+
/** Combination of {@link OperatorType} flags. */
|
|
38
|
+
type OperatorType = number;
|
|
39
|
+
interface IOperator {
|
|
40
|
+
readonly symbol: string;
|
|
41
|
+
readonly type: OperatorType;
|
|
42
|
+
/** Binding power. A higher binding power binds more tightly; 0 for tokens that do not compete for it. */
|
|
43
|
+
readonly priority: number;
|
|
44
|
+
readonly associativity: Associativity;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { Associativity, OperatorType };
|
|
48
|
+
export type { IOperator };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/abstractions/primitives.ts
|
|
4
|
+
var PrimitiveType = {
|
|
5
|
+
/** Literal value, such as a number or a text. */
|
|
6
|
+
Value: "Value",
|
|
7
|
+
/** Container of a value: a variable or a constant. */
|
|
8
|
+
Container: "Container",
|
|
9
|
+
/** Structural relation transforming an input set into a determined output. */
|
|
10
|
+
Function: "Function"
|
|
11
|
+
};
|
|
12
|
+
var ContainerType = {
|
|
13
|
+
/** Mutable value container. */
|
|
14
|
+
Variable: "Variable",
|
|
15
|
+
/** Immutable value container. */
|
|
16
|
+
Constant: "Constant"
|
|
17
|
+
};
|
|
18
|
+
var FunctionType = {
|
|
19
|
+
/** A standard function that takes arguments and returns a value. */
|
|
20
|
+
Function: "Function",
|
|
21
|
+
/** Function that operates on the context and returns an Error (`SUCCESS` when it succeeds). */
|
|
22
|
+
Procedure: "Procedure",
|
|
23
|
+
/** Temporary anonymous function created by a lambda expression. */
|
|
24
|
+
Lambda: "Lambda"
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// src/abstractions/processing.ts
|
|
28
|
+
var TokenType = {
|
|
29
|
+
None: "None",
|
|
30
|
+
Operator: "Operator",
|
|
31
|
+
Number: "Number",
|
|
32
|
+
Text: "Text",
|
|
33
|
+
Identifier: "Identifier"
|
|
34
|
+
};
|
|
35
|
+
var ExpressionKind = {
|
|
36
|
+
/** Literal value: number, text, `true`, `false`, `null`. */
|
|
37
|
+
Value: "Value",
|
|
38
|
+
/** Variable with a `$` (context) or `@` (environment) sigil. */
|
|
39
|
+
Variable: "Variable",
|
|
40
|
+
/** Identifier without a sigil or parentheses: a constant, or a named function used as a Delegate. */
|
|
41
|
+
Constant: "Constant",
|
|
42
|
+
/** Lambda parameter inside a lambda body. Unresolved until the lambda is invoked. */
|
|
43
|
+
Parameter: "Parameter",
|
|
44
|
+
/** Function or procedure call; every operator is translated into one. */
|
|
45
|
+
FunctionCall: "FunctionCall",
|
|
46
|
+
/** Lambda expression; its body is a separate tree. */
|
|
47
|
+
Lambda: "Lambda"
|
|
48
|
+
};
|
|
49
|
+
var VariableScope = {
|
|
50
|
+
Context: "Context",
|
|
51
|
+
Environment: "Environment"
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// src/abstractions/operators.ts
|
|
55
|
+
var Associativity = {
|
|
56
|
+
/** `a - b - c` = `(a - b) - c` */
|
|
57
|
+
Left: "Left",
|
|
58
|
+
/** `a ^ b ^ c` = `a ^ (b ^ c)` */
|
|
59
|
+
Right: "Right",
|
|
60
|
+
/** Operators cannot be chained: `a < b < c` is a syntax error. */
|
|
61
|
+
None: "None"
|
|
62
|
+
};
|
|
63
|
+
var OperatorType = {
|
|
64
|
+
None: 0,
|
|
65
|
+
/** Separates operands: `A [operator] B` */
|
|
66
|
+
Separator: 1,
|
|
67
|
+
/** Unary operator before its operand: `[operator] A` */
|
|
68
|
+
Prefix: 2,
|
|
69
|
+
/** Unary operator after its operand: `A [operator]` */
|
|
70
|
+
Suffix: 4,
|
|
71
|
+
/** Opening bracket. */
|
|
72
|
+
OpenBracket: 8,
|
|
73
|
+
/** Closing bracket. */
|
|
74
|
+
CloseBracket: 16,
|
|
75
|
+
/** Sequence boundary (new line, semicolon). Not an expression operator. */
|
|
76
|
+
Boundary: 32,
|
|
77
|
+
/** Structural separator inside a delimiter (comma, colon). */
|
|
78
|
+
ItemSeparator: 64
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/abstractions/structures.ts
|
|
82
|
+
var ContextType = {
|
|
83
|
+
/** Created for a single execution and disposed after it. */
|
|
84
|
+
Disposable: "Disposable",
|
|
85
|
+
/** Kept by the host application and reused across executions. */
|
|
86
|
+
Persistent: "Persistent"
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
exports.Associativity = Associativity;
|
|
90
|
+
exports.ContainerType = ContainerType;
|
|
91
|
+
exports.ContextType = ContextType;
|
|
92
|
+
exports.ExpressionKind = ExpressionKind;
|
|
93
|
+
exports.FunctionType = FunctionType;
|
|
94
|
+
exports.OperatorType = OperatorType;
|
|
95
|
+
exports.PrimitiveType = PrimitiveType;
|
|
96
|
+
exports.TokenType = TokenType;
|
|
97
|
+
exports.VariableScope = VariableScope;
|
|
98
|
+
//# sourceMappingURL=chunk-BAFJXDWP.cjs.map
|
|
99
|
+
//# sourceMappingURL=chunk-BAFJXDWP.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/abstractions/primitives.ts","../src/abstractions/processing.ts","../src/abstractions/operators.ts","../src/abstractions/structures.ts"],"names":[],"mappings":";;;AAQO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,QAAA,EAAU;AACZ;AAIO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,QAAA,EAAU;AACZ;AAIO,IAAM,YAAA,GAAe;AAAA;AAAA,EAE1B,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,MAAA,EAAQ;AACV;;;ACzBO,IAAM,SAAA,GAAY;AAAA,EACvB,IAAA,EAAM,MAAA;AAAA,EACN,QAAA,EAAU,UAAA;AAAA,EACV,MAAA,EAAQ,QAAA;AAAA,EACR,IAAA,EAAM,MAAA;AAAA,EACN,UAAA,EAAY;AACd;AAIO,IAAM,cAAA,GAAiB;AAAA;AAAA,EAE5B,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,YAAA,EAAc,cAAA;AAAA;AAAA,EAEd,MAAA,EAAQ;AACV;AAIO,IAAM,aAAA,GAAgB;AAAA,EAC3B,OAAA,EAAS,SAAA;AAAA,EACT,WAAA,EAAa;AACf;;;AChCO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,IAAA,EAAM,MAAA;AAAA;AAAA,EAEN,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,IAAA,EAAM;AACR;AAIO,IAAM,YAAA,GAAe;AAAA,EAC1B,IAAA,EAAM,CAAA;AAAA;AAAA,EAEN,SAAA,EAAW,CAAA;AAAA;AAAA,EAEX,MAAA,EAAQ,CAAA;AAAA;AAAA,EAER,MAAA,EAAQ,CAAA;AAAA;AAAA,EAER,WAAA,EAAa,CAAA;AAAA;AAAA,EAEb,YAAA,EAAc,EAAA;AAAA;AAAA,EAEd,QAAA,EAAU,EAAA;AAAA;AAAA,EAEV,aAAA,EAAe;AACjB;;;ACxBO,IAAM,WAAA,GAAc;AAAA;AAAA,EAEzB,UAAA,EAAY,YAAA;AAAA;AAAA,EAEZ,UAAA,EAAY;AACd","file":"chunk-BAFJXDWP.cjs","sourcesContent":["/**\n * Contracts of the FormuLang primitives: values, containers and types.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/primitives.md\n * @module\n */\n\n/** Kind of a primitive (docs/core/primitives.md). */\nexport const PrimitiveType = {\n /** Literal value, such as a number or a text. */\n Value: \"Value\",\n /** Container of a value: a variable or a constant. */\n Container: \"Container\",\n /** Structural relation transforming an input set into a determined output. */\n Function: \"Function\",\n} as const;\nexport type PrimitiveType = (typeof PrimitiveType)[keyof typeof PrimitiveType];\n\n/** Kind of a container (docs/core/containers.md). */\nexport const ContainerType = {\n /** Mutable value container. */\n Variable: \"Variable\",\n /** Immutable value container. */\n Constant: \"Constant\",\n} as const;\nexport type ContainerType = (typeof ContainerType)[keyof typeof ContainerType];\n\n/** Kind of a function (docs/core/functions.md). */\nexport const FunctionType = {\n /** A standard function that takes arguments and returns a value. */\n Function: \"Function\",\n /** Function that operates on the context and returns an Error (`SUCCESS` when it succeeds). */\n Procedure: \"Procedure\",\n /** Temporary anonymous function created by a lambda expression. */\n Lambda: \"Lambda\",\n} as const;\nexport type FunctionType = (typeof FunctionType)[keyof typeof FunctionType];\n\n/**\n * Type of a primitive: a base type (`Number`, `Object`, ...) and an optional extended type (`Stock.Candle`).\n * An extended type always derives from `Object` (docs/core/types.md).\n */\nexport interface IType {\n readonly base: string;\n readonly extended: string | undefined;\n isNull(): boolean;\n isUndefined(): boolean;\n /** Null, Undefined, Boolean, Number or Text (passed by value). */\n isSimple(): boolean;\n /** Error, Array, Object or Delegate (passed by reference). */\n isComplex(): boolean;\n hasExtension(): boolean;\n /** `Number`, `Object<Stock.Candle>` */\n toString(): string;\n}\n\n/** Error value: a code, a name and details. Code `0` (`SUCCESS`) is the only Error that does not abort an execution. */\nexport interface IError {\n readonly code: number;\n readonly name: string;\n readonly message: string;\n isSuccess(): boolean;\n isFailed(): boolean;\n /** `[NAME (code)] message` */\n toString(): string;\n}\n\n/** Root of everything that holds or produces a value. */\nexport interface IPrimitive {\n readonly primitiveType: PrimitiveType;\n readonly type: IType;\n /** Canonical expression of the primitive, e.g. `\"text\"` or `$x`. */\n readonly expression: string;\n readonly canAccessValue: boolean;\n /** The value, or an Error value when it cannot be read (e.g. `UNDEFINED_ERROR`). */\n getValue(): IValue;\n /** `SUCCESS` if the value can be changed, otherwise the Error that a change would return. */\n readonly canUpdateValue: IError;\n setValue(value: IValue): IError;\n}\n\n/**\n * A value. `raw` holds the JavaScript representation of the value:\n *\n * | Base type | `raw` |\n * | :-- | :-- |\n * | Null, Undefined | `null` |\n * | Boolean | `boolean` |\n * | Number | `number` |\n * | Text | `string` |\n * | Error | {@link IError} |\n * | Array | `readonly IValue[]` |\n * | Object | {@link IObject} |\n * | Delegate | `IFunction` |\n */\nexport interface IValue extends IPrimitive {\n readonly raw: unknown;\n}\n\n/** Named container of a value: a variable or a constant. */\nexport interface IContainer extends IPrimitive {\n readonly name: string;\n readonly containerType: ContainerType;\n readonly mutable: boolean;\n}\n\n/** Ordered set of named containers. Objects, contexts and environments are objects. */\nexport interface IObject {\n /** Keys in the order in which they were defined (docs/standards/core/functions/KEYS.md). */\n readonly keys: readonly string[];\n isEmpty(): boolean;\n hasKey(key: string): boolean;\n /** The container stored under the key, or `undefined` if there is none. */\n getProperty(key: string): IContainer | undefined;\n /** Changes the value of an existing container; `INVALID_KEY_ERROR` if the key does not exist. */\n setProperty(key: string, value: IValue): IError;\n /** Adds a constant; `false` if the key already exists. */\n initConstant(key: string, value: IValue): boolean;\n /** Adds a variable; `false` if the key already exists. */\n initVariable(key: string, value: IValue): boolean;\n}\n","/**\n * Contracts of the execution pipeline: validation, clean-up, tokenization, parsing and evaluation.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/execution.md\n * @module\n */\nimport type { IError, IValue } from \"./primitives.js\";\nimport type { IContext, IEnvironment } from \"./structures.js\";\n\n/** Type of a token (docs/semantics/token.md). */\nexport const TokenType = {\n None: \"None\",\n Operator: \"Operator\",\n Number: \"Number\",\n Text: \"Text\",\n Identifier: \"Identifier\",\n} as const;\nexport type TokenType = (typeof TokenType)[keyof typeof TokenType];\n\n/** Classification of an expression in the primitive map (docs/semantics/parsing.md, \"Primitive classification\"). */\nexport const ExpressionKind = {\n /** Literal value: number, text, `true`, `false`, `null`. */\n Value: \"Value\",\n /** Variable with a `$` (context) or `@` (environment) sigil. */\n Variable: \"Variable\",\n /** Identifier without a sigil or parentheses: a constant, or a named function used as a Delegate. */\n Constant: \"Constant\",\n /** Lambda parameter inside a lambda body. Unresolved until the lambda is invoked. */\n Parameter: \"Parameter\",\n /** Function or procedure call; every operator is translated into one. */\n FunctionCall: \"FunctionCall\",\n /** Lambda expression; its body is a separate tree. */\n Lambda: \"Lambda\",\n} as const;\nexport type ExpressionKind = (typeof ExpressionKind)[keyof typeof ExpressionKind];\n\n/** Scope of a variable: `$` (context) or `@` (environment). */\nexport const VariableScope = {\n Context: \"Context\",\n Environment: \"Environment\",\n} as const;\nexport type VariableScope = (typeof VariableScope)[keyof typeof VariableScope];\n\n/** Result of a stage. A stage never throws for a problem of the formula; it returns a failed result. */\nexport interface IOperationResult {\n /** `SUCCESS`, or the Error that stopped the stage. */\n readonly error: IError;\n readonly successful: boolean;\n}\n\nexport interface IToken {\n readonly type: TokenType;\n readonly value: string;\n}\n\n// Stage 1: validation\n\nexport interface IValidationResult extends IOperationResult {\n /** Errors in the order of detection; empty if the formula is valid. {@link IOperationResult.error} is the first of them. */\n readonly errors: readonly IError[];\n}\n\n/** Validation stage: a fast, shallow check of the raw formula and the environment that reports every problem at once. */\nexport interface IValidator {\n validate(formula: string, environment: IEnvironment): IValidationResult;\n}\n\n// Stage 2: clean-up\n\nexport interface ICleanUpResult extends IOperationResult {\n readonly result: string;\n}\n\nexport interface ICleaner {\n cleanUp(expression: string): ICleanUpResult;\n}\n\n// Stage 3: tokenization\n\nexport interface ITokenizationResult extends IOperationResult {\n readonly tokens: readonly IToken[];\n}\n\nexport interface ITokenizer {\n tokenize(expression: string): ITokenizationResult;\n}\n\n// Stage 4: parsing\n\ninterface ExpressionNodeBase {\n /** Canonical form of the expression, e.g. `ADD(POW($a,2),POW($b,2))`. Key of the primitive map. */\n readonly key: string;\n readonly kind: ExpressionKind;\n /** Depth of the node: 1 for the root of a sequence. */\n readonly level: number;\n readonly children: readonly IExpressionNode[];\n}\n\nexport interface ILiteralExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Value;\n readonly value: IValue;\n}\n\nexport interface IVariableExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Variable;\n readonly scope: VariableScope;\n readonly name: string;\n}\n\nexport interface IConstantExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Constant;\n readonly name: string;\n}\n\nexport interface IParameterExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Parameter;\n readonly name: string;\n}\n\nexport interface ICallExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.FunctionCall;\n /** Name of the called function or procedure. */\n readonly name: string;\n}\n\nexport interface ILambdaExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Lambda;\n readonly parameters: readonly string[];\n /** Body of the lambda: a separate tree with its own parsing queue. */\n readonly body: ISequence;\n /** Primitive map of the body. */\n readonly primitiveMap: ReadonlyMap<string, IExpressionNode>;\n}\n\n/** Node of an expression (reference) tree. The `kind` discriminates the node types. */\nexport type IExpressionNode =\n | ILiteralExpression\n | IVariableExpression\n | IConstantExpression\n | IParameterExpression\n | ICallExpression\n | ILambdaExpression;\n\n/** One top-level expression of a formula (docs/core/formula.md, \"Formula sequences\"). */\nexport interface ISequence {\n readonly root: IExpressionNode;\n /** Parsing queue: the nodes ordered bottom-up (highest level first, left to right within a level). The root is last. */\n readonly queue: readonly IExpressionNode[];\n}\n\nexport interface IParsingResult extends IOperationResult {\n /** Sequences in formula order, each with its own tree and parsing queue. */\n readonly sequences: readonly ISequence[];\n /**\n * Primitive map shared by the whole formula: every unique expression, by its canonical key.\n * Lambda bodies have their own maps and are not included.\n */\n readonly primitiveMap: ReadonlyMap<string, IExpressionNode>;\n}\n\nexport interface IParser {\n parse(tokens: readonly IToken[]): IParsingResult;\n}\n\n// Stage 5: evaluation\n\nexport interface IEvaluationResult extends IOperationResult {\n /** Value of the last evaluated sequence, or the Error that aborted the execution. */\n readonly value: IValue;\n}\n\n/** Evaluation stage: resolves the parsed sequences into values, in the order of their parsing queues. */\nexport interface IEvaluator {\n evaluate(formula: IParsingResult, context: IContext): IEvaluationResult;\n}\n\n// Finalization\n\n/** Result of the whole execution, returned by finalization (docs/semantics/execution.md). */\nexport interface IExecutionResult extends IOperationResult {\n /** Result of the formula. If the execution was aborted, the Error that aborted it. */\n readonly value: IValue;\n /** All errors found by validation; empty if validation passed. */\n readonly validationErrors: readonly IError[];\n /** Context the formula was executed in; keep it to execute the next formula in a persistent context. */\n readonly context: IContext;\n}\n","/**\n * Contracts of operators.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/operator-parsing.md\n * @module\n */\n\n/** Grouping of operators with equal binding power. */\nexport const Associativity = {\n /** `a - b - c` = `(a - b) - c` */\n Left: \"Left\",\n /** `a ^ b ^ c` = `a ^ (b ^ c)` */\n Right: \"Right\",\n /** Operators cannot be chained: `a < b < c` is a syntax error. */\n None: \"None\",\n} as const;\nexport type Associativity = (typeof Associativity)[keyof typeof Associativity];\n\n/** Roles of an operator token. The values are bit flags and can be combined. */\nexport const OperatorType = {\n None: 0,\n /** Separates operands: `A [operator] B` */\n Separator: 1,\n /** Unary operator before its operand: `[operator] A` */\n Prefix: 2,\n /** Unary operator after its operand: `A [operator]` */\n Suffix: 4,\n /** Opening bracket. */\n OpenBracket: 8,\n /** Closing bracket. */\n CloseBracket: 16,\n /** Sequence boundary (new line, semicolon). Not an expression operator. */\n Boundary: 32,\n /** Structural separator inside a delimiter (comma, colon). */\n ItemSeparator: 64,\n} as const;\n/** Combination of {@link OperatorType} flags. */\nexport type OperatorType = number;\n\nexport interface IOperator {\n readonly symbol: string;\n readonly type: OperatorType;\n /** Binding power. A higher binding power binds more tightly; 0 for tokens that do not compete for it. */\n readonly priority: number;\n readonly associativity: Associativity;\n}\n","/**\n * Contracts of the environment, contexts, standards and the extended type tree.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/environment.md\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/context.md\n * @module\n */\nimport type { IFunction } from \"./functions.js\";\nimport type { IObject } from \"./primitives.js\";\n\n/** Lifetime of a context (docs/core/context.md, \"Context types\"). */\nexport const ContextType = {\n /** Created for a single execution and disposed after it. */\n Disposable: \"Disposable\",\n /** Kept by the host application and reused across executions. */\n Persistent: \"Persistent\",\n} as const;\nexport type ContextType = (typeof ContextType)[keyof typeof ContextType];\n\n/** Node of an {@link IAbstractTree}. */\nexport interface IAbstractTreeNode<T> {\n readonly value: T;\n /** Unique identifier of the node within the tree. */\n readonly key: string;\n /** Parent node; `undefined` for the root or an orphaned node. */\n readonly parent: IAbstractTreeNode<T> | undefined;\n readonly children: readonly IAbstractTreeNode<T>[];\n isParentOf(node: IAbstractTreeNode<T>): boolean;\n isChildOf(node: IAbstractTreeNode<T>): boolean;\n isAncestorOf(node: IAbstractTreeNode<T>): boolean;\n isDescendantOf(node: IAbstractTreeNode<T>): boolean;\n}\n\n/** Tree of uniquely keyed nodes, e.g. the inheritance hierarchy of extended types. */\nexport interface IAbstractTree<T> {\n readonly root: IAbstractTreeNode<T>;\n containsNode(key: string): boolean;\n getNode(key: string): IAbstractTreeNode<T> | undefined;\n}\n\n/**\n * The environment is a layer between the host application and the user: it provides the supported standards,\n * their functions and extended types, the environment constants and the default environment variables.\n */\nexport interface IEnvironment extends IObject {\n /** Identifiers of the supported standards, e.g. `core-1.0`. */\n readonly standards: readonly string[];\n supports(standard: string): boolean;\n readonly extendedTypes: readonly string[];\n /** Inheritance hierarchy of the extended types; its root is `Object`. */\n readonly extendedTypeTree: IAbstractTree<string>;\n /** Named functions and procedures of the supported standards. */\n readonly functions: readonly IFunction[];\n /** A named function or procedure, or `undefined` if no supported standard defines it. */\n getFunction(name: string): IFunction | undefined;\n}\n\n/** Data of a formula execution. The context itself stores the context (`$`) variables and the context constants. */\nexport interface IContext extends IObject {\n readonly environment: IEnvironment;\n /** Copies of the environment (`@`) variables, made during pre-execution. A formula changes only these copies. */\n readonly environmentVariables: IObject;\n readonly contextType: ContextType;\n /**\n * Pre-execution (docs/semantics/pre-execution.md): loads the data of the context type once, then copies\n * the environment variables that the context does not have yet.\n */\n preExecute(): void;\n}\n\nexport interface IStandardVersion {\n /** `major.minor` */\n readonly version: string;\n readonly major: number;\n readonly minor: number;\n}\n\n/** A standard (Core or an extension) that an environment can support. */\nexport interface IStandard {\n /** `core`, `stock` */\n readonly id: string;\n readonly version: IStandardVersion;\n readonly name: string;\n /** Extended types defined by the standard, parents before children. */\n readonly extendedTypes: readonly string[];\n readonly extendedTypeTree: IAbstractTree<string>;\n readonly namedFunctions: readonly IFunction[];\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/abstractions/primitives.ts
|
|
2
|
+
var PrimitiveType = {
|
|
3
|
+
/** Literal value, such as a number or a text. */
|
|
4
|
+
Value: "Value",
|
|
5
|
+
/** Container of a value: a variable or a constant. */
|
|
6
|
+
Container: "Container",
|
|
7
|
+
/** Structural relation transforming an input set into a determined output. */
|
|
8
|
+
Function: "Function"
|
|
9
|
+
};
|
|
10
|
+
var ContainerType = {
|
|
11
|
+
/** Mutable value container. */
|
|
12
|
+
Variable: "Variable",
|
|
13
|
+
/** Immutable value container. */
|
|
14
|
+
Constant: "Constant"
|
|
15
|
+
};
|
|
16
|
+
var FunctionType = {
|
|
17
|
+
/** A standard function that takes arguments and returns a value. */
|
|
18
|
+
Function: "Function",
|
|
19
|
+
/** Function that operates on the context and returns an Error (`SUCCESS` when it succeeds). */
|
|
20
|
+
Procedure: "Procedure",
|
|
21
|
+
/** Temporary anonymous function created by a lambda expression. */
|
|
22
|
+
Lambda: "Lambda"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/abstractions/processing.ts
|
|
26
|
+
var TokenType = {
|
|
27
|
+
None: "None",
|
|
28
|
+
Operator: "Operator",
|
|
29
|
+
Number: "Number",
|
|
30
|
+
Text: "Text",
|
|
31
|
+
Identifier: "Identifier"
|
|
32
|
+
};
|
|
33
|
+
var ExpressionKind = {
|
|
34
|
+
/** Literal value: number, text, `true`, `false`, `null`. */
|
|
35
|
+
Value: "Value",
|
|
36
|
+
/** Variable with a `$` (context) or `@` (environment) sigil. */
|
|
37
|
+
Variable: "Variable",
|
|
38
|
+
/** Identifier without a sigil or parentheses: a constant, or a named function used as a Delegate. */
|
|
39
|
+
Constant: "Constant",
|
|
40
|
+
/** Lambda parameter inside a lambda body. Unresolved until the lambda is invoked. */
|
|
41
|
+
Parameter: "Parameter",
|
|
42
|
+
/** Function or procedure call; every operator is translated into one. */
|
|
43
|
+
FunctionCall: "FunctionCall",
|
|
44
|
+
/** Lambda expression; its body is a separate tree. */
|
|
45
|
+
Lambda: "Lambda"
|
|
46
|
+
};
|
|
47
|
+
var VariableScope = {
|
|
48
|
+
Context: "Context",
|
|
49
|
+
Environment: "Environment"
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// src/abstractions/operators.ts
|
|
53
|
+
var Associativity = {
|
|
54
|
+
/** `a - b - c` = `(a - b) - c` */
|
|
55
|
+
Left: "Left",
|
|
56
|
+
/** `a ^ b ^ c` = `a ^ (b ^ c)` */
|
|
57
|
+
Right: "Right",
|
|
58
|
+
/** Operators cannot be chained: `a < b < c` is a syntax error. */
|
|
59
|
+
None: "None"
|
|
60
|
+
};
|
|
61
|
+
var OperatorType = {
|
|
62
|
+
None: 0,
|
|
63
|
+
/** Separates operands: `A [operator] B` */
|
|
64
|
+
Separator: 1,
|
|
65
|
+
/** Unary operator before its operand: `[operator] A` */
|
|
66
|
+
Prefix: 2,
|
|
67
|
+
/** Unary operator after its operand: `A [operator]` */
|
|
68
|
+
Suffix: 4,
|
|
69
|
+
/** Opening bracket. */
|
|
70
|
+
OpenBracket: 8,
|
|
71
|
+
/** Closing bracket. */
|
|
72
|
+
CloseBracket: 16,
|
|
73
|
+
/** Sequence boundary (new line, semicolon). Not an expression operator. */
|
|
74
|
+
Boundary: 32,
|
|
75
|
+
/** Structural separator inside a delimiter (comma, colon). */
|
|
76
|
+
ItemSeparator: 64
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// src/abstractions/structures.ts
|
|
80
|
+
var ContextType = {
|
|
81
|
+
/** Created for a single execution and disposed after it. */
|
|
82
|
+
Disposable: "Disposable",
|
|
83
|
+
/** Kept by the host application and reused across executions. */
|
|
84
|
+
Persistent: "Persistent"
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export { Associativity, ContainerType, ContextType, ExpressionKind, FunctionType, OperatorType, PrimitiveType, TokenType, VariableScope };
|
|
88
|
+
//# sourceMappingURL=chunk-LBF7G2HJ.js.map
|
|
89
|
+
//# sourceMappingURL=chunk-LBF7G2HJ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/abstractions/primitives.ts","../src/abstractions/processing.ts","../src/abstractions/operators.ts","../src/abstractions/structures.ts"],"names":[],"mappings":";AAQO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,QAAA,EAAU;AACZ;AAIO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,QAAA,EAAU;AACZ;AAIO,IAAM,YAAA,GAAe;AAAA;AAAA,EAE1B,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,MAAA,EAAQ;AACV;;;ACzBO,IAAM,SAAA,GAAY;AAAA,EACvB,IAAA,EAAM,MAAA;AAAA,EACN,QAAA,EAAU,UAAA;AAAA,EACV,MAAA,EAAQ,QAAA;AAAA,EACR,IAAA,EAAM,MAAA;AAAA,EACN,UAAA,EAAY;AACd;AAIO,IAAM,cAAA,GAAiB;AAAA;AAAA,EAE5B,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,QAAA,EAAU,UAAA;AAAA;AAAA,EAEV,SAAA,EAAW,WAAA;AAAA;AAAA,EAEX,YAAA,EAAc,cAAA;AAAA;AAAA,EAEd,MAAA,EAAQ;AACV;AAIO,IAAM,aAAA,GAAgB;AAAA,EAC3B,OAAA,EAAS,SAAA;AAAA,EACT,WAAA,EAAa;AACf;;;AChCO,IAAM,aAAA,GAAgB;AAAA;AAAA,EAE3B,IAAA,EAAM,MAAA;AAAA;AAAA,EAEN,KAAA,EAAO,OAAA;AAAA;AAAA,EAEP,IAAA,EAAM;AACR;AAIO,IAAM,YAAA,GAAe;AAAA,EAC1B,IAAA,EAAM,CAAA;AAAA;AAAA,EAEN,SAAA,EAAW,CAAA;AAAA;AAAA,EAEX,MAAA,EAAQ,CAAA;AAAA;AAAA,EAER,MAAA,EAAQ,CAAA;AAAA;AAAA,EAER,WAAA,EAAa,CAAA;AAAA;AAAA,EAEb,YAAA,EAAc,EAAA;AAAA;AAAA,EAEd,QAAA,EAAU,EAAA;AAAA;AAAA,EAEV,aAAA,EAAe;AACjB;;;ACxBO,IAAM,WAAA,GAAc;AAAA;AAAA,EAEzB,UAAA,EAAY,YAAA;AAAA;AAAA,EAEZ,UAAA,EAAY;AACd","file":"chunk-LBF7G2HJ.js","sourcesContent":["/**\n * Contracts of the FormuLang primitives: values, containers and types.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/primitives.md\n * @module\n */\n\n/** Kind of a primitive (docs/core/primitives.md). */\nexport const PrimitiveType = {\n /** Literal value, such as a number or a text. */\n Value: \"Value\",\n /** Container of a value: a variable or a constant. */\n Container: \"Container\",\n /** Structural relation transforming an input set into a determined output. */\n Function: \"Function\",\n} as const;\nexport type PrimitiveType = (typeof PrimitiveType)[keyof typeof PrimitiveType];\n\n/** Kind of a container (docs/core/containers.md). */\nexport const ContainerType = {\n /** Mutable value container. */\n Variable: \"Variable\",\n /** Immutable value container. */\n Constant: \"Constant\",\n} as const;\nexport type ContainerType = (typeof ContainerType)[keyof typeof ContainerType];\n\n/** Kind of a function (docs/core/functions.md). */\nexport const FunctionType = {\n /** A standard function that takes arguments and returns a value. */\n Function: \"Function\",\n /** Function that operates on the context and returns an Error (`SUCCESS` when it succeeds). */\n Procedure: \"Procedure\",\n /** Temporary anonymous function created by a lambda expression. */\n Lambda: \"Lambda\",\n} as const;\nexport type FunctionType = (typeof FunctionType)[keyof typeof FunctionType];\n\n/**\n * Type of a primitive: a base type (`Number`, `Object`, ...) and an optional extended type (`Stock.Candle`).\n * An extended type always derives from `Object` (docs/core/types.md).\n */\nexport interface IType {\n readonly base: string;\n readonly extended: string | undefined;\n isNull(): boolean;\n isUndefined(): boolean;\n /** Null, Undefined, Boolean, Number or Text (passed by value). */\n isSimple(): boolean;\n /** Error, Array, Object or Delegate (passed by reference). */\n isComplex(): boolean;\n hasExtension(): boolean;\n /** `Number`, `Object<Stock.Candle>` */\n toString(): string;\n}\n\n/** Error value: a code, a name and details. Code `0` (`SUCCESS`) is the only Error that does not abort an execution. */\nexport interface IError {\n readonly code: number;\n readonly name: string;\n readonly message: string;\n isSuccess(): boolean;\n isFailed(): boolean;\n /** `[NAME (code)] message` */\n toString(): string;\n}\n\n/** Root of everything that holds or produces a value. */\nexport interface IPrimitive {\n readonly primitiveType: PrimitiveType;\n readonly type: IType;\n /** Canonical expression of the primitive, e.g. `\"text\"` or `$x`. */\n readonly expression: string;\n readonly canAccessValue: boolean;\n /** The value, or an Error value when it cannot be read (e.g. `UNDEFINED_ERROR`). */\n getValue(): IValue;\n /** `SUCCESS` if the value can be changed, otherwise the Error that a change would return. */\n readonly canUpdateValue: IError;\n setValue(value: IValue): IError;\n}\n\n/**\n * A value. `raw` holds the JavaScript representation of the value:\n *\n * | Base type | `raw` |\n * | :-- | :-- |\n * | Null, Undefined | `null` |\n * | Boolean | `boolean` |\n * | Number | `number` |\n * | Text | `string` |\n * | Error | {@link IError} |\n * | Array | `readonly IValue[]` |\n * | Object | {@link IObject} |\n * | Delegate | `IFunction` |\n */\nexport interface IValue extends IPrimitive {\n readonly raw: unknown;\n}\n\n/** Named container of a value: a variable or a constant. */\nexport interface IContainer extends IPrimitive {\n readonly name: string;\n readonly containerType: ContainerType;\n readonly mutable: boolean;\n}\n\n/** Ordered set of named containers. Objects, contexts and environments are objects. */\nexport interface IObject {\n /** Keys in the order in which they were defined (docs/standards/core/functions/KEYS.md). */\n readonly keys: readonly string[];\n isEmpty(): boolean;\n hasKey(key: string): boolean;\n /** The container stored under the key, or `undefined` if there is none. */\n getProperty(key: string): IContainer | undefined;\n /** Changes the value of an existing container; `INVALID_KEY_ERROR` if the key does not exist. */\n setProperty(key: string, value: IValue): IError;\n /** Adds a constant; `false` if the key already exists. */\n initConstant(key: string, value: IValue): boolean;\n /** Adds a variable; `false` if the key already exists. */\n initVariable(key: string, value: IValue): boolean;\n}\n","/**\n * Contracts of the execution pipeline: validation, clean-up, tokenization, parsing and evaluation.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/execution.md\n * @module\n */\nimport type { IError, IValue } from \"./primitives.js\";\nimport type { IContext, IEnvironment } from \"./structures.js\";\n\n/** Type of a token (docs/semantics/token.md). */\nexport const TokenType = {\n None: \"None\",\n Operator: \"Operator\",\n Number: \"Number\",\n Text: \"Text\",\n Identifier: \"Identifier\",\n} as const;\nexport type TokenType = (typeof TokenType)[keyof typeof TokenType];\n\n/** Classification of an expression in the primitive map (docs/semantics/parsing.md, \"Primitive classification\"). */\nexport const ExpressionKind = {\n /** Literal value: number, text, `true`, `false`, `null`. */\n Value: \"Value\",\n /** Variable with a `$` (context) or `@` (environment) sigil. */\n Variable: \"Variable\",\n /** Identifier without a sigil or parentheses: a constant, or a named function used as a Delegate. */\n Constant: \"Constant\",\n /** Lambda parameter inside a lambda body. Unresolved until the lambda is invoked. */\n Parameter: \"Parameter\",\n /** Function or procedure call; every operator is translated into one. */\n FunctionCall: \"FunctionCall\",\n /** Lambda expression; its body is a separate tree. */\n Lambda: \"Lambda\",\n} as const;\nexport type ExpressionKind = (typeof ExpressionKind)[keyof typeof ExpressionKind];\n\n/** Scope of a variable: `$` (context) or `@` (environment). */\nexport const VariableScope = {\n Context: \"Context\",\n Environment: \"Environment\",\n} as const;\nexport type VariableScope = (typeof VariableScope)[keyof typeof VariableScope];\n\n/** Result of a stage. A stage never throws for a problem of the formula; it returns a failed result. */\nexport interface IOperationResult {\n /** `SUCCESS`, or the Error that stopped the stage. */\n readonly error: IError;\n readonly successful: boolean;\n}\n\nexport interface IToken {\n readonly type: TokenType;\n readonly value: string;\n}\n\n// Stage 1: validation\n\nexport interface IValidationResult extends IOperationResult {\n /** Errors in the order of detection; empty if the formula is valid. {@link IOperationResult.error} is the first of them. */\n readonly errors: readonly IError[];\n}\n\n/** Validation stage: a fast, shallow check of the raw formula and the environment that reports every problem at once. */\nexport interface IValidator {\n validate(formula: string, environment: IEnvironment): IValidationResult;\n}\n\n// Stage 2: clean-up\n\nexport interface ICleanUpResult extends IOperationResult {\n readonly result: string;\n}\n\nexport interface ICleaner {\n cleanUp(expression: string): ICleanUpResult;\n}\n\n// Stage 3: tokenization\n\nexport interface ITokenizationResult extends IOperationResult {\n readonly tokens: readonly IToken[];\n}\n\nexport interface ITokenizer {\n tokenize(expression: string): ITokenizationResult;\n}\n\n// Stage 4: parsing\n\ninterface ExpressionNodeBase {\n /** Canonical form of the expression, e.g. `ADD(POW($a,2),POW($b,2))`. Key of the primitive map. */\n readonly key: string;\n readonly kind: ExpressionKind;\n /** Depth of the node: 1 for the root of a sequence. */\n readonly level: number;\n readonly children: readonly IExpressionNode[];\n}\n\nexport interface ILiteralExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Value;\n readonly value: IValue;\n}\n\nexport interface IVariableExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Variable;\n readonly scope: VariableScope;\n readonly name: string;\n}\n\nexport interface IConstantExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Constant;\n readonly name: string;\n}\n\nexport interface IParameterExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Parameter;\n readonly name: string;\n}\n\nexport interface ICallExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.FunctionCall;\n /** Name of the called function or procedure. */\n readonly name: string;\n}\n\nexport interface ILambdaExpression extends ExpressionNodeBase {\n readonly kind: typeof ExpressionKind.Lambda;\n readonly parameters: readonly string[];\n /** Body of the lambda: a separate tree with its own parsing queue. */\n readonly body: ISequence;\n /** Primitive map of the body. */\n readonly primitiveMap: ReadonlyMap<string, IExpressionNode>;\n}\n\n/** Node of an expression (reference) tree. The `kind` discriminates the node types. */\nexport type IExpressionNode =\n | ILiteralExpression\n | IVariableExpression\n | IConstantExpression\n | IParameterExpression\n | ICallExpression\n | ILambdaExpression;\n\n/** One top-level expression of a formula (docs/core/formula.md, \"Formula sequences\"). */\nexport interface ISequence {\n readonly root: IExpressionNode;\n /** Parsing queue: the nodes ordered bottom-up (highest level first, left to right within a level). The root is last. */\n readonly queue: readonly IExpressionNode[];\n}\n\nexport interface IParsingResult extends IOperationResult {\n /** Sequences in formula order, each with its own tree and parsing queue. */\n readonly sequences: readonly ISequence[];\n /**\n * Primitive map shared by the whole formula: every unique expression, by its canonical key.\n * Lambda bodies have their own maps and are not included.\n */\n readonly primitiveMap: ReadonlyMap<string, IExpressionNode>;\n}\n\nexport interface IParser {\n parse(tokens: readonly IToken[]): IParsingResult;\n}\n\n// Stage 5: evaluation\n\nexport interface IEvaluationResult extends IOperationResult {\n /** Value of the last evaluated sequence, or the Error that aborted the execution. */\n readonly value: IValue;\n}\n\n/** Evaluation stage: resolves the parsed sequences into values, in the order of their parsing queues. */\nexport interface IEvaluator {\n evaluate(formula: IParsingResult, context: IContext): IEvaluationResult;\n}\n\n// Finalization\n\n/** Result of the whole execution, returned by finalization (docs/semantics/execution.md). */\nexport interface IExecutionResult extends IOperationResult {\n /** Result of the formula. If the execution was aborted, the Error that aborted it. */\n readonly value: IValue;\n /** All errors found by validation; empty if validation passed. */\n readonly validationErrors: readonly IError[];\n /** Context the formula was executed in; keep it to execute the next formula in a persistent context. */\n readonly context: IContext;\n}\n","/**\n * Contracts of operators.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/semantics/operator-parsing.md\n * @module\n */\n\n/** Grouping of operators with equal binding power. */\nexport const Associativity = {\n /** `a - b - c` = `(a - b) - c` */\n Left: \"Left\",\n /** `a ^ b ^ c` = `a ^ (b ^ c)` */\n Right: \"Right\",\n /** Operators cannot be chained: `a < b < c` is a syntax error. */\n None: \"None\",\n} as const;\nexport type Associativity = (typeof Associativity)[keyof typeof Associativity];\n\n/** Roles of an operator token. The values are bit flags and can be combined. */\nexport const OperatorType = {\n None: 0,\n /** Separates operands: `A [operator] B` */\n Separator: 1,\n /** Unary operator before its operand: `[operator] A` */\n Prefix: 2,\n /** Unary operator after its operand: `A [operator]` */\n Suffix: 4,\n /** Opening bracket. */\n OpenBracket: 8,\n /** Closing bracket. */\n CloseBracket: 16,\n /** Sequence boundary (new line, semicolon). Not an expression operator. */\n Boundary: 32,\n /** Structural separator inside a delimiter (comma, colon). */\n ItemSeparator: 64,\n} as const;\n/** Combination of {@link OperatorType} flags. */\nexport type OperatorType = number;\n\nexport interface IOperator {\n readonly symbol: string;\n readonly type: OperatorType;\n /** Binding power. A higher binding power binds more tightly; 0 for tokens that do not compete for it. */\n readonly priority: number;\n readonly associativity: Associativity;\n}\n","/**\n * Contracts of the environment, contexts, standards and the extended type tree.\n *\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/environment.md\n * @see https://github.com/Adisoreq/FormuLang/blob/main/docs/core/context.md\n * @module\n */\nimport type { IFunction } from \"./functions.js\";\nimport type { IObject } from \"./primitives.js\";\n\n/** Lifetime of a context (docs/core/context.md, \"Context types\"). */\nexport const ContextType = {\n /** Created for a single execution and disposed after it. */\n Disposable: \"Disposable\",\n /** Kept by the host application and reused across executions. */\n Persistent: \"Persistent\",\n} as const;\nexport type ContextType = (typeof ContextType)[keyof typeof ContextType];\n\n/** Node of an {@link IAbstractTree}. */\nexport interface IAbstractTreeNode<T> {\n readonly value: T;\n /** Unique identifier of the node within the tree. */\n readonly key: string;\n /** Parent node; `undefined` for the root or an orphaned node. */\n readonly parent: IAbstractTreeNode<T> | undefined;\n readonly children: readonly IAbstractTreeNode<T>[];\n isParentOf(node: IAbstractTreeNode<T>): boolean;\n isChildOf(node: IAbstractTreeNode<T>): boolean;\n isAncestorOf(node: IAbstractTreeNode<T>): boolean;\n isDescendantOf(node: IAbstractTreeNode<T>): boolean;\n}\n\n/** Tree of uniquely keyed nodes, e.g. the inheritance hierarchy of extended types. */\nexport interface IAbstractTree<T> {\n readonly root: IAbstractTreeNode<T>;\n containsNode(key: string): boolean;\n getNode(key: string): IAbstractTreeNode<T> | undefined;\n}\n\n/**\n * The environment is a layer between the host application and the user: it provides the supported standards,\n * their functions and extended types, the environment constants and the default environment variables.\n */\nexport interface IEnvironment extends IObject {\n /** Identifiers of the supported standards, e.g. `core-1.0`. */\n readonly standards: readonly string[];\n supports(standard: string): boolean;\n readonly extendedTypes: readonly string[];\n /** Inheritance hierarchy of the extended types; its root is `Object`. */\n readonly extendedTypeTree: IAbstractTree<string>;\n /** Named functions and procedures of the supported standards. */\n readonly functions: readonly IFunction[];\n /** A named function or procedure, or `undefined` if no supported standard defines it. */\n getFunction(name: string): IFunction | undefined;\n}\n\n/** Data of a formula execution. The context itself stores the context (`$`) variables and the context constants. */\nexport interface IContext extends IObject {\n readonly environment: IEnvironment;\n /** Copies of the environment (`@`) variables, made during pre-execution. A formula changes only these copies. */\n readonly environmentVariables: IObject;\n readonly contextType: ContextType;\n /**\n * Pre-execution (docs/semantics/pre-execution.md): loads the data of the context type once, then copies\n * the environment variables that the context does not have yet.\n */\n preExecute(): void;\n}\n\nexport interface IStandardVersion {\n /** `major.minor` */\n readonly version: string;\n readonly major: number;\n readonly minor: number;\n}\n\n/** A standard (Core or an extension) that an environment can support. */\nexport interface IStandard {\n /** `core`, `stock` */\n readonly id: string;\n readonly version: IStandardVersion;\n readonly name: string;\n /** Extended types defined by the standard, parents before children. */\n readonly extendedTypes: readonly string[];\n readonly extendedTypeTree: IAbstractTree<string>;\n readonly namedFunctions: readonly IFunction[];\n}\n"]}
|