@hyperscale0/hsx 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/CHANGELOG.md +2 -400
  2. package/README.md +3 -92
  3. package/dist/src/ast.d.ts +99 -310
  4. package/dist/src/ast.d.ts.map +1 -1
  5. package/dist/src/ast.js +16 -69
  6. package/dist/src/ast.js.map +1 -1
  7. package/dist/src/cli.d.ts +6 -19
  8. package/dist/src/cli.d.ts.map +1 -1
  9. package/dist/src/cli.js +76 -315
  10. package/dist/src/cli.js.map +1 -1
  11. package/dist/src/compile.d.ts +23 -73
  12. package/dist/src/compile.d.ts.map +1 -1
  13. package/dist/src/compile.js +1113 -148
  14. package/dist/src/compile.js.map +1 -1
  15. package/dist/src/cost.d.ts +8 -77
  16. package/dist/src/cost.d.ts.map +1 -1
  17. package/dist/src/cost.js +13 -422
  18. package/dist/src/cost.js.map +1 -1
  19. package/dist/src/format.d.ts +8 -7
  20. package/dist/src/format.d.ts.map +1 -1
  21. package/dist/src/format.js +9 -175
  22. package/dist/src/format.js.map +1 -1
  23. package/dist/src/headers.d.ts +35 -0
  24. package/dist/src/headers.d.ts.map +1 -0
  25. package/dist/src/headers.js +108 -0
  26. package/dist/src/headers.js.map +1 -0
  27. package/dist/src/index.d.ts +6 -12
  28. package/dist/src/index.d.ts.map +1 -1
  29. package/dist/src/index.js +4 -9
  30. package/dist/src/index.js.map +1 -1
  31. package/dist/src/lex.d.ts +8 -28
  32. package/dist/src/lex.d.ts.map +1 -1
  33. package/dist/src/lex.js +88 -146
  34. package/dist/src/lex.js.map +1 -1
  35. package/dist/src/parse.d.ts +4 -17
  36. package/dist/src/parse.d.ts.map +1 -1
  37. package/dist/src/parse.js +402 -1040
  38. package/dist/src/parse.js.map +1 -1
  39. package/dist/src/std-bundle.d.ts.map +1 -1
  40. package/dist/src/std-bundle.js +12 -20
  41. package/dist/src/std-bundle.js.map +1 -1
  42. package/dist/src/std-library.d.ts +1 -1
  43. package/dist/src/std-library.d.ts.map +1 -1
  44. package/dist/src/std-library.js +1 -5
  45. package/dist/src/std-library.js.map +1 -1
  46. package/dist/src/tunables.d.ts +7 -0
  47. package/dist/src/tunables.d.ts.map +1 -0
  48. package/dist/src/tunables.js +26 -0
  49. package/dist/src/tunables.js.map +1 -0
  50. package/dist/src/version.d.ts +2 -4
  51. package/dist/src/version.d.ts.map +1 -1
  52. package/dist/src/version.js +2 -4
  53. package/dist/src/version.js.map +1 -1
  54. package/docs/README.md +179 -41
  55. package/docs/headers.md +44 -0
  56. package/examples/cost-table.json +99 -751
  57. package/examples/library.hsx +59 -0
  58. package/package.json +9 -17
  59. package/src/ast.ts +82 -441
  60. package/src/cli.ts +77 -362
  61. package/src/compile.ts +1535 -247
  62. package/src/cost.ts +28 -671
  63. package/src/format.ts +12 -256
  64. package/src/headers.ts +126 -0
  65. package/src/index.ts +7 -39
  66. package/src/lex.ts +99 -195
  67. package/src/parse.ts +403 -1209
  68. package/src/std-bundle.ts +12 -20
  69. package/src/std-library.ts +2 -7
  70. package/src/tunables.ts +31 -0
  71. package/src/version.ts +2 -5
  72. package/std/approvals.hsx +17 -0
  73. package/std/cards.hsx +63 -0
  74. package/std/collections.hsx +31 -0
  75. package/std/escrow.hsx +43 -0
  76. package/std/financing.hsx +372 -0
  77. package/std/insurance.hsx +59 -0
  78. package/std/lending.hsx +115 -0
  79. package/std/marketplace.hsx +24 -0
  80. package/std/money.hsx +195 -0
  81. package/std/savings.hsx +44 -0
  82. package/std/travel.hsx +98 -0
  83. package/std/wallet.hsx +41 -0
  84. package/dist/src/diagnostics.d.ts +0 -13
  85. package/dist/src/diagnostics.d.ts.map +0 -1
  86. package/dist/src/diagnostics.js +0 -587
  87. package/dist/src/diagnostics.js.map +0 -1
  88. package/dist/src/emit.d.ts +0 -51
  89. package/dist/src/emit.d.ts.map +0 -1
  90. package/dist/src/emit.js +0 -192
  91. package/dist/src/emit.js.map +0 -1
  92. package/dist/src/entry-overrides.d.ts +0 -58
  93. package/dist/src/entry-overrides.d.ts.map +0 -1
  94. package/dist/src/entry-overrides.js +0 -284
  95. package/dist/src/entry-overrides.js.map +0 -1
  96. package/dist/src/ir.d.ts +0 -73
  97. package/dist/src/ir.d.ts.map +0 -1
  98. package/dist/src/ir.js +0 -15
  99. package/dist/src/ir.js.map +0 -1
  100. package/dist/src/limits.d.ts +0 -23
  101. package/dist/src/limits.d.ts.map +0 -1
  102. package/dist/src/limits.js +0 -23
  103. package/dist/src/limits.js.map +0 -1
  104. package/dist/src/lsp/server.d.ts +0 -32
  105. package/dist/src/lsp/server.d.ts.map +0 -1
  106. package/dist/src/lsp/server.js +0 -391
  107. package/dist/src/lsp/server.js.map +0 -1
  108. package/dist/src/modules.d.ts +0 -38
  109. package/dist/src/modules.d.ts.map +0 -1
  110. package/dist/src/modules.js +0 -372
  111. package/dist/src/modules.js.map +0 -1
  112. package/dist/src/typecheck.d.ts +0 -8
  113. package/dist/src/typecheck.d.ts.map +0 -1
  114. package/dist/src/typecheck.js +0 -4131
  115. package/dist/src/typecheck.js.map +0 -1
  116. package/docs/guide/01-first-program.md +0 -24
  117. package/docs/guide/02-money.md +0 -25
  118. package/docs/guide/03-instruments.md +0 -93
  119. package/docs/guide/04-lifecycles.md +0 -40
  120. package/docs/guide/05-fees-and-splits.md +0 -45
  121. package/docs/guide/06-schedules.md +0 -26
  122. package/docs/guide/07-composition.md +0 -52
  123. package/docs/guide/08-writing-a-module.md +0 -85
  124. package/docs/guide/09-cost.md +0 -24
  125. package/docs/guide/10-diagnostics.md +0 -27
  126. package/docs/llms-full.txt +0 -3684
  127. package/docs/llms.txt +0 -44
  128. package/docs/piece-plans.md +0 -120
  129. package/docs/playground.md +0 -55
  130. package/docs/reference/cli.md +0 -36
  131. package/docs/reference/diagnostics.md +0 -901
  132. package/docs/reference/grammar.md +0 -38
  133. package/docs/reference/std/advance.md +0 -112
  134. package/docs/reference/std/cancellable_booking.md +0 -159
  135. package/docs/reference/std/captured_payment.md +0 -115
  136. package/docs/reference/std/conditional_disbursement.md +0 -101
  137. package/docs/reference/std/credit_facility.md +0 -108
  138. package/docs/reference/std/held_payment.md +0 -173
  139. package/docs/reference/std/instant_transfer.md +0 -97
  140. package/docs/reference/std/metered.md +0 -89
  141. package/docs/reference/std/pooled_split.md +0 -95
  142. package/docs/reference/std/premium_forward.md +0 -119
  143. package/docs/reference/std/reconciled_payout.md +0 -103
  144. package/docs/reference/std/rotating_pool.md +0 -130
  145. package/docs/reference/std/scheduled.md +0 -136
  146. package/docs/reference/std/security_deposit.md +0 -138
  147. package/docs/reference/std/settlement_batch.md +0 -123
  148. package/docs/reference/std/swap.md +0 -133
  149. package/docs/reference/std/threshold_pool.md +0 -138
  150. package/docs/reference/std/weighted_distribution.md +0 -135
  151. package/docs/reference/types.md +0 -61
  152. package/docs/reference/udl-output.md +0 -13
  153. package/examples/01-first-program/README.md +0 -45
  154. package/examples/01-first-program/tip-jar.hsx +0 -17
  155. package/examples/02-imports-and-modules/README.md +0 -96
  156. package/examples/02-imports-and-modules/photo-booth.hsx +0 -37
  157. package/examples/03-diagnostics/README.md +0 -68
  158. package/examples/03-diagnostics/corner-shop-fixed.hsx +0 -22
  159. package/examples/03-diagnostics/corner-shop.hsx +0 -19
  160. package/examples/04-complete-product/README.md +0 -84
  161. package/examples/04-complete-product/study-hall.hsx +0 -67
  162. package/examples/05-authored-instrument/README.md +0 -5
  163. package/examples/05-authored-instrument/payment.hsx +0 -37
  164. package/examples/05-watch-club/README.md +0 -6
  165. package/examples/05-watch-club/watch-club.hsx +0 -123
  166. package/examples/README.md +0 -48
  167. package/examples/advance/README.md +0 -3
  168. package/examples/advance/advance.hsx +0 -13
  169. package/examples/advance/advance.udl +0 -367
  170. package/examples/cancellable_booking/README.md +0 -3
  171. package/examples/cancellable_booking/cancellable_booking.hsx +0 -14
  172. package/examples/cancellable_booking/cancellable_booking.udl +0 -413
  173. package/examples/captured_payment/README.md +0 -3
  174. package/examples/captured_payment/captured_payment.hsx +0 -17
  175. package/examples/captured_payment/captured_payment.udl +0 -686
  176. package/examples/conditional_disbursement/README.md +0 -3
  177. package/examples/conditional_disbursement/conditional_disbursement.hsx +0 -15
  178. package/examples/conditional_disbursement/conditional_disbursement.udl +0 -418
  179. package/examples/credit_facility/README.md +0 -3
  180. package/examples/credit_facility/credit_facility.hsx +0 -24
  181. package/examples/credit_facility/credit_facility.udl +0 -1396
  182. package/examples/held_payment/README.md +0 -3
  183. package/examples/held_payment/held_payment.hsx +0 -13
  184. package/examples/held_payment/held_payment.udl +0 -569
  185. package/examples/instant_transfer/README.md +0 -3
  186. package/examples/instant_transfer/instant_transfer.hsx +0 -16
  187. package/examples/instant_transfer/instant_transfer.udl +0 -376
  188. package/examples/metered/README.md +0 -3
  189. package/examples/metered/metered.hsx +0 -13
  190. package/examples/metered/metered.udl +0 -253
  191. package/examples/pooled_split/README.md +0 -3
  192. package/examples/pooled_split/pooled_split.hsx +0 -15
  193. package/examples/pooled_split/pooled_split.udl +0 -432
  194. package/examples/premium_forward/README.md +0 -3
  195. package/examples/premium_forward/premium_forward.hsx +0 -19
  196. package/examples/premium_forward/premium_forward.udl +0 -604
  197. package/examples/reconciled_payout/README.md +0 -3
  198. package/examples/reconciled_payout/reconciled_payout.hsx +0 -13
  199. package/examples/reconciled_payout/reconciled_payout.udl +0 -312
  200. package/examples/rotating_pool/README.md +0 -3
  201. package/examples/rotating_pool/rotating_pool.hsx +0 -18
  202. package/examples/rotating_pool/rotating_pool.udl +0 -4370
  203. package/examples/scheduled/README.md +0 -3
  204. package/examples/scheduled/scheduled.hsx +0 -12
  205. package/examples/scheduled/scheduled.udl +0 -355
  206. package/examples/security_deposit/README.md +0 -3
  207. package/examples/security_deposit/security_deposit.hsx +0 -21
  208. package/examples/security_deposit/security_deposit.udl +0 -320
  209. package/examples/settlement_batch/README.md +0 -3
  210. package/examples/settlement_batch/settlement_batch.hsx +0 -19
  211. package/examples/settlement_batch/settlement_batch.udl +0 -757
  212. package/examples/swap/README.md +0 -3
  213. package/examples/swap/swap.hsx +0 -19
  214. package/examples/swap/swap.udl +0 -892
  215. package/examples/threshold_pool/README.md +0 -3
  216. package/examples/threshold_pool/threshold_pool.hsx +0 -16
  217. package/examples/threshold_pool/threshold_pool.udl +0 -680
  218. package/examples/weighted_distribution/README.md +0 -3
  219. package/examples/weighted_distribution/weighted_distribution.hsx +0 -20
  220. package/examples/weighted_distribution/weighted_distribution.udl +0 -348
  221. package/skills/hsx/SKILL.md +0 -449
  222. package/src/diagnostics.ts +0 -620
  223. package/src/emit.ts +0 -282
  224. package/src/entry-overrides.ts +0 -428
  225. package/src/ir.ts +0 -113
  226. package/src/limits.ts +0 -22
  227. package/src/lsp/server.ts +0 -460
  228. package/src/modules.ts +0 -452
  229. package/src/typecheck.ts +0 -5381
  230. package/std/SEMANTICS.md +0 -44
  231. package/std/money_flows/advance.hsx +0 -235
  232. package/std/money_flows/cancellable_booking.hsx +0 -402
  233. package/std/money_flows/captured_payment.hsx +0 -220
  234. package/std/money_flows/conditional_disbursement.hsx +0 -226
  235. package/std/money_flows/credit_facility.hsx +0 -214
  236. package/std/money_flows/held_payment.hsx +0 -812
  237. package/std/money_flows/index.hsx +0 -4
  238. package/std/money_flows/instant_transfer.hsx +0 -231
  239. package/std/money_flows/metered.hsx +0 -104
  240. package/std/money_flows/pooled_split.hsx +0 -109
  241. package/std/money_flows/premium_forward.hsx +0 -307
  242. package/std/money_flows/reconciled_payout.hsx +0 -119
  243. package/std/money_flows/rotating_pool.hsx +0 -305
  244. package/std/money_flows/scheduled.hsx +0 -599
  245. package/std/money_flows/security_deposit.hsx +0 -248
  246. package/std/money_flows/settlement_batch.hsx +0 -173
  247. package/std/money_flows/swap.hsx +0 -1192
  248. package/std/money_flows/threshold_pool.hsx +0 -307
  249. package/std/money_flows/weighted_distribution.hsx +0 -237
