001: package sisc.exprs.fp;
002:
003: import java.io.*;
004:
005: import sisc.data.*;
006: import sisc.interpreter.*;
007: import sisc.exprs.AppExp;
008: import sisc.exprs.FreeReferenceExp;
009: import sisc.nativefun.FixableProcedure;
010: import sisc.ser.Serializer;
011: import sisc.ser.Deserializer;
012: import sisc.util.ExpressionVisitor;
013: import sisc.util.FreeReference;
014: import sisc.util.UndefinedVarException;
015: import sisc.compiler.Compiler;
016: import sisc.compiler.CompilerConstants;
017:
018: public class FixedAppExp_0 extends Expression implements Immediate,
019: OptimisticExpression {
020:
021: transient FixableProcedure proc;
022: public FreeReference ref;
023: public OptimisticHost host;
024: public int uexpPosition;
025:
026: public FixedAppExp_0(FreeReference ref) {
027: this .ref = ref;
028: }
029:
030: public void setHost(OptimisticHost host, int uexpPos) {
031: this .host = host;
032: uexpPosition = uexpPos;
033: }
034:
035: public void eval(Interpreter r) throws ContinuationException {
036: r.acc = getValue(r);
037: r.nxp = null;
038: }
039:
040: public Value doGetValue(FixableProcedure proc, Interpreter r)
041: throws ContinuationException {
042: return proc.apply();
043: }
044:
045: public Value getValue(Interpreter r) throws ContinuationException {
046: try {
047: Expression e = ref.getValue();
048: if (e != proc) {
049: //If the definition has changed (or has never been
050: //seen before) check if its still fixable. If not,
051: //revert to the safe expression
052: if (e instanceof FixableProcedure) {
053: proc = (FixableProcedure) e;
054: } else {
055: revert(r);
056: }
057: }
058: return doGetValue(proc, r);
059: } catch (UndefinedVarException e) {
060: forceRevert(r);
061: } catch (OptimismUnwarrantedException e) {
062: throw e;
063: } catch (RuntimeException e) {
064: forceRevert(r);
065: }
066: // Should be unreachable;
067: return null;
068: }
069:
070: public Expression[] getOperands() {
071: return ZV;
072: }
073:
074: public final void forceRevert(Interpreter r) {
075: revert(r, getOperands(), CompilerConstants.REALTAIL);
076: }
077:
078: public final void revert(Interpreter r) {
079: revert(r, getOperands());
080: }
081:
082: public final void revert(Interpreter r, Expression[] rands) {
083: revert(r, rands, 0);
084: }
085:
086: public final void revert(Interpreter r, Expression[] rands,
087: int flags) {
088: if (host == null) {
089: Procedure
090: .throwPrimException(liMessage(SISCB, "nosafeexpr"));
091: }
092: try {
093: AppExp safeExpr = (AppExp) Compiler.application(r,
094: new FreeReferenceExp(ref), rands, flags,
095: getAnnotations(), r.getCtx().symenv);
096:
097: if (safeExpr instanceof OptimisticExpression) {
098: ((OptimisticExpression) safeExpr).setHost(host,
099: uexpPosition);
100: }
101: host.alter(r, uexpPosition, safeExpr);
102: throw new OptimismUnwarrantedException();
103: } catch (ContinuationException ce) {
104: Procedure.throwPrimException(ce.getMessage());
105: }
106: }
107:
108: public Value express() {
109: return list(sym("fapp"), ref.express());
110: }
111:
112: public void serialize(Serializer s) throws IOException {
113: ref.serialize(s);
114: s.writeExpression((Expression) host);
115: s.writeInt(uexpPosition);
116: }
117:
118: public FixedAppExp_0() {
119: ref = new FreeReference();
120: }
121:
122: public void deserialize(Deserializer s) throws IOException {
123: ref.deserialize(s);
124: host = (OptimisticHost) s.readExpression();
125: uexpPosition = s.readInt();
126: }
127:
128: public boolean visit(ExpressionVisitor v) {
129: return ref.visit(v) && v.visit((Expression) host);
130: }
131:
132: public void dropSafe() {
133: host = null;
134: }
135: }
136: /*
137: * The contents of this file are subject to the Mozilla Public
138: * License Version 1.1 (the "License"); you may not use this file
139: * except in compliance with the License. You may obtain a copy of
140: * the License at http://www.mozilla.org/MPL/
141: *
142: * Software distributed under the License is distributed on an "AS
143: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
144: * implied. See the License for the specific language governing
145: * rights and limitations under the License.
146: *
147: * The Original Code is the Second Interpreter of Scheme Code (SISC).
148: *
149: * The Initial Developer of the Original Code is Scott G. Miller.
150: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
151: * Scott G. Miller. All Rights Reserved.
152: *
153: * Contributor(s):
154: * Matthias Radestock
155: *
156: * Alternatively, the contents of this file may be used under the
157: * terms of the GNU General Public License Version 2 or later (the
158: * "GPL"), in which case the provisions of the GPL are applicable
159: * instead of those above. If you wish to allow use of your
160: * version of this file only under the terms of the GPL and not to
161: * allow others to use your version of this file under the MPL,
162: * indicate your decision by deleting the provisions above and
163: * replace them with the notice and other provisions required by
164: * the GPL. If you do not delete the provisions above, a recipient
165: * may use your version of this file under either the MPL or the
166: * GPL.
167: */
|