@hyperscale0/hsx 1.0.0-alpha.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.
Files changed (64) hide show
  1. package/AUTHORS +8 -0
  2. package/CHANGELOG.md +59 -0
  3. package/LICENSE +661 -0
  4. package/LICENSING.md +52 -0
  5. package/README.md +170 -0
  6. package/SECURITY.md +47 -0
  7. package/TRADEMARKS.md +35 -0
  8. package/bin/hsx.ts +15 -0
  9. package/dist/bin/hsx.d.ts +7 -0
  10. package/dist/bin/hsx.d.ts.map +1 -0
  11. package/dist/bin/hsx.js +14 -0
  12. package/dist/bin/hsx.js.map +1 -0
  13. package/dist/src/ast.d.ts +172 -0
  14. package/dist/src/ast.d.ts.map +1 -0
  15. package/dist/src/ast.js +22 -0
  16. package/dist/src/ast.js.map +1 -0
  17. package/dist/src/check.d.ts +11 -0
  18. package/dist/src/check.d.ts.map +1 -0
  19. package/dist/src/check.js +1214 -0
  20. package/dist/src/check.js.map +1 -0
  21. package/dist/src/cli.d.ts +20 -0
  22. package/dist/src/cli.d.ts.map +1 -0
  23. package/dist/src/cli.js +137 -0
  24. package/dist/src/cli.js.map +1 -0
  25. package/dist/src/compile.d.ts +39 -0
  26. package/dist/src/compile.d.ts.map +1 -0
  27. package/dist/src/compile.js +59 -0
  28. package/dist/src/compile.js.map +1 -0
  29. package/dist/src/index.d.ts +9 -0
  30. package/dist/src/index.d.ts.map +1 -0
  31. package/dist/src/index.js +7 -0
  32. package/dist/src/index.js.map +1 -0
  33. package/dist/src/lex.d.ts +23 -0
  34. package/dist/src/lex.d.ts.map +1 -0
  35. package/dist/src/lex.js +125 -0
  36. package/dist/src/lex.js.map +1 -0
  37. package/dist/src/lower.d.ts +93 -0
  38. package/dist/src/lower.d.ts.map +1 -0
  39. package/dist/src/lower.js +2081 -0
  40. package/dist/src/lower.js.map +1 -0
  41. package/dist/src/model.d.ts +307 -0
  42. package/dist/src/model.d.ts.map +1 -0
  43. package/dist/src/model.js +15 -0
  44. package/dist/src/model.js.map +1 -0
  45. package/dist/src/parse.d.ts +19 -0
  46. package/dist/src/parse.d.ts.map +1 -0
  47. package/dist/src/parse.js +484 -0
  48. package/dist/src/parse.js.map +1 -0
  49. package/dist/src/version.d.ts +16 -0
  50. package/dist/src/version.d.ts.map +1 -0
  51. package/dist/src/version.js +16 -0
  52. package/dist/src/version.js.map +1 -0
  53. package/package.json +79 -0
  54. package/spec/hsx-ir.schema.json +522 -0
  55. package/src/ast.ts +231 -0
  56. package/src/check.ts +1699 -0
  57. package/src/cli.ts +173 -0
  58. package/src/compile.ts +98 -0
  59. package/src/index.ts +16 -0
  60. package/src/lex.ts +161 -0
  61. package/src/lower.ts +2619 -0
  62. package/src/model.ts +340 -0
  63. package/src/parse.ts +580 -0
  64. package/src/version.ts +17 -0
