-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsemantic_.d
313 lines (309 loc) · 8.94 KB
/
semantic_.d
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import std.algorithm;
import scope_,lexer,expression,declaration;
import util;
Expression[] semantic(Source src,Expression[] exprs,Scope sc){
auto lineZero=src.lineZero;
Expression[][typeof(typeid(Object))] byTid;
foreach(expr;exprs){
// byTid[typeid(expr)]++;// bad error message, report dmd bug
byTid[typeid(expr)]~=expr;
if(!cast(Declaration)expr){
sc.error("toplevel entities must be declarations",expr.loc);
}
}
auto all(T)(){ return byTid.get(typeid(T),[]); }
auto secondLoc(typeof(typeid(Object)) tid){
auto exprs=byTid.get(tid,[]);
return exprs.length?exprs[min(exprs.length-1,1)].loc:lineZero;
}
// TODO: provide help with syntax for missing declarations
if(all!TopologyDecl.length!=1) sc.error("there should be exactly one topology declaration",secondLoc(typeid(TopologyDecl)));
if(all!ParametersDecl.length>1) sc.error("there can be at most one parameters declaration",all!ParametersDecl[1].loc);
if(all!PacketFieldsDecl.length!=1) sc.error("there should be exactly one declaration of packet fields",secondLoc(typeid(PacketFieldsDecl)));
if(all!ProgramsDecl.length!=1) sc.error("there should be exactly one declaration of programs to run on the nodes",secondLoc(typeid(ProgramsDecl)));
if(all!NumStepsDecl.length!=1) sc.error("there should be exactly one declaration of the number of steps to run",secondLoc(typeid(NumStepsDecl)));
if(all!QueryDecl.length<1) sc.error("there should be at least one query declaration",lineZero);
if(all!QueueCapacityDecl.length>1) sc.error("there should be at most one queue capacity declaration",lineZero);
void doSemantic(T)(){
foreach(ref expr;exprs){
auto t=cast(T)expr;
if(!t) continue;
expr=semantic(t,sc);
}
}
doSemantic!TopologyDecl;
doSemantic!ParametersDecl;
doSemantic!PacketFieldsDecl;
doSemantic!FunctionDef;
doSemantic!ProgramsDecl;
doSemantic!NumStepsDecl;
doSemantic!QueueCapacityDecl;
doSemantic!QueryDecl;
doSemantic!PostObserveDecl;
return exprs;
}
Expression semantic(Expression expr,Scope sc){
static void propErr(Expression e1,Expression e2){
if(e1.sstate==SemState.error)
e2.sstate=SemState.error;
}
static Expression finish(Expression e){
if(e.sstate!=SemState.error)
e.sstate=SemState.completed;
return e;
}
if(auto tpl=cast(TopologyDecl)expr){
if(auto tsc=cast(TopScope)sc)
if(!tsc.setTopology(tpl))
tpl.sstate=SemState.error;
return finish(tpl);
}
if(auto prm=cast(ParametersDecl)expr){
foreach(ref param;prm.params){
param=cast(ParameterDecl)semantic(param,sc);
assert(!!param);
propErr(param,prm);
}
return finish(prm);
}
if(auto prm=cast(ParameterDecl)expr){
if(!sc.insert(prm))
prm.sstate=SemState.error;
return finish(prm);
}
if(auto pfld=cast(PacketFieldsDecl)expr){
foreach(ref field;pfld.fields){
field=cast(VarDecl)semantic(field,sc.packetFieldScope());
assert(!!field);
propErr(field,pfld);
}
return finish(pfld);
}
if(auto pd=cast(ProgramsDecl)expr){
foreach(mp;pd.mappings){
if(auto decl=sc.lookup(mp.node)){
if(auto node=cast(NodeDecl)decl){
if(auto decl2=sc.lookup(mp.prg)){
if(auto prg=cast(FunctionDef)decl2){
if(node.prg){
sc.error("program for node '"~node.name.name~"' already declared",mp.loc);
}else node.prg=prg;
}else{
sc.error("not a program",mp.node.loc);
sc.note("declared here",decl.loc);
pd.sstate=SemState.error;
}
}else{
sc.error("undefined identifier",mp.prg.loc);
pd.sstate=SemState.error;
}
}else{
sc.error("not a node in the network",mp.node.loc);
sc.note("declared here",decl.loc);
pd.sstate=SemState.error;
}
}else{
sc.error("undefined identifier",mp.node.loc);
pd.sstate=SemState.error;
}
}
// TODO: ensure each node has a program:
/+if(pd.mappings.length!=tpl.nodes.length){
sc.error("there should be one program declaration for each node",pd.loc);
pd.sstate=SemState.error;
}+/
// TODO: allow a wildcard specifier
return finish(pd);
}
if(auto nsd=cast(NumStepsDecl)expr){
if(auto lit=cast(LiteralExp)nsd.num_steps){
if(lit.lit.type==Tok!"0"){
try{
import std.conv:to;
auto ns=to!int(lit.lit.str);
if(ns>=0) return finish(nsd);
}catch(Exception){}
}
}
sc.error("number of steps should be non-negative integer literal",nsd.num_steps.loc);
nsd.sstate=SemState.error;
return nsd;
}
if(auto qcd=cast(QueueCapacityDecl)expr){
if(auto lit=cast(LiteralExp)qcd.capacity){
if(lit.lit.type==Tok!"0"){
try{
import std.conv:to;
auto qc=to!int(lit.lit.str);
if(qc>=0) return finish(qcd);
}catch(Exception){}
}
}
sc.error("queue capacity should be non-negative integer literal",qcd.capacity.loc);
qcd.sstate=SemState.error;
return qcd;
}
if(auto qd=cast(QueryDecl)expr){
return finish(qd);
}
if(auto pd=cast(PostObserveDecl)expr){
return finish(pd);
}
if(auto vd=cast(VarDecl)expr){
if(!sc.insert(vd))
vd.sstate=SemState.error;
return finish(vd);
}
if(auto fd=cast(FunctionDef)expr){
if(fd.name.name=="scheduler") return finish(fd);
if(!sc.insert(fd)) fd.sstate=SemState.error;
if(!fd.fscope_) fd.fscope_=new FunctionScope(sc,fd);
if(fd.params.length){
if(fd.params.length!=2||fd.params[0].name!="pkt"||fd.params[1].name!="port"){
sc.error("function parameters must be () or (pkt,port)",fd.loc);
fd.sstate=SemState.error;
}
}
if(fd.state) foreach(ref vd;fd.state.vars)
vd=cast(StateVarDecl)semantic(vd,fd.fscope_);
foreach(ref statement;fd.body_.s){
statement=semantic(statement,fd.fscope_);
propErr(statement,fd);
}
return finish(fd);
}
if(auto be=cast(BuiltInExp)expr){
return finish(be);
}
if(auto ae=cast(BinaryExp!(Tok!"="))expr){
ae.e1=semantic(ae.e1,sc);
ae.e2=semantic(ae.e2,sc);
propErr(ae.e1,ae);
propErr(ae.e2,ae);
auto id=cast(Identifier)ae.e1;
if(!id&&!cast(FieldExp)ae.e1){
sc.error("can only assign variables or packet fields",ae.loc);
ae.sstate=SemState.error;
}
if(id && (id.name=="pkt"||id.name=="port")){
sc.error("cannot reassign parameters",ae.loc);
ae.sstate=SemState.error;
}
return finish(ae);
}
if(auto de=cast(BinaryExp!(Tok!":="))expr){
de.e2=semantic(de.e2,sc);
propErr(de.e2,de);
auto id=cast(Identifier)de.e1;
if(!id){
sc.error("left hand side of definition must be identifier",de.loc);
de.sstate=SemState.error;
return de;
}
auto vd=new VarDecl(id);
if(!sc.insert(vd))
de.sstate=SemState.error;
return finish(de);
}
if(auto be=cast(ABinaryExp)expr){
be.e1=semantic(be.e1,sc);
be.e2=semantic(be.e2,sc);
propErr(be.e1,be);
propErr(be.e2,be);
return finish(be);
}
if(auto fld=cast(FieldExp)expr){
fld.e=semantic(fld.e,sc);
propErr(fld.e,fld);
auto id=cast(Identifier)fld.e;
auto fd=sc.getFunction();
if((!id||id.name!="pkt")&&(!fd||!fd.params.length)){
sc.error("can only access fields of 'pkt'",fld.loc);
fld.sstate=SemState.error;
return fld;
}
if(!sc.packetFieldScope().lookup(fld.f)){
fld.sstate=SemState.error;
}
return finish(fld);
}
if(auto id=cast(Identifier)expr){
if(!sc.lookup(id)){
if(auto fd=sc.getFunction()){
if(fd.params.length==2 && id.name=="port" || id.name=="pkt")
return finish(id);
}
sc.error("undefined identifier",id.loc);
id.sstate=SemState.error;
}
return finish(id);
}
if(auto lit=cast(LiteralExp)expr)
return finish(lit);
if(auto ce=cast(CallExp)expr){
//ce.e=semantic(ce.e,sc);
//propErr(ce.e,ce);
foreach(ref arg;ce.args){
arg=semantic(arg,sc);
propErr(arg,ce);
}
if(ce.sstate==SemState.error) return ce;
auto be=cast(BuiltInExp)ce.e;
if((!be||be.which!=Tok!"fwd")&&!isBuiltInDistribution(cast(Identifier)ce.e)){
sc.error("can only call 'fwd' or sampling expression",ce.loc);
ce.sstate=SemState.error;
return ce;
}
if(be&&be.which==Tok!"fwd"&&ce.args.length!=1){
sc.error("expected a single argument to 'fwd'",ce.loc);
ce.sstate=SemState.error;
return ce;
}
return finish(ce);
}
if(auto ite=cast(IteExp)expr){
ite.cond=semantic(ite.cond,sc);
ite.then=cast(CompoundExp)semantic(ite.then,sc);
assert(!!ite.then);
if(ite.othw){
ite.othw=cast(CompoundExp)semantic(ite.othw,sc);
assert(ite.othw);
}
propErr(ite.cond,ite);
propErr(ite.then,ite);
if(ite.othw) propErr(ite.othw,ite);
return finish(ite);
}
if(auto obs=cast(ObserveExp)expr){
obs.e=semantic(obs.e,sc);
propErr(obs.e,obs);
return finish(obs);
}
if(auto ass=cast(AssertExp)expr){
ass.e=semantic(ass.e,sc);
propErr(ass.e,ass);
return finish(ass);
}
if(auto cmp=cast(CompoundExp)expr){
foreach(ref s;cmp.s){
s=semantic(s,sc);
}
return cmp;
}
Expression handleBinary(ref Expression e1,ref Expression e2,Expression e){
e1=semantic(e1,sc);
e2=semantic(e2,sc);
propErr(e1,e), propErr(e2,e);
return finish(e);
}
if(auto be=cast(BinaryExp!(Tok!"=="))expr){
return handleBinary(be.e1,be.e2,be);
}
sc.error("unsupported",expr.loc);
expr.sstate=SemState.error;
return expr;
}
bool isBuiltInDistribution(Identifier id){
if(!id) return false;
return true; // TODO
}