KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > functions > MakeList


1 package gnu.kawa.functions;
2 import gnu.lists.*;
3 import gnu.mapping.*;
4 import gnu.bytecode.*;
5 import gnu.expr.*;
6
7 /**
8  * Implement the Scheme standard function "list".
9  * @author Per Bothner
10  */

11
12 public class MakeList extends ProcedureN implements Inlineable
13 {
14   public static final MakeList list = new MakeList();
15   static { list.setName("list"); }
16
17   public static Object JavaDoc list$V (Object JavaDoc[] args)
18   {
19     Object JavaDoc result = LList.Empty;
20     for (int i = args.length; --i >= 0; )
21       result = new Pair (args[i], result);
22     return result;
23   }
24
25   public Object JavaDoc applyN (Object JavaDoc[] args)
26   {
27     return list$V(args);
28   }
29
30   public void compile (ApplyExp exp, Compilation comp, Target target)
31   {
32     Expression[] args = exp.getArgs();
33     compile(args, 0, comp);
34     target.compileFromStack(comp, getReturnType(args));
35   }
36
37   public static void compile (Expression[] args, int offset,
38                               Compilation comp)
39   {
40     int len = args.length - offset;
41     CodeAttr code = comp.getCode();
42     if (len == 0)
43       {
44     new QuoteExp(LList.Empty).compile(comp, Target.pushObject);
45       }
46     else if (len <= 4)
47       {
48     for (int i = 0; i < len; i++)
49       args[offset+i].compile(comp, Target.pushObject);
50     Method method
51       = Compilation.scmListType.getDeclaredMethod("list"+len, null);
52     code.emitInvokeStatic(method);
53       }
54     else
55       {
56     args[offset].compile(comp, Target.pushObject);
57     Method method
58       = Compilation.scmListType.getDeclaredMethod("list1", null);
59     code.emitInvokeStatic(method);
60     code.emitDup(1);
61     offset++; len--;
62
63     while (len >= 4)
64       {
65         args[offset].compile(comp, Target.pushObject);
66         args[offset+1].compile(comp, Target.pushObject);
67         args[offset+2].compile(comp, Target.pushObject);
68         args[offset+3].compile(comp, Target.pushObject);
69         len -= 4; offset += 4;
70         method = Compilation.scmListType.getDeclaredMethod("chain4", null);
71         code.emitInvokeStatic(method);
72       }
73
74     while (len > 0)
75       {
76         args[offset].compile(comp, Target.pushObject);
77         len -= 1; offset += 1;
78         method = Compilation.scmListType.getDeclaredMethod("chain1", null);
79         code.emitInvokeStatic(method);
80       }
81     code.emitPop(1);
82       }
83   }
84
85   public Type getReturnType (Expression[] args)
86   {
87     return args.length > 0 ? Compilation.scmPairType : Compilation.scmListType;
88   }
89
90 }
91
Popular Tags