KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > parser > UnaryExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es.parser;
30
31 import com.caucho.es.ESException;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Expr is an intermediate form representing an expression.
37  */

38 class UnaryExpr extends Expr {
39   Expr term;
40   int op;
41   
42   UnaryExpr(Block block, Expr term, int op)
43   {
44     super(block);
45     
46     this.term = term;
47     this.op = op;
48
49     if (term != null)
50       term.setUsed();
51   }
52
53   void exprStatement(Function fun) throws ESException
54   {
55     if (op == 'v') {
56       fun.addExpr(this);
57       isTop = true;
58     }
59     else
60       term.exprStatement(fun);
61   }
62
63   int getType()
64   {
65     switch (op) {
66     case '~':
67       return TYPE_INTEGER;
68
69     case '-':
70     case '+':
71       if (term.getType() == TYPE_INTEGER)
72         return TYPE_INTEGER;
73       else
74         return TYPE_NUMBER;
75
76     case '!':
77       return TYPE_BOOLEAN;
78
79     case 't':
80     case 'v':
81       return TYPE_ES;
82
83     default:
84       return TYPE_ES;
85     }
86   }
87
88   void printBooleanImpl() throws IOException JavaDoc
89   {
90     switch (op) {
91     case '!':
92       cl.print("(!");
93       term.printBoolean();
94       cl.print(")");
95       break;
96
97     default:
98       throw new IOException JavaDoc("foo");
99     }
100   }
101   
102   void printInt32Impl() throws IOException JavaDoc
103   {
104     switch (op) {
105     case '~':
106       cl.print("(~");
107       term.printInt32();
108       cl.print(")");
109       break;
110
111     case '-':
112       cl.print("(-");
113       term.printInt32();
114       cl.print(")");
115       break;
116       
117     case '+':
118       cl.print("(");
119       term.printInt32();
120       cl.print(")");
121       break;
122       
123     default:
124       throw new IOException JavaDoc("foo");
125     }
126   }
127
128   void printNumImpl() throws IOException JavaDoc
129   {
130     switch (op) {
131     case '-':
132       cl.print("(-");
133       term.printNum();
134       cl.print(")");
135       break;
136       
137     case '+':
138       cl.print("(");
139       term.printNum();
140       cl.print(")");
141       break;
142       
143     default:
144       throw new IOException JavaDoc("foo");
145     }
146   }
147
148   void printImpl() throws IOException JavaDoc
149   {
150     switch (op) {
151     case 't':
152       term.print();
153       cl.print(".typeof()");
154       break;
155
156     case 'v':
157       cl.print("_env.doVoid(");
158       term.print();
159       cl.print(")");
160       break;
161
162     default:
163       throw new IOException JavaDoc("foo");
164     }
165   }
166 }
167
Popular Tags