package/src/modules.ts DELETED
@@ -1,452 +0,0 @@
1
- import type { UdlDocument } from "@hyperscale0/udl";
2
- import type {
3
- ApplicationScopeDecl,
4
- Decl,
5
- Expr,
6
- ImportDecl,
7
- InstrumentApplyDecl,
8
- Program,
9
- } from "./ast.ts";
10
- import { lineColIn, lineIndex } from "./ast.ts";
11
- import { parseProgram } from "./parse.ts";
12
- import { bundledStandardLibrary, type StandardLibrary } from "./std-library.ts";
13
-
14
- export interface ModuleSource {
15
- readonly name: string;
16
- readonly source: string;
17
- }
18
-
19
- export interface HsxCompilerHost {
20
- readonly moduleName?: string;
21
- /** Published canonical UDL available to `use` declarations. */
22
- readonly publishedCatalog?: UdlDocument;
23
- readonly resolveModule?: (
24
- specifier: string,
25
- from: string,
26
- ) => ModuleSource | undefined;
27
- readonly standardLibrary?: StandardLibrary;
28
- }
29
-
30
- export interface ModuleIssue {
31
- readonly code: string;
32
- readonly fix: string;
33
- readonly message: string;
34
- readonly span: { readonly end: number; readonly start: number };
35
- }
36
-
37
- export interface ModuleOrigin {
38
- readonly column: number;
39
- readonly line: number;
40
- readonly moduleName: string;
41
- }
42
-
43
- export type ModuleResolution =
44
- | { readonly issues: readonly ModuleIssue[]; readonly ok: false }
45
- | {
46
- readonly ok: true;
47
- readonly origins: ReadonlyMap<object, ModuleOrigin>;
48
- readonly program: Program;
49
- };
50
-
51
- const moduleBudget = 64;
52
-
53
- export function resolveProgramModules(
54
- program: Program,
55
- host: HsxCompilerHost = {},
56
- ): ModuleResolution {
57
- const loaded = new Map<string, Program>();
58
- const resolving = new Set<string>();
59
- const seenModules = new Set<string>();
60
- const issues: ModuleIssue[] = [];
61
- const origins = new Map<object, ModuleOrigin>();
62
-
63
- const resolveImports = (current: Program, from: string): Program => {
64
- const appended: Decl[] = [];
65
- const appendedKeys = new Set<string>();
66
- const exportSourceByName = new Map<string, string>();
67
- const imports = current.decls.filter(
68
- (decl): decl is ImportDecl => decl.kind === "import",
69
- );
70
- for (const declaration of imports) {
71
- const requested = declaration.names.map((name) => name.name);
72
- const stdLib = host.standardLibrary ?? bundledStandardLibrary;
73
- const sources = standardSources(
74
- stdLib,
75
- declaration.from.value,
76
- requested,
77
- );
78
- const custom =
79
- sources.length === 0
80
- ? host.resolveModule?.(declaration.from.value, from)
81
- : undefined;
82
- const modules = custom ? [custom] : sources;
83
- if (modules.length === 0) {
84
- issues.push({
85
- code: "HSX1006",
86
- fix: "correct the module specifier or provide a deterministic module resolver",
87
- message: `module ${declaration.from.value} cannot be resolved`,
88
- span: declaration.from.span,
89
- });
90
- continue;
91
- }
92
- const resolvedNames = new Set<string>();
93
- for (const module of modules) {
94
- let moduleProgram = loaded.get(module.name);
95
- if (!moduleProgram) {
96
- if (resolving.has(module.name)) {
97
- issues.push({
98
- code: "HSX1006",
99
- fix: "remove the cyclic module import",
100
- message: `module graph has a cycle through ${module.name}`,
101
- span: declaration.span,
102
- });
103
- continue;
104
- }
105
- resolving.add(module.name);
106
- seenModules.add(module.name);
107
- if (seenModules.size > moduleBudget) {
108
- issues.push({
109
- code: "HSX1006",
110
- fix: `import no more than ${moduleBudget} modules`,
111
- message: `the module graph exceeds ${moduleBudget} modules`,
112
- span: declaration.span,
113
- });
114
- resolving.delete(module.name);
115
- continue;
116
- }
117
- const parsed = parseProgram(module.source);
118
- if (parsed.diagnostics.length > 0) {
119
- issues.push({
120
- code: "HSX1006",
121
- fix: `fix the parse error in module ${module.name}`,
122
- message: `module ${module.name} does not parse: ${parsed.diagnostics[0]?.message ?? "unknown parse error"}`,
123
- span: declaration.from.span,
124
- });
125
- resolving.delete(module.name);
126
- continue;
127
- }
128
- recordModuleOrigins(parsed.program, module, origins);
129
- moduleProgram = resolveImports(parsed.program, module.name);
130
- loaded.set(module.name, moduleProgram);
131
- resolving.delete(module.name);
132
- }
133
- for (const decl of moduleProgram.decls) {
134
- if (
135
- (decl.kind !== "instrument" &&
136
- decl.kind !== "instrument_apply" &&
137
- decl.kind !== "type" &&
138
- decl.kind !== "const") ||
139
- !("exported" in decl) ||
140
- !decl.exported ||
141
- !requested.includes(decl.name.name)
142
- )
143
- continue;
144
- resolvedNames.add(decl.name.name);
145
- const carried =
146
- decl.kind === "instrument_apply" || decl.kind === "instrument"
147
- ? withDeclarationScope(decl, moduleProgram)
148
- : decl;
149
- const priorSource = exportSourceByName.get(decl.name.name);
150
- if (priorSource && priorSource !== module.name) {
151
- issues.push({
152
- code: "HSX1006",
153
- fix: "import the name from exactly one module",
154
- message: `imported name ${decl.name.name} is exported by more than one module`,
155
- span: declaration.span,
156
- });
157
- continue;
158
- }
159
- exportSourceByName.set(decl.name.name, module.name);
160
- const key = `${decl.kind}:${decl.name.name}`;
161
- if (appendedKeys.has(key)) continue;
162
- appendedKeys.add(key);
163
- appended.push(carried);
164
- }
165
- }
166
- for (const name of declaration.names) {
167
- if (resolvedNames.has(name.name)) continue;
168
- issues.push({
169
- code: "HSX1006",
170
- fix: "import a name that the module exports",
171
- message: `module ${declaration.from.value} does not export ${name.name}`,
172
- span: name.span,
173
- });
174
- }
175
- }
176
- const importerDeclarations = new Map(
177
- current.decls.flatMap((decl) =>
178
- decl.kind === "party" || decl.kind === "port"
179
- ? [[decl.name.name, decl] as const]
180
- : [],
181
- ),
182
- );
183
- for (const decl of appended) {
184
- if (
185
- (decl.kind !== "instrument_apply" && decl.kind !== "instrument") ||
186
- !decl.declarationScope
187
- )
188
- continue;
189
- for (const local of decl.declarationScope) {
190
- const importerDeclaration = importerDeclarations.get(local.name.name);
191
- if (!importerDeclaration) continue;
192
- if (sameDeclaration(local, importerDeclaration)) continue;
193
- const importedKind =
194
- decl.kind === "instrument_apply" ? "application" : "instrument";
195
- issues.push({
196
- code: "HSX1009",
197
- fix: `rename the importer-owned ${local.name.name} declaration`,
198
- message: `imported ${importedKind} ${decl.name.name} carries module-local ${local.kind} ${local.name.name}, which collides with an importer-owned declaration`,
199
- span: importerDeclaration.span,
200
- });
201
- }
202
- }
203
- return { ...current, decls: [...current.decls, ...appended] };
204
- };
205
-
206
- const resolved = resolveImports(program, host.moduleName ?? "main");
207
- return issues.length > 0
208
- ? { issues, ok: false }
209
- : { ok: true, origins, program: resolved };
210
- }
211
-
212
- function recordModuleOrigins(
213
- program: Program,
214
- module: ModuleSource,
215
- origins: Map<object, ModuleOrigin>,
216
- ): void {
217
- const lines = lineIndex(module.source);
218
- const seen = new Set<object>();
219
- const visit = (value: unknown): void => {
220
- if (!value || typeof value !== "object" || seen.has(value)) return;
221
- seen.add(value);
222
- if ("span" in value) {
223
- const span = value.span;
224
- if (
225
- span &&
226
- typeof span === "object" &&
227
- "start" in span &&
228
- typeof span.start === "number"
229
- ) {
230
- const position = lineColIn(lines, span.start);
231
- origins.set(span, {
232
- column: position.column,
233
- line: position.line,
234
- moduleName: module.name,
235
- });
236
- }
237
- }
238
- for (const child of Object.values(value)) {
239
- if (Array.isArray(child)) child.forEach(visit);
240
- else visit(child);
241
- }
242
- };
243
- visit(program);
244
- }
245
-
246
- function withDeclarationScope(
247
- application: InstrumentApplyDecl | Extract<Decl, { kind: "instrument" }>,
248
- moduleProgram: Program,
249
- ): InstrumentApplyDecl | Extract<Decl, { kind: "instrument" }> {
250
- const declarations = new Map<string, ApplicationScopeDecl[]>();
251
- for (const decl of moduleProgram.decls) {
252
- if (!isApplicationScopeDecl(decl)) continue;
253
- const named = declarations.get(decl.name.name) ?? [];
254
- named.push(decl);
255
- declarations.set(decl.name.name, named);
256
- if (decl.kind === "instrument" && decl.declarationScope) {
257
- for (const local of decl.declarationScope) {
258
- const localNamed = declarations.get(local.name.name) ?? [];
259
- localNamed.push(local);
260
- declarations.set(local.name.name, localNamed);
261
- }
262
- }
263
- }
264
- const scope: ApplicationScopeDecl[] = [];
265
- const seen = new Set<ApplicationScopeDecl>();
266
- const pending: Decl[] = [application];
267
- while (pending.length > 0) {
268
- const source = pending.pop();
269
- if (!source) continue;
270
- const references = declarationReferences(source);
271
- for (const name of references.names) {
272
- for (const declaration of declarations.get(name) ?? []) {
273
- if (
274
- (declaration.kind === "party" && !references.parties.has(name)) ||
275
- (declaration.kind === "port" && !references.ports.has(name))
276
- )
277
- continue;
278
- if (seen.has(declaration)) continue;
279
- seen.add(declaration);
280
- scope.push(declaration);
281
- pending.push(declaration);
282
- }
283
- }
284
- }
285
- return { ...application, declarationScope: scope };
286
- }
287
-
288
- function isApplicationScopeDecl(decl: Decl): decl is ApplicationScopeDecl {
289
- return (
290
- decl.kind === "const" ||
291
- decl.kind === "instrument" ||
292
- decl.kind === "party" ||
293
- decl.kind === "port" ||
294
- decl.kind === "type"
295
- );
296
- }
297
-
298
- function declarationReferences(decl: Decl): {
299
- readonly names: ReadonlySet<string>;
300
- readonly parties: ReadonlySet<string>;
301
- readonly ports: ReadonlySet<string>;
302
- } {
303
- const names = new Set<string>();
304
- const parties = new Set<string>();
305
- const ports = new Set<string>();
306
- if (decl.kind === "instrument_apply") {
307
- collectExprNames(decl.application, names, ports);
308
- for (const argument of decl.application.args) {
309
- collectApplicationPartyNames(argument, parties);
310
- }
311
- if (decl.metadata) collectExprNames(decl.metadata, names, ports);
312
- }
313
- if (decl.kind === "instrument") {
314
- for (const parameter of decl.parameters)
315
- collectExprNames(parameter.type, names, ports);
316
- collectExprNames(decl.body, names, ports);
317
- for (const parameter of [...decl.parameters, ...decl.typeParameters]) {
318
- names.delete(parameter.name.name);
319
- parties.delete(parameter.name.name);
320
- ports.delete(parameter.name.name);
321
- }
322
- }
323
- if (decl.kind === "const") {
324
- if (decl.type) collectExprNames(decl.type, names, ports);
325
- collectExprNames(decl.value, names, ports);
326
- }
327
- if (decl.kind === "type") collectExprNames(decl.value, names, ports);
328
- if (decl.kind === "party" && decl.attrs)
329
- collectExprNames(decl.attrs, names, ports);
330
- if (decl.kind === "port") {
331
- collectExprNames(decl.body, names, ports);
332
- const allowed = decl.body.entries.find(
333
- (entry) => entry.key.name === "allowed",
334
- );
335
- if (allowed) collectApplicationPartyNames(allowed.value, parties);
336
- }
337
- if (decl.kind === "instrument_apply" || isApplicationScopeDecl(decl)) {
338
- names.delete(decl.name.name);
339
- parties.delete(decl.name.name);
340
- ports.delete(decl.name.name);
341
- }
342
- return { names, parties, ports };
343
- }
344
-
345
- function collectExprNames(
346
- expr: Expr,
347
- names: Set<string>,
348
- ports: Set<string>,
349
- ): void {
350
- if (expr.kind === "ident") names.add(expr.name);
351
- if (expr.kind === "string") {
352
- for (const match of expr.value.matchAll(/\{([A-Za-z][A-Za-z0-9_]*)\}/g)) {
353
- if (match[1]) names.add(match[1]);
354
- }
355
- }
356
- if (expr.kind === "money") names.add(expr.currency.name);
357
- if (expr.kind === "path") {
358
- for (const part of expr.parts) names.add(part.name);
359
- }
360
- if (expr.kind === "type_apply") {
361
- names.add(expr.callee.name);
362
- for (const argument of expr.args) collectExprNames(argument, names, ports);
363
- }
364
- if (expr.kind === "apply") {
365
- collectExprNames(expr.callee, names, ports);
366
- for (const argument of [...expr.typeArgs, ...expr.args]) {
367
- collectExprNames(argument, names, ports);
368
- }
369
- }
370
- if (expr.kind === "call") {
371
- names.add(expr.callee.name);
372
- for (const argument of expr.args) collectExprNames(argument, names, ports);
373
- }
374
- if (expr.kind === "binding") collectExprNames(expr.type, names, ports);
375
- if (expr.kind === "decided_amount") collectExprNames(expr.body, names, ports);
376
- if (expr.kind === "port_ref") {
377
- names.add(expr.name.name);
378
- ports.add(expr.name.name);
379
- }
380
- if (expr.kind === "settlement_ref") {
381
- names.add(expr.owner.name);
382
- names.add(expr.member.name);
383
- }
384
- if (expr.kind === "list") {
385
- for (const item of expr.items) collectExprNames(item, names, ports);
386
- }
387
- if (expr.kind !== "block") return;
388
- for (const entry of expr.entries) {
389
- collectTemplatedNameReferences(entry.key.name, names);
390
- for (const qualifier of entry.qualifiers) {
391
- collectTemplatedNameReferences(qualifier.name, names);
392
- }
393
- if (entry.iteration) collectExprNames(entry.iteration.bound, names, ports);
394
- collectExprNames(entry.value, names, ports);
395
- if (entry.iteration) names.delete(entry.iteration.binding.name);
396
- }
397
- }
398
-
399
- function collectApplicationPartyNames(expr: Expr, names: Set<string>): void {
400
- if (expr.kind === "ident") names.add(expr.name);
401
- if (expr.kind === "binding") collectApplicationPartyNames(expr.type, names);
402
- if (expr.kind === "list") {
403
- for (const item of expr.items) collectApplicationPartyNames(item, names);
404
- }
405
- }
406
-
407
- function sameDeclaration(left: Decl, right: Decl): boolean {
408
- return (
409
- left.kind === right.kind &&
410
- JSON.stringify(canonicalDeclaration(left)) ===
411
- JSON.stringify(canonicalDeclaration(right))
412
- );
413
- }
414
-
415
- function canonicalDeclaration(value: unknown): unknown {
416
- if (Array.isArray(value)) return value.map(canonicalDeclaration);
417
- if (value === null || typeof value !== "object") return value;
418
- return Object.fromEntries(
419
- Object.entries(value as Record<string, unknown>)
420
- .filter(([key]) => key !== "span" && key !== "declarationScope")
421
- .sort(([left], [right]) => left.localeCompare(right))
422
- .map(([key, child]) => [key, canonicalDeclaration(child)]),
423
- );
424
- }
425
-
426
- function collectTemplatedNameReferences(
427
- name: string,
428
- names: Set<string>,
429
- ): void {
430
- for (const match of name.matchAll(/\[([A-Za-z][A-Za-z0-9_]*)\]/g)) {
431
- if (match[1]) names.add(match[1]);
432
- }
433
- }
434
-
435
- function standardSources(
436
- standardLibrary: StandardLibrary,
437
- specifier: string,
438
- names: readonly string[],
439
- ): ModuleSource[] {
440
- if (specifier !== "std/money_flows") return [];
441
- return names.flatMap((name) => {
442
- const source = standardLibrary.source(specifier, name);
443
- return source !== undefined
444
- ? [
445
- {
446
- name: `std.money_flows.${name}`,
447
- source,
448
- },
449
- ]
450
- : [];
451
- });
452
- }