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