001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es.parser;
030:
031: import com.caucho.es.ESException;
032:
033: import java.io.IOException;
034:
035: /**
036: * Expr is an intermediate form representing an expression.
037: */
038: class UnaryExpr extends Expr {
039: Expr term;
040: int op;
041:
042: UnaryExpr(Block block, Expr term, int op) {
043: super (block);
044:
045: this .term = term;
046: this .op = op;
047:
048: if (term != null)
049: term.setUsed();
050: }
051:
052: void exprStatement(Function fun) throws ESException {
053: if (op == 'v') {
054: fun.addExpr(this );
055: isTop = true;
056: } else
057: term.exprStatement(fun);
058: }
059:
060: int getType() {
061: switch (op) {
062: case '~':
063: return TYPE_INTEGER;
064:
065: case '-':
066: case '+':
067: if (term.getType() == TYPE_INTEGER)
068: return TYPE_INTEGER;
069: else
070: return TYPE_NUMBER;
071:
072: case '!':
073: return TYPE_BOOLEAN;
074:
075: case 't':
076: case 'v':
077: return TYPE_ES;
078:
079: default:
080: return TYPE_ES;
081: }
082: }
083:
084: void printBooleanImpl() throws IOException {
085: switch (op) {
086: case '!':
087: cl.print("(!");
088: term.printBoolean();
089: cl.print(")");
090: break;
091:
092: default:
093: throw new IOException("foo");
094: }
095: }
096:
097: void printInt32Impl() throws IOException {
098: switch (op) {
099: case '~':
100: cl.print("(~");
101: term.printInt32();
102: cl.print(")");
103: break;
104:
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: default:
118: throw new IOException("foo");
119: }
120: }
121:
122: void printNumImpl() throws IOException {
123: switch (op) {
124: case '-':
125: cl.print("(-");
126: term.printNum();
127: cl.print(")");
128: break;
129:
130: case '+':
131: cl.print("(");
132: term.printNum();
133: cl.print(")");
134: break;
135:
136: default:
137: throw new IOException("foo");
138: }
139: }
140:
141: void printImpl() throws IOException {
142: switch (op) {
143: case 't':
144: term.print();
145: cl.print(".typeof()");
146: break;
147:
148: case 'v':
149: cl.print("_env.doVoid(");
150: term.print();
151: cl.print(")");
152: break;
153:
154: default:
155: throw new IOException("foo");
156: }
157: }
158: }
|