forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolveAnonVar.java
More file actions
94 lines (79 loc) · 2.32 KB
/
ResolveAnonVar.java
File metadata and controls
94 lines (79 loc) · 2.32 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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.definition.Context;
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 ResolveAnonVar {
public static KVariable ANON_VAR = KVariable("_");
private Set<KVariable> vars = new HashSet<>();
void resetVars() {
vars.clear();
}
private Rule resolve(Rule rule) {
resetVars();
gatherVars(rule.body());
gatherVars(rule.requires());
gatherVars(rule.ensures());
return new Rule(
transform(rule.body()),
transform(rule.requires()),
transform(rule.ensures()),
rule.att());
}
private Context resolve(Context context) {
resetVars();
gatherVars(context.body());
gatherVars(context.requires());
return new Context(
transform(context.body()),
transform(context.requires()),
context.att());
}
public K resolveK(K k) {
resetVars();;
gatherVars(k);
return transform(k);
}
public synchronized Sentence resolve(Sentence s) {
if (s instanceof Rule) {
return resolve((Rule) s);
} else if (s instanceof Context) {
return resolve((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) {
return new TransformK() {
@Override
public K apply(KVariable k) {
if (ANON_VAR.equals(k)) {
return newDotVariable();
}
return super.apply(k);
}
}.apply(term);
}
private int counter = 0;
KVariable newDotVariable() {
KVariable newLabel;
do {
newLabel = KVariable("_" + (counter++), Att().add("anonymous"));
} while (vars.contains(newLabel));
vars.add(newLabel);
return newLabel;
}
}