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.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +0 -34
- package/dist/index.d.ts +0 -34
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +43 -43
- package/dist/prove.js +47 -47
- package/dist/run.cjs +30 -30
- package/dist/run.js +33 -33
- package/dist/transfer.cjs +38 -38
- package/dist/transfer.js +39 -39
- package/package.json +1 -1
- package/src/ast.ts +5 -0
- package/src/bags.ts +5 -2
- package/src/builtins.ts +8 -1
- package/src/check.ts +193 -128
- package/src/cli/run.ts +2 -0
- package/src/cli/runtime-invariants.ts +7 -8
- package/src/cli/shared.ts +4 -0
- package/src/cli/tlc-arguments.ts +30 -24
- package/src/cli/tlc-usage.ts +3 -2
- package/src/evaluate.ts +102 -26
- package/src/java-overrides.ts +9 -4
- package/src/json-reader.ts +2 -1
- package/src/json.ts +11 -4
- package/src/levels.ts +15 -7
- package/src/lexer.ts +83 -72
- package/src/liveness-checker.ts +106 -77
- package/src/liveness.ts +34 -10
- package/src/parallel.ts +33 -26
- package/src/parser.ts +226 -163
- package/src/pcal/ast.ts +65 -53
- package/src/pcal/parse.ts +191 -144
- package/src/pcal/tokenize.ts +115 -91
- package/src/pcal/translate.ts +98 -83
- package/src/pcal/validate.ts +50 -48
- package/src/pretty-print.ts +6 -6
- package/src/randomization.ts +4 -1
- package/src/resolve.ts +70 -12
- package/src/runtime.ts +18 -11
- package/src/sany/builtins.ts +3 -1
- package/src/sany/generator.ts +127 -74
- package/src/sany/levels.ts +9 -5
- package/src/sany/lexer.ts +44 -40
- package/src/sany/linter.ts +1 -0
- package/src/search.ts +1 -1
- package/src/simulate.ts +59 -50
- package/src/spec-description.ts +34 -23
- package/src/standard-definitions.ts +3 -0
- package/src/standard-modules.ts +12 -1
- package/src/state-graph.ts +8 -1
- package/src/subexpressions.ts +47 -38
- package/src/successors.ts +35 -14
- package/src/symmetry.ts +19 -4
- package/src/tlc/fingerprint.ts +21 -15
- package/src/tlc/jar-data.ts +1 -0
- package/src/tlc/messages.ts +21 -2
- package/src/tlc/order.ts +38 -20
- package/src/tlc/random.ts +30 -18
- package/src/tlc/replay.ts +33 -13
- package/src/tlc/summary.ts +45 -36
- package/src/tlc/tlc-operators.ts +14 -4
- package/src/tlc/tlcext-operators.ts +15 -11
- package/src/tlc/trace-spec.ts +4 -0
- package/src/tlc/value-stream.ts +13 -9
- package/src/tlc/version.ts +1 -0
- package/src/values.ts +181 -74
package/src/pcal/translate.ts
CHANGED
|
@@ -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
|
-
|
|
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)
|
|
261
|
+
if (symbol.id !== symbol.useThis) renamed.isAny = true;
|
|
258
262
|
});
|
|
259
263
|
}
|
|
260
|
-
return
|
|
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([[
|
|
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((
|
|
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
|
-
(
|
|
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
|
|
462
|
-
const
|
|
463
|
-
const
|
|
464
|
-
const
|
|
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
|
-
|
|
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
|
|
554
|
+
const checkPC = (label: string): PcalWhen => ({
|
|
547
555
|
...createPcalBase(),
|
|
548
556
|
kind: "When",
|
|
549
|
-
exp: tokensToExpr([
|
|
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
|
|
555
|
-
makeAssign("pc", createNormalizedPcalExpr([[
|
|
562
|
+
const updatePC = (next: string | undefined) =>
|
|
563
|
+
makeAssign("pc", createNormalizedPcalExpr([[stringToken(next ?? "null")]]));
|
|
556
564
|
|
|
557
565
|
const getStackHeadTokens = (field: string) => [
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
566
|
+
identToken("Head"),
|
|
567
|
+
builtInToken("("),
|
|
568
|
+
addedToken("stack"),
|
|
569
|
+
builtInToken(")"),
|
|
570
|
+
builtInToken("."),
|
|
571
|
+
identToken(field),
|
|
564
572
|
];
|
|
565
573
|
|
|
566
574
|
const getStackTailTokens = () => [
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
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
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
592
|
+
builtInToken("<<"),
|
|
593
|
+
builtInToken("["),
|
|
594
|
+
identToken("procedure"),
|
|
595
|
+
builtInToken("|->"),
|
|
596
|
+
stringToken(procedure),
|
|
597
|
+
builtInToken(","),
|
|
590
598
|
],
|
|
591
|
-
[
|
|
599
|
+
[identToken("pc"), builtInToken("|->"), ...pcTokens],
|
|
592
600
|
];
|
|
593
601
|
variables.forEach((variable) => {
|
|
594
|
-
tokens[tokens.length - 1].push(
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
650
|
+
omitPC: boolean,
|
|
645
651
|
): PcalAlgorithm => {
|
|
646
|
-
|
|
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
|
-
[
|
|
662
|
+
[stringToken(next)],
|
|
657
663
|
[...entry.decls, ...entry.params],
|
|
658
|
-
[
|
|
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 ??=
|
|
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
|
-
[
|
|
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: !
|
|
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(
|
|
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 (!
|
|
785
|
+
if (!omitPC) {
|
|
779
786
|
result.needsGoto = explodedThen.needsGoto && explodedElse.needsGoto;
|
|
780
787
|
if (!result.needsGoto) {
|
|
781
|
-
if (explodedThen.needsGoto) last.Then.push(
|
|
782
|
-
if (explodedElse.needsGoto) last.Else.push(
|
|
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 (!
|
|
802
|
+
if (!omitPC && !result.needsGoto) {
|
|
796
803
|
explodedOrs.forEach((exploded, index) => {
|
|
797
|
-
if (exploded.needsGoto) last.ors[index].push(
|
|
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 (
|
|
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(
|
|
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
|
-
|
|
828
|
-
|
|
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: [...(
|
|
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
|
-
|
|
937
|
+
context.procedure = procedure.name;
|
|
930
938
|
const body = explodeBody(procedure.body, useThis(table, PCAL_SYMBOL.label, "Error", ""));
|
|
931
|
-
|
|
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
|
-
|
|
993
|
-
|
|
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
|
-
|
|
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
|
|
1033
|
+
const genAssign = (assign: PcalAssign, changed: number[]) => {
|
|
1023
1034
|
const thisChanged = [...changed];
|
|
1024
|
-
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
|
|
1056
|
+
const genStmt = (stmt: PcalStmt, changed: number[]): void => {
|
|
1044
1057
|
switch (stmt.kind) {
|
|
1045
1058
|
case "Assign":
|
|
1046
|
-
|
|
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) =>
|
|
1052
|
-
stmt.Else.forEach((innerStmt) =>
|
|
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) =>
|
|
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) =>
|
|
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
|
|
1093
|
+
const genBody = (body: PcalStmt[]) =>
|
|
1081
1094
|
body.forEach((stmt) => {
|
|
1082
1095
|
const changed = vars.map(() => 0);
|
|
1083
|
-
toLabeledStmt(stmt).stmts.forEach((innerStmt) =>
|
|
1096
|
+
toLabeledStmt(stmt).stmts.forEach((innerStmt) => genStmt(innerStmt, changed));
|
|
1084
1097
|
});
|
|
1085
1098
|
|
|
1086
|
-
explodedAst.prcds.forEach((procedure) =>
|
|
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(
|
|
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
|
-
|
|
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,
|
|
1117
|
+
checkPcalTlaGen(explodePcalAlgorithm(ast, table, omitPC), omitPC);
|
|
1103
1118
|
if (hasDefaultInitialization) checkForDefaultInitValue(table);
|
|
1104
1119
|
};
|