tla-ts 0.0.10-dev.28c823e4b → 0.0.10-dev.b4a856304

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 (68) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +0 -34
  3. package/dist/index.d.ts +0 -34
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +43 -43
  8. package/dist/prove.js +47 -47
  9. package/dist/run.cjs +30 -30
  10. package/dist/run.js +33 -33
  11. package/dist/transfer.cjs +38 -38
  12. package/dist/transfer.js +39 -39
  13. package/package.json +1 -1
  14. package/src/ast.ts +5 -0
  15. package/src/bags.ts +5 -2
  16. package/src/builtins.ts +8 -1
  17. package/src/check.ts +193 -128
  18. package/src/cli/run.ts +2 -0
  19. package/src/cli/runtime-invariants.ts +7 -8
  20. package/src/cli/shared.ts +4 -0
  21. package/src/cli/tlc-arguments.ts +30 -24
  22. package/src/cli/tlc-usage.ts +3 -2
  23. package/src/evaluate.ts +102 -26
  24. package/src/java-overrides.ts +9 -4
  25. package/src/json-reader.ts +2 -1
  26. package/src/json.ts +11 -4
  27. package/src/levels.ts +15 -7
  28. package/src/lexer.ts +83 -72
  29. package/src/liveness-checker.ts +106 -77
  30. package/src/liveness.ts +34 -10
  31. package/src/parallel.ts +33 -26
  32. package/src/parser.ts +226 -163
  33. package/src/pcal/ast.ts +65 -53
  34. package/src/pcal/parse.ts +191 -144
  35. package/src/pcal/tokenize.ts +115 -91
  36. package/src/pcal/translate.ts +98 -83
  37. package/src/pcal/validate.ts +50 -48
  38. package/src/pretty-print.ts +6 -6
  39. package/src/randomization.ts +4 -1
  40. package/src/resolve.ts +70 -12
  41. package/src/runtime.ts +18 -11
  42. package/src/sany/builtins.ts +3 -1
  43. package/src/sany/generator.ts +127 -74
  44. package/src/sany/levels.ts +9 -5
  45. package/src/sany/lexer.ts +44 -40
  46. package/src/sany/linter.ts +1 -0
  47. package/src/search.ts +1 -1
  48. package/src/simulate.ts +59 -50
  49. package/src/spec-description.ts +34 -23
  50. package/src/standard-definitions.ts +3 -0
  51. package/src/standard-modules.ts +12 -1
  52. package/src/state-graph.ts +8 -1
  53. package/src/subexpressions.ts +47 -38
  54. package/src/successors.ts +35 -14
  55. package/src/symmetry.ts +19 -4
  56. package/src/tlc/fingerprint.ts +21 -15
  57. package/src/tlc/jar-data.ts +1 -0
  58. package/src/tlc/messages.ts +21 -2
  59. package/src/tlc/order.ts +38 -20
  60. package/src/tlc/random.ts +30 -18
  61. package/src/tlc/replay.ts +33 -13
  62. package/src/tlc/summary.ts +45 -36
  63. package/src/tlc/tlc-operators.ts +14 -4
  64. package/src/tlc/tlcext-operators.ts +15 -11
  65. package/src/tlc/trace-spec.ts +4 -0
  66. package/src/tlc/value-stream.ts +13 -9
  67. package/src/tlc/version.ts +1 -0
  68. package/src/values.ts +181 -74
package/src/pcal/parse.ts CHANGED
@@ -44,6 +44,8 @@ import {
44
44
  type PcalTokenizerState,
45
45
  } from "./tokenize";
46
46
 
47
+ // The Java keeps this state in static fields, which SANY never resets between the modules it
48
+ // validates.
47
49
  export interface PcalSession {
48
50
  params: PcalParams;
49
51
  tokenizer: PcalTokenizerState;
@@ -69,9 +71,28 @@ export const createPcalSession = (
69
71
  log,
70
72
  });
71
73
 
74
+ interface PcalParserState {
75
+ lookaheadSize: number;
76
+ addedLabels: string[];
77
+ addedLabelsLocs: string[];
78
+ nextLabelNum: number;
79
+ allLabels: Set<string>;
80
+ hasLabel: boolean;
81
+ hasDefaultInitialization: boolean;
82
+ currentProcedure: string | undefined;
83
+ procedures: PcalProcedure[];
84
+ cSyntax: boolean;
85
+ plusLabels: string[];
86
+ minusLabels: string[];
87
+ proceduresCalled: string[];
88
+ gotoUsed: boolean;
89
+ gotoDoneUsed: boolean;
90
+ omitPC: boolean;
91
+ }
92
+
72
93
  interface PcalParseResult {
73
94
  ast: PcalAlgorithm;
74
- shouldOmitPC: boolean;
95
+ omitPC: boolean;
75
96
  hasDefaultInitialization: boolean;
76
97
  }
77
98
 
