예제 #1
0
 /** @see jaskell.compiler.JaskellVisitor#visit(ConstructorPattern) */
 public Object visit(ConstructorPattern a) {
   String cname = a.getConstructor().getName();
   /* retrieve parameter types of constructor */
   ConstructorDefinition ctor = (ConstructorDefinition) a.getConstructor().lookup(cname);
   if (ctor == null) throw new TypeError("Undefined constructor pattern  " + a);
   /* type of data constructed by constructor */
   TypeInstantiator ti = new TypeInstantiator(ctor.getDataType());
   Type rtype = ti.instance();
   Map map = ti.getMap();
   TypeSubstitution ts = new TypeSubstitution(map);
   Iterator ittypes = ctor.getParameters().iterator();
   /* retrieve type of patterns */
   Iterator it = a.getSubPatterns();
   while (it.hasNext()) {
     try {
       Pattern p = (Pattern) it.next();
       Type actual = TypeFactory.freshBinding();
       Type formal = ts.substitute((Type) ittypes.next());
       /* unify both types */
       p.setType(tu.unify(formal, actual, typeVariablesMap));
     } catch (NoSuchElementException nex) {
       throw new TypeError("Wrong number of arguments to pattern " + a);
     }
   }
   a.setType(rtype);
   return a.getType();
 }
예제 #2
0
 /** @see jaskell.compiler.JaskellVisitor#visit(Constructor) */
 public Object visit(Constructor a) {
   Type ret = a.getType();
   if (ret != null) return ret;
   String vname = a.getName();
   ConstructorDefinition def = (ConstructorDefinition) a.lookup(vname);
   if (def == null) // unknown symbol
   throw new CompilerException("Unknown constructor " + vname);
   ret = new TypeInstantiator(def.getType()).instance();
   a.setType(ret);
   return ret;
 }
예제 #3
0
 /** @see jaskell.compiler.JaskellVisitor#visit(ConstructorDefinition) */
 public Object visit(ConstructorDefinition a) {
   /* returns a FunctionType */
   return a.getType();
 }