semantifold 0.3.0 → 0.4.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 (297) hide show
  1. package/README.md +58 -15
  2. package/build/index.d.ts +48 -2
  3. package/build/index.d.ts.map +1 -1
  4. package/build/index.js +57 -8
  5. package/build/src/artifact-path.d.ts +23 -1
  6. package/build/src/artifact-path.d.ts.map +1 -1
  7. package/build/src/artifact-path.js +45 -3
  8. package/build/src/artifacts.d.ts.map +1 -1
  9. package/build/src/artifacts.js +66 -1
  10. package/build/src/backends/c-validation.d.ts.map +1 -1
  11. package/build/src/backends/c-validation.js +7 -3
  12. package/build/src/backends/cpp-validation.d.ts.map +1 -1
  13. package/build/src/backends/cpp-validation.js +7 -3
  14. package/build/src/backends/csharp.js +7 -1
  15. package/build/src/backends/dart-runtime.d.ts +2 -0
  16. package/build/src/backends/dart-runtime.d.ts.map +1 -0
  17. package/build/src/backends/dart-runtime.js +41 -0
  18. package/build/src/backends/dart.d.ts +20 -0
  19. package/build/src/backends/dart.d.ts.map +1 -0
  20. package/build/src/backends/dart.js +321 -0
  21. package/build/src/backends/effects.d.ts +60 -0
  22. package/build/src/backends/effects.d.ts.map +1 -0
  23. package/build/src/backends/effects.js +746 -0
  24. package/build/src/backends/go.js +18 -4
  25. package/build/src/backends/identifiers.d.ts +36 -4
  26. package/build/src/backends/identifiers.d.ts.map +1 -1
  27. package/build/src/backends/identifiers.js +154 -10
  28. package/build/src/backends/index.d.ts.map +1 -1
  29. package/build/src/backends/index.js +27 -4
  30. package/build/src/backends/ios.d.ts +68 -0
  31. package/build/src/backends/ios.d.ts.map +1 -0
  32. package/build/src/backends/ios.js +1503 -0
  33. package/build/src/backends/java.d.ts.map +1 -1
  34. package/build/src/backends/java.js +429 -36
  35. package/build/src/backends/javascript.d.ts +6 -0
  36. package/build/src/backends/javascript.d.ts.map +1 -1
  37. package/build/src/backends/javascript.js +380 -24
  38. package/build/src/backends/kotlin-runtime.d.ts +3 -0
  39. package/build/src/backends/kotlin-runtime.d.ts.map +1 -0
  40. package/build/src/backends/kotlin-runtime.js +29 -0
  41. package/build/src/backends/kotlin.d.ts +23 -0
  42. package/build/src/backends/kotlin.d.ts.map +1 -0
  43. package/build/src/backends/kotlin.js +136 -0
  44. package/build/src/backends/native-validation.d.ts +2 -2
  45. package/build/src/backends/native-validation.d.ts.map +1 -1
  46. package/build/src/backends/native-validation.js +2 -2
  47. package/build/src/backends/ordered-expressions.d.ts.map +1 -1
  48. package/build/src/backends/ordered-expressions.js +39 -7
  49. package/build/src/backends/php-stdlib-support.d.ts +19 -0
  50. package/build/src/backends/php-stdlib-support.d.ts.map +1 -0
  51. package/build/src/backends/php-stdlib-support.js +227 -0
  52. package/build/src/backends/php.d.ts.map +1 -1
  53. package/build/src/backends/php.js +448 -33
  54. package/build/src/backends/program.d.ts +81 -0
  55. package/build/src/backends/program.d.ts.map +1 -0
  56. package/build/src/backends/program.js +1308 -0
  57. package/build/src/backends/python.js +7 -1
  58. package/build/src/backends/ruby.d.ts.map +1 -1
  59. package/build/src/backends/ruby.js +310 -24
  60. package/build/src/backends/rust-runtime.d.ts +7 -0
  61. package/build/src/backends/rust-runtime.d.ts.map +1 -0
  62. package/build/src/backends/rust-runtime.js +32 -0
  63. package/build/src/backends/rust-validation.d.ts +9 -0
  64. package/build/src/backends/rust-validation.d.ts.map +1 -0
  65. package/build/src/backends/rust-validation.js +78 -0
  66. package/build/src/backends/rust.d.ts +24 -0
  67. package/build/src/backends/rust.d.ts.map +1 -0
  68. package/build/src/backends/rust.js +344 -0
  69. package/build/src/backends/scalars.d.ts +14 -6
  70. package/build/src/backends/scalars.d.ts.map +1 -1
  71. package/build/src/backends/scalars.js +167 -17
  72. package/build/src/backends/shared.d.ts +23 -4
  73. package/build/src/backends/shared.d.ts.map +1 -1
  74. package/build/src/backends/shared.js +2158 -62
  75. package/build/src/backends/stdlib-support.d.ts +14 -0
  76. package/build/src/backends/stdlib-support.d.ts.map +1 -0
  77. package/build/src/backends/stdlib-support.js +237 -0
  78. package/build/src/backends/swift-runtime.d.ts +3 -0
  79. package/build/src/backends/swift-runtime.d.ts.map +1 -0
  80. package/build/src/backends/swift-runtime.js +14 -0
  81. package/build/src/backends/swift.d.ts +8 -0
  82. package/build/src/backends/swift.d.ts.map +1 -0
  83. package/build/src/backends/swift.js +127 -0
  84. package/build/src/backends/typescript.d.ts.map +1 -1
  85. package/build/src/backends/typescript.js +344 -24
  86. package/build/src/backends/wasm.d.ts +43 -0
  87. package/build/src/backends/wasm.d.ts.map +1 -0
  88. package/build/src/backends/wasm.js +2164 -0
  89. package/build/src/backends/writer.d.ts +235 -4
  90. package/build/src/backends/writer.d.ts.map +1 -1
  91. package/build/src/backends/writer.js +468 -3
  92. package/build/src/diagnostic.d.ts +4 -3
  93. package/build/src/diagnostic.d.ts.map +1 -1
  94. package/build/src/diagnostic.js +5 -3
  95. package/build/src/frontends/c.d.ts.map +1 -1
  96. package/build/src/frontends/c.js +5 -3
  97. package/build/src/frontends/cpp.d.ts.map +1 -1
  98. package/build/src/frontends/cpp.js +5 -3
  99. package/build/src/frontends/csharp.d.ts.map +1 -1
  100. package/build/src/frontends/csharp.js +7 -3
  101. package/build/src/frontends/dart.d.ts +10 -0
  102. package/build/src/frontends/dart.d.ts.map +1 -0
  103. package/build/src/frontends/dart.js +913 -0
  104. package/build/src/frontends/index.d.ts +3 -1
  105. package/build/src/frontends/index.d.ts.map +1 -1
  106. package/build/src/frontends/index.js +15 -5
  107. package/build/src/frontends/java.d.ts +110 -1
  108. package/build/src/frontends/java.d.ts.map +1 -1
  109. package/build/src/frontends/java.js +1725 -70
  110. package/build/src/frontends/javascript-typescript.d.ts +69 -1
  111. package/build/src/frontends/javascript-typescript.d.ts.map +1 -1
  112. package/build/src/frontends/javascript-typescript.js +1825 -93
  113. package/build/src/frontends/kotlin.d.ts +10 -0
  114. package/build/src/frontends/kotlin.d.ts.map +1 -0
  115. package/build/src/frontends/kotlin.js +685 -0
  116. package/build/src/frontends/php.d.ts +118 -1
  117. package/build/src/frontends/php.d.ts.map +1 -1
  118. package/build/src/frontends/php.js +1583 -76
  119. package/build/src/frontends/program.d.ts +74 -0
  120. package/build/src/frontends/program.d.ts.map +1 -0
  121. package/build/src/frontends/program.js +989 -0
  122. package/build/src/frontends/ruby.d.ts +68 -1
  123. package/build/src/frontends/ruby.d.ts.map +1 -1
  124. package/build/src/frontends/ruby.js +1446 -101
  125. package/build/src/frontends/rust-parser.d.ts +11 -0
  126. package/build/src/frontends/rust-parser.d.ts.map +1 -0
  127. package/build/src/frontends/rust-parser.js +15 -0
  128. package/build/src/frontends/rust.d.ts +14 -0
  129. package/build/src/frontends/rust.d.ts.map +1 -0
  130. package/build/src/frontends/rust.js +715 -0
  131. package/build/src/frontends/scalars.d.ts +10 -0
  132. package/build/src/frontends/scalars.d.ts.map +1 -1
  133. package/build/src/frontends/scalars.js +28 -2
  134. package/build/src/frontends/swift.d.ts +10 -0
  135. package/build/src/frontends/swift.d.ts.map +1 -0
  136. package/build/src/frontends/swift.js +860 -0
  137. package/build/src/frontends/types.d.ts +186 -0
  138. package/build/src/frontends/types.d.ts.map +1 -0
  139. package/build/src/frontends/types.js +634 -0
  140. package/build/src/language-registry.d.ts +46 -4
  141. package/build/src/language-registry.d.ts.map +1 -1
  142. package/build/src/language-registry.js +167 -13
  143. package/build/src/mapping.d.ts +2 -2
  144. package/build/src/mapping.d.ts.map +1 -1
  145. package/build/src/mapping.js +31 -10
  146. package/build/src/semantic/capabilities.d.ts +66 -0
  147. package/build/src/semantic/capabilities.d.ts.map +1 -0
  148. package/build/src/semantic/capabilities.js +649 -0
  149. package/build/src/semantic/effects.d.ts +26 -0
  150. package/build/src/semantic/effects.d.ts.map +1 -0
  151. package/build/src/semantic/effects.js +764 -0
  152. package/build/src/semantic/generics.d.ts +23 -0
  153. package/build/src/semantic/generics.d.ts.map +1 -0
  154. package/build/src/semantic/generics.js +61 -0
  155. package/build/src/semantic/lifetime.d.ts +37 -0
  156. package/build/src/semantic/lifetime.d.ts.map +1 -0
  157. package/build/src/semantic/lifetime.js +527 -0
  158. package/build/src/semantic/provenance.d.ts +9 -2
  159. package/build/src/semantic/provenance.d.ts.map +1 -1
  160. package/build/src/semantic/provenance.js +462 -20
  161. package/build/src/semantic/scalars.d.ts +11 -0
  162. package/build/src/semantic/scalars.d.ts.map +1 -1
  163. package/build/src/semantic/scalars.js +16 -1
  164. package/build/src/semantic/stdlib.d.ts +254 -0
  165. package/build/src/semantic/stdlib.d.ts.map +1 -0
  166. package/build/src/semantic/stdlib.js +880 -0
  167. package/build/src/semantic/types.d.ts +2952 -202
  168. package/build/src/semantic/types.d.ts.map +1 -1
  169. package/build/src/semantic/types.js +688 -15
  170. package/build/src/semantic/validate.d.ts +85 -5
  171. package/build/src/semantic/validate.d.ts.map +1 -1
  172. package/build/src/semantic/validate.js +2576 -98
  173. package/build/src/stdlib-facades.d.ts +304 -0
  174. package/build/src/stdlib-facades.d.ts.map +1 -0
  175. package/build/src/stdlib-facades.js +1167 -0
  176. package/build/src/stdlib-negotiation.d.ts +69 -0
  177. package/build/src/stdlib-negotiation.d.ts.map +1 -0
  178. package/build/src/stdlib-negotiation.js +403 -0
  179. package/build/src/stdlib-providers.d.ts +72 -0
  180. package/build/src/stdlib-providers.d.ts.map +1 -0
  181. package/build/src/stdlib-providers.js +339 -0
  182. package/build/src/toolchains.d.ts +55 -1
  183. package/build/src/toolchains.d.ts.map +1 -1
  184. package/build/src/toolchains.js +15 -4
  185. package/docs/architecture.md +45 -13
  186. package/docs/dart.md +60 -0
  187. package/docs/goals.md +3 -3
  188. package/docs/ios.md +121 -0
  189. package/docs/kotlin.md +42 -0
  190. package/docs/language-support.md +231 -26
  191. package/docs/multi-file-modules.md +52 -0
  192. package/docs/parser-qualification.md +121 -8
  193. package/docs/plans/2026-09-05-standard-library-portability.md +10 -10
  194. package/docs/rust.md +88 -0
  195. package/docs/source-maps.md +15 -7
  196. package/docs/standard-library-portability.md +39 -8
  197. package/docs/swift.md +44 -0
  198. package/docs/testing.md +366 -4
  199. package/docs/webassembly.md +52 -0
  200. package/index.js +63 -8
  201. package/node_modules/semantifold-tree-sitter-legacy-internal/README.md +2 -2
  202. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/LICENSE +21 -0
  203. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/README.md +45 -0
  204. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/binding.gyp +30 -0
  205. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/binding.cc +20 -0
  206. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/binding_test.js +9 -0
  207. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/index.d.ts +28 -0
  208. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/index.js +11 -0
  209. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/grammar.js +1630 -0
  210. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/package.json +64 -0
  211. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/darwin-arm64/tree-sitter-rust.node +0 -0
  212. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/darwin-x64/tree-sitter-rust.node +0 -0
  213. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/linux-arm64/tree-sitter-rust.node +0 -0
  214. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/linux-x64/tree-sitter-rust.node +0 -0
  215. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/win32-arm64/tree-sitter-rust.node +0 -0
  216. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/win32-x64/tree-sitter-rust.node +0 -0
  217. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/highlights.scm +159 -0
  218. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/injections.scm +9 -0
  219. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/tags.scm +60 -0
  220. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/grammar.json +9405 -0
  221. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/node-types.json +5399 -0
  222. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/parser.c +192569 -0
  223. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/scanner.c +393 -0
  224. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/alloc.h +54 -0
  225. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/array.h +290 -0
  226. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/parser.h +266 -0
  227. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/tree-sitter-rust.wasm +0 -0
  228. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/tree-sitter.json +53 -0
  229. package/node_modules/semantifold-tree-sitter-legacy-internal/package.json +3 -2
  230. package/node_modules/semantifold-tree-sitter-legacy-internal/src/c.js +5 -4
  231. package/package.json +5 -2
  232. package/src/artifact-path.js +47 -2
  233. package/src/artifacts.js +67 -0
  234. package/src/backends/c-validation.js +6 -1
  235. package/src/backends/cpp-validation.js +6 -1
  236. package/src/backends/csharp.js +3 -0
  237. package/src/backends/dart-runtime.js +41 -0
  238. package/src/backends/dart.js +326 -0
  239. package/src/backends/effects.js +705 -0
  240. package/src/backends/go.js +14 -2
  241. package/src/backends/identifiers.js +160 -9
  242. package/src/backends/index.js +27 -2
  243. package/src/backends/ios.js +1612 -0
  244. package/src/backends/java.js +454 -36
  245. package/src/backends/javascript.js +389 -23
  246. package/src/backends/kotlin-runtime.js +29 -0
  247. package/src/backends/kotlin.js +141 -0
  248. package/src/backends/native-validation.js +1 -1
  249. package/src/backends/ordered-expressions.js +40 -6
  250. package/src/backends/php-stdlib-support.js +239 -0
  251. package/src/backends/php.js +469 -32
  252. package/src/backends/program.js +1407 -0
  253. package/src/backends/python.js +3 -0
  254. package/src/backends/ruby.js +326 -23
  255. package/src/backends/rust-runtime.js +34 -0
  256. package/src/backends/rust-validation.js +75 -0
  257. package/src/backends/rust.js +335 -0
  258. package/src/backends/scalars.js +141 -16
  259. package/src/backends/shared.js +2304 -124
  260. package/src/backends/stdlib-support.js +234 -0
  261. package/src/backends/swift-runtime.js +14 -0
  262. package/src/backends/swift.js +132 -0
  263. package/src/backends/typescript.js +349 -23
  264. package/src/backends/wasm.js +2208 -0
  265. package/src/backends/writer.js +520 -2
  266. package/src/diagnostic.js +4 -2
  267. package/src/frontends/c.js +4 -2
  268. package/src/frontends/cpp.js +4 -2
  269. package/src/frontends/csharp.js +4 -2
  270. package/src/frontends/dart.js +963 -0
  271. package/src/frontends/index.js +14 -4
  272. package/src/frontends/java.js +1977 -69
  273. package/src/frontends/javascript-typescript.js +2046 -102
  274. package/src/frontends/kotlin.js +713 -0
  275. package/src/frontends/php.js +1725 -79
  276. package/src/frontends/program.js +1062 -0
  277. package/src/frontends/ruby.js +1624 -98
  278. package/src/frontends/rust-parser.js +18 -0
  279. package/src/frontends/rust.js +714 -0
  280. package/src/frontends/scalars.js +31 -1
  281. package/src/frontends/swift.js +893 -0
  282. package/src/frontends/types.js +750 -0
  283. package/src/language-registry.js +172 -12
  284. package/src/mapping.js +33 -9
  285. package/src/semantic/capabilities.js +671 -0
  286. package/src/semantic/effects.js +737 -0
  287. package/src/semantic/generics.js +58 -0
  288. package/src/semantic/lifetime.js +517 -0
  289. package/src/semantic/provenance.js +412 -19
  290. package/src/semantic/scalars.js +17 -0
  291. package/src/semantic/stdlib.js +921 -0
  292. package/src/semantic/types.js +765 -14
  293. package/src/semantic/validate.js +2934 -209
  294. package/src/stdlib-facades.js +1211 -0
  295. package/src/stdlib-negotiation.js +446 -0
  296. package/src/stdlib-providers.js +377 -0
  297. package/src/toolchains.js +16 -3
