summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorburaq <buraq@epfl.ch>2003-10-22 09:29:37 +0000
committerburaq <buraq@epfl.ch>2003-10-22 09:29:37 +0000
commit0590ef07a2afadbf35137847dc9e28ee7d4ab90c (patch)
tree75699285b7902ce621a3f567c1c5415f83ef411a
parent3b888fff88f0402dcdd355c37cfc6309743a7236 (diff)
downloadscala-0590ef07a2afadbf35137847dc9e28ee7d4ab90c.tar.gz
scala-0590ef07a2afadbf35137847dc9e28ee7d4ab90c.tar.bz2
scala-0590ef07a2afadbf35137847dc9e28ee7d4ab90c.zip
cleanup
-rw-r--r--sources/scalac/transformer/matching/CodeFactory.java25
-rw-r--r--sources/scalac/transformer/matching/LeftTracerInScala.java382
-rw-r--r--sources/scalac/transformer/matching/RightTracerInScala.java27
3 files changed, 203 insertions, 231 deletions
diff --git a/sources/scalac/transformer/matching/CodeFactory.java b/sources/scalac/transformer/matching/CodeFactory.java
index 20817acabd..ee501e692f 100644
--- a/sources/scalac/transformer/matching/CodeFactory.java
+++ b/sources/scalac/transformer/matching/CodeFactory.java
@@ -46,11 +46,6 @@ class CodeFactory extends PatternTool {
return result ;
}
- /** returns `List[ elemType ]' */
- Type SeqListType( Type elemType ) {
- return defs.listType(elemType);
- }
-
/** returns `List[ Tuple2[ scala.Int, <elemType> ] ]' */
Type SeqTraceType( Type elemType ) {
return defs.listType(pairType(defs.INT_TYPE, elemType));
@@ -73,26 +68,6 @@ class CodeFactory extends PatternTool {
return newIterator( seqObj, getElemType_Sequence( seqObj.type() ));
}
- /** code `Nil'
- Tree _seqTraceNil( Type elemType ) {
- return newSeqNil( null );
- }
- */
-
- // `SeqCons[ elemType ]'
- /*
-
- Type _seqConsType( Type elemType ) {
- return Type.TypeRef( defs.SCALA_TYPE,
- seqConsSym(),
- new Type[] { elemType });
- }
-
- Tree newSeqNil( Type tpe ) {
- return gen.Select(gen.Ident(pos, defs.SCALA), seqNilSym());
- }
- */
-
// EXPERIMENTAL
Tree newRef( Tree init ) {
//System.out.println( "hello:"+refSym().type() );
diff --git a/sources/scalac/transformer/matching/LeftTracerInScala.java b/sources/scalac/transformer/matching/LeftTracerInScala.java
index debb09821a..a7522c5801 100644
--- a/sources/scalac/transformer/matching/LeftTracerInScala.java
+++ b/sources/scalac/transformer/matching/LeftTracerInScala.java
@@ -32,78 +32,78 @@ public class LeftTracerInScala extends TracerInScala {
Matcher m,
CodeFactory cf ) {
- super( dfa, elementType, m.owner, cf );
- this._m = m;
- this.selector = m.selector;
- //helpMap = new HashMap(); moved up
- helpVarDefs = new Vector();
+ super( dfa, elementType, m.owner, cf );
+ this._m = m;
+ this.selector = m.selector;
+ //helpMap = new HashMap(); moved up
+ helpVarDefs = new Vector();
}
Vector helpVarDefs;
Symbol makeHelpVar( Symbol realVar ) {
- Symbol helpVar = new TermSymbol( pos,
- cf.fresh.newName( realVar.name
- .toString() ),
- owner,
- 0)
- .setType( cf.SeqListType( elementType ) ) ;
-
- helpMap.put( realVar, helpVar );
-
- Tree varDef = gen.ValDef(helpVar, gen.Nil(cf.pos /*cf.newSeqNil elementType*/ ));
- // set mutable flag of symbol helpVar ??
- helpVarDefs.add( varDef );
- return helpVar;
+ Symbol helpVar = new TermSymbol( pos,
+ cf.fresh.newName( realVar.name
+ .toString() ),
+ owner,
+ 0)
+ .setType( defs.listType( elementType ) ) ;
+
+ helpMap.put( realVar, helpVar );
+
+ Tree varDef = gen.ValDef( helpVar, gen.Nil( cf.pos ));
+ // set mutable flag of symbol helpVar ??
+ helpVarDefs.add( varDef );
+ return helpVar;
}
Symbol makeHelpVarSEQ( Tree pat ) {
- String helpName = String.valueOf( pat.hashCode() ); //wicked, in'it ?
- Symbol helpVar =
- new TermSymbol( pos,
- cf.fresh.newName(Name.fromString( helpName )),
- owner,
- 0)
- .setType( cf.SeqListType( pat.type() )) ;
-
- ValDef varDef = (ValDef) gen.ValDef( helpVar,
- gen.mkDefaultValue(cf.pos,
- cf.SeqListType( pat.type() ))
- //cf.ignoreValue( )
- );
- helpVarDefs.add( varDef );
- return helpVar;
+ String helpName = String.valueOf( pat.hashCode() ); //wicked, in'it ?
+ Symbol helpVar =
+ new TermSymbol( pos,
+ cf.fresh.newName(Name.fromString( helpName )),
+ owner,
+ 0)
+ .setType( defs.listType( pat.type() )) ;
+
+ Tree varDef = gen.ValDef( helpVar,
+ gen.mkDefaultValue( cf.pos,
+ defs.listType( pat.type() ))
+ //cf.ignoreValue( )
+ );
+ helpVarDefs.add( varDef );
+ return helpVar;
}
protected void initializeSyms() {
- funSymName = "leftTracer";
+ funSymName = "leftTracer";
- nestedMap = new HashMap();
+ nestedMap = new HashMap();
- super.initializeSyms();
+ super.initializeSyms();
- this.accumSym = newParam("accum") // accumulator
- .setType( _accumType( elementType ));
+ this.accumSym = newParam("accum") // accumulator
+ .setType( _accumType( elementType ));
- this.funSym
- .setType( new Type.MethodType( new Symbol[] {
- accumSym, iterSym, stateSym},
- _accumType( elementType )));
+ this.funSym
+ .setType( new Type.MethodType( new Symbol[] {
+ accumSym, iterSym, stateSym},
+ _accumType( elementType )));
- // 2 do: rename switchresultsym to something else...
+ // 2 do: rename switchresultsym to something else...
- this.resultSym = new TermSymbol( //Kinds.VAR,
- pos,
- cf.fresh.newName("trace"),
- owner,
- 0 )
- .setType( _accumType( elementType ) ) ;
+ this.resultSym = new TermSymbol( //Kinds.VAR,
+ pos,
+ cf.fresh.newName("trace"),
+ owner,
+ 0 )
+ .setType( _accumType( elementType ) ) ;
}
// should throw an exception here really, e.g. MatchError
Tree code_fail() {
- return gen.Ident( accumSym.pos, accumSym );
+ return gen.Ident( accumSym.pos, accumSym );
}
@@ -111,47 +111,47 @@ public class LeftTracerInScala extends TracerInScala {
* returns null if there is no such transition(no translation needed)
*/
Tree code_delta( int i, Label label ) {
- Integer target = dfa.delta( i, label );
- /*
- System.out.println("LeftTracer:calling dfa.delta("+i+","+label+")");
- System.out.println("result: "+target);
- */
- if( target == null )
- return null;
-
- // (optimization) that one is a dead state (does not make sense for tracer)
- /*
- if( target == dfa.nstates - 1 )
- return code_fail();
- */
-
- /*
- Tree newAcc = cf.newSeqTraceCons(new Integer(i),
- currentElem(),
- _ref( accumSym ));
- */
- Tree hd = cf.newPair( gen.mkIntLit(cf.pos, i), currentElem() );
- Tree newAcc = gen.Cons(cf.pos,
- hd.type,
- hd,
- gen.Ident( cf.pos, accumSym ));
-
- return callFun( new Tree[] { newAcc , _iter(), gen.mkIntLit( cf.pos, target )} );
+ Integer target = dfa.delta( i, label );
+ /*
+ System.out.println("LeftTracer:calling dfa.delta("+i+","+label+")");
+ System.out.println("result: "+target);
+ */
+ if( target == null )
+ return null;
+
+ // (optimization) that one is a dead state (does not make sense for tracer)
+ /*
+ if( target == dfa.nstates - 1 )
+ return code_fail();
+ */
+
+ /*
+ Tree newAcc = cf.newSeqTraceCons(new Integer(i),
+ currentElem(),
+ _ref( accumSym ));
+ */
+ Tree hd = cf.newPair( gen.mkIntLit(cf.pos, i), currentElem() );
+ Tree newAcc = gen.Cons(cf.pos,
+ hd.type,
+ hd,
+ gen.Ident( cf.pos, accumSym ));
+
+ return callFun( new Tree[] { newAcc , _iter(), gen.mkIntLit( cf.pos, target )} );
}
public Tree code_body() {
- Tree body = code_fail(); // never reached at runtime.
+ Tree body = code_fail(); // never reached at runtime.
- // state [ nstates-1 ] is the dead state, so we skip it
+ // state [ nstates-1 ] is the dead state, so we skip it
- //`if( state == q ) <code_state> else {...}'
- for( int i = dfa.nstates-2; i >= 0; i-- ) {
- body = code_state( i, body );
- }
+ //`if( state == q ) <code_state> else {...}'
+ for( int i = dfa.nstates-2; i >= 0; i-- ) {
+ body = code_state( i, body );
+ }
- return loadCurrentElem( body );
+ return loadCurrentElem( body );
}
@@ -159,101 +159,101 @@ public class LeftTracerInScala extends TracerInScala {
*/
Tree code_state( int i, Tree elseBody ) {
- Tree runFinished; // holds result of the run
- int finalSwRes;
+ Tree runFinished; // holds result of the run
+ int finalSwRes;
- runFinished = run_finished( i );
+ runFinished = run_finished( i );
- Tree stateBody ; // action(delta) for one particular label/test
+ Tree stateBody ; // action(delta) for one particular label/test
- // default action (fail if there is none)
+ // default action (fail if there is none)
- stateBody = code_delta( i, Label.DefaultLabel);
+ stateBody = code_delta( i, Label.DefaultLabel);
- if( stateBody == null )
- stateBody = code_fail();
- // transitions of state i
+ if( stateBody == null )
+ stateBody = code_fail();
+ // transitions of state i
- HashMap trans = ((HashMap[])dfa.deltaq)[ i ];
+ HashMap trans = ((HashMap[])dfa.deltaq)[ i ];
- for( Iterator labs = ((HashMap)dfa.deltaq( i )).keySet().iterator();
- labs.hasNext() ; ) {
- Object label = labs.next();
- Integer next = (Integer) trans.get( label );
+ for( Iterator labs = ((HashMap)dfa.deltaq( i )).keySet().iterator();
+ labs.hasNext() ; ) {
+ Object label = labs.next();
+ Integer next = (Integer) trans.get( label );
- Tree action = code_delta( i, (Label) label );
+ Tree action = code_delta( i, (Label) label );
- if( action != null ) {
- stateBody = gen.If( _cur_eq( _iter(), (Label) label ),
- action,
- stateBody);
- }
- }
- stateBody = gen.If( cf.Negate( gen.Ident( cf.pos, hasnSym )),
- runFinished,
- stateBody );
- return gen.If( cf.Equals( _state(), gen.mkIntLit(cf.pos, i )),
- stateBody ,
- elseBody );
+ if( action != null ) {
+ stateBody = gen.If( _cur_eq( _iter(), (Label) label ),
+ action,
+ stateBody);
+ }
+ }
+ stateBody = gen.If( cf.Negate( gen.Ident( cf.pos, hasnSym )),
+ runFinished,
+ stateBody );
+ return gen.If( cf.Equals( _state(), gen.mkIntLit(cf.pos, i )),
+ stateBody ,
+ elseBody );
}
Tree[] getTrace() {
- initializeSyms();
- Tree tb = code_body();
- theDefDef = gen.DefDef( this.funSym,
- tb );
+ initializeSyms();
+ Tree tb = code_body();
+ theDefDef = gen.DefDef( this.funSym,
+ tb );
- Vector v = new Vector();
+ Vector v = new Vector();
- v.addAll( helpVarDefs );
+ v.addAll( helpVarDefs );
- //
- // `def leftTracer(...) = ...' the function definition
- v.add( theDefDef );
+ //
+ // `def leftTracer(...) = ...' the function definition
+ v.add( theDefDef );
- Tree emptyAcc = gen.Nil( cf.pos ); //cf._seqTraceNil( elementType );
+ Tree emptyAcc = gen.Nil( cf.pos ); //cf._seqTraceNil( elementType );
- // the valdef is needed, because passing emptyAcc as a parameter
- // results in a ClassCastException at runtime (?!)
+ // the valdef is needed, because passing emptyAcc as a parameter
+ // results in a ClassCastException at runtime (?!)
- Symbol emptyAccSym = new TermSymbol( pos,
- cf.fresh.newName("acc"),
- owner,
- 0 )
- .setType( _accumType( elementType ) ) ;
+ Symbol emptyAccSym = new TermSymbol( pos,
+ cf.fresh.newName("acc"),
+ owner,
+ 0 )
+ .setType( _accumType( elementType ) ) ;
- // `val acc = SeqNil[ elementType ]' init accumulator
- v.add( gen.ValDef( emptyAccSym, emptyAcc) );
+ // `val acc = SeqNil[ elementType ]' init accumulator
+ v.add( gen.ValDef( emptyAccSym, emptyAcc) );
- Tree run = callFun( new Tree[] {
- gen.Ident( pos, emptyAccSym ),
- cf.newIterator( selector, selector.type() ),
- gen.mkIntLit( cf.pos, 0 ) });
+ Tree run = callFun( new Tree[] {
+ gen.Ident( pos, emptyAccSym ),
+ cf.newIterator( selector, selector.type() ),
+ gen.mkIntLit( cf.pos, 0 ) });
- run = gen.ValDef( resultSym, run );
+ run = gen.ValDef( resultSym, run );
- v.add( run );
+ v.add( run );
- // vars...
- for( Iterator it = helpMap.keySet().iterator(); it.hasNext(); ) {
- v.add( bindVar( (Symbol) it.next()) );
- }
+ // vars...
+ for( Iterator it = helpMap.keySet().iterator(); it.hasNext(); ) {
+ v.add( bindVar( (Symbol) it.next()) );
+ }
- /* IF YOU NEED DEBUG OUTPUT AT RUNTIME
- v.add( cf.debugPrintRuntime( "the trace is" ) );
- v.add( cf.debugPrintRuntime( gen.Ident( pos, resultSym ) ) );
- v.add( cf.debugPrintNewlineRuntime( "" ) );
- */
+ /* IF YOU NEED DEBUG OUTPUT AT RUNTIME
+ v.add( cf.debugPrintRuntime( "the trace is" ) );
+ v.add( cf.debugPrintRuntime( gen.Ident( pos, resultSym ) ) );
+ v.add( cf.debugPrintNewlineRuntime( "" ) );
+ */
- Tree res[] = new Tree[ v.size() ];
- int j = 0;
- for( Iterator it = v.iterator(); it.hasNext(); )
- res[ j++ ] = (Tree) it.next();
+ Tree res[] = new Tree[ v.size() ];
+ int j = 0;
+ for( Iterator it = v.iterator(); it.hasNext(); )
+ res[ j++ ] = (Tree) it.next();
- _m.varMap = nestedMap;
+ _m.varMap = nestedMap;
- return res;
+ return res;
}
@@ -261,56 +261,56 @@ public class LeftTracerInScala extends TracerInScala {
// calling the AlgebraicMatcher here
Tree _cur_match( Tree pat ) {
- //System.out.println("calling algebraic matcher on type:"+pat.type);
-
- Matcher m = new Matcher( funSym,
- currentElem(),
- defs.BOOLEAN_TYPE );
-
- if( CollectVariableTraverser.containsBinding( pat )) {
- switch( pat ) {
- case Sequence(Tree[] pats):
- //System.out.println("ouch! v Left");
- Symbol hv = makeHelpVarSEQ( pat );
- nestedMap.put( pat, hv );
- Tree stm = gen.Assign( gen.Ident(Position.FIRSTPOS, hv), currentElem() );
- m.stms = new Tree[2];
- m.stms[0] = stm;
- m.stms[1] = gen.mkBooleanLit(Position.FIRSTPOS, true);
- return gen.mkBlock(m.stms);
- }
- }
-
- HashMap helpMap = FreshVariableTraverser.getVars( pat, owner, cf.fresh );
- //System.out.println("varMap: "+helpMap );
-
- m.varMap = helpMap;
-
- //replaceVars( pat );
-
- am.construct( m, new CaseDef[] {
- cf.gen.CaseDef( pat,
+ //System.out.println("calling algebraic matcher on type:"+pat.type);
+
+ Matcher m = new Matcher( funSym,
+ currentElem(),
+ defs.BOOLEAN_TYPE );
+
+ if( CollectVariableTraverser.containsBinding( pat )) {
+ switch( pat ) {
+ case Sequence(Tree[] pats):
+ //System.out.println("ouch! v Left");
+ Symbol hv = makeHelpVarSEQ( pat );
+ nestedMap.put( pat, hv );
+ Tree stm = gen.Assign( gen.Ident(Position.FIRSTPOS, hv), currentElem() );
+ m.stms = new Tree[2];
+ m.stms[0] = stm;
+ m.stms[1] = gen.mkBooleanLit(Position.FIRSTPOS, true);
+ return gen.mkBlock(m.stms);
+ }
+ }
+
+ HashMap helpMap = FreshVariableTraverser.getVars( pat, owner, cf.fresh );
+ //System.out.println("varMap: "+helpMap );
+
+ m.varMap = helpMap;
+
+ //replaceVars( pat );
+
+ am.construct( m, new CaseDef[] {
+ cf.gen.CaseDef( pat,
gen.mkBooleanLit( cf.pos, true )),
- cf.gen.CaseDef( cf.gen.Ident(pat.pos, defs.PATTERN_WILDCARD),
+ cf.gen.CaseDef( cf.gen.Ident(pat.pos, defs.PATTERN_WILDCARD),
gen.mkBooleanLit( cf.pos, false)) },
false);
- Tree res = am.toTree();
- return res;
+ Tree res = am.toTree();
+ return res;
}
/** return the accumulator + last state
*/
Tree run_finished( int state ) {
- Tree hd = cf.newPair( gen.mkIntLit( cf.pos, state ),
- gen.mkDefaultValue(cf.pos,
- elementType)
- /*cf.ignoreValue( elementType )*/);
- //System.err.println(hd.type);
- return gen.Cons( cf.pos,
- hd.type(),
- hd,
- gen.Ident( cf.pos, accumSym ));
+ Tree hd = cf.newPair( gen.mkIntLit( cf.pos, state ),
+ gen.mkDefaultValue(cf.pos,
+ elementType)
+ /*cf.ignoreValue( elementType )*/);
+ //System.err.println(hd.type);
+ return gen.Cons( cf.pos,
+ hd.type(),
+ hd,
+ gen.Ident( cf.pos, accumSym ));
}
}
diff --git a/sources/scalac/transformer/matching/RightTracerInScala.java b/sources/scalac/transformer/matching/RightTracerInScala.java
index 2be85bc9c4..d071a8295f 100644
--- a/sources/scalac/transformer/matching/RightTracerInScala.java
+++ b/sources/scalac/transformer/matching/RightTracerInScala.java
@@ -108,18 +108,16 @@ public class RightTracerInScala extends TracerInScala {
if( keepType )
helpVar.setType( realVar.type() );
else
- helpVar.setType( cf.SeqListType( elementType ) );
+ helpVar.setType( defs.listType(elementType) );
helpMap.put( realVar, helpVar );
Tree rhs;
if( keepType )
- rhs = gen.mkDefaultValue(cf.pos,
- realVar.type()); //cf.ignoreValue( realVar.type() );
+ rhs = gen.mkDefaultValue( cf.pos, realVar.type() );
else
- rhs = /* cf.newRef( cf.newSeqNil( */ gen.Nil( cf.pos );
- /* ) */;
+ rhs = gen.Nil( cf.pos );
helpVar.flags |= Modifiers.MUTABLE;
Tree varDef = gen.ValDef( helpVar, rhs );
//((ValDef) varDef).kind = Kinds.VAR;
@@ -129,7 +127,7 @@ public class RightTracerInScala extends TracerInScala {
Tree prependToHelpVar( Symbol realVar, Tree elem ) {
Tree hv = refHelpVar( realVar );
- return gen.Assign( hv, gen.Cons(cf.pos, elem.type(), elem, hv));//cf.newSeqCons( elem, hv ));
+ return gen.Assign( hv, gen.Cons( cf.pos, elem.type(), elem, hv ));
/*
return cf.Block(pos,
new Tree [] {
@@ -175,9 +173,7 @@ public class RightTracerInScala extends TracerInScala {
// same as in LeftTracer
Tree code_fail() {
-
return cf.ThrowMatchError( _m.pos, defs.UNIT_TYPE );
-
}
public Tree code_body() {
@@ -191,7 +187,6 @@ public class RightTracerInScala extends TracerInScala {
body = code_state( i, body );
}
-
Tree t3 = gen.If( cf.isEmpty( _iter() ),
run_finished( 0 ),
gen.mkBlock( new Tree[] {
@@ -296,7 +291,7 @@ public class RightTracerInScala extends TracerInScala {
for( Iterator it = helpMap3.keySet().iterator(); it.hasNext(); ) {
Symbol vsym = (Symbol) it.next();
Symbol hv = (Symbol) helpMap3.get( vsym );
- hv.setType( cf.SeqListType( elementType ) ) ;
+ hv.setType( defs.listType( elementType ) ) ;
Tree refv = gen.Ident(Position.FIRSTPOS, vsym);
Tree refhv = gen.Ident(Position.FIRSTPOS, hv);
res[ j++ ] = gen.Assign( refhv, refv );
@@ -318,13 +313,15 @@ public class RightTracerInScala extends TracerInScala {
currentElem(),
defs.BOOLEAN_TYPE );
- // there could be regular expressions under Sequence node, export those later
+ // there could be variables in regular expressions under Sequence node,
+ // export those later
Vector varsToExport = NoSeqVariableTraverser.varsNoSeq( pat );
HashMap freshenMap = new HashMap();
HashMap helpMap3 = new HashMap();
- // "freshening": never use the same symbol more than once (in later invocations of _cur_match)
+ // "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();
@@ -336,9 +333,9 @@ public class RightTracerInScala extends TracerInScala {
helpMap3.put( newSym, helpMap.get( key ));
}
- //System.out.println("RightTracerInScala::freshenMap :"+freshenMap);
- //System.out.println("RightTracerInScala:: -pat :"+pat.toString());
- //System.out.println("RightTracerInScala::varsToExport :"+varsToExport);
+ System.out.println("RightTracerInScala::freshenMap :"+freshenMap);
+ System.out.println("RightTracerInScala:: -pat :"+pat.toString());
+ System.out.println("RightTracerInScala::varsToExport :"+varsToExport);
// "freshening"