package abstrasy.pcfx;
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_atomic_div_static extends PCFx {
public PCFx_atomic_div_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 (/! x)
* (/! x n1 n2 ... nx)
* (/! x {lazy1} {lazy2} ... {lazyx})
*/
int i = 1;
startAt.isGoodArgsLength(false, 2);
startAt.requireNodeType(1, Node.VTYPE_INDIRECTION);
Node arg0 = startAt.getSubNode(i++, Node.TYPE_NUMBER | Node.VTYPE_DELEGABLE);
if (arg0.isNumber()) {
// nombre...
arg0.requireAccessType(Node.ACCESSVTYPE_MUTABLE_WRITELOCK);
if (startAt.size() == 2) {
/*
* forme (/! x) : pour x <- 1 / x
*
* Note:
* ====
* Si x = zéro, alors il y a une exception.
*/
boolean loops = true;
double nvalue;
double ovalue2;
while (loops) {
double ovalue = arg0.getNumber();
if (ovalue != 0) {
nvalue = 1 / ovalue;
ovalue2 = arg0.compareAndSwap_number(ovalue, nvalue);
loops = ovalue2 != ovalue;
ovalue = ovalue2;
}
else {
throw new InterpreterException(StdErrors.Division_by_zero);
}
}
return null;
}
/*
* sinon, il s'agit de la forme (/! x n1 n2 ... nx):
*
* Où:
* x <- x / n1 / n2 / ... / nx
*
* Qui est équvalent à:
* x <- x / y ; avec y <- (1 * n1 * n2 * ... * nx)
*
* Si y = 0, alors il y a exception...
*
* Donc, dès qu'un argument equivaut à zéro, il est inutile de continuer le traitement.
*
* Ainsi, la forme (/! x {lazy1} {lazy2} ... {lazyn}) est possible également
*
*/
double tnum = 1d;
while (i < startAt.size() && tnum != 0)
tnum *= startAt.getSubNode_LazyValue(i++, Node.TYPE_NUMBER).getNumber();
if (tnum != 0) {
boolean loops = true;
double nvalue;
double ovalue2;
double ovalue = arg0.getNumber();
while (loops) {
nvalue = ovalue / tnum;
ovalue2 = arg0.compareAndSwap_number(ovalue, nvalue);
loops = ovalue2 != ovalue;
ovalue = ovalue2;
}
}
else {
throw new InterpreterException(StdErrors.Division_by_zero);
}
}
else //{
// objet...
//String method=PCoder.STATIC_DIV_;
//if (Node.VObject.hasSlot(arg0, method, Node.TYPE_FUNCTION))
return Node.VDelegable.evalMethod(arg0, PCoder.getMethod(PCoder.PC_ATOMIC_DIV_STATIC), startAt.size() <= 2 ? null : startAt, 2);
//else
// throw new InterpreterException(StdErrors.extend(StdErrors.Function_required, method));
//}
return null;
}
}