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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/**
* $Id$
*/
package scalac.transformer.matching ;
import scalac.*;
import scalac.ast.*;
import scalac.symtab.*;
import Tree.*;
import java.util.* ;
import Scope.SymbolIterator;
import scalac.util.Name ;
import scalac.util.Names ;
import scala.tools.util.Position;
public class RightTracerInScala extends TracerInScala {
Set seqVars;
Set allVars;
Set varsToExport ;
Symbol targetSym;
HashMap helpMap;
HashMap helpMap2 ;
TreeList helpVarDefs;
/** translate right tracer to code
* @param dfa determinized left tracer
* @param left nondeterm. left tracer (only needed for variables!)
* @param cf ...
* @param pat ?
* @param elementType ...
*/
public RightTracerInScala( DetWordAutom dfa,
Set seqVars,
Symbol owner,
CodeFactory cf,
Tree pat,
Type elementType ) {
super( dfa, elementType, owner, cf );
this.seqVars = seqVars; // HANDLE THESE GLOBALLY INSTEAD OF LOCALLY
this.allVars = CollectVariableTraverser.collectVars( pat );
this.varsToExport = new HashSet();
varsToExport.addAll( allVars );
varsToExport.removeAll( seqVars );
this.helpMap = new HashMap();
helpMap2 = new HashMap();
helpVarDefs = new TreeList();
for( Iterator it = seqVars.iterator(); it.hasNext(); ) {
makeHelpVar( (Symbol) it.next() );
}
for( Iterator it = allVars.iterator(); it.hasNext(); ) {
Symbol varSym = (Symbol) it.next();
if(( varSym.name.toString().indexOf("$") == -1 )
&& ( !seqVars.contains( varSym ))) {
makeHelpVar( varSym, true );
}
}
//System.out.println("allVars: "+allVars);
//System.out.println("seqVars: "+seqVars);
//System.out.println("helpVarDefs now: "+helpVarDefs);
initializeSyms();
}
void makeHelpVar( Symbol realVar ) {
makeHelpVar( realVar, false );
}
/** makes a helpvar and puts mapping into helpMap, ValDef into helpVarDefs
*/
void makeHelpVar( Symbol realVar, boolean keepType ) {
Symbol helpVar = owner.newVariable( cf.pos,
0,
cf.fresh.newName( realVar.name
.toString()+"RTIS" ));
Tree rhs;
//System.out.println("RTiS making helpvar : "+realVar+" -> "+helpVar);
if( keepType ) {
helpVar.setType( realVar.type() );
rhs = gen.mkDefaultValue( cf.pos, realVar.type() );
} else {
helpVar.setType( defs.LIST_TYPE(elementType) );
rhs = gen.mkNil( cf.pos );
}
helpMap.put( realVar, helpVar );
helpVar.flags |= Modifiers.MUTABLE;
Tree varDef = gen.ValDef( helpVar, rhs );
//((ValDef) varDef).kind = Kinds.VAR;
helpVarDefs.append( varDef );
}
Tree prependToHelpVar( Symbol realVar, Tree elem ) {
Tree hv = refHelpVar( realVar );
return gen.Assign( hv, gen.mkNewCons( cf.pos, elementType, elem, hv ));
/*
return cf.Block(pos,
new Tree [] {
cf.debugPrintRuntime( "ASSIGN" ),
gen.Assign( hv, cf.newSeqCons( elem, hv ))
}, defs.UNIT_TYPE());
*/
}
protected void initializeSyms() {
this.funSym = owner.newLabel( pos,
cf.fresh.newName( "right" ));
this.iterSym = owner.newVariable( pos,
Modifiers.MUTABLE,
cf.fresh.newName("iter"))
.setType( cf.SeqTraceType( elementType ));
this.stateSym = owner.newVariable ( pos,
Modifiers.MUTABLE,
cf.fresh.newName("q"))
.setType( defs.int_TYPE() ) ;
this.curSym = owner.newVariable( pos,
Modifiers.MUTABLE,
cf.fresh.newName("cur"))
.setType( elementType ) ;
this.targetSym = owner.newVariable( pos,
Modifiers.MUTABLE,
cf.fresh.newName("p"))
.setType( defs.int_TYPE() ) ;
funSym.setType( new Type.MethodType( new Symbol[] { // dummy symbol MethodType
funSym.newVParam( pos, 0, cf.fresh.newName("iter"), cf.SeqTraceType( elementType )),
funSym.newVParam( pos, 0, cf.fresh.newName( "q" ), defs.int_TYPE()) },
defs.void_TYPE() )); // result
}
// load current elem and trace
Tree loadCurrentElem( Tree body ) {
return
gen.If( cf.isEmpty( _iter() ),
run_finished( 0 ), // we are done
gen.mkBlock( new Tree[] {
gen.ValDef( this.targetSym,
cf.SeqTrace_headState( gen.Ident( pos, iterSym))),
gen.ValDef( this.curSym,
cf.SeqTrace_headElem( gen.Ident( pos, iterSym )))},
body )
);
}
/** see code_state0_NEW
*/
Tree code_state0( Tree elseBody ) { // careful, map Int to Int
return gen.If( cf.Equals( _state(), gen.mkIntLit( cf.pos, 0 )),
code_state0_NEW(),
elseBody );
}
/** this one is special, we check the first element of the trace
* and choose the next state depending only on the state part
*/
Tree code_state0_NEW() { // careful, map Int to Int
HashMap hmap = (HashMap) dfa.deltaq( 0 ); // all the initial states
int i = 0;
final int n = hmap.keySet().size(); // all transitions defined
TreeMap tmapTag = new TreeMap();
TreeMap tmapBody = new TreeMap();
for( Iterator it = hmap.keySet().iterator(); it.hasNext(); i++) {
Integer targetL = (Integer) it.next();
Integer targetR = (Integer) hmap.get( targetL );
Integer I = new Integer( i );
tmapTag.put( targetL, I );
tmapBody.put( I, callFun( new Tree[] {
cf.SeqTrace_tail( _iter() ),
gen.mkIntLit( cf.pos, targetR.intValue() ) }));
}
i = 0;
int[] tags = new int[ n ];
Tree[] targets = new Tree[ n ];
for( Iterator it = tmapTag.keySet().iterator(); it.hasNext(); i++) {
Integer tagI = (Integer) it.next();
tags[ i ] = tagI.intValue();
Integer I = (Integer) tmapTag.get( tagI );
targets[ i ] = (Tree) tmapBody.get( I );
}
return gen.Switch( gen.Ident( pos, targetSym ),
tags,
targets,
code_error()/*cannot happen*/ );
}
Tree currentMatches( Label label ) {
switch( label ) {
case Pair( Integer target, Label theLab ):
return cf.Equals( gen.mkIntLit( cf.pos, target.intValue() ),
current() );
}
throw new ApplicationError("expected Pair label");
}
Tree code_state_NEW( int i ) { // precondition i != 0
Tree stateBody = code_delta( i, Label.DefaultLabel );
if( stateBody == null )
stateBody = code_error();
HashMap trans = ((HashMap[])dfa.deltaq)[ i ];
TreeMap tmapTag = new TreeMap();
TreeMap tmapBody = new TreeMap();
int j = 0;
for( Iterator labs = dfa.labels.iterator(); labs.hasNext(); j++) {
Object label = labs.next();
Integer next = (Integer) trans.get( label );
Tree action = code_delta( i, (Label) label );
if( action != null ) {
Integer J = new Integer( j );
tmapTag.put( ((Label.Pair) label).state, J );
tmapBody.put( J, action );
stateBody = gen.If( currentMatches((Label) label ),
action,
stateBody);
}
}
final int n = tmapTag.keySet().size();
j = 0;
int[] tags = new int[ n ];
Tree[] targets = new Tree[ n ];
for( Iterator it = tmapTag.keySet().iterator(); it.hasNext(); j++) {
Integer tagI = (Integer) it.next();
tags[ j ] = tagI.intValue();
Integer J = (Integer) tmapTag.get( tagI );
targets[ j ] = (Tree) tmapBody.get( J );
}
if( n > 0 ) {
return gen.Switch( gen.Ident( pos, targetSym ), tags, targets, code_error() );
} else
return code_error();
}
// calling the AlgebraicMatcher here
Tree _cur_match( Tree pat ) {
//System.out.println("RTiS._cur_match("+pat.toString()+")");
//System.out.println("calling algebraic matcher on type:"+pat.type);
//System.err.println( "curT"+currentElem().type().widen() );
PartialMatcher m = new PartialMatcher( owner,//funSym,//this.funSym,
currentElem(),
defs.boolean_TYPE() );
final HashMap freshenMap = new HashMap(); // sym2exp -> new sym
HashMap helpMap3 = new HashMap(); // new sym -> original sym
// "freshening": never use the same symbol more than once
// (in later invocations of _cur_match)
for( Iterator it = varsToExport.iterator(); it.hasNext(); ) {
Symbol key = (Symbol) it.next();
if( key.name.toString().indexOf("$") == -1 ) {
this.helpMap2.put( key, helpMap.get( key ));
// "freshening" by appending string ( a bit dangerous )
Symbol newSym = key.cloneSymbol().setOwner( owner /*funSym*/ );
newSym.name = Name.fromString( key.name + "%" );
freshenMap.put( key, newSym );
helpMap3.put( newSym, helpMap.get( key ));
//System.out.println( "key: "+ key + " key.owner:"+key.owner());
//System.out.println( "newsym owner:"+newSym.owner());
} else {
freshenMap.put( key, key );
}
}
//System.out.println("RightTracerInScala:: -pat :"+pat.toString());
/*
System.out.println("RightTracerInScala - the seqVars"+seqVars);
System.out.println("RightTracerInScala - the varsToExport"+varsToExport);
*/
//System.out.println("RightTracerInScala::freshenMap :"+freshenMap);
// "freshening"
/* TEST
*/
TreeCloner tc = new TreeCloner( cf.unit.global, freshenMap, Type.IdMap );
/*
Transformer tc = new Transformer(cf.unit.global) {
public Tree transform(Tree tree) {
tree = super.transform(tree);
if (tree.hasSymbol()) {
Object symbol = freshenMap.get(tree.symbol());
if (symbol != null) tree.setSymbol((Symbol)symbol);
}
return tree;
}
};
*/
pat = tc.transform( pat );
//System.out.println("RightTracerInScala:: -pat( after subst ) :"+pat);
// val match { case <pat> => { <do binding>; true }
// case _ => false
Tree ts[] = new Tree[ helpMap3.keySet().size() ];
int j = 0;
for( Iterator it = helpMap3.keySet().iterator(); it.hasNext(); ) {
Symbol vsym = (Symbol) it.next();
Symbol hv = (Symbol) helpMap3.get( vsym );
//hv.setType( defs.LIST_TYPE( elementType ) ) ; DEBUG ALARM ?
Tree refv = gen.Ident(cf.pos, vsym);
Tree refhv = gen.Ident(cf.pos, hv);
ts[ j++ ] = gen.Assign( refhv, refv );
// System.out.println( "the assign" + res[ j - 1 ] );
}
Tree res = gen.mkBooleanLit( cf.pos, true ); // just `true'
Tree theBody = gen.mkBlock(ts, res);
am.construct( m, new CaseDef[] {
cf.gen.CaseDef( pat, // if tree val matches pat -> update vars, return true
theBody/* "freshening */),
cf.gen.CaseDef( cf.gen.Ident( pat.pos, defs.PATTERN_WILDCARD ),
gen.mkBooleanLit( pat.pos, false )) }, // else return false
true // do binding please
);
return am.toTree();
}
/** returns translation of transition with label from i.
* returns null if there is no such transition(no translation needed)
*/
Tree code_delta( int i, Label label ) {
HashMap hmap = (HashMap) dfa.deltaq( i );
Integer ntarget = (Integer) hmap.get( label );
Tree algMatchTree = null;
if( ntarget == null )
return null;
//System.out.println("delta("+i+","+label+")" );
Label theLab = null;
switch(label) {
case Label.Pair( Integer state, Label lab2 ):
//assert ntarget == state;
theLab = lab2;
switch( lab2 ) {
case TreeLabel( Tree pat ):
algMatchTree = _cur_match( pat );
break;
}
break;
case DefaultLabel:
throw new ApplicationError(); // should not happen
}
assert dfa.qbinders != null : "qbinders ?";
Vector vars = dfa.qbinders[ i ];
//System.out.println("dfa.qbinders[ i ]"+vars);
if( vars == null ) vars = new Vector(); // TODO: make this more consistent
assert vars != null;
Tree stms[] = new Tree[ vars.size()
+ ((algMatchTree != null )? 1 : 0 ) ];
int j = 0;
for( Iterator it = vars.iterator(); it.hasNext(); ) {
Symbol var = (Symbol) it.next();
Tree rhs = gen.Ident( pos, curSym );
stms[ j++ ] = prependToHelpVar( var , rhs);
}
if( algMatchTree != null )
stms[ j++ ] = algMatchTree ;
Tree value = callFun( new Tree[] { cf.SeqTrace_tail( _iter() ),
gen.mkIntLit( cf.pos, ntarget.intValue() ) } );
return gen.mkBlock( pos, stms, value );
}
Tree stateWrap(int i) {
if( i == 0 )
return code_state0_NEW();
return code_state_NEW( i );
}
/* returns statements that do the work of the right-transducer
*/
Tree getStms( Tree trace, CompilationUnit unit, Tree body ) {
TreeList stms = new TreeList();
Tree loopbody = code_body_NEW();
stms.append( gen.ValDef( iterSym, trace ) );
stms.append( gen.ValDef( stateSym, gen.mkIntLit( cf.pos, 0 ) ) );
stms.append( helpVarDefs );
stms.append( gen.LabelDef( this.funSym,
new Ident[] {
gen.Ident( pos, iterSym ),
gen.Ident( pos, stateSym )
}, loopbody ));
// bind variables handled by this righttracer
for( Iterator it = seqVars.iterator(); it.hasNext(); ) {
stms.append( bindVar( (Symbol) it.next() ) );
}
Transformer treeCloner = new Transformer(unit.global) {
public Tree transform(Tree tree) {
tree = super.transform(tree);
if (tree.hasSymbol()) {
Object symbol = helpMap2.get(tree.symbol());
if (symbol != null) tree.setSymbol((Symbol)symbol);
}
return tree;
}
};
return gen.mkBlock(stms.toArray(), treeCloner.transform( body ));
}
/** return the accumulator. (same as in LeftTracerInScala)
* todo: move tree generation of Unit somewhere else
*/
Tree run_finished( int state ) {
return gen.mkUnitLit(Position.FIRSTPOS);
}
Tree current() { return gen.Ident( pos, targetSym );}
Tree refHelpVar( Symbol realVar ) {
Symbol hv = (Symbol)helpMap.get( realVar );
assert hv != null : realVar;
return gen.Ident(Position.FIRSTPOS, hv);
}
Tree assignToHelpVar( Symbol realVar, Tree rhs ) {
Tree hv = refHelpVar( realVar );
return gen.Assign( hv, rhs );
}
Tree bindVar(Symbol realVar) {
Tree hv = refHelpVar( realVar );
/*
System.out.println("binding realVar.name "+realVar.name+" type:"+realVar.type()+" to hv type:"+hv.type());
realVar.setOwner( owner );
System.out.println("is same as realVar"+realVar.type().isSameAs( elementType ));
System.out.println("is same as hv"+realVar.type().isSameAs( hv.type() ));
if( realVar.type().isSameAs( elementType ))
return gen.ValDef( realVar, cf.SeqList_head( hv ));
else
return gen.ValDef( realVar, hv );
*/
if( realVar.type().isSameAs( hv.getType())) {
return gen.ValDef( realVar, hv ); // e.g. x @ _*
}
return gen.ValDef( realVar, cf.SeqList_head( hv ));
}
}
|