|
@@ -76,32 +76,42 @@ public class LispImpl implements Lisp {
|
|
|
return LispElement.generate(result);
|
|
|
});
|
|
|
operators.put("+", (lisp) -> {
|
|
|
- boolean isInt = true;
|
|
|
- double result = 0;
|
|
|
- while(!lisp.isEmpty()) {
|
|
|
- LispElement<?> elt = getElement(lisp.car());
|
|
|
- result += elt.toNumber();
|
|
|
- isInt = isInt && elt.value.getClass() == BigInteger.class;
|
|
|
- lisp = lisp.cdr();
|
|
|
- }
|
|
|
- if(isInt) {
|
|
|
- return LispElement.generate((int) result);
|
|
|
- }
|
|
|
- return LispElement.generate(result);
|
|
|
+ BigInteger resultInt = new BigInteger("0");
|
|
|
+ while(!lisp.isEmpty()){
|
|
|
+ LispElement<?> eltInt = getElement(lisp.car());
|
|
|
+ if(eltInt.value.getClass() != BigInteger.class) break;
|
|
|
+ resultInt = resultInt.add(eltInt.toInt());
|
|
|
+ lisp = lisp.cdr();
|
|
|
+ }
|
|
|
+ //Si on finit la liste avec que des entier on retourne
|
|
|
+ if(lisp.isEmpty()) return LispElement.generate(resultInt);
|
|
|
+ //Sinon on continue en passant en double
|
|
|
+ double result = resultInt.doubleValue();
|
|
|
+ while(!lisp.isEmpty()) {
|
|
|
+ LispElement<?> elt = getElement(lisp.car());
|
|
|
+ result += elt.toNumber();
|
|
|
+ lisp = lisp.cdr();
|
|
|
+ }
|
|
|
+ return LispElement.generate(result);
|
|
|
});
|
|
|
operators.put("*", (lisp) -> {
|
|
|
- boolean isInt = true;
|
|
|
- double result = 1;
|
|
|
- while(!lisp.isEmpty()) {
|
|
|
- LispElement<?> elt = getElement(lisp.car());
|
|
|
- result *= elt.toNumber();
|
|
|
- isInt = isInt && elt.value.getClass() == BigInteger.class;
|
|
|
- lisp = lisp.cdr();
|
|
|
- }
|
|
|
- if(isInt) {
|
|
|
- return LispElement.generate((int) result);
|
|
|
- }
|
|
|
- return LispElement.generate(result);
|
|
|
+ BigInteger resultInt = new BigInteger("1");
|
|
|
+ while(!lisp.isEmpty()){
|
|
|
+ LispElement<?> eltInt = getElement(lisp.car());
|
|
|
+ if(eltInt.value.getClass() != BigInteger.class) break;
|
|
|
+ resultInt = resultInt.multiply(eltInt.toInt());
|
|
|
+ lisp = lisp.cdr();
|
|
|
+ }
|
|
|
+ //Si on finit la liste avec que des entier on retourne
|
|
|
+ if(lisp.isEmpty()) return LispElement.generate(resultInt);
|
|
|
+ //Sinon on continue en passant en double
|
|
|
+ double result = resultInt.doubleValue();
|
|
|
+ while(!lisp.isEmpty()) {
|
|
|
+ LispElement<?> elt = getElement(lisp.car());
|
|
|
+ result *= elt.toNumber();
|
|
|
+ lisp = lisp.cdr();
|
|
|
+ }
|
|
|
+ return LispElement.generate(result);
|
|
|
});
|
|
|
operators.put("-", (lisp) -> {
|
|
|
switch(lisp.size()) {
|