@cobol-ts/size 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,52 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # `@cobol-ts/size`
2
+
3
+ Symbolic COBOL **storage extent expressions in bits**. Layout compilation builds and canonicalises these expressions once; a record-aware caller evaluates them later using ODO controller values and enclosing occurrence indices. This package does **not** read bytes, locate fields within physical buffers, choose REDEFINES interpretations, or own cursor lifetimes.
4
+
5
+ ## Expression types
6
+
7
+ `StorageSizeExpression` includes:
8
+
9
+ | Kind | Meaning |
10
+ | --- | --- |
11
+ | `number` | Constant non-negative bit count |
12
+ | `sum` | Sum of adjacent extents |
13
+ | `max` | Allocated size of overlay alternatives |
14
+ | `occurs` | Repeat body a fixed number of times |
15
+ | `depending-on` | Sum body extents across an ODO count supplied by a resolver |
16
+ | `align` | Padding to the next requested bit boundary |
17
+ | `difference` | Non-negative distance between two extents |
18
+
19
+ `canonicaliseStorageSize(expression)` folds constants and simplifies compatible algebra **without** consulting a physical record. It does not make an inherently variable-length occurrence constant.
20
+
21
+ `evaluateStorageSize(expression, resolve, enclosingIndices?)` evaluates an expression for a particular record/count environment. `resolve` is a `ResolvePath`: a callback receiving a concrete logical controller path and returning its value. The evaluator validates the counts and extent arithmetic; the caller determines how to obtain controller values.
22
+
23
+ ## Controller paths and occurrence context
24
+
25
+ An expression `DependsOnPath` may contain field-name strings and numeric **relative enclosing-occurrence depths**. Numeric elements in the symbolic path are *not* literal application array indices. When evaluating an occurrence body, the evaluator binds its currently selected occurrence and any enclosing indices to produce a concrete controller lookup path. `resolveDependingOnCount(path, resolve, enclosingIndices?)` exposes that binding/validation step.
26
+
27
+ For example, `locateCobolEntry()` or the COBOL projection adapter can supply the enclosing indices when evaluating nested ODO extents. The expression engine permits record-dependent body widths, but the current `cursor-loader-cobol` **byte controller locator** only supports controllers at fixed physical locations. Do not infer full occurrence-local-controller support from the algebra alone.
28
+
29
+ ## Example
30
+
31
+ ```ts
32
+ import {canonicaliseStorageSize, evaluateStorageSize} from '@cobol-ts/size';
33
+
34
+ const expression = canonicaliseStorageSize({
35
+ kind:'sum',
36
+ terms:[8,{kind:'occurs',count:3,expression:16}]
37
+ });
38
+
39
+ const bits = evaluateStorageSize(expression, () => {
40
+ throw new Error('No controller expected for this constant expression');
41
+ });
42
+ // bits === 56
43
+ ```
44
+
45
+ ## Source and tests
46
+
47
+ - `src/size.expression.grammer.ts`: expression types and path conventions (filename spelling is intentional for the current source).
48
+ - `src/storage.size.expression.canonicalise.ts`: normalisation rules.
49
+ - `src/storage.size.expression.evaluate.ts`: record-aware evaluation and count binding.
50
+ - `*.spec.ts`: canonicalisation, nested indices, bounds, arithmetic, alignment and invalid expressions.
51
+
52
+ The evaluator currently makes temporary working arrays/functions on some symbolic evaluations. It is not guaranteed allocation-free; higher layers cache occurrence positions so repeated navigation need not re-evaluate already discovered extents. Run Jest and TypeScript checks in the workspace.
package/README.me ADDED
@@ -0,0 +1,224 @@
1
+ # `@cobol-ts/size`
2
+
3
+ Canonical symbolic storage-size expressions for compiled COBOL layouts. **All sizes, offsets, gaps and alignment boundaries are measured in bits.**
4
+
5
+ The package provides three independent operations: describing a size, simplifying it **once** when compiling a layout, and evaluating it **against one record** when an actual `OCCURS DEPENDING ON` count is required. It does not parse copybooks, decide whether a copybook is legal, choose a `REDEFINES` interpretation, find bytes or decode COBOL fields. Those responsibilities belong to the copybook, layout and record/projection packages.
6
+
7
+ ## Public API
8
+
9
+ The package root (`index.ts`) exports:
10
+
11
+ ```ts
12
+ import {
13
+ type StorageSizeExpression,
14
+ type DependsOnPath,
15
+ type ResolvedPath,
16
+ type ResolvePath,
17
+ isConstantSizeExpression,
18
+ canonicaliseStorageSize,
19
+ evaluateStorageSize,
20
+ resolveDependingOnCount,
21
+ } from '@cobol-ts/size';
22
+ ```
23
+
24
+ A `StorageSizeExpression` is either a nonnegative integer bit count or one of seven expression kinds:
25
+
26
+ | Kind | Shape | Meaning |
27
+ | --- | --- | --- |
28
+ | Constant | `80` | Fixed number of bits |
29
+ | `sum` | `{kind: 'sum', terms: [8, 80]}` | Adjacent extents are added |
30
+ | `max` | `{kind: 'max', terms: [80, 128]}` | Shared extent of overlapping views, notably a `REDEFINES` run |
31
+ | `occurs` | `{kind: 'occurs', count: 3, expression: body}` | Sum the body over three *separately indexed* occurrences |
32
+ | `depending-on` | `{kind: 'depending-on', path, expression: body, min: 0, max: 4}` | Resolve the actual count, check optional declared bounds, then evaluate each occurrence |
33
+ | `align` | `{kind: 'align', size: position, boundary: 32}` | Number of **extra** bits needed to reach the next 32-bit boundary |
34
+ | `difference` | `{kind: 'difference', left: bigger, right: smaller}` | A nonnegative gap; used for unequal overlay alternatives and suffix geometry |
35
+
36
+ The field names above are the actual TypeScript discriminants. The module filename `size.expression.grammer.ts` retains the historical spelling for compatibility.
37
+
38
+ ## Simple example
39
+
40
+ ```ts
41
+ import {canonicaliseStorageSize, evaluateStorageSize} from '@cobol-ts/size';
42
+
43
+ const customerSize = canonicaliseStorageSize({
44
+ kind: 'sum',
45
+ terms: [16, 240, 64], // two display digits, X(30), X(8)
46
+ });
47
+
48
+ // customerSize === 320 bits (40 bytes)
49
+ const bits = evaluateStorageSize(customerSize, () => {
50
+ throw new Error('No count lookup should be needed');
51
+ });
52
+ // bits === 320
53
+ ```
54
+
55
+ An expression that becomes a number requires no record-specific work. The layout compiler can simply retain that number.
56
+
57
+ ## Dependent counts and repetition bindings
58
+
59
+ A `depending-on` node stores a **symbolic path** to its controlling item. The path consists of strings (literal field names) and numbers (relative depths of currently enclosing occurrence bindings). In a `DependsOnPath`, `0` means the innermost enclosing index and `1` its parent. These are **not literal occurrence numbers**.
60
+
61
+ At runtime, the evaluator substitutes the currently bound zero-based occurrence indices and calls a `ResolvePath` callback with a concrete `ResolvedPath`: a path whose numeric components really are occurrence indexes. `resolveDependingOnCount` performs the same substitution and validates the returned count.
62
+
63
+ Consider an illustrative IBM-compatible arrangement in which both controllers are fixed-location items outside the tables they control:
64
+
65
+ ```cobol
66
+ 01 RECORD.
67
+ 05 OUTER-COUNT PIC 9.
68
+ 05 INNER-COUNT PIC 9.
69
+ 05 OUTER OCCURS 0 TO 3 TIMES
70
+ DEPENDING ON OUTER-COUNT.
71
+ 10 INNER OCCURS 0 TO 4 TIMES
72
+ DEPENDING ON INNER-COUNT.
73
+ 15 CODE PIC X(2).
74
+ 10 TRAILER PIC X.
75
+ 05 FINAL PIC X.
76
+ ```
77
+
78
+ For one-byte DISPLAY characters, its size expression can be:
79
+
80
+ ```ts
81
+ import {type StorageSizeExpression, evaluateStorageSize} from '@cobol-ts/size';
82
+
83
+ const size: StorageSizeExpression = {
84
+ kind: 'sum',
85
+ terms: [
86
+ 16, // the two counters
87
+ {
88
+ kind: 'depending-on',
89
+ path: ['RECORD', 'OUTER-COUNT'],
90
+ min: 0,
91
+ max: 3,
92
+ expression: {
93
+ kind: 'sum',
94
+ terms: [
95
+ {
96
+ kind: 'depending-on',
97
+ path: ['RECORD', 'INNER-COUNT'],
98
+ min: 0,
99
+ max: 4,
100
+ expression: 16, // CODE, per INNER occurrence
101
+ },
102
+ 8, // TRAILER, per OUTER occurrence
103
+ ],
104
+ },
105
+ },
106
+ 8, // FINAL
107
+ ],
108
+ };
109
+
110
+ const resolve = (path: readonly (string | number)[]): unknown => {
111
+ switch (path.join('.')) {
112
+ case 'RECORD.OUTER-COUNT': return 2;
113
+ case 'RECORD.INNER-COUNT': return 3;
114
+ default: throw new Error(`Unknown controller ${path.join('.')}`);
115
+ }
116
+ };
117
+
118
+ const bits = evaluateStorageSize(size, resolve); // 136 bits = 17 bytes
119
+ ```
120
+
121
+ This expression computes a size; the **layout locator** independently uses compiled offsets and occurrence prefixes to locate `OUTER[1].INNER[1].CODE` at bit 88. Size expressions are not themselves a field-location API.
122
+
123
+ ### Counts belonging to indexed enclosing occurrences
124
+
125
+ The algebra also supports expressions referring to a count in an enclosing occurrence, for use where the consuming compiler/dialect allows it. For example, `['RECORD', 'GROUPS', 0, 'ITEM-COUNT']` is symbolic: `0` denotes the **nearest enclosing occurrence binding**. When that binding is index 2, the resolver receives `['RECORD', 'GROUPS', 2, 'ITEM-COUNT']`.
126
+
127
+ Do not mistake expressive power of the algebra for permission to compile every such source declaration as IBM Enterprise COBOL: **the layout compiler** defines the supported controller-location rules.
128
+
129
+ ### Why `occurs` is not always multiplication
130
+
131
+ ```ts
132
+ const repeated: StorageSizeExpression = {
133
+ kind: 'occurs',
134
+ count: 3,
135
+ expression: {
136
+ kind: 'depending-on',
137
+ path: ['RECORD', 'GROUPS', 0, 'ITEM-COUNT'],
138
+ expression: 80,
139
+ },
140
+ };
141
+ ```
142
+
143
+ If the three counts are 2, 5 and 1, the total is `(2 + 5 + 1) × 80 = 640` bits. Using three times the size of any *one* occurrence would be wrong. `occurs` introduces a binding even for an explicit count of one, unless the child has simplified to a constant. A zero-count `occurs` simplifies to zero.
144
+
145
+ ## Alignment and overlay gaps
146
+
147
+ `align` returns a **padding size**, not an aligned position:
148
+
149
+ ```ts
150
+ const gap = canonicaliseStorageSize({
151
+ kind: 'align',
152
+ size: 8,
153
+ boundary: 32,
154
+ });
155
+ // gap === 24 bits
156
+ ```
157
+
158
+ Mathematically, `align(position, boundary) = (boundary - position % boundary) % boundary`. The layout compiler is responsible for knowing which position matters, deciding the alignment policy, and inserting a padding entry in the correct containing group. The size algebra makes no independent IBM storage-convention decisions.
159
+
160
+ `difference` provides the extra extent following a shorter overlay alternative:
161
+
162
+ ```ts
163
+ const rightGap = canonicaliseStorageSize({
164
+ kind: 'difference',
165
+ left: {kind: 'max', terms: [64, 96]},
166
+ right: 64,
167
+ });
168
+ // rightGap === 32 bits
169
+ ```
170
+
171
+ `difference` must be nonnegative when evaluated. It is useful for the independently compiled `rightOffset` of alternatives of unequal length.
172
+
173
+ ## Canonicalisation
174
+
175
+ `canonicaliseStorageSize(expression)` is pure, record-independent, and does not mutate the input. It:
176
+
177
+ - Canonicalises recursively; flattens nested `sum`/`max` and combines constants.
178
+ - Removes unnecessary zero terms; collapses single-term sums/maxima.
179
+ - Folds numeric alignment gaps and numeric nonnegative differences; eliminates identical difference operands and differences from zero.
180
+ - Folds fixed repetition **only when its canonical body is a number**, or when the repeat count is zero.
181
+ - Preserves symbolic `depending-on` expressions, their paths, and their `min`/`max` bounds.
182
+ - Preserves the bindings of nonconstant `occurs` expressions, including `occurs` with count one.
183
+
184
+ It **does not** consult a record, discover physical controller offsets, or guarantee that the resolver is called in the same order or the same number of times as in an unsimplified expression. A count resolver should be stable and side-effect-free.
185
+
186
+ ## Evaluation and errors
187
+
188
+ ```ts
189
+ const bits = evaluateStorageSize(expression, resolve, enclosingIndices?);
190
+ const count = resolveDependingOnCount(path, resolve, enclosingIndices?);
191
+ ```
192
+
193
+ `resolve` receives a concrete zero-based path and returns `unknown`. The evaluator requires dependent counts to be nonnegative safe integers and checks supplied `min`/`max` bounds. It rejects unbound relative path depths and negative `difference` results. The optional `enclosingIndices` supplies already-selected outer indexes when evaluating a child expression independently. All repeat bindings are private to the evaluation call.
194
+
195
+ The evaluator does not determine whether a controller can safely be decoded from source bytes. For a real record, the caller must obtain the count from a valid byte-backed location without decoding an unrelated or inactive `REDEFINES` alternative.
196
+
197
+ ## Integration with `@cobol-ts/layout`
198
+
199
+ ```text
200
+ copybook text -> copybook AST
201
+ -> compileCobolLayout (primitive size + storage policies)
202
+ -> canonical StorageSizeExpression on each compiled node
203
+ -> reusable compiled geometry: size, leftOffset, rightOffset
204
+ -> current physical record + controller resolver
205
+ -> evaluate only the expressions needed for the selected path
206
+ -> locate bit range -> decode requested primitive
207
+ ```
208
+
209
+ The compiled expressions are reusable across records. **Resolved counts, selected indices, optional caches, and buffers are record-specific**, belonging to the current leased record rather than the shared expression tree. The expression engine does not allocate occurrence objects or eagerly expand an ODO table.
210
+
211
+ Do not assume that all lengths can be obtained without framing information: choosing an appropriate anchor, handling alternative overlays, determining active versus reserved storage conventions, and reading the actual bytes remain the jobs of the layout/record integration.
212
+
213
+ ## Source and tests
214
+
215
+ | File | Purpose |
216
+ | --- | --- |
217
+ | `src/size.expression.grammer.ts` | Seven expression types, path types and constant-size guard |
218
+ | `src/storage.size.expression.canonicalise.ts` | Pure simplification |
219
+ | `src/storage.size.expression.evaluate.ts` | Runtime evaluator and controller-path resolution |
220
+ | `src/storage.size.expression.canonicalise.spec.ts` | Canonicalisation and equivalence tests |
221
+ | `src/storage.size.expression.evaluator.spec.ts` | Evaluation, nested repetition, alignment and path tests |
222
+ | `index.ts` | Public exports |
223
+
224
+ The present tests exercise the algebra; meaningful end-to-end acceptance additionally needs **published copybooks and known byte records**, checking independent physical field offsets, not only agreement between the compiler and evaluator.
@@ -0,0 +1,3 @@
1
+ export * from './src/storage.size.expression.evaluate';
2
+ export * from './src/size.expression.grammer';
3
+ export * from './src/storage.size.expression.canonicalise';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./src/storage.size.expression.evaluate"), exports);
18
+ __exportStar(require("./src/size.expression.grammer"), exports);
19
+ __exportStar(require("./src/storage.size.expression.canonicalise"), exports);
20
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@cobol-ts/size",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "description": "Evaluates the size of a COBOL copybook and its fields.",
4
+ "version": "0.0.1",
5
+ "main": "dist/index",
6
+ "types": "dist/index",
7
+ "browserslist": {
8
+ "development": [
9
+ "last 1 chrome version",
10
+ "last 1 firefox version",
11
+ "last 1 safari version"
12
+ ],
13
+ "production": [
14
+ ">0.2%",
15
+ "not dead",
16
+ "not op_mini all"
17
+ ]
18
+ },
19
+ "devDependencies": {
20
+ "@testing-library/jest-dom": "^6.1.4",
21
+ "@types/jest": "^29.0.0",
22
+ "@types/node": "^14.0.0",
23
+ "jest": "^29.0.0",
24
+ "ts-jest": "^29.0.0",
25
+ "typescript": "^5.2.2"
26
+ },
27
+ "eslintConfig": {
28
+ "extends": [
29
+ "react-app",
30
+ "react-app/jest"
31
+ ],
32
+ "overrides": [
33
+ {
34
+ "files": [
35
+ "**/*.stories.*"
36
+ ],
37
+ "rules": {
38
+ "import/no-anonymous-default-export": "off"
39
+ }
40
+ }
41
+ ]
42
+ },
43
+ "files": [
44
+ "dist/index.js",
45
+ "dist/index.d.ts",
46
+ "dist/src/",
47
+ "build/"
48
+ ],
49
+ "keywords": [
50
+ "indexing"
51
+ ],
52
+ "license": "MIT",
53
+ "repository": "git@github.com:intellimaintain/intellimaintain.git",
54
+ "scripts": {
55
+ "test": "jest --config jest.config.json --env node",
56
+ "test:watch": "npm run test -- --watch",
57
+ "start": "react-scripts start"
58
+ }
6
59
  }