tla-ts 0.0.9 → 0.0.10-dev.28c823e4b

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 (390) hide show
  1. package/README.md +29 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +28 -28
  5. package/dist/deciders.js +43 -43
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +106 -10
  8. package/dist/index.d.ts +106 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +129 -104
  21. package/dist/transfer.js +129 -104
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +4 -23
  26. package/src/bags.ts +99 -102
  27. package/src/builtins.ts +40 -78
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +599 -388
  30. package/src/cli/options.ts +11 -6
  31. package/src/cli/run.ts +0 -2
  32. package/src/cli/runtime-invariants.ts +136 -0
  33. package/src/cli/shared.ts +18 -11
  34. package/src/cli/tlapm.ts +0 -12
  35. package/src/cli/tlc-arguments.ts +640 -0
  36. package/src/cli/tlc-usage.ts +361 -0
  37. package/src/cli/tlc.ts +538 -220
  38. package/src/cli/typescript.ts +23 -0
  39. package/src/cli/xml-exporter.ts +4 -16
  40. package/src/cli.ts +0 -1
  41. package/src/codegen.ts +191 -64
  42. package/src/config.ts +140 -109
  43. package/src/coverage.ts +837 -0
  44. package/src/dependencies.ts +6 -40
  45. package/src/dfid.ts +618 -0
  46. package/src/environment.ts +240 -118
  47. package/src/errors.ts +70 -55
  48. package/src/evaluate.ts +596 -339
  49. package/src/exit-codes.ts +37 -3
  50. package/src/fingerprints.ts +10 -10
  51. package/src/index.ts +3 -0
  52. package/src/instances.ts +79 -11
  53. package/src/java-overrides.ts +4 -19
  54. package/src/json-reader.ts +455 -0
  55. package/src/json.ts +255 -139
  56. package/src/levels.ts +8 -17
  57. package/src/lexer.ts +80 -100
  58. package/src/liveness-checker.ts +82 -119
  59. package/src/liveness.ts +112 -64
  60. package/src/obligations/action.ts +4 -28
  61. package/src/obligations/anonymize.ts +84 -80
  62. package/src/obligations/equality.ts +1 -1
  63. package/src/obligations/expression.ts +2 -19
  64. package/src/obligations/format.ts +1 -9
  65. package/src/obligations/generate.ts +1 -11
  66. package/src/obligations/index.ts +0 -11
  67. package/src/obligations/levels.ts +4 -8
  68. package/src/obligations/module.ts +1 -15
  69. package/src/obligations/normalize.ts +5 -24
  70. package/src/obligations/operators.ts +1 -2
  71. package/src/obligations/pragma.ts +1 -10
  72. package/src/obligations/printer.ts +47 -46
  73. package/src/obligations/proof.ts +1 -5
  74. package/src/obligations/regroup.ts +1 -4
  75. package/src/obligations/simplify.ts +3 -14
  76. package/src/obligations/source.ts +1 -21
  77. package/src/obligations/subexpression.ts +5 -11
  78. package/src/obligations/substitution.ts +1 -3
  79. package/src/obligations/temporal.ts +8 -14
  80. package/src/obligations/tuples.ts +3 -9
  81. package/src/parallel-worker.ts +29 -39
  82. package/src/parallel.ts +53 -44
  83. package/src/parser.ts +565 -282
  84. package/src/pcal/ast.ts +53 -86
  85. package/src/pcal/parse.ts +144 -219
  86. package/src/pcal/tokenize.ts +91 -131
  87. package/src/pcal/translate.ts +83 -130
  88. package/src/pcal/validate.ts +48 -68
  89. package/src/pretty-print.ts +7 -8
  90. package/src/prover/decide-pool.ts +5 -10
  91. package/src/prover/deciders.ts +0 -4
  92. package/src/prover/first-order.ts +0 -11
  93. package/src/prover/isabelle/decide.ts +0 -6
  94. package/src/prover/isabelle/encode.ts +11 -23
  95. package/src/prover/isabelle/isar/inner-syntax.ts +108 -114
  96. package/src/prover/isabelle/isar/method.ts +25 -38
  97. package/src/prover/isabelle/isar/proof.ts +1 -10
  98. package/src/prover/isabelle/isar/statement.ts +1 -6
  99. package/src/prover/isabelle/isar/theory.ts +15 -26
  100. package/src/prover/isabelle/isar/type-check.ts +11 -25
  101. package/src/prover/isabelle/provers/blast/blast.ts +10 -41
  102. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  103. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  104. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  105. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  106. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  107. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  108. package/src/prover/isabelle/provers/classical.ts +14 -42
  109. package/src/prover/isabelle/provers/hypsubst.ts +11 -25
  110. package/src/prover/isabelle/pure/bicompose.ts +16 -13
  111. package/src/prover/isabelle/pure/consts.ts +1 -7
  112. package/src/prover/isabelle/pure/conv.ts +1 -9
  113. package/src/prover/isabelle/pure/drule.ts +24 -40
  114. package/src/prover/isabelle/pure/envir.ts +3 -52
  115. package/src/prover/isabelle/pure/goal.ts +1 -7
  116. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  117. package/src/prover/isabelle/pure/list.ts +0 -3
  118. package/src/prover/isabelle/pure/logic.ts +1 -14
  119. package/src/prover/isabelle/pure/name.ts +2 -9
  120. package/src/prover/isabelle/pure/net.ts +3 -59
  121. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  122. package/src/prover/isabelle/pure/pattern.ts +2 -19
  123. package/src/prover/isabelle/pure/raw-simplifier.ts +13 -49
  124. package/src/prover/isabelle/pure/rewriter.ts +26 -58
  125. package/src/prover/isabelle/pure/search.ts +3 -10
  126. package/src/prover/isabelle/pure/seq.ts +1 -10
  127. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  128. package/src/prover/isabelle/pure/table.ts +1 -31
  129. package/src/prover/isabelle/pure/tactic.ts +12 -22
  130. package/src/prover/isabelle/pure/tactical.ts +6 -12
  131. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  132. package/src/prover/isabelle/pure/term.ts +2 -43
  133. package/src/prover/isabelle/pure/thm.ts +13 -36
  134. package/src/prover/isabelle/pure/type.ts +2 -15
  135. package/src/prover/isabelle/pure/unify.ts +20 -30
  136. package/src/prover/isabelle/pure/variable.ts +3 -7
  137. package/src/prover/isabelle/tla/classical-setup.ts +9 -19
  138. package/src/prover/isabelle/tla/context.ts +8 -16
  139. package/src/prover/isabelle/tla/logic.ts +4 -11
  140. package/src/prover/isabelle/tla/simpdata.ts +46 -34
  141. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  142. package/src/prover/ocaml.ts +0 -6
  143. package/src/prover/prep/action.ts +4 -36
  144. package/src/prover/prep/enabled-axioms.ts +12 -19
  145. package/src/prover/prep/fingerprint.ts +0 -16
  146. package/src/prover/prep/level-comparison.ts +8 -12
  147. package/src/prover/prep/md5.ts +0 -2
  148. package/src/prover/prep/methods.ts +9 -36
  149. package/src/prover/prep/normalize.ts +4 -28
  150. package/src/prover/prove-worker.ts +0 -2
  151. package/src/prover/prove.ts +0 -36
  152. package/src/prover/ptl/decide.ts +7 -12
  153. package/src/prover/ptl/encode.ts +14 -36
  154. package/src/prover/ptl/sat.ts +24 -32
  155. package/src/prover/ptl/tableau.ts +12 -26
  156. package/src/prover/smt/axioms.ts +0 -7
  157. package/src/prover/smt/context.ts +19 -26
  158. package/src/prover/smt/data.ts +51 -54
  159. package/src/prover/smt/encode.ts +14 -35
  160. package/src/prover/smt/extensionality.ts +0 -10
  161. package/src/prover/smt/flatten.ts +3 -22
  162. package/src/prover/smt/format.ts +5 -10
  163. package/src/prover/smt/instances.ts +0 -10
  164. package/src/prover/smt/rewrite.ts +66 -101
  165. package/src/prover/smt/solver/act-cache.ts +0 -15
  166. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  167. package/src/prover/smt/solver/array.ts +1 -3
  168. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  169. package/src/prover/smt/solver/ast-lt.ts +1 -11
  170. package/src/prover/smt/solver/ast-util.ts +1 -6
  171. package/src/prover/smt/solver/ast.ts +1 -143
  172. package/src/prover/smt/solver/auf-solver.ts +6 -39
  173. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  174. package/src/prover/smt/solver/cg-table.ts +1 -18
  175. package/src/prover/smt/solver/clause.ts +1 -27
  176. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  177. package/src/prover/smt/solver/context.ts +4 -61
  178. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  179. package/src/prover/smt/solver/der.ts +2 -21
  180. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  181. package/src/prover/smt/solver/e-matching.ts +3 -34
  182. package/src/prover/smt/solver/enode.ts +1 -21
  183. package/src/prover/smt/solver/expr-ref.ts +1 -5
  184. package/src/prover/smt/solver/expr-safe-replace.ts +0 -5
  185. package/src/prover/smt/solver/fingerprints.ts +1 -12
  186. package/src/prover/smt/solver/flat-literals.ts +1 -9
  187. package/src/prover/smt/solver/goal.ts +2 -14
  188. package/src/prover/smt/solver/hash.ts +0 -8
  189. package/src/prover/smt/solver/hashtable.ts +1 -12
  190. package/src/prover/smt/solver/internalizer.ts +6 -98
  191. package/src/prover/smt/solver/lbool.ts +1 -2
  192. package/src/prover/smt/solver/literal.ts +0 -3
  193. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  194. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  195. package/src/prover/smt/solver/lp/containers.ts +12 -30
  196. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  197. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  198. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  199. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  200. package/src/prover/smt/solver/lp/dioph-eq.ts +12 -98
  201. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  202. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  203. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  204. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  205. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  206. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  207. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  208. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  209. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  210. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  211. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  212. package/src/prover/smt/solver/lp/nex.ts +1 -6
  213. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  214. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  215. package/src/prover/smt/solver/lp/nla-core.ts +35 -130
  216. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  217. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  218. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  220. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  221. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  222. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  223. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  224. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  225. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  226. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  227. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  228. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  229. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  230. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  231. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  232. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  233. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  234. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  235. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  236. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  237. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  238. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  239. package/src/prover/smt/solver/mam-compiler.ts +14 -56
  240. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  241. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  242. package/src/prover/smt/solver/model-checker.ts +2 -36
  243. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  244. package/src/prover/smt/solver/model-finder.ts +3 -15
  245. package/src/prover/smt/solver/model-generator.ts +1 -12
  246. package/src/prover/smt/solver/model-macro-solver.ts +12 -69
  247. package/src/prover/smt/solver/nlsat/algebraic.ts +55 -151
  248. package/src/prover/smt/solver/nlsat/atoms.ts +5 -19
  249. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  250. package/src/prover/smt/solver/nlsat/explain.ts +3 -47
  251. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  252. package/src/prover/smt/solver/nlsat/levelwise.ts +48 -129
  253. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  254. package/src/prover/smt/solver/nlsat/polynomial.ts +17 -156
  255. package/src/prover/smt/solver/nlsat/solver.ts +7 -67
  256. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  257. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -44
  258. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  259. package/src/prover/smt/solver/nnf.ts +3 -38
  260. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  261. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  262. package/src/prover/smt/solver/propagate-values.ts +1 -10
  263. package/src/prover/smt/solver/proto-model.ts +3 -48
  264. package/src/prover/smt/solver/pull-quant.ts +1 -12
  265. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  266. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  267. package/src/prover/smt/solver/qe-lite.ts +3 -33
  268. package/src/prover/smt/solver/qi-queue.ts +1 -16
  269. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  270. package/src/prover/smt/solver/quantifier-analyzer.ts +17 -37
  271. package/src/prover/smt/solver/quantifier-manager.ts +1 -8
  272. package/src/prover/smt/solver/random-gen.ts +1 -2
  273. package/src/prover/smt/solver/rational.ts +1 -8
  274. package/src/prover/smt/solver/ref-vector.ts +1 -6
  275. package/src/prover/smt/solver/relevancy.ts +2 -15
  276. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  277. package/src/prover/smt/solver/rewriter.ts +3 -85
  278. package/src/prover/smt/solver/rlimit.ts +2 -20
  279. package/src/prover/smt/solver/smtlib-parser.ts +2 -37
  280. package/src/prover/smt/solver/solver.ts +23 -34
  281. package/src/prover/smt/solver/sort.ts +1 -5
  282. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  283. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  284. package/src/prover/smt/solver/theory-lra-bounds.ts +0 -5
  285. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  286. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -27
  287. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  288. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  289. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  290. package/src/prover/smt/solver/theory-lra.ts +1 -8
  291. package/src/prover/smt/solver/var-subst.ts +2 -26
  292. package/src/prover/smt/table.ts +0 -8
  293. package/src/prover/tlapm/cache.ts +20 -25
  294. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  295. package/src/prover/tlapm/tlapm.ts +12 -17
  296. package/src/prover/verdict.ts +0 -2
  297. package/src/prover/zenon/check/check.ts +0 -9
  298. package/src/prover/zenon/check/definitions.ts +5 -15
  299. package/src/prover/zenon/check/equality.ts +0 -8
  300. package/src/prover/zenon/check/functions.ts +0 -4
  301. package/src/prover/zenon/check/keys.ts +0 -7
  302. package/src/prover/zenon/check/propositional.ts +0 -3
  303. package/src/prover/zenon/check/records.ts +0 -5
  304. package/src/prover/zenon/check/relations.ts +0 -13
  305. package/src/prover/zenon/check/rewrites.ts +0 -6
  306. package/src/prover/zenon/check/sets.ts +0 -3
  307. package/src/prover/zenon/check/terms.ts +0 -17
  308. package/src/prover/zenon/check/tla.ts +0 -6
  309. package/src/prover/zenon/decide.ts +0 -12
  310. package/src/prover/zenon/encode.ts +1 -8
  311. package/src/prover/zenon/eqrel.ts +0 -13
  312. package/src/prover/zenon/equality-rules.ts +0 -4
  313. package/src/prover/zenon/expr.ts +1 -26
  314. package/src/prover/zenon/index-table.ts +17 -38
  315. package/src/prover/zenon/isar-case.ts +9 -19
  316. package/src/prover/zenon/isar.ts +1 -29
  317. package/src/prover/zenon/limits.ts +0 -4
  318. package/src/prover/zenon/llproof.ts +0 -9
  319. package/src/prover/zenon/mltoll.ts +54 -76
  320. package/src/prover/zenon/node.ts +1 -11
  321. package/src/prover/zenon/ocaml.ts +1 -17
  322. package/src/prover/zenon/parse.ts +2 -7
  323. package/src/prover/zenon/phrase.ts +1 -11
  324. package/src/prover/zenon/proof.ts +0 -9
  325. package/src/prover/zenon/propositional-rules.ts +0 -5
  326. package/src/prover/zenon/rules.ts +2 -6
  327. package/src/prover/zenon/search.ts +1 -17
  328. package/src/prover/zenon/tla/functions.ts +57 -29
  329. package/src/prover/zenon/tla/llproof.ts +63 -70
  330. package/src/prover/zenon/tla/records.ts +56 -27
  331. package/src/prover/zenon/tla/rewrites.ts +12 -19
  332. package/src/prover/zenon/tla/sets.ts +68 -39
  333. package/src/prover/zenon/tla/terms.ts +0 -6
  334. package/src/prover/zenon/tla/tla.ts +59 -58
  335. package/src/randomization.ts +81 -35
  336. package/src/resolve.ts +12 -112
  337. package/src/runtime.ts +22 -22
  338. package/src/sany/builtins.ts +2 -5
  339. package/src/sany/generator.ts +149 -252
  340. package/src/sany/levels.ts +6 -34
  341. package/src/sany/lexer.ts +41 -61
  342. package/src/sany/linter.ts +1 -9
  343. package/src/sany/modules.ts +1 -5
  344. package/src/sany/nodes.ts +0 -31
  345. package/src/sany/parse-error.ts +39 -117
  346. package/src/sany/report.ts +29 -63
  347. package/src/sany/selectors.ts +15 -39
  348. package/src/sany/source.ts +1 -17
  349. package/src/sany/xml.ts +13 -63
  350. package/src/search.ts +495 -155
  351. package/src/simulate.ts +685 -0
  352. package/src/spec-description.ts +176 -69
  353. package/src/standard-definitions.ts +0 -4
  354. package/src/standard-modules.ts +254 -69
  355. package/src/state-graph.ts +50 -24
  356. package/src/subexpressions.ts +69 -52
  357. package/src/substitution-levels.ts +2 -3
  358. package/src/successors.ts +263 -94
  359. package/src/symmetry.ts +33 -25
  360. package/src/tableau.ts +27 -24
  361. package/src/tlc/fingerprint.ts +183 -67
  362. package/src/tlc/jar-data.ts +112 -6
  363. package/src/tlc/messages.ts +110 -44
  364. package/src/tlc/order.ts +37 -60
  365. package/src/tlc/random.ts +33 -45
  366. package/src/tlc/replay.ts +549 -217
  367. package/src/tlc/summary.ts +36 -47
  368. package/src/tlc/tlc-operators.ts +76 -41
  369. package/src/tlc/tlcext-operators.ts +139 -122
  370. package/src/tlc/trace-spec.ts +269 -0
  371. package/src/tlc/value-stream.ts +139 -0
  372. package/src/tlc/version.ts +1 -4
  373. package/src/transfer.ts +22 -25
  374. package/src/typescript/actions.ts +481 -0
  375. package/src/typescript/collections.ts +600 -0
  376. package/src/typescript/compile.ts +65 -0
  377. package/src/typescript/context.ts +94 -0
  378. package/src/typescript/emit.ts +121 -0
  379. package/src/typescript/expressions.ts +496 -0
  380. package/src/typescript/formula.ts +152 -0
  381. package/src/typescript/functions.ts +284 -0
  382. package/src/typescript/model.ts +163 -0
  383. package/src/typescript/names.ts +114 -0
  384. package/src/typescript/program.ts +48 -0
  385. package/src/typescript/properties.ts +139 -0
  386. package/src/typescript/scope.ts +112 -0
  387. package/src/typescript/steps.ts +165 -0
  388. package/src/typescript/types.ts +171 -0
  389. package/src/values.ts +780 -337
  390. package/src/worker.ts +7 -8
