Java TypeEnv Example

说明

java typeenv示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Java

类/类型: TypeEnv

示例#1
文件: TypeChecker.java项目: kametaro/stone

 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;
 }

示例#2
文件: TypeChecker.java项目: kametaro/stone

 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;
   }
 }

示例#3
文件: TypeChecker.java项目: kametaro/stone

 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;
 }

示例#4
文件: TypeApp.java项目: brbaker/mesh

  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;
  }

示例#5
文件: TypeChecker.java项目: kametaro/stone

 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;
 }

展开阅读全文