]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/util/TypeComparator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / types / util / TypeComparator.java
1 package org.simantics.scl.compiler.types.util;
2
3 import java.util.Comparator;
4
5 import org.simantics.scl.compiler.types.TApply;
6 import org.simantics.scl.compiler.types.TCon;
7 import org.simantics.scl.compiler.types.TFun;
8 import org.simantics.scl.compiler.types.TPred;
9 import org.simantics.scl.compiler.types.TUnion;
10 import org.simantics.scl.compiler.types.Type;
11 import org.simantics.scl.compiler.types.Types;
12
13 public enum TypeComparator implements Comparator<Type> {
14     INSTANCE;
15     
16     @Override
17     public int compare(Type t1, Type t2) {
18         t1 = Types.canonical(t1);
19         t2 = Types.canonical(t2);
20         int id1 = t1.getClassId();
21         int id2 = t2.getClassId();
22         if(id1 < id2)
23             return -1;
24         if(id1 > id2)
25             return 1;
26         int cur;
27         switch(id1) {
28         case Type.APPLY_ID: {
29             TApply p1 = (TApply)t1;
30             TApply p2 = (TApply)t2;
31             cur = compare(p1.function, p2.function);
32             if(cur != 0)
33                 return cur;
34             return compare(p1.parameter, p2.parameter);
35         }
36         case Type.CON_ID:
37             return TConComparator.INSTANCE.compare((TCon)t1, (TCon)t2);
38         case Type.FORALL_ID:
39             return 0; // TODO hard to compare
40         case Type.PRED_ID: {
41             TPred p1 = (TPred)t1;
42             TPred p2 = (TPred)t2;
43             cur = TConComparator.INSTANCE.compare(p1.typeClass, p2.typeClass);
44             if(cur != 0)
45                 return cur;
46             cur = p1.parameters.length - p2.parameters.length;
47             if(cur != 0)
48                 return cur;
49             for(int i=0;i<p1.parameters.length;++i) {
50                 cur = compare(p1.parameters[i], p2.parameters[i]);
51                 if(cur != 0)
52                     return cur;
53             }
54             return 0;
55         }
56         case Type.FUN_ID: {
57             TFun p1 = (TFun)t1;
58             TFun p2 = (TFun)t2;
59             cur = compare(p1.domain, p2.domain);
60             if(cur != 0)
61                 return cur;
62             cur = compare(p1.range, p2.range);
63             if(cur != 0)
64                 return cur;
65             return compare(p1.effect, p2.effect);
66         }
67         case Type.UNION_ID: {
68             TUnion p1 = (TUnion)t1;
69             TUnion p2 = (TUnion)t2;
70             cur = p1.effects.length - p2.effects.length;
71             if(cur != 0)
72                 return cur;
73             return 0; // TODO
74         }
75         case Type.METAVAR_ID: 
76             return 0; // cannot compare
77         case Type.VAR_ID:
78             return 0; // cannot compare
79         default:
80             throw new IllegalArgumentException();
81         }
82     }
83
84 }