tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.f16792fed

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