Package abstrasy.pcfx

Source Code of abstrasy.pcfx.PCFx_join

package abstrasy.pcfx;


import abstrasy.Heap;
import abstrasy.Node;
import abstrasy.PCoder;
import abstrasy.Tools;

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_join extends PCFx {

    public PCFx_join() {
    }

    /**
     * eval
     *
     * @param startAt Node
     * @return Node
     * @throws Exception
     * @todo Implémenter cette méthode abstrasy.PCFx
     */
    public Node eval(Node startAt) throws Exception {
        /**
     * analyse des arguments...
     */
        startAt.isGoodArgsCnt(4, 6);
        Node xnode = startAt.getSubNode(1, Node.TYPE_CLIST);
        startAt.requirePCode(2, PCoder.PC_TO);
        Node pnode = startAt.getSubNode(3, Node.TYPE_PCODE);
        Node ynode = null;
        if (startAt.size() == 6) {
            startAt.requirePCode(4, PCoder.PC_USING);
            ynode = startAt.getSubNode(5, Node.TYPE_STRING);
        }


        if (pnode.isPCode(PCoder.PC_STRING)) {
            /**
       * (join liste to string)               -> string
       * (join liste to string using chaine)  -> string
       */
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < xnode.size(); ) {
                sb.append(Node.node2VString(xnode.elementAt(i++)).getString());
                if (ynode != null && i < xnode.size()) {
                    sb.append(ynode.getString());
                }
            }
            return new Node(sb.toString());
        }
        else if (pnode.isPCode(PCoder.PC_QUOTED_SYMBOL)) {
            /**
       * (join liste to quoted-symbol) -> 'symbole
       */
            String symbol = "";
            for (int i = 0; i < xnode.size(); i++) {
                ynode = xnode.elementAt(i);
                ynode.requireNodeType(Node.TYPE_STRING | Node.TYPE_QSYMBOL | Node.TYPE_PCODE);
                String str = "";
                switch(ynode.getType()){
                    case Node.TYPE_SYMBOL:
                        str=ynode.getSymbol().getStr();
                        break;
                    case Node.TYPE_STRING:
                        str=ynode.getString();
                        break;
                    case Node.TYPE_PCODE:
                        if(ynode.isPCode(PCoder.PC_SELF)){
                            str="";
                            break;
                        }
                    default:
                        throw new InterpreterException(StdErrors.extend(StdErrors.Cannot_estimate_parameter, ynode.toString()));
                }
                if (str.indexOf('#') >= 0) {
                    str = Tools.replaceCSeq(str, "#", Heap.createSymbolReg());
                }
                if (i > 0) symbol += PCoder.SEP;
                    symbol += str;
            }
            if(!Node.isStringSymbol(symbol))
                throw new InterpreterException(StdErrors.extend(StdErrors.Invalid_symbol, symbol));
            ynode = Node.createSymbol(symbol);
            ynode.setQuoted(true);
            return ynode;
        }
       
        else {
            /**
       * Si on en arrive ici, il y a une erreur...
       * Le paramètre n'est pas d'un type géré.
       */
            throw new InterpreterException(StdErrors.Invalid_parameter);
        }

    }

}
TOP

Related Classes of abstrasy.pcfx.PCFx_join

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.