Package abstrasy.pcfx

Source Code of abstrasy.pcfx.PCFx_timed

package abstrasy.pcfx;

import abstrasy.Heap;
import abstrasy.Interpreter;

import abstrasy.interpreter.InterpreterException;

import abstrasy.Node;

import abstrasy.interpreter.SilentException;

import abstrasy.StaticHeap;

import abstrasy.interpreter.BaseContextSnapshot;
import abstrasy.interpreter.StdErrors;


/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/

public class PCFx_timed extends PCFx {

    public PCFx_timed() {
    }

    /**
     * eval
     *
     * @param startAt Node
     * @return Node
     * @throws Exception
     * @todo Implémenter cette méthode abstrasy.PCFx
     */

    private final Node evalSection(Node thenode) throws Exception {
        Node res = thenode;
        if (thenode != null && thenode.isLazy()) {
   
                Heap.push();
                res = thenode.exec(true);
                Heap.pull();
          
        }
        return res;
    }

    private final void restoreTimeOut(Interpreter myself, long old_deadlockTime, boolean old_timeOutCheck, boolean old_timeOutTimerStatus) {
        /** Restaurer l'ancien time out **/
        myself.setTimeOutCheck(false);
        myself.getTimeOutTimer().stop();
        myself.setDeadlockTime(old_deadlockTime);
        myself.setTimeOutCheck(old_timeOutCheck);
        if (old_timeOutCheck || old_timeOutTimerStatus) {
            myself.getTimeOutTimer().start();
        }
        /** **/

    }

    public Node eval(Node startAt) throws Exception {
        /*
         * forme: (timed <millis> {...})
         */
        startAt.isGoodArgsLength(true, 3);
        Node tnode = startAt.getSubNode(1, Node.TYPE_NUMBER);
        Node xnode = startAt.getSubNode(2, Node.TYPE_LAZY);
        Interpreter myself = Interpreter.mySelf();
        long old_deadlockTime = myself.getDeadlockTime();
        boolean old_timeOutCheck = myself.isTimeOutCheck();
        boolean old_timeOutTimerStatus = myself.getTimeOutTimer().isRunning();
        if (old_timeOutCheck || old_timeOutTimerStatus) {
            myself.setTimeOutCheck(false);
            myself.getTimeOutTimer().stop();
        }
        Interpreter interpreter = Interpreter.mySelf();
        BaseContextSnapshot contextSnapshot=new BaseContextSnapshot(interpreter);
        long delayed = (long) tnode.getNumber();
        if (delayed < 0) {
            throw new InterpreterException(StdErrors.Out_of_range);
        }
        myself.setDeadlockTime(System.currentTimeMillis() + (long) tnode.getNumber());
        myself.setTimeOutCheck(true);
        myself.getTimeOutTimer().start();
        try {
            xnode = evalSection(xnode);
        }

        catch (SilentException silex) {

            contextSnapshot.restore();

            restoreTimeOut(myself, old_deadlockTime, old_timeOutCheck, old_timeOutTimerStatus);
            throw silex;
        }
        catch (Exception excep) {

            contextSnapshot.restore();

            restoreTimeOut(myself, old_deadlockTime, old_timeOutCheck, old_timeOutTimerStatus);

            throw excep; // fait suivre l'exception...
        }

        restoreTimeOut(myself, old_deadlockTime, old_timeOutCheck, old_timeOutTimerStatus);

        return xnode;

    }

}
TOP

Related Classes of abstrasy.pcfx.PCFx_timed

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.