-
Notifications
You must be signed in to change notification settings - Fork 8
/
BIfStatement.java
133 lines (114 loc) · 3.48 KB
/
BIfStatement.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Vector;
/* Package: B AMN */
/******************************
* Copyright (c) 2003,2019 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
public class BIfStatement extends BStatement
{ private BStatement ifpart;
private BStatement elsepart;
private BExpression cond;
public BIfStatement(BExpression cnd,
BStatement ip, BStatement ep)
{ ifpart = ip;
elsepart = ep;
cond = cnd;
}
public BIfStatement(BExpression cnd,
BStatement ip)
{ ifpart = ip;
elsepart = new BBasicStatement("skip");
cond = cnd;
}
public BStatement getIf()
{ return ifpart; }
public BStatement getElse()
{ return elsepart; }
public BExpression getCond()
{ return cond; }
public void setElse(BStatement stat)
{ elsepart = stat; }
public void setIf(BStatement stat)
{ ifpart = stat; }
public String toString()
{ String res = "IF " + cond + "\n" +
" THEN " + ifpart + "\n";
if (elsepart != null)
{ res = res + " ELSE " + elsepart + "\n"; }
res = res + " END";
return res;
}
public Vector wr()
{ Vector res = new Vector();
res.addAll(ifpart.wr());
if (elsepart != null)
{ return VectorUtil.union(res,elsepart.wr()); }
return res;
}
public Vector rd()
{ Vector res = new Vector();
res.addAll(cond.rd());
res = VectorUtil.union(res,ifpart.rd());
if (elsepart != null)
{ return VectorUtil.union(res,elsepart.rd()); }
return res;
}
public void extendWithCase(BStatement stat)
{ if (elsepart == null)
{ elsepart = stat; }
else
{ if (elsepart instanceof BIfStatement)
{ ((BIfStatement) elsepart).extendWithCase(stat); }
else if (stat instanceof BIfStatement)
{ ((BIfStatement) stat).extendWithCase(elsepart);
elsepart = stat;
}
}
}
public BStatement substituteEq(String oldE, BExpression newE)
{ BExpression ncond = null;
BStatement nif = null;
BStatement nelse = null;
if (cond != null)
{ ncond = cond.substituteEq(oldE,newE); }
if (ifpart != null)
{ nif = ifpart.substituteEq(oldE,newE); }
if (elsepart != null)
{ nelse = elsepart.substituteEq(oldE,newE); }
return new BIfStatement(ncond,nif,nelse);
}
public BStatement simplify()
{ BExpression ncond = null;
BStatement nif = null;
BStatement nelse = null;
if (cond != null)
{ ncond = cond.simplify(); }
if (ifpart != null)
{ nif = ifpart.simplify(); }
if (elsepart != null)
{ nelse = elsepart.simplify(); }
return new BIfStatement(ncond,nif,nelse);
}
public BStatement normalise()
{ BStatement nif = null;
BStatement nelse = null;
if (ifpart != null)
{ nif = ifpart.normalise(); }
if (elsepart != null)
{ nelse = elsepart.normalise(); }
return new BIfStatement(cond,nif,nelse);
}
public BStatement seq2parallel()
{ BStatement nif = null;
BStatement nelse = null;
if (ifpart != null)
{ nif = ifpart.seq2parallel(); }
if (elsepart != null)
{ nelse = elsepart.seq2parallel(); }
return new BIfStatement(cond,nif,nelse);
}
}