@dallaylaen/ski-interpreter 2.0.0 → 2.2.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 +43 -0
- package/README.md +1 -1
- package/bin/ski.js +96 -97
- package/lib/ski-interpreter.cjs.js +2047 -0
- package/lib/ski-interpreter.cjs.js.map +7 -0
- package/lib/ski-interpreter.esm.js +2052 -0
- package/lib/ski-interpreter.esm.js.map +7 -0
- package/package.json +13 -6
- package/types/index.d.ts +3 -7
- package/types/{lib → src}/expr.d.ts +146 -48
- package/types/src/extras.d.ts +58 -0
- package/types/src/internal.d.ts +52 -0
- package/types/{lib → src}/parser.d.ts +11 -4
- package/types/{lib → src}/quest.d.ts +56 -39
- package/index.js +0 -8
- package/lib/expr.js +0 -1316
- package/lib/parser.js +0 -418
- package/lib/quest.js +0 -401
- package/lib/util.js +0 -57
- package/types/lib/util.d.ts +0 -11
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,49 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [2.2.0] - 2026-02-14
|
|
9
|
+
|
|
10
|
+
### BREAKING CHANGES
|
|
11
|
+
|
|
12
|
+
- Remove `Expr.declare()` method for good, use `toposort()` or static `declare` instead
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- `SKI.extras.toposort(list, env)` function to output named terms in dependency order
|
|
17
|
+
- `SKI.extras.declare(term, {env})` function for term declarations
|
|
18
|
+
- `Expr.unroll()` method to get a list of terms
|
|
19
|
+
that give the initial expression when applied
|
|
20
|
+
from left to right: `((a, b), (c, d)) => [a, b, (c, d)]`
|
|
21
|
+
- Parser: Support for chained assignments (`'foo=bar=baz'` expressions)
|
|
22
|
+
- Parser: Support for multi-line comment syntax (`/* comments */`)
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- Parser improvements and bug fixes for variable handling
|
|
27
|
+
- `expand()` method refactored to use `traverse()`
|
|
28
|
+
- Package now builds both CJS and ESM bundles.
|
|
29
|
+
- Moved source from `lib/` to `src/` directory
|
|
30
|
+
|
|
31
|
+
## [2.1.0] - 2026-02-12
|
|
32
|
+
|
|
33
|
+
### BREAKING CHANGES
|
|
34
|
+
|
|
35
|
+
- Quest: rename `vars` -> `env`, `title` -> `name`, and `descr` -> `intro`.
|
|
36
|
+
- Quest: remove `subst` for good, use input: `{ name, fancy }` instead
|
|
37
|
+
- App: remove `split()` method, use `foo.fun` and `foo.arg` directly or better yet `foo.unroll()` instead.
|
|
38
|
+
|
|
39
|
+
### Added
|
|
40
|
+
|
|
41
|
+
- `SKI.extras.search(seed: Expr[], options:{...}, predicate: (e: Expr, props: infer()) => 1|0|-1)` that brute forces all possible applications of `seed` (with some heuristics) returns the first matching expr (if can)
|
|
42
|
+
- Add experimental `Expr.fold<T>(initial : T, combine: (acc : T, expr) => T?)` that folds an expression tree in LO order. Composite nodes are first checked as is and then descended into. If `combine` returns null, it is discarded and the previous value is kept.
|
|
43
|
+
- Add `SKI.control.{descend, prune, stop}` helper functions for more precise `fold` control.
|
|
44
|
+
- Add optional `Expr.context{ parser, scope, env, src }` which is filled by the parser.
|
|
45
|
+
- Add `SKI.extras.deepFormat({}|[]|Expr|..., options={})` that recursively formats a deep structure with given options, leaving non-Expr values as is. Useful for debugging.
|
|
46
|
+
- Add `Expr.toJSON()`, currently just `format()` with no options
|
|
47
|
+
- Add `Expr.unroll()` that returns a list of terms
|
|
48
|
+
that give the initial expression when applied
|
|
49
|
+
from left to right: `((a, b), (c, d)) => [a, b, (c, d)]`
|
|
50
|
+
|
|
8
51
|
## [2.0.0] - 2026-02-06
|
|
9
52
|
|
|
10
53
|
### BREAKING CHANGES
|
package/README.md
CHANGED
|
@@ -147,7 +147,7 @@ const lambdaSteps = [...skiExpr.toLambda()];
|
|
|
147
147
|
## Fancy formatting
|
|
148
148
|
|
|
149
149
|
The `format` methods of the `Expr` class supports
|
|
150
|
-
a number of options, see [the source code](
|
|
150
|
+
a number of options, see [the source code](src/expr.js) for details.
|
|
151
151
|
|
|
152
152
|
## Variable scoping
|
|
153
153
|
|
package/bin/ski.js
CHANGED
|
@@ -2,121 +2,120 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs/promises');
|
|
4
4
|
|
|
5
|
-
const {SKI} = require('../
|
|
5
|
+
const { SKI } = require('../lib/ski-interpreter.cjs');
|
|
6
6
|
|
|
7
7
|
const [myname, options, positional] = parseArgs(process.argv);
|
|
8
8
|
|
|
9
9
|
if (options.help) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
console.error(myname + ': usage: ' + myname + '[-q | -v ] -e <expression>');
|
|
11
|
+
process.exit(1);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
if (typeof options.e === 'string' && positional.length > 0 || positional.length > 1) {
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
if ((typeof options.e === 'string' && positional.length) > 0 || positional.length > 1) {
|
|
15
|
+
console.error(myname + ': either -e <expr> or exactly one filename must be given');
|
|
16
|
+
process.exit(1);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const ski = new SKI();
|
|
20
20
|
|
|
21
21
|
if (options.e === undefined && !positional.length) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
rl.prompt();
|
|
42
|
-
});
|
|
43
|
-
rl.once('close', () => {
|
|
44
|
-
if (!options.q)
|
|
45
|
-
console.log('Bye, and may your bird fly high!');
|
|
46
|
-
process.exit(0)
|
|
47
|
-
});
|
|
22
|
+
// interactive console
|
|
23
|
+
const readline = require('readline');
|
|
24
|
+
const rl = readline.createInterface({
|
|
25
|
+
input: process.stdin,
|
|
26
|
+
output: process.stdout,
|
|
27
|
+
prompt: '> ',
|
|
28
|
+
terminal: true,
|
|
29
|
+
});
|
|
30
|
+
if (!options.q)
|
|
31
|
+
console.log('Welcome to SKI interactive shell. Known combinators: ' + ski.showRestrict());
|
|
32
|
+
rl.on('line', str => {
|
|
33
|
+
const flag = str.match(/^\s*([-+])([qvt])\s*$/);
|
|
34
|
+
if (flag)
|
|
35
|
+
options[flag[2]] = flag[1] === '+';
|
|
36
|
+
else {
|
|
37
|
+
runLine(err => {
|
|
38
|
+
console.log('' + err)
|
|
39
|
+
})(str);
|
|
40
|
+
}
|
|
48
41
|
rl.prompt();
|
|
42
|
+
});
|
|
43
|
+
rl.once('close', () => {
|
|
44
|
+
if (!options.q)
|
|
45
|
+
console.log('Bye, and may your bird fly high!');
|
|
46
|
+
process.exit(0)
|
|
47
|
+
});
|
|
48
|
+
rl.prompt();
|
|
49
49
|
} else {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
const prom = positional.length > 0
|
|
51
|
+
? fs.readFile(positional[0], 'utf8')
|
|
52
|
+
: Promise.resolve(options.e);
|
|
53
|
+
|
|
54
|
+
prom.then(runLine(err => { console.error('' + err); process.exit(3) })).catch(err => {
|
|
55
|
+
console.error(myname + ': ' + err);
|
|
56
|
+
process.exit(2);
|
|
57
|
+
});
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
function runLine(onErr) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
60
|
+
function runLine (onErr) {
|
|
61
|
+
return function (source) {
|
|
62
|
+
if (!source.match(/\S/))
|
|
63
|
+
return 0; // nothing to see here
|
|
64
|
+
try {
|
|
65
|
+
const expr = ski.parse(source);
|
|
66
|
+
|
|
67
|
+
const t0 = new Date();
|
|
68
|
+
for (const state of expr.walk()) {
|
|
69
|
+
if (state.final && !options.q)
|
|
70
|
+
console.log(`// ${state.steps} step(s) in ${new Date() - t0}ms`);
|
|
71
|
+
if (options.v || state.final)
|
|
72
|
+
console.log('' + state.expr.format({ terse: options.t }));
|
|
73
|
+
if (state.final && expr instanceof SKI.classes.Alias)
|
|
74
|
+
ski.add(expr.name, state.expr);
|
|
75
|
+
}
|
|
76
|
+
return 0;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
onErr(err);
|
|
79
|
+
return 1;
|
|
81
80
|
}
|
|
81
|
+
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
function parseArgs(argv) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const action = todo[next];
|
|
114
|
-
if (typeof action !== "function")
|
|
115
|
-
throw new Error('Unknown option ' + next + '; see ski.js --help');
|
|
116
|
-
|
|
117
|
-
action();
|
|
84
|
+
function parseArgs (argv) {
|
|
85
|
+
const [_, script, ...list] = argv;
|
|
86
|
+
|
|
87
|
+
const todo = {
|
|
88
|
+
'--': () => { pos.push(...list) },
|
|
89
|
+
'--help': () => { opt.help = true },
|
|
90
|
+
'-q': () => { opt.q = true },
|
|
91
|
+
'-v': () => { opt.v = true },
|
|
92
|
+
'-c': () => { opt.t = false },
|
|
93
|
+
'-t': () => { opt.t = true },
|
|
94
|
+
'-e': () => {
|
|
95
|
+
if (list.length < 1)
|
|
96
|
+
throw new Error('option -e requires an argument');
|
|
97
|
+
opt.e = list.shift();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// TODO replace with a relevant dependency
|
|
102
|
+
const pos = [];
|
|
103
|
+
const opt = {};
|
|
104
|
+
|
|
105
|
+
while (list.length > 0) {
|
|
106
|
+
const next = list.shift();
|
|
107
|
+
|
|
108
|
+
if (!next.match(/^-/)) {
|
|
109
|
+
pos.push(next);
|
|
110
|
+
continue;
|
|
118
111
|
}
|
|
119
112
|
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
const action = todo[next];
|
|
114
|
+
if (typeof action !== 'function')
|
|
115
|
+
throw new Error('Unknown option ' + next + '; see ski.js --help');
|
|
116
|
+
|
|
117
|
+
action();
|
|
118
|
+
}
|
|
122
119
|
|
|
120
|
+
return [script, opt, pos];
|
|
121
|
+
}
|