KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > rpe > RpCommandList


1 /* @author rich
2  * Created on 04-May-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.rpe;
9
10 /** A list of commands */
11 public final class RpCommandList {
12     
13     /** Data type for the command string */
14     static final class RpCommand {
15         short command;
16         short aux1;
17         private RpCommand(short command){
18             this.command = command; this.aux1 = -1;
19         }
20         private RpCommand(short command,short aux){
21             this.command = command; this.aux1 = aux;
22         }
23         public String JavaDoc toString() {
24             switch(command)
25             {
26                 case RpEval.CONST: return "Constant\tno "+aux1;
27                 case RpEval.VAR: return "Variable\tnum "+aux1;
28                 case RpEval.ADD: return "ADD";
29                 case RpEval.SUB: return "SUB";
30                 case RpEval.MUL: return "MUL";
31                 case RpEval.DIV: return "DIV";
32                 case RpEval.MOD: return "MOD";
33                 case RpEval.POW: return "POW";
34                 case RpEval.AND: return "AND";
35                 case RpEval.OR: return "OR";
36                 case RpEval.NOT: return "NOT";
37                 case RpEval.LT: return "LT";
38                 case RpEval.LE: return "LE";
39                 case RpEval.GT: return "GT";
40                 case RpEval.GE: return "GE";
41                 case RpEval.EQ: return "EQ";
42                 case RpEval.NE: return "NE";
43                 case RpEval.ASSIGN: return "Assign\tnum "+aux1;
44                 case RpEval.FUN: return "Function\tnum "+aux1;
45             }
46             return "WARNING unknown command: "+command+" "+aux1;
47         }
48     }
49
50     /** Incremental size for list of commands **/
51     private static final int STACK_INC=10;
52     /** List of commands **/
53     RpCommand commands[] = new RpCommand[STACK_INC];
54     /** Current position in the command Stack. **/
55     private short commandPos;
56     /** The return type at end of evaluation */
57     private int finalType;
58     
59     /** Package private constructor */
60     RpCommandList() {}
61     /** Adds a command to the list */
62     final void addCommand(short command,short aux)
63     {
64         if(commandPos == commands.length)
65         {
66             RpCommand newCommands[] = new RpCommand[commands.length+STACK_INC];
67             System.arraycopy(commands,0,newCommands,0,commands.length);
68             commands = newCommands;
69         }
70         commands[commandPos]=new RpCommand(command,aux);
71         ++commandPos;
72 // ++maxCommands;
73
}
74     final void addCommand(short command)
75     {
76         if(commandPos == commands.length)
77         {
78             RpCommand newCommands[] = new RpCommand[commands.length+STACK_INC];
79             System.arraycopy(commands,0,newCommands,0,commands.length);
80             commands = newCommands;
81         }
82         commands[commandPos]=new RpCommand(command);
83         ++commandPos;
84 // ++maxCommands;
85
}
86
87     public int getNumCommands() { return commandPos;}
88     public int getFinalType() { return finalType; }
89     public void setFinalType(int i) { finalType = i;}
90     
91     public String JavaDoc toString() {
92         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
93         for(int i=0;i<commandPos;++i) {
94             sb.append(commands[i].toString());
95             sb.append("\n");
96         }
97         return sb.toString();
98     }
99 }
Popular Tags