@@ -86,6 +107,7 @@ const getUnion = (first: string[], second: string[]) => [
86
107
  const isDisjoint = (first: string[], second: string[]) =>
87
108
  !second.some((name) => first.includes(name));
88
109
 
110
+ // Replaces the contents of target in place, since callers share it.
89
111
  const copyInto = (source: string[], target: string[]) => {
90
112
  target.splice(0, target.length, ...source);
91
113
  };
@@ -98,29 +120,31 @@ export const parsePcalAlgorithm = (
98
120
  const params = session.params;
99
121
  const lookahead = session.lookahead;
100
122
  const lastToken = session.lastToken;
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;
123
+ const state: PcalParserState = {
124
+ lookaheadSize: 0,
125
+ addedLabels: [],
126
+ addedLabelsLocs: [],
127
+ nextLabelNum: 1,
128
+ allLabels: new Set(),
129
+ hasLabel: false,
130
+ hasDefaultInitialization: false,
131
+ currentProcedure: undefined,
132
+ procedures: [],
133
+ cSyntax: false,
134
+ plusLabels: [],
135
+ minusLabels: [],
136
+ proceduresCalled: [],
137
+ gotoUsed: false,
138
+ gotoDoneUsed: false,
139
+ omitPC: params.inputVersionNumber >= 1500,
140
+ };
117
141
 
118
142
  const throwParsingError = (message: string): never => {
119
143
  throw new PcalError(`${message}\n line ${lastToken.line}, column ${lastToken.column}`);
120
144
  };
121
145
 
122
146
  const getAlgToken = () => {
123
- if (lookaheadSize === 0) {
147
+ if (state.lookaheadSize === 0) {
124
148
  peekPcalReader(reader);
125
149
  lastToken.column = reader.vcolumn + 1;
126
150
  lastToken.line = reader.line + 1;
@@ -130,19 +154,19 @@ export const parsePcalAlgorithm = (
130
154
  lastToken.column = lookahead.columns[0] + 1;
131
155
  lastToken.line = lookahead.lines[0] + 1;
132
156
  const result = lookahead.tokens[0];
133
- for (let index = 1; index < lookaheadSize; index++) {
157
+ for (let index = 1; index < state.lookaheadSize; index++) {
134
158
  lookahead.tokens[index - 1] = lookahead.tokens[index];
135
159
  lookahead.columns[index - 1] = lookahead.columns[index];
136
160
  lookahead.lines[index - 1] = lookahead.lines[index];
137
161
  }
138
- lookaheadSize -= 1;
162
+ state.lookaheadSize -= 1;
139
163
  lastToken.text = result;
140
164
  return result;
141
165
  };
142
166
 
143
167
  const peekAtAlgToken = (tokenNumber: number) => {
144
- while (lookaheadSize < tokenNumber) {
145
- const size = lookaheadSize;
168
+ while (state.lookaheadSize < tokenNumber) {
169
+ const size = state.lookaheadSize;
146
170
  if (peekPcalReader(reader) === "\t") {
147
171
  throw new PcalError(
148
172
  `Premature end of file, perhaps because of unclosed comment, near\n line ${lookahead.lines[size] + 1}, column ${lookahead.columns[size] + 1}`,
@@ -151,43 +175,38 @@ export const parsePcalAlgorithm = (
151
175
  lookahead.columns[size] = reader.vcolumn;
152
176
  lookahead.lines[size] = reader.line;
153
177
  lookahead.tokens[size] = getPcalAlgorithmToken(session.tokenizer, reader);
154
- lookaheadSize += 1;
178
+ state.lookaheadSize += 1;
155
179
  }
156
180
  return lookahead.tokens[tokenNumber - 1];
157
181
  };
158
182
 
159
183
  const peekPastAlgToken = (tokenNumber: number) => {
160
- if (tokenNumber < lookaheadSize) {
184
+ if (tokenNumber < state.lookaheadSize) {
161
185
  throw new PcalBugError(
162
186
  "ParseAlgorithm.PeekPastAlgToken called to peek after a token\n it has already peeked at",
163
187
  );
164
188
  }
165
- while (tokenNumber > lookaheadSize) {
166
- lookahead.tokens[lookaheadSize] = getPcalAlgorithmToken(session.tokenizer, reader);
167
- lookaheadSize += 1;
189
+ while (tokenNumber > state.lookaheadSize) {
190
+ lookahead.tokens[state.lookaheadSize] = getPcalAlgorithmToken(session.tokenizer, reader);
191
+ state.lookaheadSize += 1;
168
192
  }
169
193
  return peekPcalReader(reader);
170
194
  };
171
195
 
196
+ // An expression's delimiter becomes the next token.
172
197
  const getExpr = (isDefineBlock = false) => {
173
- if (lookaheadSize !== 0) {
198
+ if (state.lookaheadSize !== 0) {
174
199
  throw new PcalBugError("ParseAlgorithm: GetExpr called after lookahead");
175
200
  }
176
201
  const result = tokenizePcalExpr(session.tokenizer, reader, isDefineBlock);
177
202
  lookahead.tokens[0] = session.tokenizer.delimiter;
178
203
  lookahead.columns[0] = session.tokenizer.delimiterCol;
179
204
  lookahead.lines[0] = session.tokenizer.delimiterLine;
180
- lookaheadSize = 1;
205
+ state.lookaheadSize = 1;
181
206
  return result;
182
207
  };
183
208
 
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
-
209
+ // A missing ";" is fine before a token that can't start a statement.
191
210
  const gobbleThis = (expected: string, alternate?: string) => {
192
211
  if (expected === ";") {
193
212
  const next = peekAtAlgToken(1);
@@ -200,7 +219,15 @@ export const parsePcalAlgorithm = (
200
219
  );
201
220
  }
202
221
  }
203
- mustGobbleThis(expected, alternate);
222
+ const token = getAlgToken();
223
+ if (token !== expected && token !== alternate) {
224
+ throwParsingError(`Expected "${expected}" but found "${token}"`);
225
+ }
226
+ };
227
+
228
+ const mustGobbleThis = (expected: string) => {
229
+ const token = getAlgToken();
230
+ if (token !== expected) throwParsingError(`Expected "${expected}" but found "${token}"`);
204
231
  };
205
232
 
206
233
  const gobbleEqualOrIf = () => {
@@ -212,10 +239,10 @@ export const parsePcalAlgorithm = (
212
239
 
213
240
  const gobbleCommaOrSemicolon = () => gobbleThis(peekAtAlgToken(1) === "," ? "," : ";");
214
241
 
215
- const gobbleBeginOrLeftBrace = () => gobbleThis(isCSyntax ? "{" : "begin");
242
+ const gobbleBeginOrLeftBrace = () => gobbleThis(state.cSyntax ? "{" : "begin");
216
243
 
217
244
  const gobbleEndOrRightBrace = (name: string) => {
218
- if (isCSyntax) {
245
+ if (state.cSyntax) {
219
246
  gobbleThis("}");
220
247
  return;
221
248
  }
@@ -239,11 +266,13 @@ export const parsePcalAlgorithm = (
239
266
  val: createDefaultVarInit(),
240
267
  };
241
268
  if (!["=", "\\in", "∈"].includes(peekAtAlgToken(1))) {
242
- hasDefaultInitialization = true;
269
+ state.hasDefaultInitialization = true;
243
270
  return decl;
244
271
  }
245
272
  decl.isEq = gobbleEqualOrIf();
246
273
  decl.val = getExpr();
274
+ // ParseAlgorithm.GetVarDecl takes the end of the value's origin, which an empty value lacks,
275
+ // before checking for one.
247
276
  if (decl.val.tokens.length === 0) throw new PcalRuntimeError("NullPointerException");
248
277
  return decl;
249
278
  };
@@ -269,7 +298,7 @@ export const parsePcalAlgorithm = (
269
298
  val: createDefaultVarInit(),
270
299
  };
271
300
  if (peekAtAlgToken(1) !== "=") {
272
- hasDefaultInitialization = true;
301
+ state.hasDefaultInitialization = true;
273
302
  return decl;
274
303
  }
275
304
  gobbleThis("=");
@@ -303,23 +332,23 @@ export const parsePcalAlgorithm = (
303
332
  if (label === "Done") throwParsingError("Cannot use `Done' as a label.");
304
333
  if (label === "Error") throwParsingError("Cannot use `Error' as a label.");
305
334
  gobbleThis(":");
306
- hasLabel = true;
307
- allLabels.add(label);
335
+ state.hasLabel = true;
336
+ state.allLabels.add(label);
308
337
  const modifier = peekAtAlgToken(1);
309
338
  if (modifier !== "+" && modifier !== "-") return label;
310
339
  gobbleThis(modifier);
311
- (modifier === "+" ? plusLabels : minusLabels).push(label);
312
- shouldOmitPC = false;
340
+ (modifier === "+" ? state.plusLabels : state.minusLabels).push(label);
341
+ state.omitPC = false;
313
342
  return label;
314
343
  };
315
344
 
316
345
  const getStmtSeq = (): PcalStmt[] => {
317
346
  const result: PcalStmt[] = [];
318
347
  const isEnd = (token: string) =>
319
- (!isCSyntax && ["end", "else", "elsif", "or"].includes(token)) ||
320
- (isCSyntax && token === "}");
348
+ (!state.cSyntax && ["end", "else", "elsif", "or"].includes(token)) ||
349
+ (state.cSyntax && token === "}");
321
350
  while (!isEnd(peekAtAlgToken(1))) result.push(...getCStmt());
322
- if (isCSyntax && result.length === 0) throwParsingError("Empty statement list");
351
+ if (state.cSyntax && result.length === 0) throwParsingError("Empty statement list");
323
352
  return result;
324
353
  };
325
354
 
@@ -337,7 +366,7 @@ export const parsePcalAlgorithm = (
337
366
 
338
367
  const getCStmt = () => {
339
368
  const label = getLabel();
340
- if (isCSyntax && peekAtAlgToken(1) === "{") return getCStmtSeq(label);
369
+ if (state.cSyntax && peekAtAlgToken(1) === "{") return getCStmtSeq(label);
341
370
  const stmt = getStmt();
342
371
  stmt.lbl = label;
343
372
  return [stmt];
@@ -353,6 +382,8 @@ export const parsePcalAlgorithm = (
353
382
  const col = lastToken.column;
354
383
  const line = lastToken.line;
355
384
  const exp = getExpr();
385
+ // ParseAlgorithm.GetWhen takes the end of the expression's origin, which an empty
386
+ // expression lacks, before checking for one.
356
387
  if (exp.tokens.length === 0) throw new PcalRuntimeError("NullPointerException");
357
388
  gobbleThis(";");
358
389
  return { ...createPcalBase(), kind: "When", col, line, exp };
@@ -391,7 +422,7 @@ export const parsePcalAlgorithm = (
391
422
  kind: "Return",
392
423
  col: lastToken.column,
393
424
  line: lastToken.line,
394
- from: currentProcedure,
425
+ from: state.currentProcedure,
395
426
  };
396
427
  gobbleThis(";");
397
428
  return result;
@@ -400,11 +431,11 @@ export const parsePcalAlgorithm = (
400
431
  mustGobbleThis("goto");
401
432
  const col = lastToken.column;
402
433
  const line = lastToken.line;
403
- const target = getAlgToken();
404
- isGotoUsed = true;
405
- if (target === "Done" || target === '"Done"') isGotoDoneUsed = true;
434
+ const to = getAlgToken();
435
+ state.gotoUsed = true;
436
+ if (to === "Done" || to === '"Done"') state.gotoDoneUsed = true;
406
437
  gobbleThis(";");
407
- return { ...createPcalBase(), kind: "Goto", col, line, to: target };
438
+ return { ...createPcalBase(), kind: "Goto", col, line, to };
408
439
  }
409
440
  if (next === "while") return getWhile();
410
441
  if (peekPastAlgToken(1)[0] === "(") return getMacroCall();
@@ -415,13 +446,13 @@ export const parsePcalAlgorithm = (
415
446
  mustGobbleThis("while");
416
447
  const col = lastToken.column;
417
448
  const line = lastToken.line;
418
- if (isCSyntax) gobbleThis("(");
449
+ if (state.cSyntax) gobbleThis("(");
419
450
  const test = getExpr();
420
- if (isCSyntax) gobbleThis(")");
451
+ if (state.cSyntax) gobbleThis(")");
421
452
  if (test.tokens.length === 0) throwParsingError("Empty while test at ");
422
- if (!isCSyntax) gobbleThis("do");
423
- const unlabDo = isCSyntax ? getCStmt() : getStmtSeq();
424
- if (!isCSyntax) {
453
+ if (!state.cSyntax) gobbleThis("do");
454
+ const unlabDo = state.cSyntax ? getCStmt() : getStmtSeq();
455
+ if (!state.cSyntax) {
425
456
  gobbleThis("end");
426
457
  gobbleThis("while");
427
458
  gobbleThis(";");
@@ -443,27 +474,28 @@ export const parsePcalAlgorithm = (
443
474
  unlabElse: [],
444
475
  labElse: [],
445
476
  };
446
- if (isCSyntax) gobbleThis("(");
477
+ if (state.cSyntax) gobbleThis("(");
447
478
  result.test = getExpr();
448
- if (isCSyntax) gobbleThis(")");
479
+ if (state.cSyntax) gobbleThis(")");
449
480
  if (result.test.tokens.length === 0) throwParsingError("Empty if test at ");
450
- if (!isCSyntax) gobbleThis("then");
451
- result.unlabThen = isCSyntax ? getCStmt() : getStmtSeq();
481
+ if (!state.cSyntax) gobbleThis("then");
482
+ result.unlabThen = state.cSyntax ? getCStmt() : getStmtSeq();
452
483
  const next = peekAtAlgToken(1);
453
484
  if (next === "else") {
454
485
  mustGobbleThis("else");
455
- result.unlabElse = isCSyntax ? getCStmt() : getStmtSeq();
486
+ result.unlabElse = state.cSyntax ? getCStmt() : getStmtSeq();
456
487
  }
457
- if (!isCSyntax && next === "elsif") result.unlabElse = [getIf(depth + 1)];
458
- if (!isCSyntax && !["else", "elsif", "end"].includes(next)) {
488
+ if (!state.cSyntax && next === "elsif") result.unlabElse = [getIf(depth + 1)];
489
+ if (!state.cSyntax && !["else", "elsif", "end"].includes(next)) {
459
490
  throwParsingError('Expecting "else", "elsif", or "end"');
460
491
  }
461
- if (!isCSyntax && depth === 0) {
492
+ if (!state.cSyntax && depth === 0) {
462
493
  gobbleThis("end");
463
494
  gobbleThis("if");
464
495
  gobbleThis(";");
465
496
  }
466
- // ParseAlgorithm.GetIf reads the last then statement, which throws when there is none.
497
+ // ParseAlgorithm.GetIf takes the origin of the last statement, which an empty then clause
498
+ // with no else clause doesn't have.
467
499
  if (result.unlabElse.length === 0) {
468
500
  getElement(result.unlabThen, result.unlabThen.length - 1);
469
501
  }
@@ -483,7 +515,7 @@ export const parsePcalAlgorithm = (
483
515
  const clause: PcalClause = {
484
516
  ...createPcalBase(),
485
517
  kind: "Clause",
486
- unlabOr: isCSyntax ? getCStmt() : getStmtSeq(),
518
+ unlabOr: state.cSyntax ? getCStmt() : getStmtSeq(),
487
519
  labOr: [],
488
520
  };
489
521
  if (clause.unlabOr.length === 0) {
@@ -493,7 +525,7 @@ export const parsePcalAlgorithm = (
493
525
  if (peekAtAlgToken(1) !== "or") break;
494
526
  mustGobbleThis("or");
495
527
  }
496
- if (!isCSyntax) {
528
+ if (!state.cSyntax) {
497
529
  mustGobbleThis("end");
498
530
  gobbleThis("either");
499
531
  gobbleThis(";");
@@ -505,7 +537,7 @@ export const parsePcalAlgorithm = (
505
537
  const getWith = (depth: number): PcalWith => {
506
538
  if (depth === 0) {
507
539
  gobbleThis("with");
508
- if (isCSyntax) gobbleThis("(");
540
+ if (state.cSyntax) gobbleThis("(");
509
541
  }
510
542
  const result: PcalWith = {
511
543
  ...createPcalBase(),
@@ -519,10 +551,10 @@ export const parsePcalAlgorithm = (
519
551
  };
520
552
  result.isEq = gobbleEqualOrIf();
521
553
  result.exp = getExpr();
522
- if (!isCSyntax || peekAtAlgToken(1) !== ")") gobbleCommaOrSemicolon();
554
+ if (!state.cSyntax || peekAtAlgToken(1) !== ")") gobbleCommaOrSemicolon();
523
555
  if (result.exp.tokens.length === 0) throwParsingError("Empty with expression at ");
524
- const isBlockBody = !isCSyntax && peekAtAlgToken(1) === "do";
525
- const isBraceBody = isCSyntax && peekAtAlgToken(1) === ")";
556
+ const isBlockBody = !state.cSyntax && peekAtAlgToken(1) === "do";
557
+ const isBraceBody = state.cSyntax && peekAtAlgToken(1) === ")";
526
558
  if (isBlockBody) gobbleThis("do");
527
559
  if (isBraceBody) mustGobbleThis(")");
528
560
  result.Do = isBlockBody ? getStmtSeq() : isBraceBody ? getCStmt() : [getWith(depth + 1)];
@@ -604,7 +636,7 @@ export const parsePcalAlgorithm = (
604
636
  };
605
637
  gobbleThis("(");
606
638
  result.args = getCallArgs();
607
- if (!proceduresCalled.includes(result.to)) proceduresCalled.push(result.to);
639
+ if (!state.proceduresCalled.includes(result.to)) state.proceduresCalled.push(result.to);
608
640
  return result;
609
641
  };
610
642
 
@@ -619,7 +651,7 @@ export const parsePcalAlgorithm = (
619
651
  col: call.col,
620
652
  line: call.line,
621
653
  to: call.to,
622
- from: currentProcedure,
654
+ from: state.currentProcedure,
623
655
  args: call.args,
624
656
  };
625
657
  }
@@ -634,8 +666,8 @@ export const parsePcalAlgorithm = (
634
666
  after: getAlgToken(),
635
667
  args: call.args,
636
668
  };
637
- isGotoUsed = true;
638
- if (result.to === "Done" || result.to === '"Done"') isGotoDoneUsed = true;
669
+ state.gotoUsed = true;
670
+ if (result.to === "Done" || result.to === '"Done"') state.gotoDoneUsed = true;
639
671
  gobbleThis(";");
640
672
  return result;
641
673
  };
@@ -696,10 +728,10 @@ export const parsePcalAlgorithm = (
696
728
  decls: [],
697
729
  body: [],
698
730
  };
699
- currentProcedure = result.name;
700
- plusLabels = [];
701
- minusLabels = [];
702
- proceduresCalled = [];
731
+ state.currentProcedure = result.name;
732
+ state.plusLabels = [];
733
+ state.minusLabels = [];
734
+ state.proceduresCalled = [];
703
735
  gobbleThis("(");
704
736
  while (peekAtAlgToken(1) !== ")") {
705
737
  if (result.params.length > 0) gobbleThis(",");
@@ -713,10 +745,10 @@ export const parsePcalAlgorithm = (
713
745
  result.body = getStmtSeq();
714
746
  gobbleEndOrRightBrace("procedure");
715
747
  if (peekAtAlgToken(1) === ";") getAlgToken();
716
- currentProcedure = undefined;
717
- result.plusLabels = plusLabels;
718
- result.minusLabels = minusLabels;
719
- result.proceduresCalled = proceduresCalled;
748
+ state.currentProcedure = undefined;
749
+ result.plusLabels = state.plusLabels;
750
+ result.minusLabels = state.minusLabels;
751
+ result.proceduresCalled = state.proceduresCalled;
720
752
  return result;
721
753
  };
722
754
 
@@ -724,14 +756,14 @@ export const parsePcalAlgorithm = (
724
756
  gobbleThis("process");
725
757
  const col = lastToken.column;
726
758
  const line = lastToken.line;
727
- if (isCSyntax) gobbleThis("(");
759
+ if (state.cSyntax) gobbleThis("(");
728
760
  const name = getAlgToken();
729
761
  const isEq = gobbleEqualOrIf();
730
762
  const id = getExpr();
731
- plusLabels = [];
732
- minusLabels = [];
733
- proceduresCalled = [];
734
- if (isCSyntax) gobbleThis(")");
763
+ state.plusLabels = [];
764
+ state.minusLabels = [];
765
+ state.proceduresCalled = [];
766
+ if (state.cSyntax) gobbleThis(")");
735
767
  if (id.tokens.length === 0) throwParsingError("Empty process id at ");
736
768
  const decls = peekAtAlgToken(1) === "begin" || peekAtAlgToken(1) === "{" ? [] : getVarDecls();
737
769
  gobbleBeginOrLeftBrace();
@@ -745,9 +777,9 @@ export const parsePcalAlgorithm = (
745
777
  line,
746
778
  name,
747
779
  fairness: 0,
748
- minusLabels,
749
- plusLabels,
750
- proceduresCalled,
780
+ minusLabels: state.minusLabels,
781
+ plusLabels: state.plusLabels,
782
+ proceduresCalled: state.proceduresCalled,
751
783
  isEq,
752
784
  id,
753
785
  decls,
@@ -756,18 +788,19 @@ export const parsePcalAlgorithm = (
756
788
  };
757
789
 
758
790
  const getUnusedLabel = (): string => {
759
- const label = `${params.labelRoot}${nextLabelNum}`;
760
- nextLabelNum += 1;
761
- return allLabels.has(label) ? getUnusedLabel() : label;
791
+ const label = `${params.labelRoot}${state.nextLabelNum}`;
792
+ state.nextLabelNum += 1;
793
+ return state.allLabels.has(label) ? getUnusedLabel() : label;
762
794
  };
763
795
 
764
- const ensureLabel = (stmt: PcalStmt) => {
796
+ const needsLabel = (stmt: PcalStmt) => {
765
797
  if (stmt.lbl !== "") return;
766
798
  stmt.lbl = getUnusedLabel();
767
- addedLabels.push(stmt.lbl);
768
- addedLabelsLocs.push(getPcalLocation(stmt));
799
+ state.addedLabels.push(stmt.lbl);
800
+ state.addedLabelsLocs.push(getPcalLocation(stmt));
769
801
  };
770
802
 
803
+ // True when the statement after the sequence needs a label.
771
804
  const addLabels = (
772
805
  stmts: PcalStmt[],
773
806
  isFirstLabeled: boolean,
@@ -776,20 +809,19 @@ export const parsePcalAlgorithm = (
776
809
  outAssigned: string[],
777
810
  ): boolean => {
778
811
  copyInto(inAssigned, outAssigned);
779
- let isNextLabeled = isFirstLabeled;
780
- let hasStepLabel = false;
812
+ const progress = { isNextLabeled: isFirstLabeled, hasLabel: false };
781
813
  const startStep = (stmt: PcalStmt) => {
782
- ensureLabel(stmt);
783
- hasStepLabel = true;
814
+ needsLabel(stmt);
815
+ progress.hasLabel = true;
784
816
  outAssigned.length = 0;
785
817
  };
786
818
  stmts.forEach((stmt) => {
787
819
  if (stmt.lbl !== "") {
788
- hasStepLabel = true;
820
+ progress.hasLabel = true;
789
821
  outAssigned.length = 0;
790
822
  if (isInWith) throw new PcalError("Label in `with' statement", stmt);
791
823
  }
792
- if (isNextLabeled) {
824
+ if (progress.isNextLabeled) {
793
825
  if (isInWith) {
794
826
  throw new PcalError(
795
827
  "Statement follows `call' or `return' inside a `with' statement.",
@@ -797,7 +829,7 @@ export const parsePcalAlgorithm = (
797
829
  );
798
830
  }
799
831
  startStep(stmt);
800
- isNextLabeled = false;
832
+ progress.isNextLabeled = false;
801
833
  }
802
834
  switch (stmt.kind) {
803
835
  case "LabelIf": {
@@ -810,7 +842,7 @@ export const parsePcalAlgorithm = (
810
842
  outAssigned,
811
843
  thenAssigned,
812
844
  );
813
- isNextLabeled =
845
+ progress.isNextLabeled =
814
846
  addLabels(stmt.unlabElse, false, isInWith, outAssigned, elseAssigned) || isThenLabeled;
815
847
  copyInto(getUnion(thenAssigned, elseAssigned), outAssigned);
816
848
  return;
@@ -819,8 +851,9 @@ export const parsePcalAlgorithm = (
819
851
  const eitherAssigned: string[] = [];
820
852
  stmt.clauses.forEach((clause) => {
821
853
  const orAssigned: string[] = [];
822
- isNextLabeled =
823
- addLabels(clause.unlabOr, false, isInWith, outAssigned, orAssigned) || isNextLabeled;
854
+ progress.isNextLabeled =
855
+ addLabels(clause.unlabOr, false, isInWith, outAssigned, orAssigned) ||
856
+ progress.isNextLabeled;
824
857
  copyInto(getUnion(orAssigned, eitherAssigned), eitherAssigned);
825
858
  });
826
859
  copyInto(eitherAssigned, outAssigned);
@@ -830,11 +863,11 @@ export const parsePcalAlgorithm = (
830
863
  if (isInWith) throw new PcalError("`while' inside a `with' statement", stmt);
831
864
  startStep(stmt);
832
865
  addLabels(stmt.unlabDo, false, false, outAssigned, []);
833
- isNextLabeled = false;
866
+ progress.isNextLabeled = false;
834
867
  return;
835
868
  case "With": {
836
869
  const withAssigned: string[] = [];
837
- isNextLabeled = addLabels(
870
+ progress.isNextLabeled = addLabels(
838
871
  stmt.Do,
839
872
  false,
840
873
  true,
@@ -846,6 +879,9 @@ export const parsePcalAlgorithm = (
846
879
  return;
847
880
  }
848
881
  }
882
+ // A call or return assigns the stack, and a return or a recursive call the parameters and
883
+ // variables of every procedure. A statement that assigns a variable assigned since the
884
+ // last label starts a new step.
849
885
  const isCall = stmt.kind === "Call" || stmt.kind === "CallGoto";
850
886
  const isReturn = stmt.kind === "Return" || stmt.kind === "CallReturn";
851
887
  const assigned =
@@ -857,14 +893,14 @@ export const parsePcalAlgorithm = (
857
893
  : isCall || isReturn
858
894
  ? [
859
895
  "stack",
860
- ...(isReturn || stmt.to === currentProcedure
861
- ? procedures.flatMap((procedure) =>
896
+ ...(isReturn || stmt.to === state.currentProcedure
897
+ ? state.procedures.flatMap((procedure) =>
862
898
  [...procedure.params, ...procedure.decls].map((decl) => decl.var),
863
899
  )
864
900
  : []),
865
901
  ]
866
902
  : [];
867
- isNextLabeled = isCall || isReturn || stmt.kind === "Goto";
903
+ progress.isNextLabeled = isCall || isReturn || stmt.kind === "Goto";
868
904
  if (isDisjoint(outAssigned, assigned)) {
869
905
  copyInto(getUnion(outAssigned, assigned), outAssigned);
870
906
  return;
@@ -880,7 +916,7 @@ export const parsePcalAlgorithm = (
880
916
  startStep(stmt);
881
917
  copyInto(assigned, outAssigned);
882
918
  });
883
- return hasStepLabel || isNextLabeled;
919
+ return progress.hasLabel || progress.isNextLabeled;
884
920
  };
885
921
 
886
922
  const fixStmtSeq = (stmts: PcalStmt[]) => {
@@ -942,6 +978,8 @@ export const parsePcalAlgorithm = (
942
978
  return result;
943
979
  };
944
980
 
981
+ // 0 for a statement that may have another after it in the step, 1 for a call, return or goto,
982
+ // which ends a step, and 2 for one with a labeled statement inside.
945
983
  const classifyStmt = (stmts: PcalStmt[], index: number): number => {
946
984
  const node = stmts[index];
947
985
  switch (node.kind) {
@@ -997,7 +1035,7 @@ export const parsePcalAlgorithm = (
997
1035
  return stmts[index + 1]?.kind === "Return" ? 0 : 1;
998
1036
  case "Return":
999
1037
  case "CallReturn":
1000
- if (currentProcedure === undefined) {
1038
+ if (state.currentProcedure === undefined) {
1001
1039
  throw new PcalError(`return statement not in a procedure at ${getPcalLocation(node)}`);
1002
1040
  }
1003
1041
  return 1;
@@ -1165,6 +1203,8 @@ export const parsePcalAlgorithm = (
1165
1203
  }
1166
1204
  };
1167
1205
 
1206
+ // A parameter assigned to becomes the variable its argument starts with, and the rest of the
1207
+ // argument goes before the subscript.
1168
1208
  const substituteInSingleAssign = (
1169
1209
  assign: PcalSingleAssign,
1170
1210
  args: PcalExpr[],
@@ -1236,6 +1276,7 @@ export const parsePcalAlgorithm = (
1236
1276
  stmts.splice(0, stmts.length, ...expanded);
1237
1277
  };
1238
1278
 
1279
+ // ParseAlgorithm.ExpandMacroCall: the last macro of the name wins.
1239
1280
  const expandMacroCall = (call: PcalMacroCall, macros: PcalMacro[]) => {
1240
1281
  const definition = macros.findLast((macro) => macro.name === call.name);
1241
1282
  if (definition === undefined) {
@@ -1261,10 +1302,11 @@ export const parsePcalAlgorithm = (
1261
1302
  });
1262
1303
  };
1263
1304
 
1305
+ // A body that is a single labeled "while TRUE" without labels inside lets the translation omit pc.
1264
1306
  const checkBody = (body: PcalStmt[]) => {
1265
1307
  if (body.length === 0) return;
1266
1308
  const keepPC = () => {
1267
- shouldOmitPC = false;
1309
+ state.omitPC = false;
1268
1310
  };
1269
1311
  const labeled = body[0];
1270
1312
  if (body.length > 1 || labeled.kind !== "LabeledStmt") return keepPC();
@@ -1274,50 +1316,50 @@ export const parsePcalAlgorithm = (
1274
1316
  if (!isOneTokenPcalExpr(whileStmt.test) || whileStmt.test.tokens[0][0].string !== "TRUE") {
1275
1317
  return keepPC();
1276
1318
  }
1277
- if (whileStmt.labDo.length > 0) shouldOmitPC = false;
1319
+ if (whileStmt.labDo.length > 0) state.omitPC = false;
1278
1320
  const hasLabeled = whileStmt.unlabDo.some((stmt) =>
1279
1321
  ["LabelIf", "LabelEither", "LabeledStmt"].includes(stmt.kind),
1280
1322
  );
1281
- if (hasLabeled) shouldOmitPC = false;
1323
+ if (hasLabeled) state.omitPC = false;
1282
1324
  };
1283
1325
 
1284
1326
  const throwAddedLabels = (): never => {
1285
1327
  const header =
1286
- addedLabels.length > 1
1328
+ state.addedLabels.length > 1
1287
1329
  ? "Missing labels at the following locations:"
1288
1330
  : "Missing label at the following location:";
1289
- throw new PcalError([header, ...addedLabelsLocs].join("\n "));
1331
+ throw new PcalError([header, ...state.addedLabelsLocs].join("\n "));
1290
1332
  };
1291
1333
 
1292
1334
  const fixProcedures = (macros: PcalMacro[]) =>
1293
- procedures.forEach((procedure) => {
1294
- currentProcedure = procedure.name;
1335
+ state.procedures.forEach((procedure) => {
1336
+ state.currentProcedure = procedure.name;
1295
1337
  expandMacrosInStmtSeq(procedure.body, macros);
1296
1338
  procedure.body = makeLabeledStmtSeq(procedure.body);
1297
1339
  });
1298
1340
 
1299
1341
  const parse = (): PcalAlgorithm => {
1300
- if (isFairAlgorithm && getAlgToken() !== "algorithm") {
1301
- throwParsingError("`--fair' not followed by `algorithm'");
1342
+ if (isFairAlgorithm) {
1343
+ if (getAlgToken() !== "algorithm") throwParsingError("`--fair' not followed by `algorithm'");
1302
1344
  }
1303
1345
  const name = getAlgToken();
1304
- isCSyntax = peekAtAlgToken(1) === "{";
1305
- if (isCSyntax) mustGobbleThis("{");
1346
+ state.cSyntax = peekAtAlgToken(1) === "{";
1347
+ if (state.cSyntax) mustGobbleThis("{");
1306
1348
  const decls =
1307
1349
  peekAtAlgToken(1) === "variable" || peekAtAlgToken(1) === "variables" ? getVarDecls() : [];
1308
- let defs = createPcalExpr();
1350
+ const defs = { value: createPcalExpr() };
1309
1351
  if (peekAtAlgToken(1) === "define") {
1310
1352
  mustGobbleThis("define");
1311
- if (isCSyntax) gobbleThis("{");
1312
- defs = getExpr(true);
1353
+ if (state.cSyntax) gobbleThis("{");
1354
+ defs.value = getExpr(true);
1313
1355
  gobbleEndOrRightBrace("define");
1314
1356
  gobbleThis(";");
1315
1357
  }
1316
1358
  const macros: PcalMacro[] = [];
1317
1359
  while (peekAtAlgToken(1) === "macro") macros.push(getMacro(macros));
1318
1360
  while (peekAtAlgToken(1) === "procedure") {
1319
- procedures.push(getProcedure());
1320
- shouldOmitPC = false;
1361
+ state.procedures.push(getProcedure());
1362
+ state.omitPC = false;
1321
1363
  }
1322
1364
  const isMultiprocess =
1323
1365
  (peekAtAlgToken(1) === "fair" &&
@@ -1347,19 +1389,19 @@ export const parsePcalAlgorithm = (
1347
1389
  checkBody(proc.body);
1348
1390
  });
1349
1391
  fixProcedures(macros);
1350
- if (addedLabels.length > 0) {
1392
+ if (state.addedLabels.length > 0) {
1351
1393
  if (!params.labelFlag) throwAddedLabels();
1352
1394
  session.log("Labels added.");
1353
1395
  }
1354
- if (isGotoDoneUsed || isGotoUsed) shouldOmitPC = false;
1396
+ if (state.gotoDoneUsed || state.gotoUsed) state.omitPC = false;
1355
1397
  return {
1356
1398
  ...createPcalBase(),
1357
1399
  kind: "Multiprocess",
1358
1400
  name,
1359
1401
  decls,
1360
- defs,
1402
+ defs: defs.value,
1361
1403
  macros,
1362
- prcds: procedures,
1404
+ prcds: state.procedures,
1363
1405
  procs,
1364
1406
  };
1365
1407
  }
@@ -1379,23 +1421,24 @@ export const parsePcalAlgorithm = (
1379
1421
  checkForDuplicateMacros(macros);
1380
1422
  expandMacrosInStmtSeq(body, macros);
1381
1423
  fixProcedures(macros);
1382
- if (isCSyntax) gobbleThis("}");
1424
+ // The first brace ends the body that gobbleBeginOrLeftBrace began.
1425
+ if (state.cSyntax) gobbleThis("}");
1383
1426
  gobbleEndOrRightBrace("algorithm");
1384
1427
  const labeledBody = makeLabeledStmtSeq(body);
1385
- if (addedLabels.length > 0) {
1386
- if (hasLabel && !params.labelFlag) throwAddedLabels();
1428
+ if (state.addedLabels.length > 0) {
1429
+ if (state.hasLabel && !params.labelFlag) throwAddedLabels();
1387
1430
  session.log("Labels added.");
1388
1431
  }
1389
- if (isGotoUsed || isGotoDoneUsed) shouldOmitPC = false;
1432
+ if (state.gotoUsed || state.gotoDoneUsed) state.omitPC = false;
1390
1433
  checkBody(labeledBody);
1391
1434
  return {
1392
1435
  ...createPcalBase(),
1393
1436
  kind: "Uniprocess",
1394
1437
  name,
1395
1438
  decls,
1396
- defs,
1439
+ defs: defs.value,
1397
1440
  macros,
1398
- prcds: procedures,
1441
+ prcds: state.procedures,
1399
1442
  body: labeledBody,
1400
1443
  };
1401
1444
  };
@@ -1404,7 +1447,11 @@ export const parsePcalAlgorithm = (
1404
1447
  // on input it doesn't expect, into a parsing error at the last token.
1405
1448
  try {
1406
1449
  const ast = parse();
1407
- return { ast, shouldOmitPC, hasDefaultInitialization };
1450
+ return {
1451
+ ast,
1452
+ omitPC: state.omitPC,
1453
+ hasDefaultInitialization: state.hasDefaultInitialization,
1454
+ };
1408
1455
  } catch (error) {
1409
1456
  if (!(error instanceof PcalRuntimeError)) throw error;
1410
1457
  return throwParsingError("Unknown error at or before");