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