+ function has been done
This commit is contained in:
parent
30ad70d94f
commit
e56f862183
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
package serene.simple;
|
package serene.simple;
|
||||||
|
|
||||||
|
|
||||||
class SereneException extends RuntimeException {
|
public class SereneException extends RuntimeException {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public SereneException(String message) {
|
public SereneException(String message) {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package serene.simple.builtin;
|
package serene.simple.builtin;
|
||||||
|
|
||||||
import serene.simple.BaseScope;
|
import serene.simple.BaseScope;
|
||||||
|
import serene.simple.SNumber;
|
||||||
|
import serene.simple.SereneException;
|
||||||
|
|
||||||
|
|
||||||
public class PlusFn extends AFn {
|
public class PlusFn extends AFn {
|
||||||
|
@ -8,7 +10,13 @@ public class PlusFn extends AFn {
|
||||||
return "+";
|
return "+";
|
||||||
};
|
};
|
||||||
|
|
||||||
public Object eval(BaseScope scope) {
|
public Object eval(BaseScope scope) throws SereneException{
|
||||||
return 0;
|
SNumber.IOps result = (SNumber.IOps) new SNumber.LongNumber();
|
||||||
|
|
||||||
|
for(Object x: this.arguments()) {
|
||||||
|
result = (SNumber.IOps) result.add(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue