forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGuardOrPatterns.java
More file actions
115 lines (99 loc) · 3.13 KB
/
GuardOrPatterns.java
File metadata and controls
115 lines (99 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2019 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.builtin.KLabels;
import org.kframework.builtin.Sorts;
import org.kframework.definition.Context;
import org.kframework.definition.Module;
import org.kframework.definition.Production;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kore.*;
import java.util.HashSet;
import java.util.Set;
import static org.kframework.kore.KORE.*;
public class GuardOrPatterns {
private Set<KVariable> vars = new HashSet<>();
private final boolean kore;
public GuardOrPatterns(boolean kore) {
this.kore = kore;
}
void resetVars() {
vars.clear();
}
private Rule resolve(Module m, Rule rule) {
resetVars();
gatherVars(rule.body());
gatherVars(rule.requires());
gatherVars(rule.ensures());
return new Rule(
transform(rule.body(), m),
transform(rule.requires(), m),
transform(rule.ensures(), m),
rule.att());
}
private Context resolve(Module m, Context context) {
resetVars();
gatherVars(context.body());
gatherVars(context.requires());
return new Context(
transform(context.body(), m),
transform(context.requires(), m),
context.att());
}
public K resolveK(Module m, K k) {
resetVars();;
gatherVars(k);
return transform(k, m);
}
public synchronized Sentence resolve(Module m, Sentence s) {
if (s instanceof Rule) {
return resolve(m, (Rule) s);
} else if (s instanceof Context) {
return resolve(m, (Context) s);
} else {
return s;
}
}
void gatherVars(K term) {
new VisitK() {
@Override
public void apply(KVariable v) {
vars.add(v);
super.apply(v);
}
}.apply(term);
}
K transform(K term, Module m) {
return new TransformK() {
@Override
public K apply(KApply k) {
if (k.klabel().head().equals(KLabels.ML_OR)) {
if (kore) {
AddSortInjections inj = new AddSortInjections(m);
return KAs(k, newDotVariable(inj.sort(k, Sorts.K())));
} else {
return KAs(k, newDotVariable(k.items().get(1).att().get(Production.class).sort()));
}
}
return super.apply(k);
}
@Override
public K apply(KAs k) {
return k;
}
@Override
public K apply(KRewrite k) {
return KRewrite(k.left(), apply(k.right()), k.att());
}
}.apply(term);
}
private int counter = 0;
KVariable newDotVariable(Sort s) {
KVariable newLabel;
do {
newLabel = KVariable("_" + (counter++), Att().add("anonymous").add(Sort.class, s));
} while (vars.contains(newLabel));
vars.add(newLabel);
return newLabel;
}
}