package/src/model.ts ADDED
@@ -0,0 +1,340 @@
1
+ /**
2
+ * The checked program model: what an HSX program MEANS once every name
3
+ * resolves and every archetype's parameter surface is validated. This is the
4
+ * only input the lowering consumes, it never re-reads the AST, and every
5
+ * node keeps its source span so later stages report at source coordinates.
6
+ */
7
+
8
+ import type { Span } from "./ast.ts";
9
+
10
+ export const PARTY_KINDS = ["business", "person"] as const;
11
+ export type PartyKind = (typeof PARTY_KINDS)[number];
12
+
13
+ export const ASSET_KINDS = [
14
+ "access",
15
+ "claim",
16
+ "good",
17
+ "service",
18
+ "ticket",
19
+ ] as const;
20
+ export type AssetKind = (typeof ASSET_KINDS)[number];
21
+
22
+ export interface CheckedParty {
23
+ readonly kind: PartyKind;
24
+ readonly name: string;
25
+ readonly origin: Span;
26
+ }
27
+
28
+ export interface CheckedAsset {
29
+ readonly kind: AssetKind;
30
+ readonly name: string;
31
+ readonly origin: Span;
32
+ /** Where ownership of the asset itself changes hands. Money stays on-platform either way. */
33
+ readonly titleTransfer: "off_platform" | "on_platform";
34
+ }
35
+
36
+ /** A typed money amount the program declares, e.g. `price: money(SAR)`. */
37
+ export interface MoneyField {
38
+ readonly currency: string;
39
+ readonly name: string;
40
+ readonly origin: Span;
41
+ }
42
+
43
+ /** One platform fee term: `buyer: 1%`, the named party bears bps to the platform. */
44
+ export interface FeeTerm {
45
+ readonly bearer: string;
46
+ readonly bps: number;
47
+ readonly origin: Span;
48
+ }
49
+
50
+ /** One share of a cancellation split: `buyer: 99.5%`. */
51
+ export interface SplitShare {
52
+ readonly bps: number;
53
+ readonly origin: Span;
54
+ readonly to: string;
55
+ }
56
+
57
+ /** `on_cancel(funded) { ... }`, how the held amount unwinds, summing to 100%. */
58
+ export interface CancelPolicy {
59
+ readonly origin: Span;
60
+ readonly shares: readonly SplitShare[];
61
+ readonly when: "funded";
62
+ }
63
+
64
+ /**
65
+ * A finite payment schedule: `count` anchors, one every `every` (a fixed
66
+ * calendar-free ISO duration in days or weeks), the first anchored on the
67
+ * stored date field `firstDueField`. Finiteness is by construction: count is
68
+ * a literal, so the schedule can never be unbounded.
69
+ */
70
+ export interface ScheduleTerms {
71
+ readonly count: number;
72
+ /** Fixed ISO duration between anchors, single unit: P<n>D or P<n>W. */
73
+ readonly every: { readonly days: number; readonly raw: string };
74
+ /** camelCase date field carrying the first anchor's due date. */
75
+ readonly firstDueField: string;
76
+ readonly origin: Span;
77
+ }
78
+
79
+ /**
80
+ * The checked `held_payment` instantiation: payer funds `amount` into the
81
+ * settlement's own custody; a decision port releases it to the payee; fees
82
+ * accrue to the platform; cancellation splits the held amount exactly.
83
+ */
84
+ export interface CheckedHeldPayment {
85
+ readonly amount: MoneyField;
86
+ readonly archetype: "held_payment";
87
+ readonly fees: readonly FeeTerm[];
88
+ readonly name: string;
89
+ readonly onCancel?: CancelPolicy;
90
+ readonly origin: Span;
91
+ readonly payee: string;
92
+ readonly payer: string;
93
+ readonly release: PortRelease;
94
+ /**
95
+ * `release: port <p> | at(<field>)`, the camelCase stored date field the
96
+ * hold releases to the payee on when no decision was recorded, and the
97
+ * cutoff both caller exits share. Absent when the port is the only way out.
98
+ */
99
+ readonly releaseDeadlineField?: string;
100
+ }
101
+
102
+ /** A fixed, calendar-free decision window. */
103
+ interface FixedWindow {
104
+ readonly days: number;
105
+ readonly raw: string;
106
+ }
107
+
108
+ /** An exact on-top service fee declared as money, never a caller-computed rate. */
109
+ export interface SwapFee {
110
+ readonly amount: MoneyField;
111
+ readonly bearer: string;
112
+ }
113
+
114
+ /** One side of a strict two-party atomic swap. */
115
+ interface SwapSide {
116
+ readonly amount: MoneyField;
117
+ readonly fee?: SwapFee;
118
+ readonly party: string;
119
+ }
120
+
121
+ /**
122
+ * The checked `swap`: exactly two parties fund one amount each into the same
123
+ * escrow, then the entire exchange releases, settles, cancels, or claws back
124
+ * as one indivisible lifecycle.
125
+ */
126
+ export interface CheckedSwap {
127
+ readonly archetype: "swap";
128
+ readonly dispute?: PortRelease & { readonly window: FixedWindow };
129
+ readonly name: string;
130
+ readonly origin: Span;
131
+ readonly release: PortRelease;
132
+ readonly sides: readonly [SwapSide, SwapSide];
133
+ }
134
+
135
+ /**
136
+ * The checked `instant_transfer`: payer pays `amount` straight through to the
137
+ * payee, no custody. Fees partition the amount (payee side) or ride on top
138
+ * (payer side); the pieces conserve the amount exactly.
139
+ */
140
+ export interface CheckedInstantTransfer {
141
+ readonly amount: MoneyField;
142
+ readonly archetype: "instant_transfer";
143
+ readonly fees: readonly FeeTerm[];
144
+ readonly name: string;
145
+ readonly origin: Span;
146
+ readonly payee: string;
147
+ readonly payer: string;
148
+ }
149
+
150
+ /**
151
+ * The checked `premium_forward`: payer funds the premium into custody; a
152
+ * decision port binds the policy, which forwards the premium to the carrier
153
+ * exactly once, minus the platform's commission. Pre-binding cancellation
154
+ * splits the held premium.
155
+ */
156
+ export interface CheckedPremiumForward {
157
+ readonly amount: MoneyField;
158
+ readonly archetype: "premium_forward";
159
+ /** `bind: port confirm_policy`, the binding condition. */
160
+ readonly bind: PortRelease;
161
+ readonly carrier: string;
162
+ /** Platform commission carved from the premium at forwarding, in bps. */
163
+ readonly commissionBps: number;
164
+ readonly name: string;
165
+ readonly onCancel?: CancelPolicy;
166
+ readonly origin: Span;
167
+ readonly payer: string;
168
+ }
169
+
170
+ /**
171
+ * The checked `deposit`: the held amount is a reservation on the payer's own
172
+ * account in the holder's favor, placed as a hold, then either claimed
173
+ * (posted to the holder) or returned (voided back to the payer). The hold
174
+ * pairing law accounts for the full amount on both exits.
175
+ */
176
+ export interface CheckedDeposit {
177
+ readonly amount: MoneyField;
178
+ readonly archetype: "deposit";
179
+ /** Port that claims the deposit for the holder. */
180
+ readonly claim: PortRelease;
181
+ readonly holder: string;
182
+ readonly name: string;
183
+ readonly origin: Span;
184
+ readonly payer: string;
185
+ /** Port that returns the deposit to the payer. */
186
+ readonly return: PortRelease;
187
+ }
188
+
189
+ /**
190
+ * The checked `scheduled`: the total partitions into `count` installments,
191
+ * each collected on its own stored-date anchor. The schedule is finite by
192
+ * construction and every anchor is its own idempotent verb.
193
+ */
194
+ export interface CheckedScheduled {
195
+ readonly amount: MoneyField;
196
+ readonly archetype: "scheduled";
197
+ readonly name: string;
198
+ readonly origin: Span;
199
+ readonly payee: string;
200
+ readonly payer: string;
201
+ readonly schedule: ScheduleTerms;
202
+ }
203
+
204
+ /**
205
+ * Where an advance's repayment comes from. `schedule` collects it from the
206
+ * advanced party over finite anchors; `carve` takes it out of the release of a
207
+ * hold the advanced party is already owed, so the advance never mints money it
208
+ * is not secured by.
209
+ */
210
+ export type AdvanceSource =
211
+ | { readonly kind: "schedule"; readonly schedule: ScheduleTerms }
212
+ | {
213
+ readonly kind: "carve";
214
+ readonly origin: Span;
215
+ /** The held_payment settlement whose release repays this advance. */
216
+ readonly settlement: string;
217
+ };
218
+
219
+ /**
220
+ * The checked `advance`: the funder disburses the advance to the advanced
221
+ * party, who repays it from the source below. Conservation on the advance
222
+ * itself is one partition law, the repayable total equals advance + fee, and
223
+ * a scheduled source adds a second, that the repayments sum to it.
224
+ */
225
+ export interface CheckedAdvance {
226
+ readonly advanced: string;
227
+ readonly amount: MoneyField;
228
+ readonly archetype: "advance";
229
+ /** The funder's discount on the advance, in bps of the advance amount. */
230
+ readonly feeBps: number;
231
+ readonly funder: string;
232
+ readonly name: string;
233
+ readonly origin: Span;
234
+ readonly source: AdvanceSource;
235
+ }
236
+
237
+ /** One rate-card line of a metered settlement: a named meter and its unit price. */
238
+ export interface MeterRate {
239
+ readonly field: MoneyField;
240
+ readonly meter: string;
241
+ readonly origin: Span;
242
+ }
243
+
244
+ /**
245
+ * The checked `metered`: per-unit charges on a committed rate card, each
246
+ * usage emission being the ledger transfer itself, until the period closes on
247
+ * its stored end date. Emission matches ledger by construction because the
248
+ * meter event and the transfer are the same admission.
249
+ */
250
+ export interface CheckedMetered {
251
+ readonly archetype: "metered";
252
+ /** camelCase date field carrying the period's close date. */
253
+ readonly closeByField: string;
254
+ readonly name: string;
255
+ readonly origin: Span;
256
+ readonly payee: string;
257
+ readonly payer: string;
258
+ readonly rates: readonly MeterRate[];
259
+ }
260
+
261
+ /**
262
+ * The checked `pooled_split`: one instance per payout period. The funder
263
+ * pools the period total piece-wise (one piece per recipient share), and the
264
+ * pool distributes to the named recipients on the stored payout date. The
265
+ * shares sum to 100% and the integer remainder goes to `remainderTo`.
266
+ */
267
+ export interface CheckedPooledSplit {
268
+ readonly amount: MoneyField;
269
+ readonly archetype: "pooled_split";
270
+ /** camelCase date field carrying the period's payout date. */
271
+ readonly distributeDueField: string;
272
+ readonly name: string;
273
+ readonly origin: Span;
274
+ readonly payer: string;
275
+ /** The recipient whose piece carries the integer-division remainder. */
276
+ readonly remainderTo: string;
277
+ readonly shares: readonly SplitShare[];
278
+ }
279
+
280
+ /** Release decided through a decision port: `release: port confirm_handover`. */
281
+ interface PortRelease {
282
+ readonly origin: Span;
283
+ readonly port: string;
284
+ }
285
+
286
+ export type CheckedSettlement =
287
+ | CheckedAdvance
288
+ | CheckedDeposit
289
+ | CheckedHeldPayment
290
+ | CheckedInstantTransfer
291
+ | CheckedMetered
292
+ | CheckedPooledSplit
293
+ | CheckedPremiumForward
294
+ | CheckedScheduled
295
+ | CheckedSwap;
296
+
297
+ export type PortFieldType =
298
+ | { readonly asset: string; readonly kind: "asset_id" }
299
+ | { readonly currency: string; readonly kind: "money" }
300
+ | { readonly kind: "date" }
301
+ | { readonly kind: "text" };
302
+
303
+ export interface PortField {
304
+ readonly name: string;
305
+ readonly origin: Span;
306
+ readonly type: PortFieldType;
307
+ }
308
+
309
+ /** A decision port: the typed seam where the tenant's own backend decides. */
310
+ export interface CheckedPort {
311
+ readonly allowed: readonly string[];
312
+ readonly fields: readonly PortField[];
313
+ readonly name: string;
314
+ readonly origin: Span;
315
+ }
316
+
317
+ export interface CheckedProgram {
318
+ readonly assets: readonly CheckedAsset[];
319
+ readonly name: string;
320
+ readonly parties: readonly CheckedParty[];
321
+ readonly ports: readonly CheckedPort[];
322
+ readonly settlements: readonly CheckedSettlement[];
323
+ readonly title: string;
324
+ }
325
+
326
+ /**
327
+ * A semantic problem. `error` blocks lowering; `warning` is the compiler's
328
+ * lint voice, the program is legal but the author should look.
329
+ */
330
+ export interface CheckDiagnostic {
331
+ readonly message: string;
332
+ readonly severity: "error" | "warning";
333
+ readonly span: Span;
334
+ }
335
+
336
+ export interface CheckResult {
337
+ readonly diagnostics: readonly CheckDiagnostic[];
338
+ /** Present exactly when no error-severity diagnostic exists. */
339
+ readonly program?: CheckedProgram;
340
+ }