Newer
Older
zweic / sources / zweic / Type.scala
@glproj03 glproj03 on 26 Dec 2005 1 KB tests added, use script.sh
/*  zweic -- a compiler for zwei
 *
 *  Stephane Micheloud & LAMP
 *
 *  $Id$
 */

package zweic;

// 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(c)  => "ClassType: " + c.name;
	case IIntType => "IntType"
	case INullType => "NullType"
    case IBadType => "<Bad>"
  }

  def isSubtype(that: Type): Boolean = Pair(this, that) match {
	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 {
	case Pair(x, y) => x == y
    case _ => false
  }

  def lub(that: Type): Option[Type] = Pair(this, that) match {
	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 => Some(IClassType(t))
		case List() => Some(IBadType)
	  }
	case x => Some(IBadType)
  }

}

// Class type
case class IClassType(c: ClassSymbol) extends Type;

// Integer type
case object IIntType extends Type;

// Null type
case object INullType extends Type;

// Bad type
case object IBadType extends Type;