Example #1
0
 /*
  * (non-Javadoc)
  *
  * @see com.ibm.jaql.lang.rewrite.Rewrite#rewrite(com.ibm.jaql.lang.expr.core.Expr)
  */
 @Override
 public boolean rewrite(Expr expr) {
   assert expr instanceof InlinePragma;
   Expr c = expr.child(0);
   if (c instanceof VarExpr) {
     VarExpr varExpr = (VarExpr) c;
     Var var = varExpr.var();
     Expr def = varExpr.findVarDef();
     //    // TODO: I think this case is gone now; function parameters are in bindings that are
     // found.
     //      if (def == null)
     //      {
     //        // must be a function parameter // TODO: findVarDef SHOULD find it, and params
     // should use Bindings
     //        return false;
     //      }
     if (def instanceof BindingExpr) {
       BindingExpr b = (BindingExpr) def;
       assert var == b.var; // or else findDef is broken
       Expr p = def.parent();
       if (p instanceof DoExpr) {
         expr.replaceInParent(cloneExpr(b.eqExpr()));
         return true;
       }
       // else if( p instanceof DefineFunctionExpr )
       // We couldn't inline, yet...
       return false;
     } else // TODO: I don't think this case arises anymore...
     {
       assert false;
       return false;
       //        assert var.isGlobal();
       //        Expr replaceBy;
       //        if (var.value != null)
       //        {
       //          // If the global is already computed, inline its value
       //          replaceBy = new ConstExpr(var.value);
       //        }
       //        else
       //        {
       //          // If the global is not already computed, inline its expr
       //          replaceBy = cloneExpr(def);
       //        }
       //        expr.replaceInParent(replaceBy);
       //        return true;
     }
   }
   // If this inline request is not over a VarExpr, just remove it.
   expr.replaceInParent(c);
   return true;
 }
  /*
   * (non-Javadoc)
   *
   * @see com.ibm.jaql.lang.rewrite.Rewrite#rewrite(com.ibm.jaql.lang.expr.core.Expr)
   */
  @Override
  public boolean rewrite(Expr expr) {
    ForExpr fe = (ForExpr) expr;
    Expr inExpr = fe.binding().inExpr();
    Expr c = fe.collectExpr();

    // for( i in []   ) e    => []
    // for( i in null ) e    => []
    // for( i in e    ) []   => []
    // for( i in e    ) null => []
    // TODO: was this, but it ran slowly: if (inExpr.getSchema().isEmpty(ARRAY,NULL).always() ||
    // c.getSchema().isEmpty(ARRAY,NULL).always())
    if (isEmptyArrayOrNull(inExpr) || isEmptyArrayOrNull(c)) {
      fe.replaceInParent(new ArrayExpr());
      return true;
    }

    // look for collect [$i]
    if (!(c instanceof ArrayExpr) || c.numChildren() != 1) {
      return false;
    }
    c = c.child(0);

    // for( $i in e1 ) [$i] => asArray(e1) => e1 (when non-null array)
    if (c instanceof VarExpr) {
      VarExpr ve = (VarExpr) c;
      if (ve.var() == fe.var()) {
        if (inExpr.getSchema().is(ARRAY, NULL).maybeNot() || inExpr.getSchema().is(NULL).maybe()) {
          inExpr = new AsArrayFn(inExpr);
        }
        fe.replaceInParent(inExpr);
        return true;
      }
    }

    // e1 -> expand [e2] ==> e1 -> transform e2
    expr = new TransformExpr(fe.binding(), c);
    fe.replaceInParent(expr);
    return true;
  }