Ejemplo n.º 1
0
 public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
   if (tenv.get(0, index) != null)
     throw new TypeException("duplicate variable: " + name(), this);
   varType = TypeInfo.get(type());
   tenv.put(0, index, varType);
   valueType = ((ASTreeTypeEx) initializer()).typeCheck(tenv);
   valueType.assertSubtypeOf(varType, tenv, this);
   return varType;
 }
Ejemplo n.º 2
0
 public TypeInfo typeCheckForAssign(TypeEnv tenv, TypeInfo valueType) throws TypeException {
   type = tenv.get(nest, index);
   if (type == null) {
     type = valueType;
     tenv.put(0, index, valueType);
     return valueType;
   } else {
     valueType.assertSubtypeOf(type, tenv, this);
     return type;
   }
 }
Ejemplo n.º 3
0
 public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
   TypeInfo[] params = ((ParamListEx2) parameters()).types();
   TypeInfo retType = TypeInfo.get(type());
   funcType = TypeInfo.function(retType, params);
   TypeInfo oldType = tenv.put(0, index, funcType);
   if (oldType != null) throw new TypeException("function redefinition: " + name(), this);
   bodyEnv = new TypeEnv(size, tenv);
   for (int i = 0; i < params.length; i++) bodyEnv.put(0, i, params[i]);
   TypeInfo bodyType = ((ASTreeTypeEx) revise(body())).typeCheck(bodyEnv);
   bodyType.assertSubtypeOf(retType, tenv, this);
   return funcType;
 }
Ejemplo n.º 4
0
  public SubstMap unify(final Loc loc, final Type other, final TypeEnv env) {
    if (env.checkVisited(this, other)) return SubstMap.EMPTY;

    if (other instanceof TypeVar) return SubstMap.bindVar(loc, (TypeVar) other, this);

    // if our application expression can be evaluated, unify against that.
    if (isAbsApply()) return eval().unify(loc, other, env);

    final Type otherEval = other.deref().eval();

    if (otherEval instanceof TypeApp) {
      final TypeApp otherApp = (TypeApp) otherEval;
      final SubstMap baseSubst = base.unify(loc, otherApp.base, env);

      if (baseSubst != null) {
        final SubstMap argSubst =
            arg.subst(baseSubst).unify(loc, otherApp.arg.subst(baseSubst), env);

        if (argSubst != null) return baseSubst.compose(loc, argSubst);
      }
    } else if (kind.equals(otherEval.getKind())) {
      // other is not an application expression, but kinds match.
      // other type classes do opportunistic matching of type apps
      // implementations of unify. This approach is really ad-hoc,
      // needs to be rationalized.

      return otherEval.unify(loc, this, env);
    }

    return null;
  }
Ejemplo n.º 5
0
 public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
   type = tenv.get(nest, index);
   if (type == null) throw new TypeException("undefined name: " + name(), this);
   else return type;
 }