+ function has been done

This commit is contained in:
Sameer Rahmani 2019-12-28 19:41:22 +00:00
parent 30ad70d94f
commit e56f862183
3 changed files with 90 additions and 3 deletions

View File

@ -0,0 +1,79 @@
package serene.simple;
import serene.simple.SereneException;
public class SNumber {
public static interface IOps {
public IOps add(Object x);
public IOps add(Double x);
public IOps add(Long x);
public Object value();
}
public static class LongNumber implements IOps {
private long v;
public LongNumber() {
this.v = 0;
}
public LongNumber(long v) {
this.v = v;
}
public IOps add(Object x) throws SereneException {
if (x instanceof Long) {
return this.add((long) x);
}
else if (x instanceof Double) {
return this.add((Double) x);
}
throw new SereneException("Can't cast anything beside Long and Double");
}
public IOps add(Long x) {
return new LongNumber(this.v + x);
}
public IOps add(Double x) {
DoubleNumber y = new DoubleNumber(x);
return y.add(this.v);
}
public Object value() {
return this.v;
}
}
public static class DoubleNumber implements IOps {
private Double v;
public DoubleNumber(Double v) {
this.v = v;
}
public IOps add(Object x) throws SereneException {
if (x instanceof Long) {
return this.add((long) x);
}
else if (x instanceof Double) {
return this.add((Double) x);
}
throw new SereneException("Can't cast anything beside Long and Double");
}
public IOps add(Double x) {
return new DoubleNumber(this.v + x);
}
public IOps add(Long x) {
return new DoubleNumber(this.v + x.doubleValue());
}
public Object value() {
return this.v;
}
}
}

View File

@ -1,7 +1,7 @@
package serene.simple;
class SereneException extends RuntimeException {
public class SereneException extends RuntimeException {
private static final long serialVersionUID = 1L;
public SereneException(String message) {

View File

@ -1,6 +1,8 @@
package serene.simple.builtin;
import serene.simple.BaseScope;
import serene.simple.SNumber;
import serene.simple.SereneException;
public class PlusFn extends AFn {
@ -8,7 +10,13 @@ public class PlusFn extends AFn {
return "+";
};
public Object eval(BaseScope scope) {
return 0;
public Object eval(BaseScope scope) throws SereneException{
SNumber.IOps result = (SNumber.IOps) new SNumber.LongNumber();
for(Object x: this.arguments()) {
result = (SNumber.IOps) result.add(x);
}
return result.value();
}
}