Exemplo n.º 1
0
 /*
  * Definitions in the let block are lifted to top-level and the corresponding
  * references in the body are replaced by new definitions.
  *
  * @see jaskell.compiler.JaskellVisitor#visit(jaskell.compiler.core.Let)
  */
 public Object visit(Let let) {
   HashMap subst = new HashMap(); /* substitution map */
   Set lambdas = new HashSet(); /* newly created toplevels ? */
   Iterator it = let.getBindings().entrySet().iterator();
   /* first lift all definitions in this let */
   while (it.hasNext()) {
     Map.Entry entry = (Map.Entry) it.next();
     String name = (String) entry.getKey();
     Expression e = (Expression) entry.getValue();
     /* reset lift flag */
     lift = false;
     Expression ndef = (Expression) e.visit(this);
     ndef.setParent(let);
     /* lift new definition */
     Set captured = new HashSet();
     CaptureCollector cc = new CaptureCollector(captured);
     ndef.visit(cc);
     String vname;
     try {
       vname = lift(ndef, captured);
       lambdas.add(vname);
       /*
        * store new application spine in a map for later substitution
        */
       subst.put(name, applyLifted(vname, captured));
     } catch (SymbolException e1) {
       e1.printStackTrace();
     }
   }
   /* second, replace old occurences in new definitions */
   it = lambdas.iterator();
   while (it.hasNext()) {
     String n = (String) it.next();
     Expression e;
     try {
       e = (Expression) ns.resolve(n);
       ns.rebind(n, (Expression) e.visit(new Substitution(subst)));
     } catch (SymbolException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
     }
   }
   /* thirs, replace occurences in body of let and return it */
   return let.getBody().visit(new Substitution(subst));
 }
Exemplo n.º 2
0
 /** @see jaskell.compiler.JaskellVisitor#visit(Abstraction) */
 public Object visit(Abstraction a) {
   try {
     Type t = a.getType();
     if (t != null) return subst.substitute(t);
     log.finest("Visiting abstraction : " + a);
     Expression body = a.getBody();
     /* duplicate bindings map to assign types to variables */
     pushContext(a.getBindings());
     /* create fresh type variables as type for each bound
      * variable */
     Iterator it = namesMap.values().iterator();
     LinkedList tl = new LinkedList();
     while (it.hasNext()) {
       LocalBinding name = (LocalBinding) it.next();
       Type vt = TypeFactory.freshBinding();
       name.setType(vt);
       tl.add(vt);
     }
     Type tv = TypeFactory.freshBinding();
     /* create type with all variables for function */
     Type ft = Types.fun(tl, tv);
     log.finer("In abstraction, setting type to " + ft);
     a.setType(ft);
     /* analyze body */
     Type bt = (Type) body.visit(this);
     /* unify return type of function with type of body */
     Type ret = tu.unify(PrimitiveType.getReturnType(ft), bt, typeVariablesMap);
     TyvarSubstitution tys = new TyvarSubstitution(typeVariablesMap);
     tys.visit(a);
     log.finer("Done abstraction, setting type from " + ft + " to " + a.getType());
     popContext();
     return a.getType();
   } catch (TypeError te) {
     if (te.getLineCol() == null) te.setLineCol(a.getTag("source"));
     throw te;
   }
 }