KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > autogen


1                                 // Generate glue code
2

3 import java.io.*;
4 import java.util.*;
5
6 class typeinfo
7 {
8   String JavaDoc scm_name;
9   String JavaDoc java_name;
10   String JavaDoc java_inp_type[];
11
12   typeinfo(String JavaDoc sname, String JavaDoc jname, String JavaDoc jit[])
13   {
14     scm_name = sname;
15     java_name = jname;
16     java_inp_type = jit;
17   }
18
19   void write(PrintStream out)
20   {
21     int mode;
22
23     out.println("//Autogenerated by typeinfo on " + new Date());
24
25     out.println("class scm" + java_name + " extends Procedure implements Obj");
26     out.println("{");
27     out.println(" Obj apply(Cell args, Env f)\n throws Exception\n {");
28     out.println(" Cell t = args;");
29     out.println(" Obj tmp;");
30     for (int i=0; i<java_inp_type.length; i++)
31       {
32         out.println(" if (t == null) { throw new SchemeError(\""
33                     + scm_name + " expects "
34                     + java_inp_type.length +
35                     " arguments\"); }");
36         out.println(" tmp = (t.car!=null)?t.car.eval(f):null; t = t.cdr;");
37
38         if (java_inp_type[i].equals("String")) { mode = 0; }
39         else if ((java_inp_type[i].equals("short")) ||
40                  (java_inp_type[i].equals("int")) ||
41                  (java_inp_type[i].equals("long"))) { mode = 1; }
42         else if ((java_inp_type[i].equals("float")) ||
43                  (java_inp_type[i].equals("double"))) { mode = 3; }
44
45         else { mode = 2; }
46
47         switch(mode)
48           {
49           case 0: // String
50
out.println
51               (" if ((tmp != null) && !(tmp instanceof Selfrep))" +
52                " { throw new SchemeError(\"" +
53                scm_name + " expects a String for arg #" + (i+1) +
54                "\"); }");
55             out.println
56               (" String arg" + i + " = (tmp!=null)?((Selfrep)tmp).val:null;");
57             break;
58           case 1: // number
59
case 3:
60             out.println
61               (" if (!(tmp instanceof Selfrep))" +
62                " { throw new SchemeError(\"" +
63                scm_name + " expects a number for arg #" + (i+1) +
64                "\"); }");
65             if (mode == 1)
66               {
67                 out.println
68                   (" "+java_inp_type[i]+" arg" + i +
69                    " = (" + java_inp_type[i] + ")(Math.round(((Selfrep)tmp).num));");
70               }
71             else
72               {
73                 out.println
74                   (" "+java_inp_type[i]+" arg" + i +
75                    " = (" + java_inp_type[i] + ")(((Selfrep)tmp).num);");
76               }
77                 
78             break;
79           default: // primnode
80
out.println
81               (" if ((tmp != null) && !(tmp instanceof primnode))" +
82                " { throw new SchemeError(\"" +
83                scm_name + " expects a " + java_inp_type[i] +
84                " for arg #" + (i+1) + "\"); }");
85             out.println
86               (" if ((tmp != null) && !((((primnode)tmp).val) instanceof " +
87                java_inp_type[i] + ")) { throw new SchemeError(\"" +
88                scm_name + " expects a " + java_inp_type[i] +
89                " for arg #" + (i+1) + "\"); }");
90             out.println
91               (" "+java_inp_type[i]+" arg" + i +
92                " = (tmp != null)?(" + java_inp_type[i] + ")(((primnode)tmp).val):null;");
93             break;
94           }
95       }
96
97                                 // Now create the new object
98
out.print(" return new primnode(new " +
99               java_name + "(");
100
101     if (java_inp_type.length != 0) out.print("arg0");
102
103     for (int i=1; i<java_inp_type.length; i++)
104       { out.print(", arg" + i); }
105     out.println("));\n }");
106     out.println(" public String toString()");
107     out.println(" { return (\"<#" + scm_name + "#>\"); }");
108     out.println("}");
109   }
110 }
111
112 class procinfo
113                                 // Create simple procedures
114
// automatically
115
{
116   String JavaDoc java_name;
117   String JavaDoc scm_name;
118   String JavaDoc java_inp_type[];
119
120   procinfo(String JavaDoc sname, String JavaDoc jname, String JavaDoc jit[])
121   {
122     java_name = jname;
123     scm_name = sname;
124     java_inp_type = jit;
125   }
126
127   void write(PrintStream out)
128   {
129     int mode;
130
131     out.println("//Autogenerated by procinfo on " + new Date());
132     out.println
133       ("class scm" + java_name + " extends Procedure implements Obj\n{");
134     out.println
135       (" Obj apply(Cell args, Env f)\n throws Exception\n {\n");
136     out.println
137       (" Cell t = args;");
138     out.println
139       (" Obj tmp;");
140     for (int i=0; i<java_inp_type.length; i++)
141       {
142         out.println
143           (" if (t == null) { throw new SchemeError(\""
144            + scm_name + " expects "
145            + java_inp_type.length +
146            " arguments\"); }");
147         out.println
148           (" tmp = (t.car!=null)?t.car.eval(f):null; t = t.cdr;");
149
150         if (java_inp_type[i].equals("String")) { mode = 0; }
151         else if ((java_inp_type[i].equals("short")) ||
152                  (java_inp_type[i].equals("int")) ||
153                  (java_inp_type[i].equals("float")) ||
154                  (java_inp_type[i].equals("double")) ||
155                  (java_inp_type[i].equals("long"))) { mode = 1; }
156         else { mode = 2; }
157
158         switch(mode)
159           {
160           case 0: // String
161
out.println
162               (" if ((tmp != null) && !(tmp instanceof Selfrep))" +
163                " { throw new SchemeError(\"" +
164                scm_name + " expects a String for arg #" + (i+1) +
165                "\"); }");
166             out.println
167               (" String arg" + i + " = (tmp!=null)?((Selfrep)tmp).val:null;");
168             break;
169           case 1: // number
170
out.println
171               (" if (!(tmp instanceof Selfrep))" +
172                " { throw new SchemeError(\"" +
173                scm_name + " expects a number for arg #" + (i+1) +
174                "\"); }");
175             out.println
176               (" "+java_inp_type[i]+" arg" + i +
177                " = (" + java_inp_type[i] + ")(((Selfrep)tmp).num);");
178             break;
179           default: // primnode
180
out.println
181               (" if ((tmp != null) && !(tmp instanceof primnode))" +
182                " { throw new SchemeError(\"" +
183                scm_name + " expects a " + java_inp_type[i] +
184                " for arg #" + (i+1) + "\"); }");
185             out.println
186               (" if ((tmp != null) && !((((primnode)tmp).val) instanceof " +
187                java_inp_type[i] + ")) { throw new SchemeError(\"" +
188                scm_name + " expects a " + java_inp_type[i] +
189                " for arg #" + (i+1) + "\"); }");
190             out.println
191               (" "+java_inp_type[i]+" arg" + i +
192                " = (tmp != null)?(" + java_inp_type[i] + ")(((primnode)tmp).val):null;");
193             break;
194           }
195       }
196                                 // now call the method
197
out.print(" arg0." + java_name + "(arg1");
198     for (int i=2; i<java_inp_type.length; i++)
199       { out.print(", arg" + i); }
200     out.println(");\n return null;\n }");
201     out.println(" public String toString()");
202     out.println(" { return (\"<#" + scm_name + "#>\"); }");
203     out.println("}");
204   }
205 }
206
207 class autogen implements jas.RuntimeConstants
208 {
209   
210   static String JavaDoc procs[][] =
211     {
212                                 // Manipulate class env
213
{"ClassEnv", "CP"}, {"jas-class-addcpe", "addCPItem"},
214       {"ClassEnv", "Var"}, {"jas-class-addfield", "addField"},
215       {"ClassEnv", "CP"}, {"jas-class-addinterface", "addInterface"},
216       {"ClassEnv", "CP"}, {"jas-class-setclass", "setClass"},
217       {"ClassEnv", "CP"}, {"jas-class-setsuperclass", "setSuperClass"},
218       {"ClassEnv", "short", "String", "String", "CodeAttr", "ExceptAttr"},
219                           {"jas-class-addmethod", "addMethod"},
220       {"ClassEnv", "short"}, {"jas-class-setaccess", "setClassAccess"},
221       {"ClassEnv", "String"}, {"jas-class-setsource", "setSource"},
222       {"ClassEnv", "scmOutputStream"}, {"jas-class-write", "write"},
223
224                                 // Add things to exceptions
225
{"ExceptAttr", "CP"}, {"jas-exception-add", "addException"},
226
227                                 // Manipulate code attrs
228
{"CodeAttr", "Insn"}, {"jas-code-addinsn", "addInsn"},
229       {"CodeAttr", "short"}, {"jas-code-stack-size", "setStackSize"},
230       {"CodeAttr", "short"}, {"jas-code-var-size", "setVarSize"},
231       {"CodeAttr", "Catchtable"}, {"jas-set-catchtable", "setCatchtable"},
232
233
234                                 // add things to catchtables
235
{"Catchtable", "CatchEntry"}, {"jas-add-catch-entry", "addEntry"},
236     };
237
238   static String JavaDoc types[][] =
239     {
240       {"String"}, {"make-ascii-cpe", "AsciiCP"},
241       {"String"}, {"make-class-cpe", "ClassCP"},
242       {"String", "String"}, {"make-name-type-cpe", "NameTypeCP"},
243       {"String", "String", "String"}, {"make-field-cpe", "FieldCP"},
244       {"String", "String", "String"}, {"make-interface-cpe", "InterfaceCP"},
245       {"String", "String", "String"}, {"make-method-cpe", "MethodCP"},
246       {"int"}, {"make-integer-cpe", "IntegerCP"},
247       {"float"}, {"make-float-cpe", "FloatCP"},
248       {"long"}, {"make-long-cpe", "LongCP"},
249       {"double"}, {"make-double-cpe", "DoubleCP"},
250       {"String"}, {"make-string-cpe", "StringCP"},
251       {"short", "CP", "CP", "ConstAttr"}, {"make-field", "Var"},
252       {"CP"}, {"make-const", "ConstAttr"},
253       {"String"}, {"make-outputstream", "scmOutputStream"},
254       {"String"}, {"make-label", "Label"},
255       {}, {"make-class-env", "ClassEnv"},
256       {}, {"make-code", "CodeAttr"},
257       {}, {"make-exception", "ExceptAttr"},
258       {}, {"make-catchtable", "Catchtable"},
259       {"Label", "Label", "Label", "CP"}, {"make-catch-entry", "CatchEntry"},
260       {"int", "int"}, {"iinc", "IincInsn"},
261       {"CP", "int"}, {"multianewarray", "MultiarrayInsn"},
262       {"CP", "int"}, {"invokeinterface", "InvokeinterfaceInsn"},
263     };
264
265   public static void main(String JavaDoc argv[])
266     throws IOException
267   {
268
269     PrintStream initer =
270       new PrintStream(new FileOutputStream("AutoInit.java"));
271     initer.println("package scm;\n\nimport jas.*;");
272     initer.println("class AutoInit\n{\n static void fillit(Env e)\n {");
273
274                                 // Generate types in the system.
275
PrintStream doit = new PrintStream(new FileOutputStream("AutoTypes.java"));
276     doit.println("package scm;\n\nimport jas.*;");
277     for (int x = 0; x<types.length; x += 2)
278       {
279         typeinfo tp = new typeinfo(types[x+1][0], types[x+1][1], types[x]);
280         tp.write(doit);
281         initer.println("e.definevar(Symbol.intern(\"" +
282                        types[x+1][0] + "\"), new scm" +
283                        types[x+1][1] + "());");
284       }
285
286                                 // Add some simple procedures
287
doit = new PrintStream(new FileOutputStream("AutoProcs.java"));
288     doit.println("package scm;\n\nimport jas.*;");
289     for (int x = 0; x<procs.length; x += 2)
290       {
291         procinfo p = new procinfo(procs[x+1][0], procs[x+1][1], procs[x]);
292         initer.println("e.definevar(Symbol.intern(\"" +
293                        procs[x+1][0] + "\"), new scm" +
294                        procs[x+1][1] + "());");
295         p.write(doit);
296       }
297
298     initer.println(" }\n}");
299   }
300 }
301
Popular Tags