forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddTopCellToRules.java
More file actions
87 lines (76 loc) · 2.73 KB
/
AddTopCellToRules.java
File metadata and controls
87 lines (76 loc) · 2.73 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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.compile.ConfigurationInfo;
import org.kframework.compile.LabelInfo;
import org.kframework.definition.Context;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kil.Attribute;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.KLabel;
import org.kframework.kore.KRewrite;
import static org.kframework.kore.KORE.*;
/**
* This pass adds the implicit top and k cells to
* the bodies of rules and contexts.
* A K cell is added only if the body is a single item,
* which is not already a cell or a rewrite on cells.
* The top cell is added unless the body is already an
* instance of the top cell.
* Rules with the anywhere attribute are not modified.
*/
// TODO: rules defining functions shouldn't be wrapped
public class AddTopCellToRules {
private final ConfigurationInfo cfg;
private final LabelInfo labelInfo;
public AddTopCellToRules(ConfigurationInfo cfg, LabelInfo labelInfo) {
this.cfg = cfg;
this.labelInfo = labelInfo;
}
public K addImplicitCells(K term) {
if (labelInfo.isFunction(term)) return term;
return addRootCell(term);
}
protected K addRootCell(K term) {
KLabel root = cfg.getCellLabel(cfg.getRootCell());
if (term instanceof KApply && ((KApply) term).klabel().equals(root)) {
return term;
} else if (term instanceof KRewrite) {
KRewrite rew = (KRewrite) term;
K left = addRootCell(rew.left());
if (left == rew.left()) {
return KRewrite(rew.left(), rew.right());
} else {
return IncompleteCellUtils.make(root, true, term, true);
}
} else {
return IncompleteCellUtils.make(root, true, term, true);
}
}
public Rule addImplicitCells(Rule rule) {
return new Rule(
addImplicitCells(rule.body()),
rule.requires(),
rule.ensures(),
rule.att());
}
public Context addImplicitCells(Context context) {
return new Context(
addImplicitCells(context.body()),
context.requires(),
context.att());
}
public Sentence addImplicitCells(Sentence s) {
if (s.att().contains(Attribute.MACRO_KEY) || s.att().contains(Attribute.ANYWHERE_KEY)) {
return s;
}
if (s instanceof Rule) {
return addImplicitCells((Rule) s);
} else if (s instanceof Context) {
return addImplicitCells((Context) s);
} else {
return s;
}
}
}