001: package sisc.exprs;
002:
003: import sisc.data.*;
004: import sisc.exprs.fp.OptimismUnwarrantedException;
005: import sisc.exprs.fp.OptimisticExpression;
006: import sisc.exprs.fp.OptimisticHost;
007: import sisc.exprs.fp.Utils;
008:
009: import java.io.*;
010: import sisc.interpreter.*;
011: import sisc.ser.Serializer;
012: import sisc.ser.Deserializer;
013: import sisc.util.ExpressionVisitor;
014:
015: public class EvalExp extends Expression implements OptimisticHost {
016: public static final int POS_PRE = 0, POS_POST = 1;
017:
018: public Expression pre, post;
019: public boolean preImmediate;
020:
021: public EvalExp(Expression pre, Expression post, boolean preImmediate) {
022: this .pre = pre;
023: this .post = post;
024: this .preImmediate = preImmediate;
025: }
026:
027: public void setHosts() {
028: /*
029: 'post' must not be an OptimisticExpression if 'pre' is
030: non-immediate or an OptimisticExpression. It could end up on
031: the stack and hence be evaluated outside the context of the
032: eval method here.
033: */
034: if (!preImmediate || pre instanceof OptimisticExpression) {
035: Utils.assertNonOptimistic(post);
036: }
037:
038: Utils.linkOptimistic(this , pre, POS_PRE);
039: Utils.linkOptimistic(this , post, POS_POST);
040: }
041:
042: public void eval(Interpreter r) throws ContinuationException {
043: boolean retry;
044: do {
045: try {
046: if (preImmediate) {
047: r.acc = pre.getValue(r);
048: r.next(post);
049: } else {
050: r.push(post);
051: r.next(pre);
052: }
053: retry = false;
054: } catch (OptimismUnwarrantedException uwe) {
055: retry = true;
056: }
057: } while (retry);
058: }
059:
060: private Value expressHelper() {
061: return new Pair(pre.express(),
062: ((post instanceof EvalExp) ? ((EvalExp) post)
063: .expressHelper() : list(post.express())));
064: }
065:
066: public Value express() {
067: return new Pair(sym("begin"), expressHelper());
068: }
069:
070: public void serialize(Serializer s) throws IOException {
071: s.writeExpression(pre);
072: s.writeExpression(post);
073: s.writeBoolean(preImmediate);
074: }
075:
076: public EvalExp() {
077: }
078:
079: public void deserialize(Deserializer s) throws IOException {
080: pre = s.readExpression();
081: post = s.readExpression();
082: preImmediate = s.readBoolean();
083: }
084:
085: public boolean visit(ExpressionVisitor v) {
086: return v.visit(pre) && v.visit(post);
087: }
088:
089: /* (non-Javadoc)
090: * @see sisc.exprs.OptimisticHost#alter(int, sisc.data.Expression)
091: */
092: public synchronized void alter(Interpreter r, int uexpPosition,
093: Expression replaceWith) {
094: switch (uexpPosition) {
095: case POS_PRE:
096: pre = replaceWith;
097: if (preImmediate && !(replaceWith instanceof Immediate)) {
098: preImmediate = false;
099: }
100: break;
101: case POS_POST:
102: post = replaceWith;
103: break;
104: }
105: }
106:
107: }
108: /*
109: * The contents of this file are subject to the Mozilla Public
110: * License Version 1.1 (the "License"); you may not use this file
111: * except in compliance with the License. You may obtain a copy of
112: * the License at http://www.mozilla.org/MPL/
113: *
114: * Software distributed under the License is distributed on an "AS
115: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
116: * implied. See the License for the specific language governing
117: * rights and limitations under the License.
118: *
119: * The Original Code is the Second Interpreter of Scheme Code (SISC).
120: *
121: * The Initial Developer of the Original Code is Scott G. Miller.
122: * Portions created by Scott G. Miller are Copyright (C) 2000-2007
123: * Scott G. Miller. All Rights Reserved.
124: *
125: * Contributor(s):
126: * Matthias Radestock
127: *
128: * Alternatively, the contents of this file may be used under the
129: * terms of the GNU General Public License Version 2 or later (the
130: * "GPL"), in which case the provisions of the GPL are applicable
131: * instead of those above. If you wish to allow use of your
132: * version of this file only under the terms of the GPL and not to
133: * allow others to use your version of this file under the MPL,
134: * indicate your decision by deleting the provisions above and
135: * replace them with the notice and other provisions required by
136: * the GPL. If you do not delete the provisions above, a recipient
137: * may use your version of this file under either the MPL or the
138: * GPL.
139: */
|