Package kiss.lang

Examples of kiss.lang.Type


    this.type=type;
    this.body=body;
  }
 
  public static Cast create(Type type, Expression body) {
    Type bt=body.getType();
    if (bt.intersection(type)==Nothing.INSTANCE) {
      throw new KissException("Can't cast type "+bt+" to "+type);
    }
    return new Cast(type,body.specialise(type));
  }
View Full Code Here


  }
 
  @Override
  public Expression optimise() {
    Expression b=body.optimise();
    Type bt=body.getType();
    if (b.isConstant()) {
      Object val=b.eval();
      if (type.checkInstance(val)) throw new KissException("Impossible to cast value "+val+" to type: "+type);
      // TODO: is this logic sound? what about interface casts?
      return b;
    }
    Type t=type;
    if (t.contains(bt)) t=bt;
    if ((b==body)&&(t==type)) return this;
    return create(t,b);
  }
View Full Code Here

  @Override
  public Expression specialise(Type type) {
    if (type==this.type) return this;
    if (type.contains(this.type)) return this;
    Type it = type.intersection(this.type);
    if (it==Nothing.INSTANCE) return null;
   
    return create(it,body.specialise(it));
  }
View Full Code Here

   */
  private static Type[] compress(Type... types) {
    int n=types.length;
    int found=0;
    for (int i=0; i<n; i++) {
      Type t=types[i];
      if ((t==null)||(t instanceof Nothing)) {
        types[i]=null;
        continue;
      }
      found++;
      for (int j=0; j<i; j++) {
        // try to eliminate already contained types
        Type jt=types[j];
        if ((jt!=null)&&jt.contains(t)) {
          types[i]=null;
          found--;
          break;
        }
      }
    }
    if (found==n) return types; // no compression needed
   
    Type[] nts=new Type[found];
    found=0;
    for (int i=0; i<n; i++) {
      Type t=types[i];
      if (t==null) continue;
      nts[found++]=t;
    }
    if (found!=nts.length) throw new RuntimeException("This shouldn't happen! found="+found+ " nts.length="+nts.length);
    return nts;   
View Full Code Here

  }
 
  private Type include(Type t) {
    int n=types.length;
    for (int i=0; i<n; i++) {
      Type ct=types[i];
      if (ct.contains(t)) return this;
    }
    // TODO: smarter union, then t contains current union members
    Type[] nts=new Type[n+1];
    System.arraycopy(types, 0, nts, 0, n);
    nts[n]=t;
View Full Code Here

  @Override
  public void validate() {
    int n=types.length;
    if (n<2) throw new KissException("Union must have at least two members");
    for (int i=0; i<n; i++) {
      Type t=types[i];
      t.validate();
      if (t instanceof Nothing) throw new KissException(this+ " should not contain Nothing type");
    }
  }
View Full Code Here

  }
 
  @Override
  public Expression optimise() {
    Expression body=this.body.optimise();
    Type bt=body.getType();
    if (type.contains(bt)) return Constant.TRUE;
    if (type.intersection(bt)==Nothing.INSTANCE) return Constant.FALSE;
    return update(type,body);
  }
View Full Code Here

    return update(nFunc,nParams);
  }

  @Override
  public Type getType() {
    Type ft=func.getType();
    if (ft instanceof FunctionType) {
      return ((FunctionType)ft).getReturnType();
    }
    return Reference.INSTANCE;
  }
View Full Code Here

    if (t instanceof FunctionType) {
      FunctionType ft=(FunctionType)t;
      int n=getArity();
      if (ft.getArity()!=n) return Nothing.INSTANCE;
     
      Type rt=getReturnType().intersection(ft.returnType);
      if (rt instanceof Nothing) return Nothing.INSTANCE;
      boolean match=(rt==returnType);
     
      Type[] ips=new Type[n];
      for (int i=0; i<n ; i++) {
        Type it=paramTypes[i].intersection(ft.paramTypes[i]);
        ips[i]=it;
        match &= (it==paramTypes[i]);
      }
      if (match) return this;
      return create(rt,ips);
View Full Code Here

    if ((t==this)||(t instanceof Anything)||(t instanceof Reference)) return this;
   
    // handle possible null cases
    if (t instanceof Null) return t;
    if (t instanceof Maybe) {
      Type mt=((Maybe)t).type;
      Type it = type.intersection(mt);
      if (it==type) return this;
      if (it==mt) return t;
      return Maybe.create(it);
    }
   
View Full Code Here

TOP

Related Classes of kiss.lang.Type

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.