Changeset 417


Ignore:
Timestamp:
Jun 7, 2010 1:51:28 PM (14 years ago)
Author:
Anya Helene Bagge
Message:

mer pattern matching

Location:
trunk/MagnoliaPlugin/src/org/magnolialang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/commands/SelectConstruct.java

    r414 r417  
    99import org.eclipse.imp.editor.UniversalEditor;
    1010import org.eclipse.imp.pdb.facts.IConstructor;
     11import org.eclipse.imp.pdb.facts.IMap;
    1112import org.eclipse.imp.pdb.facts.ISourceLocation;
    1213import org.eclipse.imp.pdb.facts.IString;
     
    2324import org.magnolialang.parsetree.TreeImplodeVisitor;
    2425import org.magnolialang.parsetree.TreeImploder;
     26import org.magnolialang.parsetree.XaTreeAdapter;
    2527import org.magnolialang.parsetree.XaTreeFactory;
     28import org.rascalmpl.values.ValueFactoryFactory;
    2629import org.rascalmpl.values.uptr.TreeAdapter;
    2730
     
    5760       
    5861                               
    59                                 System.out.println(imploded.toString().substring(0, 200));
    60                                 System.out.println(imploded2.toString().substring(0, 200));
     62                                System.out.println(imploded.toString().substring(0, Math.min(200,imploded.toString().length())));
     63                                System.out.println(imploded2.toString().substring(0, Math.min(200,imploded2.toString().length())));
    6164                                System.out.println("Imploded equality: " + imploded.isEqual(imploded2));
    6265                                System.out.println("Imploded Java equality: " + imploded.equals(imploded2));
    6366                                System.out.println("Imploded term equality: " + imploded.toString().equals(imploded2.toString()));
     67                               
     68                                IMap m = XaTreeAdapter.match(imploded, imploded2);
     69                                IValue pattern = XaTreeFactory.cons("Apply", "Expr", ValueFactoryFactory.getValueFactory().list(XaTreeFactory.termvar("f", "Fun"), XaTreeFactory.termvar("as", "Expr")));
     70                                m = XaTreeAdapter.match(pattern, imploded);
    6471                                //String up1 = ((IString)RascalInterpreter.getInstance().call("unparse", "import XaTree;", imploded)).getValue();
    6572                                //String up2 = ((IString)RascalInterpreter.getInstance().call("unparse", "import XaTree;", imploded2)).getValue();
  • trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeAdapter.java

    r416 r417  
    55import org.eclipse.imp.pdb.facts.type.TypeFactory;
    66import org.eclipse.imp.pdb.facts.type.TypeStore;
     7import org.eclipse.imp.pdb.facts.visitors.BottomUpVisitor;
     8import org.eclipse.imp.pdb.facts.visitors.IValueVisitor;
     9import org.eclipse.imp.pdb.facts.visitors.IdentityVisitor;
     10import org.eclipse.imp.pdb.facts.visitors.NullVisitor;
     11import org.eclipse.imp.pdb.facts.visitors.VisitorException;
     12import org.magnolialang.eclipse.editor.ImplementationError;
    713import org.rascalmpl.values.ValueFactoryFactory;
     14import org.rascalmpl.values.uptr.visitors.TreeVisitor;
     15
    816import static org.magnolialang.parsetree.XaTreeFactory.*;
    917public class XaTreeAdapter {
     
    1523        private static IValueFactory vf = ValueFactoryFactory.getValueFactory();
    1624
     25        public static IMap match(IValue pattern, IValue tree) {
     26                if(pattern instanceof IConstructor && tree instanceof IConstructor)
     27                        return match((IConstructor)pattern, (IConstructor)tree, vf.map(Type_XaTree, Type_XaTree));
     28                else
     29                        return null;
     30        }
     31
    1732        public static IMap match(IConstructor pattern, IConstructor tree) {
    1833                return match(pattern, tree, vf.map(Type_XaTree, Type_XaTree));
    1934        }
    2035        public static IMap match(IConstructor pattern, IConstructor tree, IMap env) {
    21                 if(env == null)
     36                if(env == null || pattern == null || tree == null)
    2237                        return null;
     38                else if(pattern == tree)
     39                        return env;
    2340                else if(isCons(pattern))
    2441                        return matchCons(pattern, tree, env);
     
    4360
    4461                IList pargs = (IList) pattern.get("args");
    45                 IList targs = (IList) pattern.get("args");
     62                IList targs = (IList) tree.get("args");
    4663                if(pargs.length() != targs.length())
    4764                        return null;
     
    5875
    5976                IList pargs = (IList) pattern.get("args");
    60                 IList targs = (IList) pattern.get("args");
     77                IList targs = (IList) tree.get("args");
    6178                if(pargs.length() != targs.length())
    6279                        return null;
     
    88105                && ((IConstructor)tree).getConstructorType() == Type_TermVar;
    89106        }
     107       
     108        public static boolean isGround(IValue tree) {
     109                try {
     110                        return tree.accept(new NullVisitor<Boolean>() {
     111                                public Boolean visitConstructor(IConstructor c) {
     112                                        if(isLeaf(c))
     113                                                return true;
     114                                        else if(isVar(c))
     115                                                return false;
     116                                        for(IValue child : (IList) c.get("args"))
     117                                                if(!isGround(child))
     118                                                        return false;
     119                                        return true;
     120                                }});
     121                } catch (VisitorException e) {
     122                        return false;
     123                }
     124        }
    90125
     126        public static IList vars(IValue tree) {
     127               
     128                final IListWriter lw = vf.listWriter(Type_XaTree);
     129               
     130                try {
     131                        tree.accept(new IdentityVisitor() {
     132                                public IValue visitConstructor(IConstructor c) throws VisitorException {
     133                                        if(isVar(c))
     134                                                lw.append(c);
     135                                        else if(isCons(c) || isSeq(c))
     136                                                for(IValue child : (IList) c.get("args"))
     137                                                        child.accept(this);
     138                                        return c;
     139                                }});
     140                } catch (VisitorException e) {
     141                        throw new ImplementationError("Visitor error", e);
     142                }
    91143
     144                return lw.done();
     145        }
    92146}
Note: See TracChangeset for help on using the changeset viewer.