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