package/src/pcal/parse.ts CHANGED
@@ -44,16 +44,12 @@ import {
44
44
  type PcalTokenizerState,
45
45
  } from "./tokenize";
46
46
 
47
- // The translator's state that its Java code keeps in static fields, which SANY never resets
48
- // between the modules it validates: the options, the tokenizer's state, ParseAlgorithm's
49
- // lookahead buffer and last token, and whether a macro's parsing was cut short by an error.
50
47
  export interface PcalSession {
51
48
  params: PcalParams;
52
49
  tokenizer: PcalTokenizerState;
53
50
  lookahead: { tokens: string[]; columns: number[]; lines: number[] };
54
51
  lastToken: { column: number; line: number; text: string };
55
52
  isInMacro: boolean;
56
- // ToolIO.out.println, which PcalDebug's reports go to.
57
53
  log: (line: string) => void;
58
54
  }
59
55
 
@@ -73,53 +69,27 @@ export const createPcalSession = (
73
69
  log,
74
70
  });
75
71
 
76
- // ParseAlgorithm's static fields that Init resets for each algorithm.
77
- interface PcalParserState {
78
- lookaheadSize: number;
79
- addedLabels: string[];
80
- addedLabelsLocs: string[];
81
- nextLabelNum: number;
82
- allLabels: Set<string>;
83
- hasLabel: boolean;
84
- hasDefaultInitialization: boolean;
85
- currentProcedure: string | undefined;
86
- procedures: PcalProcedure[];
87
- cSyntax: boolean;
88
- plusLabels: string[];
89
- minusLabels: string[];
90
- proceduresCalled: string[];
91
- gotoUsed: boolean;
92
- gotoDoneUsed: boolean;
93
- omitPC: boolean;
94
- }
95
-
96
72
  interface PcalParseResult {
97
73
  ast: PcalAlgorithm;
98
- omitPC: boolean;
74
+ shouldOmitPC: boolean;
99
75
  hasDefaultInitialization: boolean;
100
76
  }
