Beispiel #1
0
 private void checkArgTypes(ast.types.Fun funType, Env<String, Type> env) {
   if (funType.getDomain().length == args.length) {
     for (int i = 0; i < funType.getDomain().length; i++) {
       Type suppliedType = args[i].type(env);
       Type expectedType = funType.getDomain()[i];
       if (!Type.equals(expectedType, suppliedType, env)) {
         throw new TypeMatchError(
             getLineStart(), getLineEnd(), suppliedType, Type.eval(expectedType, env));
       }
     }
   } else
     throw new TypeError(
         getLineStart(),
         getLineEnd(),
         "expecting " + funType.getDomain().length + " args but supplied with " + args.length);
 }
Beispiel #2
0
 public Type type(Env<String, Type> env) {
   Type t = op.type(env).deref(env);
   ast.types.Fun funType =
       Type.expect(op.getLineStart(), op.getLineEnd(), ast.types.Fun.class, this, t);
   checkArgTypes(funType, env);
   setType(funType.getRange());
   return getType();
 }
Beispiel #3
0
  public Type type(Env<String, Type> env) {

    // The case expression may include declarations that
    // type the bound variables in patterns. The pattern
    // variables are updated...

    processDeclarations(env);

    // Get the types of the supplied values...

    Type[] suppliedTypes = new Type[exps.length];
    Type[] expectedTypes = new Type[exps.length];

    for (int i = 0; i < exps.length; i++) {
      suppliedTypes[i] = exps[i].type(env);
      expectedTypes[i] = ast.types.Void.VOID;
    }

    // The return type. Any void return types are ignored...

    Type resultType = null;

    for (BArm arm : arms) {
      int lineStart = arm.patterns[0].getLineStart();
      int lineEnd = arm.patterns[0].getLineEnd();
      if (arm.patterns.length == exps.length) {
        HandlerType handlerType = arm.type(env);
        for (int i = 0; i < exps.length; i++) {
          if (!Term.equals(handlerType.getTypes()[i], suppliedTypes[i], env)) {
            throw new TypeMatchError(
                lineStart, lineEnd, suppliedTypes[i], handlerType.getTypes()[i]);
          }
        }
        if (resultType == null) resultType = handlerType.getResult();
        else if (!Type.equals(resultType, handlerType.getResult(), env))
          throw new TypeError(
              lineStart,
              lineEnd,
              "incompatible case arm return types "
                  + resultType
                  + " and "
                  + handlerType.getResult());
      } else throw new TypeError(lineStart, lineEnd, "incorrect number of arm patterns.");
    }

    return resultType;
  }