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
@@ -68,7 +68,6 @@ interface PcalSymbol {
68
68
  useThis: string;
69
69
  }
70
70
 
71
- // initialPC is the first label, which a body with no statements lacks.
72
71
  interface PcalProcedureEntry {
73
72
  name: string;
74
73
  params: PcalPVarDecl[];
@@ -83,8 +82,6 @@ interface PcalSymbolTable {
83
82
  errorReport: string;
84
83
  }
85
84
 
86
- // A Return or CallReturn outside a procedure has no from, and PcalSymTab.FindSym never matches a
87
- // null id.
88
85
  const findSymbol = (
89
86
  table: PcalSymbolTable,
90
87
  type: number,
@@ -98,7 +95,6 @@ const findSymbol = (
98
95
  const findSymbolInContext = (table: PcalSymbolTable, id: string, context: string) =>
99
96
  table.symbols.find((symbol) => symbol.id === id && symbol.context === context);
100
97
 
101
- // PcalSymTab.FindProc returns the number of procedures when it finds none.
102
98
  const findProcedureIndex = (table: PcalSymbolTable, name: string) => {
103
99
  const index = table.procedures.findIndex((procedure) => procedure.name === name);
104
100
  return index === -1 ? table.procedures.length : index;
@@ -249,7 +245,7 @@ const createPcalSymbolTable = (ast: PcalAlgorithm) => {
249
245
  const disambiguatePcalSymbols = (table: PcalSymbolTable) => {
250
246
  const isAmbiguous = (id: string) =>
251
247
  table.symbols.filter((symbol) => symbol.useThis === id).length > 1;
252
- const renamed = { isAny: false };
248
+ let isRenamed = false;
253
249
  for (let type = 0; type <= PCAL_SYMBOL_TYPE_COUNT; type += 1) {
254
250
  table.symbols.forEach((symbol) => {
255
251
  if (symbol.type !== type) return;
@@ -258,10 +254,10 @@ const disambiguatePcalSymbols = (table: PcalSymbolTable) => {
258
254
  symbol.useThis +=
259
255
  suffixLength === 1 ? "_" : (symbol.context[suffixLength - 2] ?? String(type));
260
256
  }
261
- if (symbol.id !== symbol.useThis) renamed.isAny = true;
257
+ if (symbol.id !== symbol.useThis) isRenamed = true;
262
258
  });
263
259
  }
264
- return renamed.isAny;
260
+ return isRenamed;
265
261
  };
266
262
 
267
263
  const checkForDefaultInitValue = (table: PcalSymbolTable) => {
@@ -283,7 +279,7 @@ const fixPcalIds = (ast: PcalAlgorithm, table: PcalSymbolTable, log: (line: stri
283
279
  const useMe = useThisVar(table, token.string, context);
284
280
  if (names.includes(token.string) || useMe === token.string) return;
285
281
  names.push(token.string);
286
- replacements.push(createNormalizedPcalExpr([[identToken(useMe)]]));
282
+ replacements.push(createNormalizedPcalExpr([[createIdentToken(useMe)]]));
287
283
  }),
288
284
  );
289
285
  if (replacements.length > 0) substitutePcalExpr(expr, replacements, names, false);
@@ -417,15 +413,13 @@ const fixPcalIds = (ast: PcalAlgorithm, table: PcalSymbolTable, log: (line: stri
417
413
  process.name = useThis(table, PCAL_SYMBOL.process, process.name, "");
418
414
  };
419
415
 
420
- // A process calling an unknown procedure stops the fixing of the remaining processes with an
421
- // error message, but not the translation.
422
416
  const fixMultiprocess = (procedures: PcalProcedure[], processes: PcalProcess[]) => {
423
417
  procedures.forEach(fixProcedure);
424
418
  const procedureNames = procedures.map((procedure) => procedure.name);
425
419
  const isCalled = procedures.map((procedure) =>
426
420
  procedureNames.map((name) => procedure.proceduresCalled.includes(name)),
427
421
  );
428
- procedureNames.forEach((_, middle) =>
422
+ procedureNames.forEach((_name, middle) =>
429
423
  isCalled.forEach((row) =>
430
424
  row.forEach((isPath, end) => {
431
425
  row[end] = isPath || (row[middle] && isCalled[middle][end]);
@@ -434,13 +428,12 @@ const fixPcalIds = (ast: PcalAlgorithm, table: PcalSymbolTable, log: (line: stri
434
428
  );
435
429
  procedures.forEach((procedure, index) => {
436
430
  procedure.proceduresCalled = procedureNames.filter(
437
- (_, calleeIndex) => isCalled[index][calleeIndex],
431
+ (_name, calleeIndex) => isCalled[index][calleeIndex],
438
432
  );
439
433
  });
440
434
  for (const process of processes) {
441
435
  fixProcess(process);
442
436
  const called = process.proceduresCalled;
443
- // Callees appended below are themselves visited, as the Java loop rereads the size.
444
437
  for (let calledIndex = 0; calledIndex < called.length; calledIndex += 1) {
445
438
  const procedureIndex = procedureNames.indexOf(called[calledIndex]);
446
439
  if (procedureIndex === -1) {
@@ -465,10 +458,10 @@ const fixPcalIds = (ast: PcalAlgorithm, table: PcalSymbolTable, log: (line: stri
465
458
  }
466
459
  };
467
460
 
468
- const stringToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.string);
469
- const addedToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.added);
470
- const builtInToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.builtin);
471
- const identToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.ident);
461
+ const createStringToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.string);
462
+ const createAddedToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.added);
463
+ const createBuiltInToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.builtin);
464
+ const createIdentToken = (string: string) => createPcalToken(string, 0, PCAL_TOKEN.ident);
472
465
 
473
466
  const tokensToExpr = (tokens: PcalToken[], spaces: number) => {
474
467
  const line: PcalToken[] = [];
@@ -483,29 +476,29 @@ const tokensToExpr = (tokens: PcalToken[], spaces: number) => {
483
476
 
484
477
  const prettifyStackTopExpr = (expr: PcalExpr) =>
485
478
  expr.tokens.forEach((line, lineIndex) => {
486
- const cursor = { column: lineIndex === 0 || lineIndex === expr.tokens.length - 1 ? 1 : 6 };
479
+ let column = lineIndex === 0 || lineIndex === expr.tokens.length - 1 ? 1 : 6;
487
480
  line.forEach((token) => {
488
- token.column = cursor.column;
489
- cursor.column += getPcalTokenWidth(token);
481
+ token.column = column;
482
+ column += getPcalTokenWidth(token);
490
483
  if (token.type !== PCAL_TOKEN.builtin) return;
491
484
  switch (token.string) {
492
485
  case "|->":
493
486
  case "↦":
494
487
  token.column = Math.max(16, token.column + 1);
495
- cursor.column = token.column + 5;
488
+ column = token.column + 5;
496
489
  return;
497
490
  case "[":
498
491
  case "<<":
499
- cursor.column += 1;
492
+ column += 1;
500
493
  return;
501
494
  case "]":
502
495
  case ">>":
503
496
  token.column += 1;
504
- cursor.column += 1;
497
+ column += 1;
505
498
  return;
506
499
  case "\\o":
507
500
  token.column += 1;
508
- cursor.column += 2;
501
+ column += 2;
509
502
  }
510
503
  });
511
504
  });
@@ -540,7 +533,6 @@ const makeAssign = (variable: string, expr: PcalExpr, line = 0, col = 0) =>
540
533
  col,
541
534
  );
542
535
 
543
- // PcalTranslate.IsTRUE indexes the first line of an expression without checking it has one.
544
536
  const isTrue = (expr: PcalExpr) => {
545
537
  if (expr.tokens.length > 1) return false;
546
538
  const line = getElement(expr.tokens, 0);
@@ -551,31 +543,31 @@ const isTrue = (expr: PcalExpr) => {
551
543
  return false;
552
544
  };
553
545
 
554
- const checkPC = (label: string): PcalWhen => ({
546
+ const createPCCheck = (label: string): PcalWhen => ({
555
547
  ...createPcalBase(),
556
548
  kind: "When",
557
- exp: tokensToExpr([addedToken("pc"), builtInToken("="), stringToken(label)], 1),
549
+ exp: tokensToExpr([createAddedToken("pc"), createBuiltInToken("="), createStringToken(label)], 1),
558
550
  });
559
551
 
560
552
  // HACK: PcalSymTab.ProcedureEntry leaves iPC null for a procedure with no statements, which
561
553
  // StringToken keeps and AST.toString prints as "null".
562
- const updatePC = (next: string | undefined) =>
563
- makeAssign("pc", createNormalizedPcalExpr([[stringToken(next ?? "null")]]));
554
+ const createPCUpdate = (next: string | undefined) =>
555
+ makeAssign("pc", createNormalizedPcalExpr([[createStringToken(next ?? "null")]]));
564
556
 
565
557
  const getStackHeadTokens = (field: string) => [
566
- identToken("Head"),
567
- builtInToken("("),
568
- addedToken("stack"),
569
- builtInToken(")"),
570
- builtInToken("."),
571
- identToken(field),
558
+ createIdentToken("Head"),
559
+ createBuiltInToken("("),
560
+ createAddedToken("stack"),
561
+ createBuiltInToken(")"),
562
+ createBuiltInToken("."),
563
+ createIdentToken(field),
572
564
  ];
573
565
 
574
566
  const getStackTailTokens = () => [
575
- identToken("Tail"),
576
- builtInToken("("),
577
- addedToken("stack"),
578
- builtInToken(")"),
567
+ createIdentToken("Tail"),
568
+ createBuiltInToken("("),
569
+ createAddedToken("stack"),
570
+ createBuiltInToken(")"),
579
571
  ];
580
572
 
581
573
  const createStackHeadExpr = (field: string) =>
@@ -589,20 +581,24 @@ const createStackPushExpr = (
589
581
  ) => {
590
582
  const tokens: PcalToken[][] = [
591
583
  [
592
- builtInToken("<<"),
593
- builtInToken("["),
594
- identToken("procedure"),
595
- builtInToken("|->"),
596
- stringToken(procedure),
597
- builtInToken(","),
584
+ createBuiltInToken("<<"),
585
+ createBuiltInToken("["),
586
+ createIdentToken("procedure"),
587
+ createBuiltInToken("|->"),
588
+ createStringToken(procedure),
589
+ createBuiltInToken(","),
598
590
  ],
599
- [identToken("pc"), builtInToken("|->"), ...pcTokens],
591
+ [createIdentToken("pc"), createBuiltInToken("|->"), ...pcTokens],
600
592
  ];
601
593
  variables.forEach((variable) => {
602
- tokens[tokens.length - 1].push(builtInToken(","));
603
- tokens.push([identToken(variable.var), builtInToken("|->"), identToken(variable.var)]);
594
+ tokens[tokens.length - 1].push(createBuiltInToken(","));
595
+ tokens.push([
596
+ createIdentToken(variable.var),
597
+ createBuiltInToken("|->"),
598
+ createIdentToken(variable.var),
599
+ ]);
604
600
  });
605
- tokens[tokens.length - 1].push(builtInToken("]"), builtInToken(">>"));
601
+ tokens[tokens.length - 1].push(createBuiltInToken("]"), createBuiltInToken(">>"));
606
602
  tokens.push(tailTokens);
607
603
  const expr = createPcalExpr(tokens);
608
604
  prettifyStackTopExpr(expr);
@@ -632,7 +628,7 @@ const createCallStmts = (
632
628
  return [
633
629
  assign,
634
630
  ...entry.decls.map((decl) => makeAssign(decl.var, decl.val, call.line, call.col)),
635
- updatePC(entry.initialPC),
631
+ createPCUpdate(entry.initialPC),
636
632
  ];
637
633
  };
638
634
 
@@ -642,14 +638,12 @@ interface PcalExplodedStmts {
642
638
  needsGoto: boolean;
643
639
  }
644
640
 
645
- // Statement lists nested in the original AST are exploded in place, as the Java does, so the
646
- // original AST changes too.
647
641
  const explodePcalAlgorithm = (
648
642
  ast: PcalAlgorithm,
649
643
  table: PcalSymbolTable,
650
- omitPC: boolean,
644
+ shouldOmitPC: boolean,
651
645
  ): PcalAlgorithm => {
652
- const context: { procedure: string | undefined } = { procedure: undefined };
646
+ let currentProcedure: string | undefined;
653
647
 
654
648
  const explodeCall = (call: PcalCall, next: string) => {
655
649
  const procedureIndex = findProcedureIndex(table, call.to);
@@ -659,15 +653,15 @@ const explodePcalAlgorithm = (
659
653
  const entry = table.procedures[procedureIndex];
660
654
  const stackPush = createStackPushExpr(
661
655
  call.to,
662
- [stringToken(next)],
656
+ [createStringToken(next)],
663
657
  [...entry.decls, ...entry.params],
664
- [builtInToken("\\o"), addedToken("stack")],
658
+ [createBuiltInToken("\\o"), createAddedToken("stack")],
665
659
  );
666
660
  return createCallStmts(makeAssign("stack", stackPush, call.line, call.col), entry, call);
667
661
  };
668
662
 
669
663
  const explodeReturn = (stmt: PcalReturn) => {
670
- stmt.from ??= context.procedure;
664
+ stmt.from ??= currentProcedure;
671
665
  const from = stmt.from;
672
666
  if (from === undefined) throw new PcalError("`return' statement not in procedure", stmt);
673
667
  const procedureIndex = findProcedureIndex(table, from);
@@ -703,7 +697,6 @@ const explodePcalAlgorithm = (
703
697
  }
704
698
  const toEntry = table.procedures[toIndex];
705
699
  const assign = createAssign([], stmt.line, stmt.col);
706
- // A tail call to the procedure itself reuses its stack frame.
707
700
  if (from !== stmt.to) {
708
701
  fromEntry.decls.forEach((decl) =>
709
702
  assign.ass.push(
@@ -720,7 +713,7 @@ const explodePcalAlgorithm = (
720
713
  stmt.to,
721
714
  getStackHeadTokens("pc"),
722
715
  [...toEntry.decls, ...toEntry.params],
723
- [builtInToken("\\o"), ...getStackTailTokens()],
716
+ [createBuiltInToken("\\o"), ...getStackTailTokens()],
724
717
  );
725
718
  assign.ass.push(
726
719
  createSingleAssign("stack", createNormalizedPcalExpr([]), stackPush, stmt.line, stmt.col),
@@ -730,7 +723,7 @@ const explodePcalAlgorithm = (
730
723
  };
731
724
 
732
725
  const copyAndExplodeLastStmt = (stmts: PcalStmt[], next: string): PcalExplodedStmts => {
733
- if (stmts.length === 0) return { stmts: [], labeledStmts: [], needsGoto: !omitPC };
726
+ if (stmts.length === 0) return { stmts: [], labeledStmts: [], needsGoto: !shouldOmitPC };
734
727
  const result: PcalExplodedStmts = { stmts, labeledStmts: [], needsGoto: false };
735
728
  const last = stmts[stmts.length - 1];
736
729
  switch (last.kind) {
@@ -745,7 +738,7 @@ const explodePcalAlgorithm = (
745
738
  }
746
739
  case "Goto":
747
740
  stmts.pop();
748
- stmts.push(updatePC(last.to));
741
+ stmts.push(createPCUpdate(last.to));
749
742
  break;
750
743
  case "Call":
751
744
  stmts.pop();
@@ -782,11 +775,11 @@ const explodePcalAlgorithm = (
782
775
  result.labeledStmts.push(...explodedThen.labeledStmts, ...explodedElse.labeledStmts);
783
776
  last.Then = explodedThen.stmts;
784
777
  last.Else = explodedElse.stmts;
785
- if (!omitPC) {
778
+ if (!shouldOmitPC) {
786
779
  result.needsGoto = explodedThen.needsGoto && explodedElse.needsGoto;
787
780
  if (!result.needsGoto) {
788
- if (explodedThen.needsGoto) last.Then.push(updatePC(next));
789
- if (explodedElse.needsGoto) last.Else.push(updatePC(next));
781
+ if (explodedThen.needsGoto) last.Then.push(createPCUpdate(next));
782
+ if (explodedElse.needsGoto) last.Else.push(createPCUpdate(next));
790
783
  }
791
784
  }
792
785
  break;
@@ -799,9 +792,9 @@ const explodePcalAlgorithm = (
799
792
  return exploded;
800
793
  });
801
794
  result.needsGoto = explodedOrs.every((exploded) => exploded.needsGoto);
802
- if (!omitPC && !result.needsGoto) {
795
+ if (!shouldOmitPC && !result.needsGoto) {
803
796
  explodedOrs.forEach((exploded, index) => {
804
- if (exploded.needsGoto) last.ors[index].push(updatePC(next));
797
+ if (exploded.needsGoto) last.ors[index].push(createPCUpdate(next));
805
798
  });
806
799
  }
807
800
  break;
@@ -816,13 +809,13 @@ const explodePcalAlgorithm = (
816
809
  default:
817
810
  result.needsGoto = true;
818
811
  }
819
- if (omitPC) result.needsGoto = false;
812
+ if (shouldOmitPC) result.needsGoto = false;
820
813
  return result;
821
814
  };
822
815
 
823
816
  const copyAndExplodeLastStmtWithGoto = (stmts: PcalStmt[], next: string) => {
824
817
  const exploded = copyAndExplodeLastStmt(stmts, next);
825
- if (exploded.needsGoto) exploded.stmts.push(updatePC(next));
818
+ if (exploded.needsGoto) exploded.stmts.push(createPCUpdate(next));
826
819
  return exploded;
827
820
  };
828
821
 
@@ -831,9 +824,8 @@ const explodePcalAlgorithm = (
831
824
  if (first?.kind === "While") return explodeWhile(labeledStmt, first, next);
832
825
  const exploded = copyAndExplodeLastStmtWithGoto([...labeledStmt.stmts], next);
833
826
  const result: PcalLabeledStmt[] = [];
834
- // With omitPC, PcalTranslate.ExplodeLabeledStmt drops the labeled statement itself.
835
- if (!omitPC) {
836
- exploded.stmts.unshift(checkPC(labeledStmt.label));
827
+ if (!shouldOmitPC) {
828
+ exploded.stmts.unshift(createPCCheck(labeledStmt.label));
837
829
  result.push({
838
830
  ...createPcalBase(),
839
831
  kind: "LabeledStmt",
@@ -874,7 +866,7 @@ const explodePcalAlgorithm = (
874
866
  col: labeledStmt.col,
875
867
  line: labeledStmt.line,
876
868
  label: labeledStmt.label,
877
- stmts: [...(omitPC ? [] : [checkPC(labeledStmt.label)]), ...loopStmts],
869
+ stmts: [...(shouldOmitPC ? [] : [createPCCheck(labeledStmt.label)]), ...loopStmts],
878
870
  };
879
871
  return [
880
872
  newLabeledStmt,
@@ -934,9 +926,9 @@ const explodePcalAlgorithm = (
934
926
  explodeLabeledStmtSeq(body.map(toLabeledStmt), next);
935
927
 
936
928
  const explodeProcedure = (procedure: PcalProcedure): PcalProcedure => {
937
- context.procedure = procedure.name;
929
+ currentProcedure = procedure.name;
938
930
  const body = explodeBody(procedure.body, useThis(table, PCAL_SYMBOL.label, "Error", ""));
939
- context.procedure = undefined;
931
+ currentProcedure = undefined;
940
932
  return {
941
933
  ...createPcalBase(),
942
934
  kind: "Procedure",
@@ -997,9 +989,8 @@ const explodePcalAlgorithm = (
997
989
  };
998
990
  };
999
991
 
1000
- // Only the parts of PcalTLAGen.generate that change the AST or throw.
1001
- const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1002
- const vars = omitPC ? [] : ["pc"];
992
+ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, shouldOmitPC: boolean) => {
993
+ const vars = shouldOmitPC ? [] : ["pc"];
1003
994
  vars.push(...explodedAst.decls.map((decl) => decl.var));
1004
995
  if (explodedAst.prcds.length > 0) vars.push("stack");
1005
996
  explodedAst.prcds.forEach((procedure) => {
@@ -1011,7 +1002,6 @@ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1011
1002
  }
1012
1003
  if (vars.length === 0) throw new PcalError("The algorithm has no variables.");
1013
1004
 
1014
- // Changed.Set counts only the first of duplicate variable names.
1015
1005
  const setChanged = (changed: number[], variable: string) => {
1016
1006
  const index = vars.indexOf(variable);
1017
1007
  if (index === -1) return 0;
@@ -1024,22 +1014,19 @@ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1024
1014
  changed[index] = Math.max(changed[index], count);
1025
1015
  });
1026
1016
 
1027
- // PcalTLAGen.SortSass: a stable sort by variable name, in String.compareTo's UTF-16 order.
1028
- const sortSass = (assigns: PcalSingleAssign[]) =>
1017
+ const sortSingleAssigns = (assigns: PcalSingleAssign[]) =>
1029
1018
  [...assigns].sort((left, right) =>
1030
1019
  left.lhs.var < right.lhs.var ? -1 : left.lhs.var > right.lhs.var ? 1 : 0,
1031
1020
  );
1032
1021
 
1033
- const genAssign = (assign: PcalAssign, changed: number[]) => {
1022
+ const checkAssign = (assign: PcalAssign, changed: number[]) => {
1034
1023
  const thisChanged = [...changed];
1035
- assign.ass = sortSass(assign.ass);
1024
+ assign.ass = sortSingleAssigns(assign.ass);
1036
1025
  new Set(assign.ass.map((single) => single.lhs.var)).forEach((variable) => {
1037
1026
  const group = assign.ass.filter((single) => single.lhs.var === variable);
1038
1027
  if (!vars.includes(variable)) {
1039
1028
  throw new PcalError(`Assignment to undeclared variable ${variable}`, group[0]);
1040
1029
  }
1041
- // GenAssign also notes whether the assignment after the group lacks a subscript, but its
1042
- // loop ends before reading that, so only the group's own assignments count.
1043
1030
  const hasAssignmentWithNoSubscript = group.some(
1044
1031
  (single) => single.lhs.sub.tokens.length === 0,
1045
1032
  );
@@ -1053,16 +1040,16 @@ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1053
1040
  mergeChanged(changed, thisChanged);
1054
1041
  };
1055
1042
 
1056
- const genStmt = (stmt: PcalStmt, changed: number[]): void => {
1043
+ const checkStmt = (stmt: PcalStmt, changed: number[]): void => {
1057
1044
  switch (stmt.kind) {
1058
1045
  case "Assign":
1059
- genAssign(stmt, changed);
1046
+ checkAssign(stmt, changed);
1060
1047
  return;
1061
1048
  case "If": {
1062
1049
  const thenChanged = [...changed];
1063
1050
  const elseChanged = [...changed];
1064
- stmt.Then.forEach((innerStmt) => genStmt(innerStmt, thenChanged));
1065
- stmt.Else.forEach((innerStmt) => genStmt(innerStmt, elseChanged));
1051
+ stmt.Then.forEach((innerStmt) => checkStmt(innerStmt, thenChanged));
1052
+ stmt.Else.forEach((innerStmt) => checkStmt(innerStmt, elseChanged));
1066
1053
  mergeChanged(changed, thenChanged);
1067
1054
  mergeChanged(changed, elseChanged);
1068
1055
  return;
@@ -1071,14 +1058,14 @@ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1071
1058
  const allChanged = [...changed];
1072
1059
  stmt.ors.forEach((orStmts) => {
1073
1060
  const orChanged = [...changed];
1074
- orStmts.forEach((innerStmt) => genStmt(innerStmt, orChanged));
1061
+ orStmts.forEach((innerStmt) => checkStmt(innerStmt, orChanged));
1075
1062
  mergeChanged(allChanged, orChanged);
1076
1063
  });
1077
1064
  mergeChanged(changed, allChanged);
1078
1065
  return;
1079
1066
  }
1080
1067
  case "With":
1081
- stmt.Do.forEach((innerStmt) => genStmt(innerStmt, changed));
1068
+ stmt.Do.forEach((innerStmt) => checkStmt(innerStmt, changed));
1082
1069
  return;
1083
1070
  case "When":
1084
1071
  case "PrintS":
@@ -1090,30 +1077,28 @@ const checkPcalTlaGen = (explodedAst: PcalAlgorithm, omitPC: boolean) => {
1090
1077
  }
1091
1078
  };
1092
1079
 
1093
- const genBody = (body: PcalStmt[]) =>
1080
+ const checkBody = (body: PcalStmt[]) =>
1094
1081
  body.forEach((stmt) => {
1095
1082
  const changed = vars.map(() => 0);
1096
- toLabeledStmt(stmt).stmts.forEach((innerStmt) => genStmt(innerStmt, changed));
1083
+ toLabeledStmt(stmt).stmts.forEach((innerStmt) => checkStmt(innerStmt, changed));
1097
1084
  });
1098
1085
 
1099
- explodedAst.prcds.forEach((procedure) => genBody(procedure.body));
1086
+ explodedAst.prcds.forEach((procedure) => checkBody(procedure.body));
1100
1087
  (explodedAst.kind === "Uniprocess"
1101
1088
  ? [explodedAst.body]
1102
1089
  : explodedAst.procs.map((process) => process.body)
1103
- ).forEach(genBody);
1090
+ ).forEach(checkBody);
1104
1091
  };
1105
1092
 
1106
- // What removeNameConflicts and translate do to the AST, and the errors they report, without the
1107
- // TLA+ translation, which SANY discards.
1108
1093
  export const translatePcalAlgorithm = (
1109
1094
  ast: PcalAlgorithm,
1110
- omitPC: boolean,
1095
+ shouldOmitPC: boolean,
1111
1096
  hasDefaultInitialization: boolean,
1112
1097
  log: (line: string) => void,
1113
1098
  ) => {
1114
1099
  const table = createPcalSymbolTable(ast);
1115
1100
  if (disambiguatePcalSymbols(table)) log("Warning: symbols were renamed.");
1116
1101
  fixPcalIds(ast, table, log);
1117
- checkPcalTlaGen(explodePcalAlgorithm(ast, table, omitPC), omitPC);
1102
+ checkPcalTlaGen(explodePcalAlgorithm(ast, table, shouldOmitPC), shouldOmitPC);
1118
1103
  if (hasDefaultInitialization) checkForDefaultInitValue(table);
1119
1104
  };