From e31cc564d5ad9a112a258360f98707aed7c3a619 Mon Sep 17 00:00:00 2001 From: buraq Date: Sun, 13 Jul 2003 11:42:52 +0000 Subject: completed migration. code is now operational --- .../transformer/matching/AlgebraicMatcher.java | 13 +-- .../scalac/transformer/matching/Autom2Scala.java | 11 +- .../transformer/matching/BindingBerrySethi.java | 11 +- .../scalac/transformer/matching/CodeFactory.java | 118 ++++++++++++--------- .../scalac/transformer/matching/DetWordAutom.java | 5 +- .../transformer/matching/LeftTracerInScala.java | 15 +-- .../transformer/matching/PatternMatcher.java | 4 +- .../scalac/transformer/matching/PatternTool.java | 2 +- .../transformer/matching/RightTracerInScala.java | 12 ++- .../transformer/matching/SequenceMatcher.java | 10 +- .../scalac/transformer/matching/SplitNested.java | 6 +- 11 files changed, 121 insertions(+), 86 deletions(-) (limited to 'sources') diff --git a/sources/scalac/transformer/matching/AlgebraicMatcher.java b/sources/scalac/transformer/matching/AlgebraicMatcher.java index 45995c39d2..5cda065dbd 100644 --- a/sources/scalac/transformer/matching/AlgebraicMatcher.java +++ b/sources/scalac/transformer/matching/AlgebraicMatcher.java @@ -18,6 +18,7 @@ import Tree.*; import scalac.transformer.TransMatch.Matcher ; import scalac.util.Name ; +import scalac.util.Names ; //import scalac.ast.printer.TextTreePrinter ; import java.util.Vector ; @@ -238,7 +239,7 @@ public class AlgebraicMatcher extends PatternTool { case Typed(Ident(Name name), Tree tpe): // typed pattern theType = getConstrType( tpe.type ); assert (env != null ) : "env is null"; - if (/*(env != null) &&*/ (name != WILDCARD_N)) + if (/*(env != null) &&*/ (name != Names.WILDCARD)) env.newBoundVar(tree.pos, ((Tree.Typed)tree).expr.symbol(), theType, @@ -249,7 +250,7 @@ public class AlgebraicMatcher extends PatternTool { else return mk.ConstrPat( tree.pos, theType ); case Bind(Name name, Ident(Name id)): // x @ _ - if( id == WILDCARD_N ) { + if( id == Names.WILDCARD ) { env.newBoundVar(tree.pos, tree.symbol(), theType, @@ -258,11 +259,11 @@ public class AlgebraicMatcher extends PatternTool { } throw new ApplicationError("cannot handle "+tree); case Ident(Name name): // pattern without args or variable - if (tree.symbol().isPrimaryConstructor()) + if ((tree.symbol() != null) && tree.symbol().isPrimaryConstructor()) return mk.ConstrPat(tree.pos, theType); else if (name.isVariable()) { assert (env != null ) : "env is null"; - if (/*(env != null) &&*/ (name != WILDCARD_N)) + if (/*(env != null) &&*/ (name != Names.WILDCARD)) env.newBoundVar(tree.pos, tree.symbol(), theType, @@ -583,8 +584,8 @@ public class AlgebraicMatcher extends PatternTool { toTree(node.and), toTree(node.or, selector)).setType(defs.BOOLEAN_TYPE); case VariablePat(Tree tree): - System.out.print("VariablePat"); - System.out.println( tree ); + //System.out.print("VariablePat"); + //System.out.println( tree ); return make.If( selector.pos, cf.Equals(selector, tree), toTree(node.and), diff --git a/sources/scalac/transformer/matching/Autom2Scala.java b/sources/scalac/transformer/matching/Autom2Scala.java index 5c23384948..ba2539ed09 100644 --- a/sources/scalac/transformer/matching/Autom2Scala.java +++ b/sources/scalac/transformer/matching/Autom2Scala.java @@ -9,6 +9,7 @@ import scalac.symtab.Definitions ; import scalac.ast.Tree; import scalac.ast.TreeGen; import scalac.util.Name; +import scalac.util.Names; import Tree.*; import scalac.transformer.TransMatch.Matcher ; @@ -18,7 +19,6 @@ import ch.epfl.lamp.util.Position; public class Autom2Scala { - static final Name WILDCARD_N = Name.fromString("_"); static final Name CURRENT_ELEM = Name.fromString("cur"); final int FAIL = -1; @@ -126,7 +126,6 @@ public class Autom2Scala { funSym,//clazzOwner, 0) .setType( elementType ); - } @@ -278,12 +277,12 @@ public class Autom2Scala { Tree.Empty, handleBody( freeVars )), (CaseDef) cf.make.CaseDef( pat.pos, - cf.make.Ident(pat.pos, WILDCARD_N) - .setSymbol( Symbol.NONE ) + cf.make.Ident(pat.pos, Names.WILDCARD) + //.setSymbol( Symbol.NONE ) .setType(pat.type()), Tree.Empty, - gen.mkBooleanLit( pat.pos, false )) }/*, - false*/ + gen.mkBooleanLit( pat.pos, false )) }, + false ); Tree res = am.toTree().setType( defs.BOOLEAN_TYPE ); return res; diff --git a/sources/scalac/transformer/matching/BindingBerrySethi.java b/sources/scalac/transformer/matching/BindingBerrySethi.java index ede37647c4..9763c389d0 100644 --- a/sources/scalac/transformer/matching/BindingBerrySethi.java +++ b/sources/scalac/transformer/matching/BindingBerrySethi.java @@ -3,6 +3,7 @@ package scalac.transformer.matching ; import scalac.ApplicationError ; import scalac.ast.Tree ; import scalac.util.Name ; +import scalac.util.Names ; import Tree.* ; import java.util.* ; @@ -57,14 +58,14 @@ public class BindingBerrySethi extends BerrySethi { this.varAt.put( i, activeBinders.clone() ); // below @ ? break; case Ident( Name name ): - assert ( name == Name.fromString("_")); + assert ( name == Names.WILDCARD )||( name.toString().indexOf("$") > -1 ) : "found variable label "+name; Vector binders = (Vector) activeBinders.clone(); - - if( name != Name.fromString("_")) { + /* + if( name != Names.WILDCARD) { binders.add( pat.symbol() ); } - + */ this.varAt.put( i, binders ); } @@ -97,7 +98,7 @@ public class BindingBerrySethi extends BerrySethi { public NondetWordAutom automatonFrom( Tree pat, Integer finalTag ) { this.finalTag = finalTag ; - //System.out.println( "enter automatonFrom("+TextTreePrinter.toString(pat)+")"); + //System.out.println( "enter automatonFrom("+ pat +")"); switch( pat ) { case Sequence( Tree[] subexpr ): diff --git a/sources/scalac/transformer/matching/CodeFactory.java b/sources/scalac/transformer/matching/CodeFactory.java index 0633b01c12..033621c810 100644 --- a/sources/scalac/transformer/matching/CodeFactory.java +++ b/sources/scalac/transformer/matching/CodeFactory.java @@ -38,6 +38,14 @@ class CodeFactory extends PatternTool { return defs.getType( Names.scala_Ref ).symbol() ; } + Symbol seqSym() { + return defs.getType( Names.scala_Seq ).symbol() ; + } + + Symbol iteratorSym() { + return defs.getType( Names.scala_Iterator ).symbol() ; + } + Symbol seqListSym() { return defs.getType( Names.scala_List ).symbol() ; } @@ -69,10 +77,20 @@ class CodeFactory extends PatternTool { Symbol seqTraceConsSym() { return defs.getType( Name.fromString( "scala.SeqTraceCons" ) ).symbol(); } + Symbol seqTraceNilSym() { return defs.getType( Name.fromString( "scala.SeqTraceNil" ) ).symbol(); } + Symbol iterableSym() { + return defs.getType( Names.scala_Iterable ).symbol(); + } + + Symbol newIterSym() { + Scope scp = iterableSym().members(); return scp.lookup( Names.elements ); + } + + public CodeFactory( Unit unit, Infer infer ) { super( unit, infer ); } @@ -115,61 +133,52 @@ class CodeFactory extends PatternTool { return result ; } - // `SeqList[ elemType ]' + /** returns `List[ elemType ]' */ Type SeqListType( Type elemType ) { return Type.TypeRef( defs.SCALA_TYPE, seqListSym(), new Type[] { elemType }); } - // `SeqTrace[ elemType ]' + /** returns `SeqTrace[ elemType ]' */ Type SeqTraceType( Type elemType ) { return Type.TypeRef( defs.SCALA_TYPE, seqTraceSym(), new Type[] { elemType }); } - /** `SequenceIterator[ elemType ]' // TODO: Move to TypeFactory - */ + /** returns `Iterator[ elemType ]' */ Type _seqIterType( Type elemType ) { Symbol seqIterSym = defs.getType( Names.scala_Iterator ).symbol(); - return Type.TypeRef( defs.SCALA_TYPE/*PREFIX*/, - seqIterSym(), + return Type.TypeRef( defs.SCALA_TYPE, seqIterSym(), new Type[] { elemType }); } - /** returns code `.elements' - * the parameter needs to have type attribute `Sequence[]' - * it is not checked whether seqObj really has type `Sequence' - */ - Tree newIterator( Tree seqObj ) { - Type elemType = getElemType( seqObj.type() ); - //System.out.println( "elemType:"+elemType ); - - //Tree t1 = gen.Select(seqObj, newIterSym); - - Scope scp = defs.getClass( Names.scala_Iterable ) /* sequenceSym */ - .members(); - Symbol newIterSym = scp.lookup/*Term */( Names.elements ); - - Tree t1 = make.Select( Position.NOPOS, seqObj, newIterSym.name ) /*todo: newIterSym() */ - .setSymbol( newIterSym ) + /** returns `' */ + Tree newIterator( Tree seqObj, Type elemType ) { + Symbol newIterSym = newIterSym(); + Tree t1 = gen.Select( Position.NOPOS, seqObj, newIterSym) .setType( Type.MethodType(new Symbol[] {},_seqIterType( elemType ))); - //System.out.println( "t1.type:"+t1.type() ); - Tree theIterator = gen.Apply(seqObj.pos, t1, Tree.EMPTY_ARRAY) .setType( _seqIterType( elemType ) ); - //System.out.println( "theit.type:"+theIterator.type() ); - return theIterator; } + /** returns code `.elements' + * the parameter needs to have type attribute `Sequence[]' + */ + Tree newIterator( Tree seqObj ) { + return newIterator( seqObj, getElemType_Sequence( seqObj.type() )); + } + + /** FIXME - short type + */ Tree ignoreValue( Type asType ) { if( asType.isSameAs(defs.BYTE_TYPE )) return make.Literal(Position.NOPOS, new Integer( 0 )) @@ -196,20 +205,20 @@ class CodeFactory extends PatternTool { else if( asType.isSameAs(defs.STRING_TYPE )) return make.Literal(Position.NOPOS, "") .setType( defs.STRING_TYPE ); - /** FIX ME FOR THE NEW VERSION - else - return gen.Apply( Null( asType ), - Tree.EMPTY_ARRAY); - */ - return null; // should not happen FIXME + /** FIX ME FOR THE NEW VERSION*/ + else + return /*gen.Apply( */Null( asType )/*, + Tree.EMPTY_ARRAY)*/; + + //throw new ApplicationError("don't know how to handle "+asType); } /** FIX ME FOR THE NEW VERSION - Tree Null( Type asType ) { - return gen.TypeApply(pos, gen.mkId(pos, defs.NULL ), - new Tree[] { gen.mkType(pos, asType) } ); - } - */ + */ + Tree Null( Type asType ) { + return gen.Ident(pos, defs.NULL );/*gen.TypeApply(pos, gen.Ident(pos, defs.NULL ), + new Tree[] { gen.mkType(pos, asType) } );*/ + } // the caller needs to set the type ! Tree _applyNone( Tree arg ) { @@ -242,6 +251,9 @@ class CodeFactory extends PatternTool { /** code `new SeqTraceCons[ elemType ]( state, head, tail )' */ Tree newSeqTraceCons( Integer state, Tree head, Tree tail ) { + assert head != null : "head null"; + assert tail != null : "tail null"; + assert state != null : "state null"; return gen.New( Position.NOPOS, defs.SCALA_TYPE, seqTraceConsSym(), new Type[] { head.type() }, new Tree[] { Int( state ), head, tail }); @@ -293,31 +305,39 @@ class CodeFactory extends PatternTool { new Tree[] { head, tail }); } - /** gets first arg of typeref ( can be Seq, Iterator, whatever ) + /** returns A for T <: Sequence[ A ] */ - Type getElemType( Type seqType ) { - //System.err.println("getElemType("+seqType+")"); - //Symbol seqClass = defs.getType( Name.fromString("scala.Seq") ).symbol(); - //assert seqClass != Symbol.NONE : "did not find Seq"; + Type getElemType_Sequence( Type tpe ) { + //System.err.println("getElemType_Sequence("+tpe.widen()+")"); + Type tpe1 = tpe.widen().baseType( seqSym() ); - //Type seqType1 = seqType.baseType( seqClass ); + if( tpe1 == Type.NoType ) + throw new ApplicationError("arg "+tpe+" not subtype of Sequence[ A ]"); - switch( seqType ) { + return tpe1.typeArgs()[ 0 ]; + } + + /** returns A for T <: Iterator[ A ] + */ + Type getElemType_Iterator( Type tpe ) { + //System.err.println("getElemType_Iterator("+tpe+")"); + + Type tpe1 = tpe.widen().baseType( iteratorSym() ); + + switch( tpe1 ) { case TypeRef(_,_,Type[] args): - assert( args.length==1 ) : "weird type:"+seqType; return args[ 0 ]; default: - //System.err.println("somethings wrong: "+seqType); - return seqType.widen(); + throw new ApplicationError("arg "+tpe+" not subtype of Iterator[ A ]"); } + } /** `it.next()' */ public Tree _next( Tree iter ) { - Type elemType = getElemType( iter.type() ); + Type elemType = getElemType_Iterator( iter.type() ); Symbol nextSym = seqIterSym_next(); - return _applyNone( gen.Select( iter, nextSym )).setType( elemType ); } diff --git a/sources/scalac/transformer/matching/DetWordAutom.java b/sources/scalac/transformer/matching/DetWordAutom.java index 1f81e8c04d..f1e487c7c6 100644 --- a/sources/scalac/transformer/matching/DetWordAutom.java +++ b/sources/scalac/transformer/matching/DetWordAutom.java @@ -599,7 +599,10 @@ public class DetWordAutom extends FiniteAutom { // Renumbering ////System.out.println( "output: a dfa with "+ix+"states"); - assert ( !ix_final.isEmpty() ) : "no final states found"; + + // FIX: empty regular expression (as in "List()") is valid + //assert ( !ix_final.isEmpty() ) : "no final states found"; + ////System.out.println( "final state:"+ix_final); //System.out.println( "indexMap: " +indexMap); diff --git a/sources/scalac/transformer/matching/LeftTracerInScala.java b/sources/scalac/transformer/matching/LeftTracerInScala.java index c56eeee368..86200ae0b8 100644 --- a/sources/scalac/transformer/matching/LeftTracerInScala.java +++ b/sources/scalac/transformer/matching/LeftTracerInScala.java @@ -5,6 +5,7 @@ import scalac.ast.*; import scalac.symtab.*; import Tree.*; import scalac.util.Name; +import scalac.util.Names; import scalac.transformer.TransMatch.Matcher ; @@ -73,10 +74,10 @@ public class LeftTracerInScala extends Autom2Scala { cf.fresh.newName(Name.fromString( helpName )), owner, 0) - .setType( pat.type() ) ; + .setType( cf.SeqListType( pat.type() )) ; ValDef varDef = (ValDef) gen.ValDef( helpVar, - cf.ignoreValue( pat.type() )); + cf.ignoreValue( cf.SeqListType( pat.type() ))); //varDef.kind = Kinds.VAR; helpVarDefs.add( varDef ); return helpVar; @@ -278,7 +279,7 @@ public class LeftTracerInScala extends Autom2Scala { Tree run = callFun( new Tree[] { gen.Ident( pos, emptyAccSym ), - cf.newIterator( selector ), + cf.newIterator( selector, selector.type() ), cf.Int( 0 ) }); run = gen.ValDef( Position.NOPOS, resultSym, run ); @@ -348,12 +349,12 @@ public class LeftTracerInScala extends Autom2Scala { Tree.Empty, handleBody( fv.helpMap )), (CaseDef) cf.make.CaseDef( pat.pos, - cf.make.Ident(pat.pos, WILDCARD_N) - .setSymbol( Symbol.NONE ) + cf.make.Ident(pat.pos, Names.WILDCARD) + //.setSymbol( Symbol.NONE ) FIXED .setType(pat.type()), Tree.Empty, - gen.mkBooleanLit(Position.NOPOS, false)) }/*, - false */ + gen.mkBooleanLit(Position.NOPOS, false)) }, + false ); Tree res = am.toTree().setType( defs.BOOLEAN_TYPE ); //System.out.println("freeVars: "+freeVars); diff --git a/sources/scalac/transformer/matching/PatternMatcher.java b/sources/scalac/transformer/matching/PatternMatcher.java index 0d16eac6bd..c8d2db2cc6 100644 --- a/sources/scalac/transformer/matching/PatternMatcher.java +++ b/sources/scalac/transformer/matching/PatternMatcher.java @@ -348,7 +348,7 @@ public class PatternMatcher extends PatternTool { (header.type.isSubType(getConstrType(tpe.type))) ? mk.DefaultPat(tree.pos, getConstrType(tpe.type)) : mk.ConstrPat(tree.pos, getConstrType(tpe.type)); - if ((env != null) && (name != WILDCARD_N)) + if ((env != null) && (name != Names.WILDCARD)) switch (node) { case ConstrPat(Symbol casted): env.newBoundVar( @@ -371,7 +371,7 @@ public class PatternMatcher extends PatternTool { if (tree.symbol().isPrimaryConstructor()) return mk.ConstrPat(tree.pos, getConstrType(tree.type)); else if (name.isVariable()) { - if ((env != null) && (name != WILDCARD_N)) + if ((env != null) && (name != Names.WILDCARD)) env.newBoundVar( tree.pos, tree.symbol(), diff --git a/sources/scalac/transformer/matching/PatternTool.java b/sources/scalac/transformer/matching/PatternTool.java index 3478721d00..bad82cde60 100644 --- a/sources/scalac/transformer/matching/PatternTool.java +++ b/sources/scalac/transformer/matching/PatternTool.java @@ -32,7 +32,7 @@ abstract class PatternTool { public static final Name SCALA_MATCHERROR_N = Name.fromString("scala.MatchError"); public static final Name MATCHERROR_N = Name.fromString("MatchError"); public static final Name FAIL_N = Name.fromString("fail"); - public static final Name WILDCARD_N = Name.fromString("_"); + //public static final Name WILDCARD_N = Name.fromString("_"); use Names public static final Name LENGTH_N = Name.fromString("length"); public static final Name APPLY_N = Name.fromString("apply"); diff --git a/sources/scalac/transformer/matching/RightTracerInScala.java b/sources/scalac/transformer/matching/RightTracerInScala.java index a83419eb91..b2d242d6eb 100644 --- a/sources/scalac/transformer/matching/RightTracerInScala.java +++ b/sources/scalac/transformer/matching/RightTracerInScala.java @@ -12,6 +12,9 @@ import Scope.SymbolIterator; import scalac.ast.printer.TextTreePrinter ; +import scalac.util.Name ; +import scalac.util.Names ; + import ch.epfl.lamp.util.Position; public class RightTracerInScala extends Autom2Scala { @@ -347,15 +350,18 @@ public class RightTracerInScala extends Autom2Scala { this.helpMap2.put( key, helpMap.get( key )); } + // find this weird ? pattern matcher expects var. symbol for _ pattern FIXME ?!! CANNOT BE TRUE + //Symbol obfuscvble = new TermSymbol(0, Name.fromString("ga$ga$ga$"), _m.owner, 0).setType( pat.type() ); + am.construct( m, new CaseDef[] { (CaseDef) cf.make.CaseDef( pat.pos, pat, Tree.Empty, handleBody( helpMap2 )), (CaseDef) cf.make.CaseDef( pat.pos, - cf.make.Ident(pat.pos, WILDCARD_N) - .setSymbol( Symbol.NONE ) - .setType( pat.type() ), + cf.make.Ident(pat.pos, Names.WILDCARD) + //.setSymbol( Symbol.NONE ) FIXED + .setType( pat.type() ), Tree.Empty, gen.mkBooleanLit( pat.pos, false )) }, true // do binding please diff --git a/sources/scalac/transformer/matching/SequenceMatcher.java b/sources/scalac/transformer/matching/SequenceMatcher.java index ef68b8a909..7a952ce556 100644 --- a/sources/scalac/transformer/matching/SequenceMatcher.java +++ b/sources/scalac/transformer/matching/SequenceMatcher.java @@ -88,8 +88,9 @@ public class SequenceMatcher extends PatternTool { } */ - // translates the det/switching automaton to scala code - + /** translates the det/switching automaton to scala code + * precondition: pat.type() corresponds to element type + */ Tree addBinderToBody( Tree pat, Tree body ) { SplitNested spn = new SplitNested( pat, _m.owner, cf ); @@ -110,7 +111,7 @@ public class SequenceMatcher extends PatternTool { body = nbody; } - Type elementType = cf.getElemType( pat.type() ); + Type elementType = cf.getElemType_Sequence( pat.type() ); BindingBerrySethi build = new BindingBerrySethi(); NondetWordAutom left = build.automatonFrom( pat, new Integer(0) ); @@ -216,6 +217,7 @@ public class SequenceMatcher extends PatternTool { /** constructs a word recognizer from an array of patterns which * should all be SequencePatterns ( no wildcard * ) + * precondition: pat.type corresponds to element type * @param _m Matcher object, holds the result * @param pat the (Sequence) patterns * @param body the bodies @@ -238,7 +240,7 @@ public class SequenceMatcher extends PatternTool { Type seqType = pat[ 0 ].type(); - elementType = cf.getElemType( seqType ); + elementType = cf.getElemType_Sequence( seqType ); NondetWordAutom manyNfa[] = new NondetWordAutom[ pat.length ]; diff --git a/sources/scalac/transformer/matching/SplitNested.java b/sources/scalac/transformer/matching/SplitNested.java index 4f9ee29123..74af71ccc7 100644 --- a/sources/scalac/transformer/matching/SplitNested.java +++ b/sources/scalac/transformer/matching/SplitNested.java @@ -28,7 +28,9 @@ public class SplitNested { return new Tree.Apply( fun, split( trees )) .setType( pat.type() ); - case Sequence(_): // remove nested sequences, make vars + case Sequence( Tree[] trees ): // FIXED like Subseq now// remove nested sequences, make vars + return Tree.Sequence( split( trees )); + /* Name n = cf.fresh.newName("nestseq"); Symbol v = new TermSymbol( 0, n, @@ -41,7 +43,7 @@ public class SplitNested { .setType( v.type() )) .setSymbol( v ) .setType( v.type() ); - + */ case Bind(Name name, Tree subtree): // remove nested sequences, make vars return new Tree.ExtBind(name, split( subtree )) .setType( pat.type() ) -- cgit v1.2.3