package abstrasy.pcfx;
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_replace_static extends PCFx {
/**
* respecte la protection de la finalité des listes
*/
public PCFx_replace_static() {
}
/**
* eval
*
* @param startAt Node
* @return Node
* @throws Exception
* @todo Implémenter cette méthode abstrasy.PCFx
*/
public Node eval(Node startAt) throws Exception {
// forme : (replace! anc by nouv in liste)
// 0 1 2 3 4 5
startAt.requirePCode(2, PCoder.PC_BY);
startAt.requirePCode(4, PCoder.PC_IN);
startAt.isGoodArgsCnt(6);
startAt.requireNodeType(5, Node.VTYPE_INDIRECTION);
Node xnode = startAt.getSubNode(5, Node.TYPE_CLIST | Node.TYPE_STRING);
/**
* déterminer les types possibles pour la comparaison
*/
long typePossible = 0;
if (xnode.getType()==Node.TYPE_STRING) {
typePossible = Node.TYPE_STRING;
}
else {
typePossible = Node.VTYPE_EQUALISABLE;
}
Node snode = startAt.getSubNode(1, typePossible);
Node s2node = startAt.getSubNode(3, typePossible);
int startPos = 0;
if (xnode.getQType()==Node.TYPE_CLIST) {
/**
* (replace! x by y in [...])
*/
xnode.requireAccessType(Node.ACCESSTYPE_WRITELOCK);
for(int i=startPos;i<xnode.size();i++) {
if (Node.equalsNodes(xnode.elementAt(i), snode))
xnode.setElementAt(s2node.secure(), i);
}
}
else if (xnode.getQType()==Node.TYPE_STRING){
/**
* (replace! "x" by "y" in "...")
*/
xnode.requireAccessType(Node.ACCESSVTYPE_MUTABLE_WRITELOCK);
if (!(snode.getQType()==Node.TYPE_STRING))
throw new InterpreterException(StdErrors.Argument_type_mismatch);
xnode.setString(Tools.replaceCSeq(xnode.getString(), snode.getString(), s2node.getString()));
}
return null;
}
}