/** Desugar a generalized While clause. */ @Override public Node forWhile(While w) { GeneratorClause g = w.getTestExpr(); if (g.getBind().size() > 0) { // while binds <- expr do body end // desugars to // while __whileCond(expr, fn (binds) => body) do end ArrayList<Expr> args = new ArrayList<Expr>(2); args.add(g.getInit()); args.add(bindsAndBody(g, w.getBody())); Expr cond = ExprFactory.make_RewriteFnApp( NodeUtil.getSpan(g), WHILECOND_NAME, ExprFactory.makeTupleExpr(NodeUtil.getSpan(w), args)); w = ExprFactory.makeWhile(NodeUtil.getSpan(w), cond); } return (Expr) super.forWhile(w); }
/** * Add an if clause to a (potentially) pre-existing else clause. The else clase can be null, or * can be an if expression. */ private Expr addIfClause(IfClause c, Expr elsePart) { GeneratorClause g = c.getTestClause(); if (g.getBind().size() > 0) { // if binds <- expr then body else elsePart end desugars to // __cond(expr, fn (binds) => body, elsePart) ArrayList<Expr> args = new ArrayList<Expr>(3); args.add(g.getInit()); args.add(bindsAndBody(g, c.getBody())); if (elsePart != null) args.add(thunk(elsePart)); return (Expr) recur( ExprFactory.make_RewriteFnApp( NodeUtil.getSpan(c), COND_NAME, ExprFactory.makeTupleExpr(NodeUtil.getSpan(c), args))); } // if expr then body else elsePart end is preserved // (but we replace elif chains by nesting). if (elsePart == null) { return (Expr) super.forIf(ExprFactory.makeIf(NodeUtil.getSpan(c), c)); } else { return (Expr) super.forIf(ExprFactory.makeIf(NodeUtil.getSpan(c), c, ExprFactory.makeBlock(elsePart))); } }