示例#1
0
 /**
  * Returns an Abstraction object resulting from lifting the given expression with captured
  * variable set. if <code>f
  * x y</code> is an expression with <code>y</code> a captured variable, then it is transformed
  * into <code>lambdaXXX
  * = \ y -> f x y</code> which can later be used to replace the original expression with <code>
  * lambdaXXX y</code>.
  *
  * @param e
  * @param captured
  * @return a String object
  * @throws SymbolException
  */
 private String lift(Expression e, Set captured) throws SymbolException {
   if (e instanceof Abstraction) return lift((Abstraction) e, captured);
   Abstraction abs = new Abstraction();
   /* bind captured variables */
   Iterator it = captured.iterator();
   while (it.hasNext()) {
     LocalBinding lb = (LocalBinding) it.next();
     LocalBinding nlb = new LocalBinding(lb.getName());
     abs.bind(nlb);
   }
   /* restore body */
   abs.setBody(e);
   String newname = "lambda" + counter++;
   ns.bind(newname, abs);
   return newname;
 }
示例#2
0
 /*
  * (non-Javadoc)
  *
  * @see jaskell.compiler.JaskellVisitor#visit(jaskell.compiler.core.Module)
  */
 public Object visit(Namespace a) {
   /* set current namespace */
   ns = a;
   /*
    * visit definitions We set an infinite loop to cope with modifications
    * introduced by visiting sub expressions. When something is modified -
    * through lift - we restart the whole process. TODO : change this to defer
    * registering new bindings at end
    */
   while (true) {
     try {
       Iterator it = a.getAllBindings().entrySet().iterator();
       while (it.hasNext()) {
         Map.Entry entry = (Map.Entry) it.next();
         String functionName = (String) entry.getKey();
         Expression def = (Expression) entry.getValue();
         if (def instanceof Abstraction)
           ((Abstraction) def).setClassName(ns.getName() + "." + functionName);
         log.finest("Analyzing lifting for " + functionName);
         lift = false;
         entry.setValue((Expression) def.visit(this));
       }
       break;
     } catch (ConcurrentModificationException cmex) {
       /* restart the process */
     }
   }
   return a;
 }
示例#3
0
 /**
  * This method produces a new Abstraction which is the result of adding captured variables as new
  * arguments and bind it in current ns. It returns the name of the bound funciton.
  *
  * @param a
  * @param captured
  * @return a String object denoting the name given to this function
  * @throws SymbolException
  */
 private String lift(Abstraction a, Set captured) throws SymbolException {
   Abstraction abs = a;
   if (!captured.isEmpty()) {
     /* recreate abstraction adding captured variables */
     abs = new Abstraction();
     /* bind captured variables */
     Iterator it = captured.iterator();
     while (it.hasNext()) {
       LocalBinding lb = (LocalBinding) it.next();
       LocalBinding nlb = new LocalBinding(lb.getName());
       abs.bind(nlb);
     }
     /* rebind old variables */
     it = a.getBindings().values().iterator();
     while (it.hasNext()) {
       LocalBinding lb = (LocalBinding) it.next();
       LocalBinding nlb = new LocalBinding(lb.getName());
       abs.bind(nlb);
     }
     /* restore body */
     abs.setBody(a.getBody());
   }
   /*
    * bind new abstraction in ns if a is an anonymous abstraction - no class
    * name - then it is renamed lambdaXXX and bound in namespace. Else, it is
    * given a new name and a reference to this new name is stored in substMap.
    */
   String newname = "lambda" + counter++;
   ns.bind(newname, abs);
   abs.setClassName(ns.getName() + "." + newname);
   return newname;
 }
示例#4
0
 /*
  * If we visit a nested abstraction, we just launch a new lift operation on
  * this abstraction using current context as namespace and returns an
  * Application object
  *
  * @see jaskell.compiler.JaskellVisitor#visit(jaskell.compiler.core.Abstraction)
  */
 public Object visit(Abstraction a) {
   if (!lift) {
       /* top level abstractions */
     lift = true;
     a.setBody((Expression) a.getBody().visit(this));
     return a;
   }
   /* first visit body */
   a.setBody((Expression) a.getBody().visit(this));
   /* retrieve outer LocalBindings */
   Set captured = new HashSet();
   CaptureCollector cc = new CaptureCollector(captured);
   a.visit(cc);
   /* return the newly computed abstraction as an application spine */
   String vname;
   try {
     vname = lift(a, captured);
     Expression ex = applyLifted(vname, captured);
     ex.setParent(a.getParent());
     return ex;
   } catch (SymbolException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return null;
   }
 }
示例#5
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;
   }
 }