101
77
 
102
- // PcalParams.DefaultVarInit: the value of a variable declared without one.
103
78
  const createDefaultVarInit = () =>
104
79
  createNormalizedPcalExpr([[createPcalToken("defaultInitValue", 0, 0)]]);
105
80
 
106
- // ParseAlgorithm.Union.
107
81
  const getUnion = (first: string[], second: string[]) => [
108
82
  ...first,
109
83
  ...second.filter((name, index) => !first.includes(name) && second.indexOf(name) === index),
110
84
  ];
111
85
 
112
- // ParseAlgorithm.HasEmptyIntersection.
113
86
  const isDisjoint = (first: string[], second: string[]) =>
114
87
  !second.some((name) => first.includes(name));
115
88
 
116
- // ParseAlgorithm.Copy: replaces the contents of target, which callers share.
117
89
  const copyInto = (source: string[], target: string[]) => {
118
90
  target.splice(0, target.length, ...source);
119
91
  };
120
92
 
121
- // ParseAlgorithm.getAlgorithm: parses the algorithm the reader is at, just after "--algorithm" or
122
- // "--fair", adds the labels it needs, and expands its macros.
123
93
  export const parsePcalAlgorithm = (
124
94
  session: PcalSession,
125
95
  reader: PcalCharReader,
@@ -128,33 +98,29 @@ export const parsePcalAlgorithm = (
128
98
  const params = session.params;
129
99
  const lookahead = session.lookahead;
130
100
  const lastToken = session.lastToken;
131
- // ParseAlgorithm.Init.
132
- const state: PcalParserState = {
133
- lookaheadSize: 0,
134
- addedLabels: [],
135
- addedLabelsLocs: [],
136
- nextLabelNum: 1,
137
- allLabels: new Set(),
138
- hasLabel: false,
139
- hasDefaultInitialization: false,
140
- currentProcedure: undefined,
141
- procedures: [],
142
- cSyntax: false,
143
- plusLabels: [],
144
- minusLabels: [],
145
- proceduresCalled: [],
146
- gotoUsed: false,
147
- gotoDoneUsed: false,
148
- omitPC: params.inputVersionNumber >= 1500,
149
- };
101
+ let lookaheadSize = 0;
102
+ const addedLabels: string[] = [];
103
+ const addedLabelsLocs: string[] = [];
104
+ let nextLabelNum = 1;
105
+ const allLabels = new Set<string>();
106
+ let hasLabel = false;
107
+ let hasDefaultInitialization = false;
108
+ let currentProcedure: string | undefined;
109
+ const procedures: PcalProcedure[] = [];
110
+ let isCSyntax = false;
111
+ let plusLabels: string[] = [];
112
+ let minusLabels: string[] = [];
113
+ let proceduresCalled: string[] = [];
114
+ let isGotoUsed = false;
115
+ let isGotoDoneUsed = false;
116
+ let shouldOmitPC = params.inputVersionNumber >= 1500;
150
117
 
151
- // ParseAlgorithm.ParsingError.
152
118
  const throwParsingError = (message: string): never => {
153
119
  throw new PcalError(`${message}\n line ${lastToken.line}, column ${lastToken.column}`);
154
120
  };
155
121
 
156
122
  const getAlgToken = () => {
157
- if (state.lookaheadSize === 0) {
123
+ if (lookaheadSize === 0) {
158
124
  peekPcalReader(reader);
159
125
  lastToken.column = reader.vcolumn + 1;
160
126
  lastToken.line = reader.line + 1;
@@ -164,19 +130,19 @@ export const parsePcalAlgorithm = (
164
130
  lastToken.column = lookahead.columns[0] + 1;
165
131
  lastToken.line = lookahead.lines[0] + 1;
166
132
  const result = lookahead.tokens[0];
167
- for (let index = 1; index < state.lookaheadSize; index++) {
133
+ for (let index = 1; index < lookaheadSize; index++) {
168
134
  lookahead.tokens[index - 1] = lookahead.tokens[index];
169
135
  lookahead.columns[index - 1] = lookahead.columns[index];
170
136
  lookahead.lines[index - 1] = lookahead.lines[index];
171
137
  }
172
- state.lookaheadSize -= 1;
138
+ lookaheadSize -= 1;
173
139
  lastToken.text = result;
174
140
  return result;
175
141
  };
176
142
 
177
143
  const peekAtAlgToken = (tokenNumber: number) => {
178
- while (state.lookaheadSize < tokenNumber) {
179
- const size = state.lookaheadSize;
144
+ while (lookaheadSize < tokenNumber) {
145
+ const size = lookaheadSize;
180
146
  if (peekPcalReader(reader) === "\t") {
181
147
  throw new PcalError(
182
148
  `Premature end of file, perhaps because of unclosed comment, near\n line ${lookahead.lines[size] + 1}, column ${lookahead.columns[size] + 1}`,
@@ -185,39 +151,43 @@ export const parsePcalAlgorithm = (
185
151
  lookahead.columns[size] = reader.vcolumn;
186
152
  lookahead.lines[size] = reader.line;
187
153
  lookahead.tokens[size] = getPcalAlgorithmToken(session.tokenizer, reader);
188
- state.lookaheadSize += 1;
154
+ lookaheadSize += 1;
189
155
  }
190
156
  return lookahead.tokens[tokenNumber - 1];
191
157
  };
192
158
 
193
- // ParseAlgorithm.PeekPastAlgToken: the rest of the line after the next tokenNumber tokens.
194
159
  const peekPastAlgToken = (tokenNumber: number) => {
195
- if (tokenNumber < state.lookaheadSize) {
160
+ if (tokenNumber < lookaheadSize) {
196
161
  throw new PcalBugError(
197
162
  "ParseAlgorithm.PeekPastAlgToken called to peek after a token\n it has already peeked at",
198
163
  );
199
164
  }
200
- while (tokenNumber > state.lookaheadSize) {
201
- lookahead.tokens[state.lookaheadSize] = getPcalAlgorithmToken(session.tokenizer, reader);
202
- state.lookaheadSize += 1;
165
+ while (tokenNumber > lookaheadSize) {
166
+ lookahead.tokens[lookaheadSize] = getPcalAlgorithmToken(session.tokenizer, reader);
167
+ lookaheadSize += 1;
203
168
  }
204
169
  return peekPcalReader(reader);
205
170
  };
206
171
 
207
- // ParseAlgorithm.getExprInternal: an expression's delimiter becomes the next token.
208
172
  const getExpr = (isDefineBlock = false) => {
209
- if (state.lookaheadSize !== 0) {
173
+ if (lookaheadSize !== 0) {
210
174
  throw new PcalBugError("ParseAlgorithm: GetExpr called after lookahead");
211
175
  }
212
176
  const result = tokenizePcalExpr(session.tokenizer, reader, isDefineBlock);
213
177
  lookahead.tokens[0] = session.tokenizer.delimiter;
214
178
  lookahead.columns[0] = session.tokenizer.delimiterCol;
215
179
  lookahead.lines[0] = session.tokenizer.delimiterLine;
216
- state.lookaheadSize = 1;
180
+ lookaheadSize = 1;
217
181
  return result;
218
182
  };
219
183
 
220
- // ParseAlgorithm.GobbleThis: a missing ";" is fine before a token that can't start a statement.
184
+ const mustGobbleThis = (expected: string, alternate?: string) => {
185
+ const token = getAlgToken();
186
+ if (token !== expected && token !== alternate) {
187
+ throwParsingError(`Expected "${expected}" but found "${token}"`);
188
+ }
189
+ };
190
+
221
191
  const gobbleThis = (expected: string, alternate?: string) => {
222
192
  if (expected === ";") {
223
193
  const next = peekAtAlgToken(1);
@@ -230,15 +200,7 @@ export const parsePcalAlgorithm = (
230
200
  );
231
201
  }
232
202
  }
233
- const token = getAlgToken();
234
- if (token !== expected && token !== alternate) {
235
- throwParsingError(`Expected "${expected}" but found "${token}"`);
236
- }
237
- };
238
-
239
- const mustGobbleThis = (expected: string) => {
240
- const token = getAlgToken();
241
- if (token !== expected) throwParsingError(`Expected "${expected}" but found "${token}"`);
203
+ mustGobbleThis(expected, alternate);
242
204
  };
243
205
 
244
206
  const gobbleEqualOrIf = () => {
@@ -250,10 +212,10 @@ export const parsePcalAlgorithm = (
250
212
 
251
213
  const gobbleCommaOrSemicolon = () => gobbleThis(peekAtAlgToken(1) === "," ? "," : ";");
252
214
 
253
- const gobbleBeginOrLeftBrace = () => gobbleThis(state.cSyntax ? "{" : "begin");
215
+ const gobbleBeginOrLeftBrace = () => gobbleThis(isCSyntax ? "{" : "begin");
254
216
 
255
217
  const gobbleEndOrRightBrace = (name: string) => {
256
- if (state.cSyntax) {
218
+ if (isCSyntax) {
257
219
  gobbleThis("}");
258
220
  return;
259
221
  }
@@ -277,13 +239,11 @@ export const parsePcalAlgorithm = (
277
239
  val: createDefaultVarInit(),
278
240
  };
279
241
  if (!["=", "\\in", "∈"].includes(peekAtAlgToken(1))) {
280
- state.hasDefaultInitialization = true;
242
+ hasDefaultInitialization = true;
281
243
  return decl;
282
244
  }
283
245
  decl.isEq = gobbleEqualOrIf();
284
246
  decl.val = getExpr();
285
- // ParseAlgorithm.GetVarDecl takes the end of the value's origin, which an empty value lacks,
286
- // before checking for one.
287
247
  if (decl.val.tokens.length === 0) throw new PcalRuntimeError("NullPointerException");
288
248
  return decl;
289
249
  };
@@ -309,7 +269,7 @@ export const parsePcalAlgorithm = (
309
269
  val: createDefaultVarInit(),
310
270
  };
311
271
  if (peekAtAlgToken(1) !== "=") {
312
- state.hasDefaultInitialization = true;
272
+ hasDefaultInitialization = true;
313
273
  return decl;
314
274
  }
315
275
  gobbleThis("=");
@@ -328,7 +288,6 @@ export const parsePcalAlgorithm = (
328
288
  return result;
329
289
  };
330
290
 
331
- // ParseAlgorithm.IsLabelNext: an identifier followed by ":" but not ":=".
332
291
  const isLabelNext = () => {
333
292
  const keywords =
334
293
  "while if assert print else elsif either or end with when await skip call return goto";
@@ -344,23 +303,23 @@ export const parsePcalAlgorithm = (
344
303
  if (label === "Done") throwParsingError("Cannot use `Done' as a label.");
345
304
  if (label === "Error") throwParsingError("Cannot use `Error' as a label.");
346
305
  gobbleThis(":");
347
- state.hasLabel = true;
348
- state.allLabels.add(label);
306
+ hasLabel = true;
307
+ allLabels.add(label);
349
308
  const modifier = peekAtAlgToken(1);
350
309
  if (modifier !== "+" && modifier !== "-") return label;
351
310
  gobbleThis(modifier);
352
- (modifier === "+" ? state.plusLabels : state.minusLabels).push(label);
353
- state.omitPC = false;
311
+ (modifier === "+" ? plusLabels : minusLabels).push(label);
312
+ shouldOmitPC = false;
354
313
  return label;
355
314
  };
356
315
 
357
316
  const getStmtSeq = (): PcalStmt[] => {
358
317
  const result: PcalStmt[] = [];
359
318
  const isEnd = (token: string) =>
360
- (!state.cSyntax && ["end", "else", "elsif", "or"].includes(token)) ||
361
- (state.cSyntax && token === "}");
319
+ (!isCSyntax && ["end", "else", "elsif", "or"].includes(token)) ||
320
+ (isCSyntax && token === "}");
362
321
  while (!isEnd(peekAtAlgToken(1))) result.push(...getCStmt());
363
- if (state.cSyntax && result.length === 0) throwParsingError("Empty statement list");
322
+ if (isCSyntax && result.length === 0) throwParsingError("Empty statement list");
364
323
  return result;
365
324
  };
366
325
 
@@ -378,7 +337,7 @@ export const parsePcalAlgorithm = (
378
337
 
379
338
  const getCStmt = () => {
380
339
  const label = getLabel();
381
- if (state.cSyntax && peekAtAlgToken(1) === "{") return getCStmtSeq(label);
340
+ if (isCSyntax && peekAtAlgToken(1) === "{") return getCStmtSeq(label);
382
341
  const stmt = getStmt();
383
342
  stmt.lbl = label;
384
343
  return [stmt];
@@ -394,8 +353,6 @@ export const parsePcalAlgorithm = (
394
353
  const col = lastToken.column;
395
354
  const line = lastToken.line;
396
355
  const exp = getExpr();
397
- // ParseAlgorithm.GetWhen takes the end of the expression's origin, which an empty
398
- // expression lacks, before checking for one.
399
356
  if (exp.tokens.length === 0) throw new PcalRuntimeError("NullPointerException");
400
357
  gobbleThis(";");
401
358
  return { ...createPcalBase(), kind: "When", col, line, exp };
@@ -434,7 +391,7 @@ export const parsePcalAlgorithm = (
434
391
  kind: "Return",
435
392
  col: lastToken.column,
436
393
  line: lastToken.line,
437
- from: state.currentProcedure,
394
+ from: currentProcedure,
438
395
  };
439
396
  gobbleThis(";");
440
397
  return result;
@@ -443,11 +400,11 @@ export const parsePcalAlgorithm = (
443
400
  mustGobbleThis("goto");
444
401
  const col = lastToken.column;
445
402
  const line = lastToken.line;
446
- const to = getAlgToken();
447
- state.gotoUsed = true;
448
- if (to === "Done" || to === '"Done"') state.gotoDoneUsed = true;
403
+ const target = getAlgToken();
404
+ isGotoUsed = true;
405
+ if (target === "Done" || target === '"Done"') isGotoDoneUsed = true;
449
406
  gobbleThis(";");
450
- return { ...createPcalBase(), kind: "Goto", col, line, to };
407
+ return { ...createPcalBase(), kind: "Goto", col, line, to: target };
451
408
  }
452
409
  if (next === "while") return getWhile();
453
410
  if (peekPastAlgToken(1)[0] === "(") return getMacroCall();
@@ -458,13 +415,13 @@ export const parsePcalAlgorithm = (
458
415
  mustGobbleThis("while");
459
416
  const col = lastToken.column;
460
417
  const line = lastToken.line;
461
- if (state.cSyntax) gobbleThis("(");
418
+ if (isCSyntax) gobbleThis("(");
462
419
  const test = getExpr();
463
- if (state.cSyntax) gobbleThis(")");
420
+ if (isCSyntax) gobbleThis(")");
464
421
  if (test.tokens.length === 0) throwParsingError("Empty while test at ");
465
- if (!state.cSyntax) gobbleThis("do");
466
- const unlabDo = state.cSyntax ? getCStmt() : getStmtSeq();
467
- if (!state.cSyntax) {
422
+ if (!isCSyntax) gobbleThis("do");
423
+ const unlabDo = isCSyntax ? getCStmt() : getStmtSeq();
424
+ if (!isCSyntax) {
468
425
  gobbleThis("end");
469
426
  gobbleThis("while");
470
427
  gobbleThis(";");
@@ -486,28 +443,27 @@ export const parsePcalAlgorithm = (
486
443
  unlabElse: [],
487
444
  labElse: [],
488
445
  };
489
- if (state.cSyntax) gobbleThis("(");
446
+ if (isCSyntax) gobbleThis("(");
490
447
  result.test = getExpr();
491
- if (state.cSyntax) gobbleThis(")");
448
+ if (isCSyntax) gobbleThis(")");
492
449
  if (result.test.tokens.length === 0) throwParsingError("Empty if test at ");
493
- if (!state.cSyntax) gobbleThis("then");
494
- result.unlabThen = state.cSyntax ? getCStmt() : getStmtSeq();
450
+ if (!isCSyntax) gobbleThis("then");
451
+ result.unlabThen = isCSyntax ? getCStmt() : getStmtSeq();
495
452
  const next = peekAtAlgToken(1);
496
453
  if (next === "else") {
497
454
  mustGobbleThis("else");
498
- result.unlabElse = state.cSyntax ? getCStmt() : getStmtSeq();
455
+ result.unlabElse = isCSyntax ? getCStmt() : getStmtSeq();
499
456
  }
500
- if (!state.cSyntax && next === "elsif") result.unlabElse = [getIf(depth + 1)];
501
- if (!state.cSyntax && !["else", "elsif", "end"].includes(next)) {
457
+ if (!isCSyntax && next === "elsif") result.unlabElse = [getIf(depth + 1)];
458
+ if (!isCSyntax && !["else", "elsif", "end"].includes(next)) {
502
459
  throwParsingError('Expecting "else", "elsif", or "end"');
503
460
  }
504
- if (!state.cSyntax && depth === 0) {
461
+ if (!isCSyntax && depth === 0) {
505
462
  gobbleThis("end");
506
463
  gobbleThis("if");
507
464
  gobbleThis(";");
508
465
  }
509
- // ParseAlgorithm.GetIf takes the origin of the last statement, which an empty then clause
510
- // with no else clause doesn't have.
466
+ // ParseAlgorithm.GetIf reads the last then statement, which throws when there is none.
511
467
  if (result.unlabElse.length === 0) {
512
468
  getElement(result.unlabThen, result.unlabThen.length - 1);
513
469
  }
@@ -527,7 +483,7 @@ export const parsePcalAlgorithm = (
527
483
  const clause: PcalClause = {
528
484
  ...createPcalBase(),
529
485
  kind: "Clause",
530
- unlabOr: state.cSyntax ? getCStmt() : getStmtSeq(),
486
+ unlabOr: isCSyntax ? getCStmt() : getStmtSeq(),
531
487
  labOr: [],
532
488
  };
533
489
  if (clause.unlabOr.length === 0) {
@@ -537,7 +493,7 @@ export const parsePcalAlgorithm = (
537
493
  if (peekAtAlgToken(1) !== "or") break;
538
494
  mustGobbleThis("or");
539
495
  }
540
- if (!state.cSyntax) {
496
+ if (!isCSyntax) {
541
497
  mustGobbleThis("end");
542
498
  gobbleThis("either");
543
499
  gobbleThis(";");
@@ -546,11 +502,10 @@ export const parsePcalAlgorithm = (
546
502
  return result;
547
503
  };
548
504
 
549
- // ParseAlgorithm.InnerGetWith: "with x = e, y \in f" nests a with for each variable.
550
505
  const getWith = (depth: number): PcalWith => {
551
506
  if (depth === 0) {
552
507
  gobbleThis("with");
553
- if (state.cSyntax) gobbleThis("(");
508
+ if (isCSyntax) gobbleThis("(");
554
509
  }
555
510
  const result: PcalWith = {
556
511
  ...createPcalBase(),
@@ -564,10 +519,10 @@ export const parsePcalAlgorithm = (
564
519
  };
565
520
  result.isEq = gobbleEqualOrIf();
566
521
  result.exp = getExpr();
567
- if (!state.cSyntax || peekAtAlgToken(1) !== ")") gobbleCommaOrSemicolon();
522
+ if (!isCSyntax || peekAtAlgToken(1) !== ")") gobbleCommaOrSemicolon();
568
523
  if (result.exp.tokens.length === 0) throwParsingError("Empty with expression at ");
569
- const isBlockBody = !state.cSyntax && peekAtAlgToken(1) === "do";
570
- const isBraceBody = state.cSyntax && peekAtAlgToken(1) === ")";
524
+ const isBlockBody = !isCSyntax && peekAtAlgToken(1) === "do";
525
+ const isBraceBody = isCSyntax && peekAtAlgToken(1) === ")";
571
526
  if (isBlockBody) gobbleThis("do");
572
527
  if (isBraceBody) mustGobbleThis(")");
573
528
  result.Do = isBlockBody ? getStmtSeq() : isBraceBody ? getCStmt() : [getWith(depth + 1)];
@@ -620,7 +575,6 @@ export const parsePcalAlgorithm = (
620
575
  return result;
621
576
  };
622
577
 
623
- // The arguments of a call or macro call, through its ")" and ";".
624
578
  const getCallArgs = () => {
625
579
  const args: PcalExpr[] = [];
626
580
  if (peekPastAlgToken(0)[0] !== ")") {
@@ -650,7 +604,7 @@ export const parsePcalAlgorithm = (
650
604
  };
651
605
  gobbleThis("(");
652
606
  result.args = getCallArgs();
653
- if (!state.proceduresCalled.includes(result.to)) state.proceduresCalled.push(result.to);
607
+ if (!proceduresCalled.includes(result.to)) proceduresCalled.push(result.to);
654
608
  return result;
655
609
  };
656
610
 
@@ -665,7 +619,7 @@ export const parsePcalAlgorithm = (
665
619
  col: call.col,
666
620
  line: call.line,
667
621
  to: call.to,
668
- from: state.currentProcedure,
622
+ from: currentProcedure,
669
623
  args: call.args,
670
624
  };
671
625
  }
@@ -680,8 +634,8 @@ export const parsePcalAlgorithm = (
680
634
  after: getAlgToken(),
681
635
  args: call.args,
682
636
  };
683
- state.gotoUsed = true;
684
- if (result.to === "Done" || result.to === '"Done"') state.gotoDoneUsed = true;
637
+ isGotoUsed = true;
638
+ if (result.to === "Done" || result.to === '"Done"') isGotoDoneUsed = true;
685
639
  gobbleThis(";");
686
640
  return result;
687
641
  };
@@ -742,10 +696,10 @@ export const parsePcalAlgorithm = (
742
696
  decls: [],
743
697
  body: [],
744
698
  };
745
- state.currentProcedure = result.name;
746
- state.plusLabels = [];
747
- state.minusLabels = [];
748
- state.proceduresCalled = [];
699
+ currentProcedure = result.name;
700
+ plusLabels = [];
701
+ minusLabels = [];
702
+ proceduresCalled = [];
749
703
  gobbleThis("(");
750
704
  while (peekAtAlgToken(1) !== ")") {
751
705
  if (result.params.length > 0) gobbleThis(",");
@@ -759,10 +713,10 @@ export const parsePcalAlgorithm = (
759
713
  result.body = getStmtSeq();
760
714
  gobbleEndOrRightBrace("procedure");
761
715
  if (peekAtAlgToken(1) === ";") getAlgToken();
762
- state.currentProcedure = undefined;
763
- result.plusLabels = state.plusLabels;
764
- result.minusLabels = state.minusLabels;
765
- result.proceduresCalled = state.proceduresCalled;
716
+ currentProcedure = undefined;
717
+ result.plusLabels = plusLabels;
718
+ result.minusLabels = minusLabels;
719
+ result.proceduresCalled = proceduresCalled;
766
720
  return result;
767
721
  };
768
722
 
@@ -770,14 +724,14 @@ export const parsePcalAlgorithm = (
770
724
  gobbleThis("process");
771
725
  const col = lastToken.column;
772
726
  const line = lastToken.line;
773
- if (state.cSyntax) gobbleThis("(");
727
+ if (isCSyntax) gobbleThis("(");
774
728
  const name = getAlgToken();
775
729
  const isEq = gobbleEqualOrIf();
776
730
  const id = getExpr();
777
- state.plusLabels = [];
778
- state.minusLabels = [];
779
- state.proceduresCalled = [];
780
- if (state.cSyntax) gobbleThis(")");
731
+ plusLabels = [];
732
+ minusLabels = [];
733
+ proceduresCalled = [];
734
+ if (isCSyntax) gobbleThis(")");
781
735
  if (id.tokens.length === 0) throwParsingError("Empty process id at ");
782
736
  const decls = peekAtAlgToken(1) === "begin" || peekAtAlgToken(1) === "{" ? [] : getVarDecls();
783
737
  gobbleBeginOrLeftBrace();
@@ -791,9 +745,9 @@ export const parsePcalAlgorithm = (
791
745
  line,
792
746
  name,
793
747
  fairness: 0,
794
- minusLabels: state.minusLabels,
795
- plusLabels: state.plusLabels,
796
- proceduresCalled: state.proceduresCalled,
748
+ minusLabels,
749
+ plusLabels,
750
+ proceduresCalled,
797
751
  isEq,
798
752
  id,
799
753
  decls,
@@ -801,23 +755,19 @@ export const parsePcalAlgorithm = (
801
755
  };
802
756
  };
803
757
 
804
- // ParseAlgorithm.NeedsLabel: the label root and the next number no label of the algorithm has.
805
758
  const getUnusedLabel = (): string => {
806
- const label = `${params.labelRoot}${state.nextLabelNum}`;
807
- state.nextLabelNum += 1;
808
- return state.allLabels.has(label) ? getUnusedLabel() : label;
759
+ const label = `${params.labelRoot}${nextLabelNum}`;
760
+ nextLabelNum += 1;
761
+ return allLabels.has(label) ? getUnusedLabel() : label;
809
762
  };
810
763
 
811
- const needsLabel = (stmt: PcalStmt) => {
764
+ const ensureLabel = (stmt: PcalStmt) => {
812
765
  if (stmt.lbl !== "") return;
813
766
  stmt.lbl = getUnusedLabel();
814
- state.addedLabels.push(stmt.lbl);
815
- state.addedLabelsLocs.push(getPcalLocation(stmt));
767
+ addedLabels.push(stmt.lbl);
768
+ addedLabelsLocs.push(getPcalLocation(stmt));
816
769
  };
817
770
 
818
- // ParseAlgorithm.InnerAddLabels: labels the statements that must start a step, tracking the
819
- // variables assigned since the last label in outAssigned. True when the statement after the
820
- // sequence needs a label.
821
771
  const addLabels = (
822
772
  stmts: PcalStmt[],
823
773
  isFirstLabeled: boolean,
@@ -826,19 +776,20 @@ export const parsePcalAlgorithm = (
826
776
  outAssigned: string[],
827
777
  ): boolean => {
828
778
  copyInto(inAssigned, outAssigned);
829
- const progress = { isNextLabeled: isFirstLabeled, hasLabel: false };
779
+ let isNextLabeled = isFirstLabeled;
780
+ let hasStepLabel = false;
830
781
  const startStep = (stmt: PcalStmt) => {
831
- needsLabel(stmt);
832
- progress.hasLabel = true;
782
+ ensureLabel(stmt);
783
+ hasStepLabel = true;
833
784
  outAssigned.length = 0;
834
785
  };
835
786
  stmts.forEach((stmt) => {
836
787
  if (stmt.lbl !== "") {
837
- progress.hasLabel = true;
788
+ hasStepLabel = true;
838
789
  outAssigned.length = 0;
839
790
  if (isInWith) throw new PcalError("Label in `with' statement", stmt);
840
791
  }
841
- if (progress.isNextLabeled) {
792
+ if (isNextLabeled) {
842
793
  if (isInWith) {
843
794
  throw new PcalError(
844
795
  "Statement follows `call' or `return' inside a `with' statement.",
@@ -846,7 +797,7 @@ export const parsePcalAlgorithm = (
846
797
  );
847
798
  }
848
799
  startStep(stmt);
849
- progress.isNextLabeled = false;
800
+ isNextLabeled = false;
850
801
  }
851
802
  switch (stmt.kind) {
852
803
  case "LabelIf": {
@@ -859,7 +810,7 @@ export const parsePcalAlgorithm = (
859
810
  outAssigned,
860
811
  thenAssigned,
861
812
  );
862
- progress.isNextLabeled =
813
+ isNextLabeled =
863
814
  addLabels(stmt.unlabElse, false, isInWith, outAssigned, elseAssigned) || isThenLabeled;
864
815
  copyInto(getUnion(thenAssigned, elseAssigned), outAssigned);
865
816
  return;
@@ -868,9 +819,8 @@ export const parsePcalAlgorithm = (
868
819
  const eitherAssigned: string[] = [];
869
820
  stmt.clauses.forEach((clause) => {
870
821
  const orAssigned: string[] = [];
871
- progress.isNextLabeled =
872
- addLabels(clause.unlabOr, false, isInWith, outAssigned, orAssigned) ||
873
- progress.isNextLabeled;
822
+ isNextLabeled =
823
+ addLabels(clause.unlabOr, false, isInWith, outAssigned, orAssigned) || isNextLabeled;
874
824
  copyInto(getUnion(orAssigned, eitherAssigned), eitherAssigned);
875
825
  });
876
826
  copyInto(eitherAssigned, outAssigned);
@@ -880,11 +830,11 @@ export const parsePcalAlgorithm = (
880
830
  if (isInWith) throw new PcalError("`while' inside a `with' statement", stmt);
881
831
  startStep(stmt);
882
832
  addLabels(stmt.unlabDo, false, false, outAssigned, []);
883
- progress.isNextLabeled = false;
833
+ isNextLabeled = false;
884
834
  return;
885
835
  case "With": {
886
836
  const withAssigned: string[] = [];
887
- progress.isNextLabeled = addLabels(
837
+ isNextLabeled = addLabels(
888
838
  stmt.Do,
889
839
  false,
890
840
  true,
@@ -896,9 +846,6 @@ export const parsePcalAlgorithm = (
896
846
  return;
897
847
  }
898
848
  }
899
- // A call or return assigns the stack, and a return or a recursive call the parameters and
900
- // variables of every procedure. A statement that assigns a variable assigned since the
901
- // last label starts a new step.
902
849
  const isCall = stmt.kind === "Call" || stmt.kind === "CallGoto";
903
850
  const isReturn = stmt.kind === "Return" || stmt.kind === "CallReturn";
904
851
  const assigned =
@@ -910,14 +857,14 @@ export const parsePcalAlgorithm = (
910
857
  : isCall || isReturn
911
858
  ? [
912
859
  "stack",
913
- ...(isReturn || stmt.to === state.currentProcedure
914
- ? state.procedures.flatMap((procedure) =>
860
+ ...(isReturn || stmt.to === currentProcedure
861
+ ? procedures.flatMap((procedure) =>
915
862
  [...procedure.params, ...procedure.decls].map((decl) => decl.var),
916
863
  )
917
864
  : []),
918
865
  ]
919
866
  : [];
920
- progress.isNextLabeled = isCall || isReturn || stmt.kind === "Goto";
867
+ isNextLabeled = isCall || isReturn || stmt.kind === "Goto";
921
868
  if (isDisjoint(outAssigned, assigned)) {
922
869
  copyInto(getUnion(outAssigned, assigned), outAssigned);
923
870
  return;
@@ -933,11 +880,9 @@ export const parsePcalAlgorithm = (
933
880
  startStep(stmt);
934
881
  copyInto(assigned, outAssigned);
935
882
  });
936
- return progress.hasLabel || progress.isNextLabeled;
883
+ return hasStepLabel || isNextLabeled;
937
884
  };
938
885
 
939
- // ParseAlgorithm.FixStmtSeq: splits each compound statement's sequences into the statements
940
- // before their first label and the labeled statements from it on.
941
886
  const fixStmtSeq = (stmts: PcalStmt[]) => {
942
887
  const split = (sequence: PcalStmt[]) => {
943
888
  const index = sequence.findIndex((stmt) => stmt.lbl !== "");
@@ -972,7 +917,6 @@ export const parsePcalAlgorithm = (
972
917
  });
973
918
  };
974
919
 
975
- // ParseAlgorithm.InnerMakeLabeledStmtSeq: groups the statements into labeled statements.
976
920
  const makeLabeledStmts = (stmts: PcalStmt[]) => {
977
921
  const result: PcalLabeledStmt[] = [];
978
922
  stmts.forEach((stmt) => {
@@ -998,9 +942,6 @@ export const parsePcalAlgorithm = (
998
942
  return result;
999
943
  };
1000
944
 
1001
- // ParseAlgorithm.ClassifyStmtSeq for one statement: 0 for one that may have a statement after
1002
- // it in the step, 1 for a call, return or goto, which ends a step, and 2 for one with a labeled
1003
- // statement inside. An if or either without labels inside becomes an If or Either.
1004
945
  const classifyStmt = (stmts: PcalStmt[], index: number): number => {
1005
946
  const node = stmts[index];
1006
947
  switch (node.kind) {
@@ -1056,7 +997,7 @@ export const parsePcalAlgorithm = (
1056
997
  return stmts[index + 1]?.kind === "Return" ? 0 : 1;
1057
998
  case "Return":
1058
999
  case "CallReturn":
1059
- if (state.currentProcedure === undefined) {
1000
+ if (currentProcedure === undefined) {
1060
1001
  throw new PcalError(`return statement not in a procedure at ${getPcalLocation(node)}`);
1061
1002
  }
1062
1003
  return 1;
@@ -1077,7 +1018,6 @@ export const parsePcalAlgorithm = (
1077
1018
  }
1078
1019
  };
1079
1020
 
1080
- // ParseAlgorithm.ClassifyStmtSeq: a statement that ends a step can only end its sequence.
1081
1021
  const classifyStmtSeq = (stmts: PcalStmt[]): number => {
1082
1022
  for (let index = 0; index < stmts.length; index++) {
1083
1023
  const stmtClass = classifyStmt(stmts, index);
@@ -1090,7 +1030,6 @@ export const parsePcalAlgorithm = (
1090
1030
  return 0;
1091
1031
  };
1092
1032
 
1093
- // ParseAlgorithm.AddLabelsToStmtSeq followed by MakeLabeledStmtSeq.
1094
1033
  const makeLabeledStmtSeq = (stmts: PcalStmt[]) => {
1095
1034
  addLabels(stmts, true, false, [], []);
1096
1035
  const result = makeLabeledStmts(stmts);
@@ -1098,8 +1037,6 @@ export const parsePcalAlgorithm = (
1098
1037
  return result;
1099
1038
  };
1100
1039
 
1101
- // ParseAlgorithm.SubstituteInStmt: a copy of a macro's statement with the macro's arguments
1102
- // for its parameters, which records the position of the macro call.
1103
1040
  const substituteInStmt = (
1104
1041
  stmt: PcalStmt,
1105
1042
  args: PcalExpr[],
@@ -1228,8 +1165,6 @@ export const parsePcalAlgorithm = (
1228
1165
  }
1229
1166
  };
1230
1167
 
1231
- // ParseAlgorithm.SubstituteInSingleAssign: a parameter assigned to becomes the variable its
1232
- // argument starts with, and the rest of the argument goes before the subscript.
1233
1168
  const substituteInSingleAssign = (
1234
1169
  assign: PcalSingleAssign,
1235
1170
  args: PcalExpr[],
@@ -1276,7 +1211,6 @@ export const parsePcalAlgorithm = (
1276
1211
  return result;
1277
1212
  };
1278
1213
 
1279
- // ParseAlgorithm.ExpandMacrosInStmtSeq: replaces each macro call with the macro's body.
1280
1214
  const expandMacrosInStmtSeq = (stmts: PcalStmt[], macros: PcalMacro[]) => {
1281
1215
  const expanded = stmts.flatMap((node) => {
1282
1216
  switch (node.kind) {
@@ -1302,7 +1236,6 @@ export const parsePcalAlgorithm = (
1302
1236
  stmts.splice(0, stmts.length, ...expanded);
1303
1237
  };
1304
1238
 
1305
- // ParseAlgorithm.ExpandMacroCall: the last macro of the name wins.
1306
1239
  const expandMacroCall = (call: PcalMacroCall, macros: PcalMacro[]) => {
1307
1240
  const definition = macros.findLast((macro) => macro.name === call.name);
1308
1241
  if (definition === undefined) {
@@ -1328,12 +1261,10 @@ export const parsePcalAlgorithm = (
1328
1261
  });
1329
1262
  };
1330
1263
 
1331
- // ParseAlgorithm.checkBody: a body that is a single labeled "while TRUE" without labels inside
1332
- // lets the translation omit pc.
1333
1264
  const checkBody = (body: PcalStmt[]) => {
1334
1265
  if (body.length === 0) return;
1335
1266
  const keepPC = () => {
1336
- state.omitPC = false;
1267
+ shouldOmitPC = false;
1337
1268
  };
1338
1269
  const labeled = body[0];
1339
1270
  if (body.length > 1 || labeled.kind !== "LabeledStmt") return keepPC();
@@ -1343,51 +1274,50 @@ export const parsePcalAlgorithm = (
1343
1274
  if (!isOneTokenPcalExpr(whileStmt.test) || whileStmt.test.tokens[0][0].string !== "TRUE") {
1344
1275
  return keepPC();
1345
1276
  }
1346
- if (whileStmt.labDo.length > 0) state.omitPC = false;
1277
+ if (whileStmt.labDo.length > 0) shouldOmitPC = false;
1347
1278
  const hasLabeled = whileStmt.unlabDo.some((stmt) =>
1348
1279
  ["LabelIf", "LabelEither", "LabeledStmt"].includes(stmt.kind),
1349
1280
  );
1350
- if (hasLabeled) state.omitPC = false;
1281
+ if (hasLabeled) shouldOmitPC = false;
1351
1282
  };
1352
1283
 
1353
- // ParseAlgorithm.AddedMessagesError.
1354
1284
  const throwAddedLabels = (): never => {
1355
1285
  const header =
1356
- state.addedLabels.length > 1
1286
+ addedLabels.length > 1
1357
1287
  ? "Missing labels at the following locations:"
1358
1288
  : "Missing label at the following location:";
1359
- throw new PcalError([header, ...state.addedLabelsLocs].join("\n "));
1289
+ throw new PcalError([header, ...addedLabelsLocs].join("\n "));
1360
1290
  };
1361
1291
 
1362
1292
  const fixProcedures = (macros: PcalMacro[]) =>
1363
- state.procedures.forEach((procedure) => {
1364
- state.currentProcedure = procedure.name;
1293
+ procedures.forEach((procedure) => {
1294
+ currentProcedure = procedure.name;
1365
1295
  expandMacrosInStmtSeq(procedure.body, macros);
1366
1296
  procedure.body = makeLabeledStmtSeq(procedure.body);
1367
1297
  });
1368
1298
 
1369
1299
  const parse = (): PcalAlgorithm => {
1370
- if (isFairAlgorithm) {
1371
- if (getAlgToken() !== "algorithm") throwParsingError("`--fair' not followed by `algorithm'");
1300
+ if (isFairAlgorithm && getAlgToken() !== "algorithm") {
1301
+ throwParsingError("`--fair' not followed by `algorithm'");
1372
1302
  }
1373
1303
  const name = getAlgToken();
1374
- state.cSyntax = peekAtAlgToken(1) === "{";
1375
- if (state.cSyntax) mustGobbleThis("{");
1304
+ isCSyntax = peekAtAlgToken(1) === "{";
1305
+ if (isCSyntax) mustGobbleThis("{");
1376
1306
  const decls =
1377
1307
  peekAtAlgToken(1) === "variable" || peekAtAlgToken(1) === "variables" ? getVarDecls() : [];
1378
- const defs = { value: createPcalExpr() };
1308
+ let defs = createPcalExpr();
1379
1309
  if (peekAtAlgToken(1) === "define") {
1380
1310
  mustGobbleThis("define");
1381
- if (state.cSyntax) gobbleThis("{");
1382
- defs.value = getExpr(true);
1311
+ if (isCSyntax) gobbleThis("{");
1312
+ defs = getExpr(true);
1383
1313
  gobbleEndOrRightBrace("define");
1384
1314
  gobbleThis(";");
1385
1315
  }
1386
1316
  const macros: PcalMacro[] = [];
1387
1317
  while (peekAtAlgToken(1) === "macro") macros.push(getMacro(macros));
1388
1318
  while (peekAtAlgToken(1) === "procedure") {
1389
- state.procedures.push(getProcedure());
1390
- state.omitPC = false;
1319
+ procedures.push(getProcedure());
1320
+ shouldOmitPC = false;
1391
1321
  }
1392
1322
  const isMultiprocess =
1393
1323
  (peekAtAlgToken(1) === "fair" &&
@@ -1417,19 +1347,19 @@ export const parsePcalAlgorithm = (
1417
1347
  checkBody(proc.body);
1418
1348
  });
1419
1349
  fixProcedures(macros);
1420
- if (state.addedLabels.length > 0) {
1350
+ if (addedLabels.length > 0) {
1421
1351
  if (!params.labelFlag) throwAddedLabels();
1422
1352
  session.log("Labels added.");
1423
1353
  }
1424
- if (state.gotoDoneUsed || state.gotoUsed) state.omitPC = false;
1354
+ if (isGotoDoneUsed || isGotoUsed) shouldOmitPC = false;
1425
1355
  return {
1426
1356
  ...createPcalBase(),
1427
1357
  kind: "Multiprocess",
1428
1358
  name,
1429
1359
  decls,
1430
- defs: defs.value,
1360
+ defs,
1431
1361
  macros,
1432
- prcds: state.procedures,
1362
+ prcds: procedures,
1433
1363
  procs,
1434
1364
  };
1435
1365
  }
@@ -1449,24 +1379,23 @@ export const parsePcalAlgorithm = (
1449
1379
  checkForDuplicateMacros(macros);
1450
1380
  expandMacrosInStmtSeq(body, macros);
1451
1381
  fixProcedures(macros);
1452
- // The first brace ends the body that gobbleBeginOrLeftBrace began.
1453
- if (state.cSyntax) gobbleThis("}");
1382
+ if (isCSyntax) gobbleThis("}");
1454
1383
  gobbleEndOrRightBrace("algorithm");
1455
1384
  const labeledBody = makeLabeledStmtSeq(body);
1456
- if (state.addedLabels.length > 0) {
1457
- if (state.hasLabel && !params.labelFlag) throwAddedLabels();
1385
+ if (addedLabels.length > 0) {
1386
+ if (hasLabel && !params.labelFlag) throwAddedLabels();
1458
1387
  session.log("Labels added.");
1459
1388
  }
1460
- if (state.gotoUsed || state.gotoDoneUsed) state.omitPC = false;
1389
+ if (isGotoUsed || isGotoDoneUsed) shouldOmitPC = false;
1461
1390
  checkBody(labeledBody);
1462
1391
  return {
1463
1392
  ...createPcalBase(),
1464
1393
  kind: "Uniprocess",
1465
1394
  name,
1466
1395
  decls,
1467
- defs: defs.value,
1396
+ defs,
1468
1397
  macros,
1469
- prcds: state.procedures,
1398
+ prcds: procedures,
1470
1399
  body: labeledBody,
1471
1400
  };
1472
1401
  };
@@ -1475,11 +1404,7 @@ export const parsePcalAlgorithm = (
1475
1404
  // on input it doesn't expect, into a parsing error at the last token.
1476
1405
  try {
1477
1406
  const ast = parse();
1478
- return {
1479
- ast,
1480
- omitPC: state.omitPC,
1481
- hasDefaultInitialization: state.hasDefaultInitialization,
1482
- };
1407
+ return { ast, shouldOmitPC, hasDefaultInitialization };
1483
1408
  } catch (error) {
1484
1409
  if (!(error instanceof PcalRuntimeError)) throw error;
1485
1410
  return throwParsingError("Unknown error at or before");