KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > standard > prim_method


1 package kawa.standard;
2 import kawa.lang.*;
3 import gnu.bytecode.ClassType;
4 import gnu.bytecode.Type;
5 import gnu.expr.*;
6 import gnu.lists.*;
7
8 public class prim_method extends Syntax
9 {
10   public static final prim_method virtual_method = new prim_method(182);
11   static { virtual_method.setName("primitive-virtual-method"); }
12
13   public static final prim_method static_method = new prim_method(184);
14   static { static_method.setName("primitive-static-method"); }
15
16   public static final prim_method interface_method = new prim_method(185);
17   static { interface_method.setName("primitive-interface-method"); }
18
19   public static final prim_method op1 = new prim_method();
20   static { op1.setName("primitive-op1"); }
21
22   static private Pattern pattern3 = new ListPat (3);
23   static private Pattern pattern4 = new ListPat (4);
24
25   int op_code;
26
27   int opcode () { return op_code; }
28
29   public prim_method (int opcode)
30   {
31     op_code = opcode;
32   }
33
34   public prim_method ()
35   {
36   }
37
38   public Expression rewrite (Object JavaDoc obj, Translator tr)
39   {
40     Object JavaDoc[] match = new Object JavaDoc [4];
41     if (! (op_code == 0 ? pattern3.match(obj, match, 1)
42        : pattern4.match(obj, match, 0))) // virtual or static
43
return tr.syntaxError ("wrong number of arguments to "+getName()
44                  +"(opcode:"+op_code+")");
45
46     if (! (match[3] instanceof LList))
47       return tr.syntaxError ("missing/invalid parameter list in "+getName());
48     LList argp = (LList) match[3];
49
50     int narg = argp.size();
51     Type[] args = new Type[narg];
52     for (int i = 0; i < narg; i++)
53       {
54     Pair p = (Pair)argp;
55     args[i] = tr.exp2Type(p);
56     argp = (LList)p.cdr;
57       }
58     Type rtype = tr.exp2Type(new Pair(match[2], null));
59     PrimProcedure proc;
60     if (op_code == 0)
61       {
62     int opcode = ((Number JavaDoc)(match[1])).intValue();
63     proc = new PrimProcedure(opcode, rtype, args);
64       }
65     else
66       {
67         ClassType cl = null;
68         Type ctype = tr.exp2Type((Pair) obj);
69         try
70           {
71             cl = (ClassType) ctype;
72             cl.getReflectClass();
73           }
74         catch (Exception JavaDoc ex)
75           {
76             char code;
77             if (cl == null)
78               code = 'e';
79             else
80               {
81                 code = 'w';
82                 ((ClassType) cl).setExisting(false);
83               }
84             tr.error(code, "unknown class: " + match[0]);
85           }
86         Pair p;
87         if (match[1] instanceof Pair
88             && (p = (Pair) match[1]).car == "quote")
89           match[1] = ((Pair) p.cdr).car;
90         proc = new PrimProcedure(op_code, cl,
91                                  match[1].toString(), rtype, args);
92       }
93     return new QuoteExp(proc);
94   }
95 }
96
Popular Tags