Пример #1
0
    public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
      String op = operator();
      if ("=".equals(op)) return typeCheckForAssign(tenv);
      else {
        leftType = ((ASTreeTypeEx) left()).typeCheck(tenv);
        rightType = ((ASTreeTypeEx) right()).typeCheck(tenv);

        if ("+".equals(op)) return leftType.plus(rightType, tenv);
        else if ("==".equals(op)) return TypeInfo.INT;
        else {
          leftType.assertSubtypeOf(TypeInfo.INT, tenv, this);
          rightType.assertSubtypeOf(TypeInfo.INT, tenv, this);
          return TypeInfo.INT;
        }
      }
    }
Пример #2
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;
 }
Пример #3
0
 public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
   TypeInfo condType = ((ASTreeTypeEx) condition()).typeCheck(tenv);
   condType.assertSubtypeOf(TypeInfo.INT, tenv, this);
   TypeInfo thenType = ((ASTreeTypeEx) thenBlock()).typeCheck(tenv);
   TypeInfo elseType;
   ASTree elseBk = elseBlock();
   if (elseBk == null) elseType = TypeInfo.INT;
   else elseType = ((ASTreeTypeEx) elseBk).typeCheck(tenv);
   return thenType.union(elseType, tenv);
 }
Пример #4
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;
   }
 }
Пример #5
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;
 }
Пример #6
0
    public TypeInfo typeCheck(TypeEnv tenv, TypeInfo target) throws TypeException {
      if (!(target instanceof TypeInfo.FunctionType)) throw new TypeException("bad function", this);
      funcType = (TypeInfo.FunctionType) target;
      TypeInfo[] params = funcType.parameterTypes;

      if (size() != params.length) throw new TypeException("bad number of arguments", this);
      argTypes = new TypeInfo[params.length];
      int num = 0;
      for (ASTree a : this) {
        TypeInfo t = argTypes[num] = ((ASTreeTypeEx) a).typeCheck(tenv);
        t.assertSubtypeOf(params[num++], tenv, this);
      }
      return funcType.returnType;
    }
Пример #7
0
 public TypeInfo typeCheck(TypeEnv tenv) throws TypeException {
   TypeInfo t = ((ASTreeTypeEx) operand()).typeCheck(tenv);
   t.assertSubtypeOf(TypeInfo.INT, tenv, this);
   return TypeInfo.INT;
 }
Пример #8
0
 public TypeInfo typeInfo(TypeEnv tenv) throws TypeException {
   TypeInfo condType = ((ASTreeTypeEx) condition()).typeCheck(tenv);
   condType.assertSubtypeOf(TypeInfo.INT, tenv, this);
   TypeInfo bodyType = ((ASTreeTypeEx) body()).typeCheck(tenv);
   return bodyType.union(TypeInfo.INT, tenv);
 }