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 java.io.IOException;
032:
033: /**
034: * Expr is an intermediate form representing an expression.
035: */
036: class SpecialExpr extends Expr {
037: final static int THIS = 't';
038: final static int HAS_NEXT = 'm';
039: final static int NEXT = 'n';
040: final static int EXCEPTION = 'e';
041: final static int ARRAY = 'a';
042:
043: private int code;
044: private String str;
045: private Expr expr;
046:
047: SpecialExpr(Block block, int code) {
048: super (block);
049:
050: if (code == THIS)
051: block.function.setThis();
052: this .code = code;
053: }
054:
055: SpecialExpr(Block block, int code, String str) {
056: super (block);
057:
058: this .code = code;
059: this .str = str;
060: }
061:
062: SpecialExpr(Block block, int code, Expr expr) {
063: super (block);
064:
065: this .code = code;
066: this .expr = expr;
067: }
068:
069: int getType() {
070: switch (code) {
071: case HAS_NEXT:
072: return TYPE_BOOLEAN;
073:
074: case THIS:
075: case NEXT:
076: case EXCEPTION:
077: case ARRAY:
078: return TYPE_ES;
079:
080: default:
081: throw new RuntimeException();
082: }
083: }
084:
085: void printBooleanImpl() throws IOException {
086: switch (code) {
087: case HAS_NEXT:
088: cl.print(str + ".hasNext()");
089: break;
090:
091: default:
092: throw new RuntimeException();
093: }
094: }
095:
096: void printImpl() throws IOException {
097: switch (code) {
098: case THIS:
099: cl.print("_this");
100: break;
101:
102: case NEXT:
103: cl.print("((ESBase) " + str + ".next())");
104: break;
105:
106: case EXCEPTION:
107: cl.print("_env.wrap(new ESWrapperException(" + str + "))");
108: break;
109:
110: case ARRAY:
111: cl.print("_env.array(");
112: expr.print();
113: cl.print(")");
114: break;
115:
116: default:
117: throw new RuntimeException();
118: }
119: }
120: }
|