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 AssignExpr extends Expr {
039: private Expr lhs;
040: private IdExpr var;
041: private Expr field;
042: private Expr rhs;
043:
044: AssignExpr(Block block, IdExpr var, Expr rhs) {
045: super (block);
046:
047: this .var = var;
048: this .rhs = rhs;
049: rhs.getType();
050: if (!(rhs instanceof LiteralExpr || rhs instanceof IdExpr))
051: var.setUsed();
052: }
053:
054: AssignExpr(Block block, Expr lhs, Expr field, Expr rhs) {
055: super (block);
056:
057: this .lhs = lhs;
058: this .field = field;
059: this .rhs = rhs;
060: rhs.getType();
061: lhs.setUsed();
062: }
063:
064: void exprStatement(Function fun) throws ESException {
065: isTop = true;
066: noValue = true;
067:
068: fun.addExpr(this );
069: }
070:
071: /**
072: * The assignment operator
073: */
074: void printImpl() throws IOException {
075: cl.setLine(getFilename(), getLine());
076:
077: if (var != null && (var.isJavaLocal() || var.isJavaGlobal())) {
078: if (!isTop && !noValue) {
079: switch (var.getType()) {
080: case TYPE_NUMBER:
081: case TYPE_INTEGER:
082: cl.print("ESNumber.create(");
083: break;
084:
085: case TYPE_BOOLEAN:
086: cl.print("ESBoolean.create(");
087: break;
088:
089: default:
090: cl.print("(");
091: break;
092: }
093: } else if (noValue
094: && !var.isUsed()
095: && (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
096: return;
097: else if (!noValue)
098: cl.print("(");
099:
100: cl.print(var.getId());
101: cl.print(" = ");
102: switch (var.getType()) {
103: case TYPE_NUMBER:
104: rhs.printNum();
105: break;
106:
107: case TYPE_INTEGER:
108: rhs.printInt32();
109: break;
110:
111: case TYPE_BOOLEAN:
112: rhs.printBoolean();
113: break;
114:
115: case TYPE_JAVA:
116: if (var.getType() == TYPE_JAVA
117: && !var.getJavaClass().isAssignableFrom(
118: rhs.getJavaClass()))
119: cl.print("(" + var.getJavaClass().getName() + ") ");
120: rhs.printJava();
121: break;
122:
123: default:
124: rhs.print();
125: break;
126: }
127:
128: if (!noValue)
129: cl.print(")");
130: } else if (noValue && var != null) {
131: if (var.isGlobalScope())
132: cl.print("_env.global.setProperty(");
133: // XXX: should expand to anything without a side-effect
134: else if (!var.isUsed()
135: && var.isLocal()
136: && (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
137: return;
138: else if (var.getVar().hasInit())
139: cl.print("_arg.setProperty(");
140: else
141: cl.print("_env.setScopeProperty(");
142:
143: printLiteral(var.getId());
144: cl.print(", ");
145: rhs.print();
146: cl.print(")");
147: } else if (var != null) {
148: if (var.isGlobalScope())
149: cl.print("_env.setGlobalProperty(");
150: else if (!var.isUsed()
151: && var.isLocal()
152: && (rhs instanceof LiteralExpr || rhs instanceof IdExpr))
153: return;
154: else if (var.getVar().hasInit())
155: cl.print("_arg.setProperty(");
156: else
157: cl.print("_env.setScopeProperty(");
158:
159: printLiteral(var.getId());
160: cl.print(", ");
161: rhs.print();
162: cl.print(")");
163: } else if (noValue) {
164: lhs.print();
165: cl.print(".setProperty(");
166: if (field.getType() == TYPE_INTEGER)
167: field.printInt32();
168: else
169: field.printStr();
170: cl.print(", ");
171: rhs.print();
172: cl.print(")");
173: } else {
174: cl.print("_env.setProperty(");
175: lhs.print();
176: cl.print(", ");
177: field.printStr();
178: cl.print(", ");
179: rhs.print();
180: cl.print(")");
181: }
182: }
183: }
|