Type.scala done but not tested
1 parent 13f6332 commit 8d9e572468aec1723cf01f5a17a1065642dc7b3e
@glproj03 glproj03 authored on 20 Dec 2005
Showing 1 changed file
View
34
sources/zweic/Type.scala
 
// Internal type
abstract class Type {
 
def intersection[a](c1: List[a], c2: List[a]):List[a] = {
c1.filter(x => c2.contains(x))
}
 
override def toString(): String = this match {
case IClassType => "ClassType";
case IClassType(c) => "ClassType";
case IIntType => "IntType"
case INullType => "NullType"
case IBadType => "<Bad>"
}
 
def isSubtype(that: Type): Boolean = Pair(this, that) match {
// ... à compléter ...
case _ => false
case Pair(INullType, x) => true
case Pair(x, INullType) => false
case Pair(x, y) =>
x.lub(y) match {
case Some(a) => a.isSametype(x)
case None => false
}
}
 
def isSametype(that: Type): Boolean = Pair(this, that) match {
// ... à compléter ...
case Pair(x, y) => x == y
case _ => false
}
 
def lub(that: Type): Option[Type] = Pair(this, that) match {
case Pair(IIntType, IIntType) => IIntType
case Pair(IClassType(x), INullType) => t1
case Pair(INullType, IClassType(x)) => t2
case Pair(IIntType, IIntType) => Some(IIntType)
case Pair(c@IClassType(x), INullType) => Some(c)
case Pair(INullType, c@IClassType(x)) => Some(c)
case Pair(IClassType(c1), IClassType(c2)) =>
intersection(c1.superclasses, c2.superclasses) match {
case t :: ts => t
case List() => badLub(t1, t2)
case t :: ts => Some(IClassType(t))
case List() => Some(IBadType)
}
case x => badLub(t1, t2)
case x => Some(IBadType)
}
 
}