Package abstrasy.pcfx

Source Code of abstrasy.pcfx.PCFx_remove_static

package abstrasy.pcfx;


import abstrasy.Hash;
import abstrasy.Node;
import abstrasy.PCoder;

import abstrasy.interpreter.InterpreterException;
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_remove_static extends PCFx {

    public PCFx_remove_static() {
    }

    private void getRelIndex_for_list(int plageStart, String key, int maxS) throws Exception {
        if ((plageStart < 0) || (plageStart >= maxS)) {
            throw new InterpreterException(StdErrors.extend(StdErrors.Out_of_range, "" + plageStart + (key != null ? ":\"" + key + "\"": "")));
        }
    }

    private void getRelIndex_for_string(int plageStart, int maxS) throws Exception {
        if ((plageStart < 0) || (plageStart >= maxS)) {
            throw new InterpreterException(StdErrors.extend(StdErrors.Out_of_range, "" + plageStart));
        }
    }

    private final void removeChar_(Node xnode, int index) {
        StringBuffer stmp = new StringBuffer(xnode.getString());
        stmp.delete(index, index + 1);
        xnode.setString(stmp.toString());
    }

    /**
     * eval
     *
     * @param startAt Node
     * @return Node
     * @throws Exception
     * @todo Implémenter cette méthode abstrasy.PCFx
     */
    public Node eval(Node startAt) throws Exception {
        startAt.requirePCode(2, PCoder.PC_FROM);
        startAt.requireNodeType(3, Node.TYPE_SYMBOL);
        startAt.isGoodArgsCnt(4);
        /**
         * forme (remove! index/liste from liste/string/hash)
         *        0       1           2    3
         */


        Node xnode = startAt.getSubNode(3, Node.TYPE_HASH | Node.TYPE_CLIST | Node.TYPE_STRING | Node.VTYPE_DELEGABLE);


        /*
         * Cas où (remove! clé from objet)... Il s'agit d'une surcharge...
         */
        if (xnode.isDelegable())
            return Node.VDelegable.evalMethod(xnode, PCoder.getMethod(PCoder.PC_REMOVE_STATIC), Node.createCList().append(startAt.getSubNode(1, Node.VTYPE_VALUABLE)));

        /*
         * autres...
         */
        switch (xnode.getType()) {
           
          
            case Node.TYPE_CLIST:
                {
                    xnode.requireAccessType(Node.ACCESSVTYPE_MUTABLE_WRITELOCK);
                    Node inode = startAt.getSubNode(1, Node.TYPE_CLIST | Node.TYPE_NUMBER);
                    switch (inode.getType()) {
                        case Node.TYPE_NUMBER:
                            {
                                /*
                                 * (remove! index from [...])
                                 */
                                int index = (int) inode.getNumber();
                                getRelIndex_for_list(index, null, xnode.size());
                                xnode.removeElementAt(index);
                                break;
                            }
                        case Node.TYPE_CLIST:
                            {
                                /*
                                 * (remove! [indexes] from [...])
                                 */
                                int maxS = xnode.size();
                                for (int i = 0; i < inode.size(); i++) {
                                    Node enode = inode.getSubNode(i, Node.TYPE_NUMBER);
                                    int index = (int) enode.getNumber();
                                    getRelIndex_for_list(index, null, maxS);
                                    xnode.removeElementAt(index - i);
                                }
                                break;
                            }
                        default:
                            throw new InterpreterException(StdErrors.Internal_error);
                    }
                }
           
           
            case Node.TYPE_STRING:
                {
                    xnode.requireAccessType(Node.ACCESSTYPE_WRITELOCK);
                    Node inode = startAt.getSubNode(1, Node.TYPE_CLIST | Node.TYPE_NUMBER);
                    switch (inode.getType()) {
                        case Node.TYPE_NUMBER:
                            {
                                /*
                                 * (remove! index from "...")
                                 */
                                int index = (int) inode.getNumber();
                                getRelIndex_for_string(index, xnode.getString().length());
                                removeChar_(xnode, index);
                                break;
                            }
                        case Node.TYPE_CLIST:
                            {
                                /*
                                 * (remove! [indexes] from "...")
                                 */
                                int maxS = xnode.getString().length();
                                for (int i = 0; i < inode.size(); i++) {
                                    Node enode = inode.getSubNode(i, Node.TYPE_NUMBER);
                                    int index = (int) enode.getNumber();
                                    getRelIndex_for_string(index, maxS);
                                    removeChar_(xnode, index - i);
                                }
                                break;
                            }
                        default:
                            throw new InterpreterException(StdErrors.Internal_error);
                    }
                }
           
            case Node.TYPE_HASH:
                {
                    xnode.requireAccessType(Node.ACCESSVTYPE_MUTABLE_WRITELOCK);
                    Node inode = startAt.getSubNode(1, Node.TYPE_CLIST | Hash.KEY_TYPES);
                    switch (inode.getType()) {
                        case Node.TYPE_CLIST:
                            {
                                /*
                                 * (remove! [primvals] from (hash ...))
                                 */
                                Hash hash=xnode.getHash();
                                for (int i = 0; i < inode.size(); i++) {
                                    Node enode = inode.getSubNode(i, Hash.KEY_TYPES);
                                    if(!hash.remove(enode))
                                        throw new InterpreterException(StdErrors.extend(StdErrors.Not_found, enode.toString()));
                                }
                                break;
                            }
                        default:
                            {
                                /*
                                 * (remove! primval from (hash ...))
                                 */
                                Hash hash=xnode.getHash();
                                if(!hash.remove(inode))
                                        throw new InterpreterException(StdErrors.extend(StdErrors.Not_found, inode.toString()));
                                break;
                            }
                    }
                }
           
            default:
                throw new InterpreterException(StdErrors.Internal_error)
        }


    }

}
TOP

Related Classes of abstrasy.pcfx.PCFx_remove_static

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.