Changeset 417
- Timestamp:
- Jun 7, 2010 1:51:28 PM (14 years ago)
- Location:
- trunk/MagnoliaPlugin/src/org/magnolialang
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagnoliaPlugin/src/org/magnolialang/eclipse/commands/SelectConstruct.java
r414 r417 9 9 import org.eclipse.imp.editor.UniversalEditor; 10 10 import org.eclipse.imp.pdb.facts.IConstructor; 11 import org.eclipse.imp.pdb.facts.IMap; 11 12 import org.eclipse.imp.pdb.facts.ISourceLocation; 12 13 import org.eclipse.imp.pdb.facts.IString; … … 23 24 import org.magnolialang.parsetree.TreeImplodeVisitor; 24 25 import org.magnolialang.parsetree.TreeImploder; 26 import org.magnolialang.parsetree.XaTreeAdapter; 25 27 import org.magnolialang.parsetree.XaTreeFactory; 28 import org.rascalmpl.values.ValueFactoryFactory; 26 29 import org.rascalmpl.values.uptr.TreeAdapter; 27 30 … … 57 60 58 61 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()))); 61 64 System.out.println("Imploded equality: " + imploded.isEqual(imploded2)); 62 65 System.out.println("Imploded Java equality: " + imploded.equals(imploded2)); 63 66 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); 64 71 //String up1 = ((IString)RascalInterpreter.getInstance().call("unparse", "import XaTree;", imploded)).getValue(); 65 72 //String up2 = ((IString)RascalInterpreter.getInstance().call("unparse", "import XaTree;", imploded2)).getValue(); -
trunk/MagnoliaPlugin/src/org/magnolialang/parsetree/XaTreeAdapter.java
r416 r417 5 5 import org.eclipse.imp.pdb.facts.type.TypeFactory; 6 6 import org.eclipse.imp.pdb.facts.type.TypeStore; 7 import org.eclipse.imp.pdb.facts.visitors.BottomUpVisitor; 8 import org.eclipse.imp.pdb.facts.visitors.IValueVisitor; 9 import org.eclipse.imp.pdb.facts.visitors.IdentityVisitor; 10 import org.eclipse.imp.pdb.facts.visitors.NullVisitor; 11 import org.eclipse.imp.pdb.facts.visitors.VisitorException; 12 import org.magnolialang.eclipse.editor.ImplementationError; 7 13 import org.rascalmpl.values.ValueFactoryFactory; 14 import org.rascalmpl.values.uptr.visitors.TreeVisitor; 15 8 16 import static org.magnolialang.parsetree.XaTreeFactory.*; 9 17 public class XaTreeAdapter { … … 15 23 private static IValueFactory vf = ValueFactoryFactory.getValueFactory(); 16 24 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 17 32 public static IMap match(IConstructor pattern, IConstructor tree) { 18 33 return match(pattern, tree, vf.map(Type_XaTree, Type_XaTree)); 19 34 } 20 35 public static IMap match(IConstructor pattern, IConstructor tree, IMap env) { 21 if(env == null )36 if(env == null || pattern == null || tree == null) 22 37 return null; 38 else if(pattern == tree) 39 return env; 23 40 else if(isCons(pattern)) 24 41 return matchCons(pattern, tree, env); … … 43 60 44 61 IList pargs = (IList) pattern.get("args"); 45 IList targs = (IList) pattern.get("args");62 IList targs = (IList) tree.get("args"); 46 63 if(pargs.length() != targs.length()) 47 64 return null; … … 58 75 59 76 IList pargs = (IList) pattern.get("args"); 60 IList targs = (IList) pattern.get("args");77 IList targs = (IList) tree.get("args"); 61 78 if(pargs.length() != targs.length()) 62 79 return null; … … 88 105 && ((IConstructor)tree).getConstructorType() == Type_TermVar; 89 106 } 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 } 90 125 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 } 91 143 144 return lw.done(); 145 } 92 146 }
Note:
See TracChangeset
for help on using the changeset viewer.