KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > PushApply


1 package gnu.expr;
2
3 /** Re-arranges ApplyExp where the function is a LetExp or BeginExp.
4     Optimizes ((let (...) body) . args) to (let (...) (body . args)).
5     Optimizes ((begin ... last) . args) to (begin ... (last . args)).
6     This helps optimize Scheme "named let" (and some other forms)
7     by making it more likely the application will be to a known procedure.
8     This optimization has to be done after Declarations are bound. */

9
10 public class PushApply extends ExpWalker
11 {
12   public static void pushApply (Expression exp)
13   {
14     PushApply walker = new PushApply();
15     walker.walk(exp);
16   }
17
18   protected Expression walkApplyExp(ApplyExp exp)
19   {
20     Expression func = exp.func;
21     if (func instanceof LetExp
22         && ! (func instanceof FluidLetExp)) // [APPLY-LET]
23
{
24     // Optimize ((let (...) body) . args) to (let (...) (body . args)).
25
LetExp let = (LetExp) func;
26     Expression body = let.body;
27     let.body = exp;
28     exp.func = body;
29     return let.walk(this);
30       }
31     if (func instanceof BeginExp) // [APPLY-BEGIN]
32
{
33     // Optimize ((begin ... last) . args) to (begin ... (last . args)).
34
BeginExp begin = (BeginExp) func;
35     Expression[] stmts = begin.exps;
36     int last_index = begin.exps.length - 1;
37     exp.func = stmts[last_index];
38     stmts[last_index] = exp;
39     return begin.walk(this);
40       }
41     exp.walkChildren(this);
42     return exp;
43   }
44 }
45
Popular Tags