@@ -0,0 +1,234 @@
1
+ // @ts-check
2
+
3
+ import {generateTask037PhpProviderContent} from "./php-stdlib-support.js"
4
+
5
+ /**
6
+ * @typedef {"php" | "ruby" | "javascript" | "typescript"} StdlibProviderTarget
7
+ * @typedef {{when: (operations: Set<string>) => boolean, text: string}} SupportSegment
8
+ */
9
+
10
+ /**
11
+ * Emits the protected native entry shims that portable code is allowed to call.
12
+ * @param {StdlibProviderTarget} language - Adopted target.
13
+ * @param {readonly string[]} operations - Used canonical operations in contract order.
14
+ * @returns {string[]} One shim line per used operation.
15
+ */
16
+ function providerShims(language, operations) {
17
+ const prefix = `__semantifold_provider_${language}_`
18
+ const shims = []
19
+
20
+ for (const operation of operations) {
21
+ if (language == "php") {
22
+ if (operation == "probeEffect") shims.push(`function ${prefix}probeEffect(string $label, int $value, bool $fail): int { return probeEffect($label, $value, $fail); }`)
23
+ else if (operation == "probeAcquire") shims.push(`function ${prefix}probeAcquire(bool $fail): ProbeResource { return probeAcquire($fail); }`)
24
+ else if (operation == "probeRead") shims.push(`function ${prefix}probeRead(ProbeResource $resource, bool $fail): ?string { return probeRead($resource, $fail); }`)
25
+ else if (operation == "probeClose") shims.push(`function ${prefix}probeClose(ProbeResource $resource, bool $fail): void { probeClose($resource, $fail); }`)
26
+ else shims.push(`function ${prefix}probeTrace(): string { return probeTrace(); }`)
27
+ } else if (language == "ruby") {
28
+ const parameter = operation == "probeEffect" ? "label, value, fail_value"
29
+ : operation == "probeAcquire" ? "fail_value"
30
+ : operation == "probeTrace" ? "" : "resource, fail_value"
31
+ const call = operation == "probeEffect" ? "probeEffect(label, value, fail_value)"
32
+ : operation == "probeAcquire" ? "probeAcquire(fail_value)"
33
+ : operation == "probeTrace" ? "probeTrace()" : `${operation}(resource, fail_value)`
34
+ shims.push(`def ${prefix}${operation}(${parameter})\n ${call}\nend`)
35
+ } else if (language == "javascript") {
36
+ const parameters = operation == "probeEffect" ? "label, value, fail"
37
+ : operation == "probeAcquire" ? "fail"
38
+ : operation == "probeTrace" ? "" : "resource, fail"
39
+ const body = operation == "probeEffect" ? "return probeEffect(label, value, fail)"
40
+ : operation == "probeAcquire" ? "return probeAcquire(fail)"
41
+ : operation == "probeTrace" ? "return probeTrace()"
42
+ : operation == "probeClose" ? "probeClose(resource, fail)" : "return probeRead(resource, fail)"
43
+ shims.push(`export function ${prefix}${operation}(${parameters}) { ${body} }`)
44
+ } else {
45
+ const signature = operation == "probeEffect" ? "(label: string, value: number, fail: boolean): number"
46
+ : operation == "probeAcquire" ? "(fail: boolean): ProbeResource"
47
+ : operation == "probeTrace" ? "(): string"
48
+ : operation == "probeClose" ? "(resource: ProbeResource, fail: boolean): void" : "(resource: ProbeResource, fail: boolean): string | null"
49
+ const body = operation == "probeEffect" ? "return probeEffect(label, value, fail)"
50
+ : operation == "probeAcquire" ? "return probeAcquire(fail)"
51
+ : operation == "probeTrace" ? "return probeTrace()"
52
+ : operation == "probeClose" ? "probeClose(resource, fail)" : "return probeRead(resource, fail)"
53
+ shims.push(`export function ${prefix}${operation}${signature} { ${body} }`)
54
+ }
55
+ }
56
+ return shims
57
+ }
58
+
59
+ /**
60
+ * Returns the ordered tree-shakable support segments for one adopted target.
61
+ * The full operation set reproduces the single-module support carrier byte-for-byte.
62
+ * @param {StdlibProviderTarget} language - Adopted target.
63
+ * @returns {SupportSegment[]} Ordered segments.
64
+ */
65
+ function supportSegments(language) {
66
+ const any = () => true
67
+
68
+ /**
69
+ * Reports whether any resource-lifecycle operation is used.
70
+ * @param {Set<string>} operations - Used operations.
71
+ * @returns {boolean} Whether a resource operation is used.
72
+ */
73
+ const resource = (operations) => operations.has("probeAcquire") || operations.has("probeRead") || operations.has("probeClose")
74
+
75
+ if (language == "php") return [
76
+ {when: (operations) => operations.has("probeEffect"), text: "final class ProbeOperationFailure extends RuntimeException {}\n"},
77
+ {when: (operations) => operations.has("probeAcquire"), text: "final class ProbeAcquireFailure extends RuntimeException {}\n"},
78
+ {when: (operations) => operations.has("probeRead"), text: "final class ProbeReadFailure extends RuntimeException {}\n"},
79
+ {when: (operations) => operations.has("probeClose"), text: "final class ProbeCloseFailure extends RuntimeException {}\n"},
80
+ {when: (operations) => operations.has("probeRead") || operations.has("probeClose"), text: "final class ProbeResourceClosed extends RuntimeException {}\n"},
81
+ {
82
+ when: resource,
83
+ text: "final class ProbeResource {\n public bool $closed = false;\n public function __construct(public mixed $handle) {}\n}\n"
84
+ },
85
+ {when: any, text: "$GLOBALS['__semantifold_task034_trace'] = [];\n"},
86
+ {
87
+ when: (operations) => operations.has("probeEffect"),
88
+ text: "function probeEffect(string $label, int $value, bool $fail): int {\n $GLOBALS['__semantifold_task034_trace'][] = 'effect:' . $label;\n if ($fail) throw new ProbeOperationFailure('ProbeOperationFailure');\n return $value;\n}\n"
89
+ },
90
+ {
91
+ when: (operations) => operations.has("probeAcquire"),
92
+ text: "function probeAcquire(bool $fail): ProbeResource {\n $GLOBALS['__semantifold_task034_trace'][] = 'acquire';\n if ($fail) throw new ProbeAcquireFailure('ProbeAcquireFailure');\n try { $handle = @fopen((string) getenv('SEMANTIFOLD_TASK034_PROBE_PATH'), 'rb'); }\n catch (Throwable) { throw new ProbeAcquireFailure('ProbeAcquireFailure'); }\n if ($handle === false) throw new ProbeAcquireFailure('ProbeAcquireFailure');\n return new ProbeResource($handle);\n}\n"
93
+ },
94
+ {
95
+ when: (operations) => operations.has("probeRead"),
96
+ text: "function probeRead(ProbeResource $resource, bool $fail): ?string {\n if ($resource->closed) throw new ProbeResourceClosed('ProbeResourceClosed');\n $GLOBALS['__semantifold_task034_trace'][] = 'read';\n if ($fail) throw new ProbeReadFailure('ProbeReadFailure');\n try { $line = @fgets($resource->handle); }\n catch (Throwable) { throw new ProbeReadFailure('ProbeReadFailure'); }\n if ($line === false) {\n if (feof($resource->handle)) return null;\n throw new ProbeReadFailure('ProbeReadFailure');\n }\n return rtrim($line, \"\\r\\n\");\n}\n"
97
+ },
98
+ {
99
+ when: (operations) => operations.has("probeClose"),
100
+ text: "function probeClose(ProbeResource $resource, bool $fail): void {\n if ($resource->closed) throw new ProbeResourceClosed('ProbeResourceClosed');\n $GLOBALS['__semantifold_task034_trace'][] = 'close';\n try { $closed = @fclose($resource->handle); }\n catch (Throwable) { $resource->closed = true; throw new ProbeCloseFailure('ProbeCloseFailure'); }\n $resource->closed = true;\n if ($fail || !$closed) throw new ProbeCloseFailure('ProbeCloseFailure');\n}\n"
101
+ },
102
+ {
103
+ when: (operations) => operations.has("probeTrace"),
104
+ text: "function probeTrace(): string { return implode(',', $GLOBALS['__semantifold_task034_trace']); }\n"
105
+ }
106
+ ]
107
+ if (language == "ruby") return [
108
+ {when: (operations) => operations.has("probeEffect"), text: "class ProbeOperationFailure < StandardError; end\n"},
109
+ {when: (operations) => operations.has("probeAcquire"), text: "class ProbeAcquireFailure < StandardError; end\n"},
110
+ {when: (operations) => operations.has("probeRead"), text: "class ProbeReadFailure < StandardError; end\n"},
111
+ {when: (operations) => operations.has("probeClose"), text: "class ProbeCloseFailure < StandardError; end\n"},
112
+ {when: (operations) => operations.has("probeRead") || operations.has("probeClose"), text: "class ProbeResourceClosed < StandardError; end\n"},
113
+ {
114
+ when: resource,
115
+ text: "class ProbeResource\n attr_accessor :handle, :closed\n def initialize(handle); @handle = handle; @closed = false; end\nend\n"
116
+ },
117
+ {when: any, text: "$__semantifold_task034_trace = []\n"},
118
+ {
119
+ when: (operations) => operations.has("probeEffect"),
120
+ text: "def probeEffect(label, value, fail_value)\n $__semantifold_task034_trace << \"effect:#{label}\"\n raise ProbeOperationFailure, \"ProbeOperationFailure\" if fail_value\n value\nend\n"
121
+ },
122
+ {
123
+ when: (operations) => operations.has("probeAcquire"),
124
+ text: "def probeAcquire(fail_value)\n $__semantifold_task034_trace << \"acquire\"\n raise ProbeAcquireFailure, \"ProbeAcquireFailure\" if fail_value\n begin; ProbeResource.new(File.open(ENV.fetch(\"SEMANTIFOLD_TASK034_PROBE_PATH\"), \"r\")); rescue ProbeAcquireFailure; raise; rescue StandardError; raise ProbeAcquireFailure, \"ProbeAcquireFailure\"; end\nend\n"
125
+ },
126
+ {
127
+ when: (operations) => operations.has("probeRead"),
128
+ text: "def probeRead(resource, fail_value)\n raise ProbeResourceClosed, \"ProbeResourceClosed\" if resource.closed\n $__semantifold_task034_trace << \"read\"\n raise ProbeReadFailure, \"ProbeReadFailure\" if fail_value\n begin; value = resource.handle.gets; value.nil? ? nil : value.chomp; rescue ProbeReadFailure; raise; rescue StandardError; raise ProbeReadFailure, \"ProbeReadFailure\"; end\nend\n"
129
+ },
130
+ {
131
+ when: (operations) => operations.has("probeClose"),
132
+ text: "def probeClose(resource, fail_value)\n raise ProbeResourceClosed, \"ProbeResourceClosed\" if resource.closed\n $__semantifold_task034_trace << \"close\"\n begin; resource.handle.close; rescue StandardError; resource.closed = true; raise ProbeCloseFailure, \"ProbeCloseFailure\"; end\n resource.closed = true\n raise ProbeCloseFailure, \"ProbeCloseFailure\" if fail_value\n nil\nend\n"
133
+ },
134
+ {when: (operations) => operations.has("probeTrace"), text: "def probeTrace; $__semantifold_task034_trace.join(\",\"); end\n"}
135
+ ]
136
+ const fs = /** @type {SupportSegment} */ ({when: (operations) => operations.has("probeAcquire"), text: 'const __semantifoldTask034Fs = process.getBuiltinModule("node:fs")\n'})
137
+ const trace = language == "javascript"
138
+ ? {when: any, text: "const __semantifoldTask034Trace = []\n"}
139
+ : {when: any, text: "const __semantifoldTask034Trace: string[] = []\n"}
140
+ const resourceClass = language == "javascript"
141
+ ? {when: resource, text: "class ProbeResource { constructor(handle) { this.handle = handle; this.closed = false; this.position = 0 } }\n"}
142
+ : {when: resource, text: "class ProbeResource { closed = false; position = 0; constructor(readonly handle: number) {} }\n"}
143
+
144
+ if (language == "javascript") return [
145
+ fs,
146
+ {when: (operations) => operations.has("probeEffect"), text: "class ProbeOperationFailure extends Error {}\n"},
147
+ {when: (operations) => operations.has("probeAcquire"), text: "class ProbeAcquireFailure extends Error {}\n"},
148
+ {when: (operations) => operations.has("probeRead"), text: "class ProbeReadFailure extends Error {}\n"},
149
+ {when: (operations) => operations.has("probeClose"), text: "class ProbeCloseFailure extends Error {}\n"},
150
+ {when: (operations) => operations.has("probeRead") || operations.has("probeClose"), text: "class ProbeResourceClosed extends Error {}\n"},
151
+ resourceClass,
152
+ trace,
153
+ {
154
+ when: (operations) => operations.has("probeEffect"),
155
+ text: 'function probeEffect(label, value, fail) { __semantifoldTask034Trace.push(`effect:${label}`); if (fail) throw new ProbeOperationFailure("ProbeOperationFailure"); return value }\n'
156
+ },
157
+ {
158
+ when: (operations) => operations.has("probeAcquire"),
159
+ text: 'function probeAcquire(fail) { __semantifoldTask034Trace.push("acquire"); if (fail) throw new ProbeAcquireFailure("ProbeAcquireFailure"); try { return new ProbeResource(__semantifoldTask034Fs.openSync(process.env.SEMANTIFOLD_TASK034_PROBE_PATH, "r")) } catch { throw new ProbeAcquireFailure("ProbeAcquireFailure") } }\n'
160
+ },
161
+ {
162
+ when: (operations) => operations.has("probeRead"),
163
+ text: 'function probeRead(resource, fail) { if (resource.closed) throw new ProbeResourceClosed("ProbeResourceClosed"); __semantifoldTask034Trace.push("read"); if (fail) throw new ProbeReadFailure("ProbeReadFailure"); try { const bytes = []; const buffer = Buffer.alloc(1); while (true) { const count = __semantifoldTask034Fs.readSync(resource.handle, buffer, 0, 1, resource.position++); if (count === 0) return bytes.length === 0 ? null : Buffer.from(bytes).toString("utf8"); if (buffer[0] === 10) return Buffer.from(bytes).toString("utf8").replace(/\\r$/u, ""); bytes.push(buffer[0]) } } catch { throw new ProbeReadFailure("ProbeReadFailure") } }\n'
164
+ },
165
+ {
166
+ when: (operations) => operations.has("probeClose"),
167
+ text: 'function probeClose(resource, fail) { if (resource.closed) throw new ProbeResourceClosed("ProbeResourceClosed"); __semantifoldTask034Trace.push("close"); try { __semantifoldTask034Fs.closeSync(resource.handle) } catch { resource.closed = true; throw new ProbeCloseFailure("ProbeCloseFailure") } resource.closed = true; if (fail) throw new ProbeCloseFailure("ProbeCloseFailure") }\n'
168
+ },
169
+ {when: (operations) => operations.has("probeTrace"), text: 'function probeTrace() { return __semantifoldTask034Trace.join(",") }\n'}
170
+ ]
171
+ return [
172
+ fs,
173
+ {when: (operations) => operations.has("probeEffect"), text: "class ProbeOperationFailure extends Error {}\n"},
174
+ {when: (operations) => operations.has("probeAcquire"), text: "class ProbeAcquireFailure extends Error {}\n"},
175
+ {when: (operations) => operations.has("probeRead"), text: "class ProbeReadFailure extends Error {}\n"},
176
+ {when: (operations) => operations.has("probeClose"), text: "class ProbeCloseFailure extends Error {}\n"},
177
+ {when: (operations) => operations.has("probeRead") || operations.has("probeClose"), text: "class ProbeResourceClosed extends Error {}\n"},
178
+ resourceClass,
179
+ trace,
180
+ {
181
+ when: (operations) => operations.has("probeEffect"),
182
+ text: 'function probeEffect(label: string, value: number, fail: boolean): number { __semantifoldTask034Trace.push(`effect:${label}`); if (fail) throw new ProbeOperationFailure("ProbeOperationFailure"); return value }\n'
183
+ },
184
+ {
185
+ when: (operations) => operations.has("probeAcquire"),
186
+ text: 'function probeAcquire(fail: boolean): ProbeResource { __semantifoldTask034Trace.push("acquire"); if (fail) throw new ProbeAcquireFailure("ProbeAcquireFailure"); try { return new ProbeResource(__semantifoldTask034Fs.openSync(process.env.SEMANTIFOLD_TASK034_PROBE_PATH as string, "r")) } catch { throw new ProbeAcquireFailure("ProbeAcquireFailure") } }\n'
187
+ },
188
+ {
189
+ when: (operations) => operations.has("probeRead"),
190
+ text: 'function probeRead(resource: ProbeResource, fail: boolean): string | null { if (resource.closed) throw new ProbeResourceClosed("ProbeResourceClosed"); __semantifoldTask034Trace.push("read"); if (fail) throw new ProbeReadFailure("ProbeReadFailure"); try { const bytes: number[] = []; const buffer = Buffer.alloc(1); while (true) { const count = __semantifoldTask034Fs.readSync(resource.handle, buffer, 0, 1, resource.position++); if (count === 0) return bytes.length === 0 ? null : Buffer.from(bytes).toString("utf8"); if (buffer[0] === 10) return Buffer.from(bytes).toString("utf8").replace(/\\r$/u, ""); bytes.push(buffer[0]) } } catch { throw new ProbeReadFailure("ProbeReadFailure") } }\n'
191
+ },
192
+ {
193
+ when: (operations) => operations.has("probeClose"),
194
+ text: 'function probeClose(resource: ProbeResource, fail: boolean): void { if (resource.closed) throw new ProbeResourceClosed("ProbeResourceClosed"); __semantifoldTask034Trace.push("close"); try { __semantifoldTask034Fs.closeSync(resource.handle) } catch { resource.closed = true; throw new ProbeCloseFailure("ProbeCloseFailure") } resource.closed = true; if (fail) throw new ProbeCloseFailure("ProbeCloseFailure") }\n'
195
+ },
196
+ {when: (operations) => operations.has("probeTrace"), text: 'function probeTrace(): string { return __semantifoldTask034Trace.join(",") }\n'}
197
+ ]
198
+ }
199
+
200
+ /**
201
+ * Generates the complete tree-shaken stdlib provider artifact content for one adopted target.
202
+ * @param {StdlibProviderTarget} language - Adopted target host provider.
203
+ * @param {readonly string[]} operations - Used canonical operations in contract order.
204
+ * @param {string} [module] - Qualified canonical module identity; omitted for the Task 034 probe.
205
+ * @returns {string} Complete provider source text.
206
+ */
207
+ export function generateStdlibProviderContent(language, operations, module) {
208
+ if (module !== undefined && module != "semantifold.task034.resource-probe") {
209
+ if (language != "php") throw new RangeError(`Canonical module '${module}' has no '${language}' provider content.`)
210
+ return generateTask037PhpProviderContent(module, operations)
211
+ }
212
+ const used = new Set(operations)
213
+
214
+ if (used.size == 0) throw new Error("A stdlib provider requires at least one used operation.")
215
+ const segments = supportSegments(language)
216
+ const start = language == "ruby" ? "# semantifold-task034-support\n" : "/* semantifold-task034-support */\n"
217
+ const end = language == "ruby" ? "# semantifold-task034-support-end\n\n" : "/* semantifold-task034-support-end */\n\n"
218
+ const support = start +
219
+ segments.filter(({when}) => when(used)).map(({text}) => text).join("") +
220
+ end
221
+ const header = language == "php" ? "<?php\ndeclare(strict_types=1);\n" : ""
222
+ const classExports = language == "javascript" || language == "typescript" ?
223
+ `export {${[
224
+ ["ProbeOperationFailure", used.has("probeEffect")],
225
+ ["ProbeAcquireFailure", used.has("probeAcquire")],
226
+ ["ProbeReadFailure", used.has("probeRead")],
227
+ ["ProbeCloseFailure", used.has("probeClose")],
228
+ ["ProbeResourceClosed", used.has("probeRead") || used.has("probeClose")],
229
+ ["ProbeResource", used.has("probeAcquire") || used.has("probeRead") || used.has("probeClose")]
230
+ ].filter(([, present]) => present).map(([name]) => name).join(", ")}}\n` : ""
231
+ const shims = providerShims(language, operations)
232
+
233
+ return header + support + classExports + (shims.length > 0 ? shims.join("\n") + "\n" : "")
234
+ }
@@ -0,0 +1,14 @@
1
+ // @ts-check
2
+
3
+ /** Exact target-only support for Unicode-scalar string equality. */
4
+ export const swiftRuntime = `func semantifold_string_equal(_ left: String, _ right: String) -> Bool {
5
+ return left.unicodeScalars.elementsEqual(right.unicodeScalars)
6
+ }
7
+
8
+ func semantifold_string_not_equal(_ left: String, _ right: String) -> Bool {
9
+ return !left.unicodeScalars.elementsEqual(right.unicodeScalars)
10
+ }
11
+
12
+ func semantifold_keep_mutable<T>(_ value: inout T) {
13
+ }
14
+ `
@@ -0,0 +1,132 @@
1
+ // @ts-check
2
+
3
+ import {emitExpression} from "./shared.js"
4
+ import {emitScalarType} from "./scalars.js"
5
+ import {swiftRuntime} from "./swift-runtime.js"
6
+
7
+ /**
8
+ * Emits one deterministic dependency-free Swift program through the source-aware writer.
9
+ * @param {import("../semantic/types.js").SemanticModule} module - Validated semantic module.
10
+ * @param {import("./writer.js").SourceWriter} writer - Source-aware writer.
11
+ * @returns {void}
12
+ */
13
+ export function generateSwift(module, writer) {
14
+ writer.synthetic(swiftRuntime, "Exact Swift Unicode-scalar string equality support", [module], [""])
15
+
16
+ module.functions.forEach((declaration, functionIndex) => {
17
+ const path = `/functions/${functionIndex}`
18
+
19
+ writer.synthetic("\n", "declaration separator", [declaration], [path])
20
+ writer.mapped("func", {mappingKind: "anchor", node: declaration, path})
21
+ writer.synthetic(" ", "declaration spacing", [declaration], [path])
22
+ writer.mapped(declaration.name, {mappingKind: "exact", node: declaration, path, role: "name"})
23
+ writer.synthetic("(", "function parameter scaffold", [declaration], [path])
24
+ declaration.parameters.forEach((parameter, parameterIndex) => {
25
+ const parameterPath = `${path}/parameters/${parameterIndex}`
26
+
27
+ if (parameterIndex) writer.synthetic(", ", "parameter separator", [parameter], [parameterPath])
28
+ writer.synthetic("_ ", "unlabeled Swift parameter scaffold", [parameter], [parameterPath])
29
+ writer.mapped(parameter.name, {mappingKind: "exact", node: parameter, path: parameterPath, role: "name"})
30
+ writer.synthetic(": ", "parameter annotation scaffold", [parameter], [parameterPath])
31
+ writer.mapped(emitScalarType("swift", parameter.type), {mappingKind: "exact", node: parameter.type,
32
+ path: `${parameterPath}/type`, role: "type"})
33
+ })
34
+ writer.synthetic(") -> ", "function return scaffold", [declaration], [path])
35
+ writer.mapped(emitScalarType("swift", declaration.returnType), {mappingKind: "exact", node: declaration.returnType,
36
+ path: `${path}/returnType`, role: "type"})
37
+ writer.synthetic(" {\n", "Swift function body scaffold", [declaration], [path])
38
+ emitBlock(writer, declaration.body, " ", `${path}/body`)
39
+ writer.synthetic("}\n", "Swift function body scaffold", [declaration], [path])
40
+ })
41
+
42
+ writer.synthetic("\n", "Swift top-level entry scaffold", [module.entryPoint], ["/entryPoint"])
43
+ emitBlock(writer, module.entryPoint.body, "", "/entryPoint/body")
44
+ }
45
+
46
+ /**
47
+ * Emits one ordered Swift block.
48
+ * @param {import("./writer.js").SourceWriter} writer - Source-aware writer.
49
+ * @param {import("../semantic/types.js").Block} block - Semantic block.
50
+ * @param {string} indent - Current indentation.
51
+ * @param {string} path - Exact block path.
52
+ * @returns {void}
53
+ */
54
+ function emitBlock(writer, block, indent, path) {
55
+ block.statements.forEach((statement, index) => emitStatement(writer, statement, indent, `${path}/statements/${index}`))
56
+ }
57
+
58
+ /**
59
+ * Emits one validated Swift statement.
60
+ * @param {import("./writer.js").SourceWriter} writer - Source-aware writer.
61
+ * @param {import("../semantic/types.js").Statement} statement - Semantic statement.
62
+ * @param {string} indent - Current indentation.
63
+ * @param {string} path - Statement occurrence path.
64
+ * @returns {void}
65
+ */
66
+ function emitStatement(writer, statement, indent, path) {
67
+ writer.synthetic(indent, "indentation", [statement], [path])
68
+ if (statement.kind == "LocalDeclaration") {
69
+ writer.mapped(statement.mutable ? "var" : "let", {mappingKind: "anchor", node: statement, path})
70
+ writer.synthetic(" ", "declaration spacing", [statement], [path])
71
+ writer.mapped(statement.name, {mappingKind: "exact", node: statement, path, role: "name"})
72
+ writer.synthetic(": ", "local annotation scaffold", [statement], [path])
73
+ writer.mapped(emitScalarType("swift", statement.type), {mappingKind: "exact", node: statement.type,
74
+ path: `${path}/type`, role: "type"})
75
+ writer.synthetic(" ", "assignment spacing", [statement], [path])
76
+ writer.mapped("=", {mappingKind: "exact", node: statement, path, role: "operator"})
77
+ writer.synthetic(" ", "assignment spacing", [statement], [path])
78
+ emitExpression(writer, statement.initializer, `${path}/initializer`, "swift", identity)
79
+ writer.synthetic("\n", "line break", [statement], [path])
80
+ if (statement.mutable) {
81
+ writer.synthetic(`${indent}semantifold_keep_mutable(&${statement.name})\n`, "Swift mutable-local warning marker", [statement], [path])
82
+ }
83
+ return
84
+ }
85
+ if (statement.kind == "AssignmentStatement") {
86
+ writer.mapped(statement.target.name, {mappingKind: "exact", node: statement.target, path: `${path}/target`, role: "name"})
87
+ writer.synthetic(" ", "assignment spacing", [statement], [path])
88
+ writer.mapped("=", {mappingKind: "exact", node: statement, path, role: "operator"})
89
+ writer.synthetic(" ", "assignment spacing", [statement], [path])
90
+ emitExpression(writer, statement.expression, `${path}/expression`, "swift", identity)
91
+ writer.synthetic("\n", "line break", [statement], [path])
92
+ return
93
+ }
94
+ if (statement.kind == "ExpressionStatement") throw new TypeError("Swift Task 005 expression statement reached emission.")
95
+ if (statement.kind == "ReturnStatement") {
96
+ if (!statement.expression) throw new TypeError("Swift bare return reached emission.")
97
+ writer.mapped("return", {mappingKind: "anchor", node: statement, path})
98
+ writer.synthetic(" ", "return spacing", [statement], [path])
99
+ emitExpression(writer, statement.expression, `${path}/expression`, "swift", identity)
100
+ writer.synthetic("\n", "line break", [statement], [path])
101
+ return
102
+ }
103
+ if (statement.kind == "PrintStatement") {
104
+ writer.synthetic("print(", "Swift print plumbing", [statement], [path])
105
+ emitExpression(writer, statement.expression, `${path}/expression`, "swift", identity)
106
+ writer.synthetic(")\n", "Swift print plumbing", [statement], [path])
107
+ return
108
+ }
109
+ if (statement.kind != "IfStatement") throw new TypeError("Unsupported Swift statement reached emission.")
110
+
111
+ writer.mapped("if", {mappingKind: "anchor", node: statement, path})
112
+ writer.synthetic(" ", "conditional spacing", [statement], [path])
113
+ emitExpression(writer, statement.condition, `${path}/condition`, "swift", identity)
114
+ writer.synthetic(" {\n", "Swift conditional scaffold", [statement.consequent], [`${path}/consequent`])
115
+ emitBlock(writer, statement.consequent, `${indent} `, `${path}/consequent`)
116
+ writer.synthetic(`${indent}}`, "Swift conditional scaffold", [statement.consequent], [`${path}/consequent`])
117
+ if (statement.alternate) {
118
+ writer.synthetic(" else {\n", "Swift alternate scaffold", [statement.alternate], [`${path}/alternate`])
119
+ emitBlock(writer, statement.alternate, `${indent} `, `${path}/alternate`)
120
+ writer.synthetic(`${indent}}`, "Swift alternate scaffold", [statement.alternate], [`${path}/alternate`])
121
+ }
122
+ writer.synthetic("\n", "line break", [statement], [path])
123
+ }
124
+
125
+ /**
126
+ * Returns an unchanged Swift identifier.
127
+ * @param {string} name - Validated identifier.
128
+ * @returns {string} Unchanged identifier.
129
+ */
130
+ function identity(name) {
131
+ return name
132
+ }