mod function has been added
This commit is contained in:
parent
5dd0be7493
commit
b19016f2f0
|
@ -28,6 +28,7 @@ import serene.simple.builtin.PlusFn;
|
|||
import serene.simple.builtin.MinusFn;
|
||||
import serene.simple.builtin.TimesFn;
|
||||
import serene.simple.builtin.ObelusFn;
|
||||
import serene.simple.builtin.ModFn;
|
||||
import serene.simple.builtin.NewFn;
|
||||
|
||||
public class RootScope extends AScope {
|
||||
|
@ -39,6 +40,7 @@ public class RootScope extends AScope {
|
|||
put("-", new MinusFn());
|
||||
put("*", new TimesFn());
|
||||
put("/", new ObelusFn());
|
||||
put("mod", new ModFn());
|
||||
put("System", System.class);
|
||||
put("Boolean", Boolean.class);
|
||||
put("String", String.class);
|
||||
|
@ -46,7 +48,6 @@ public class RootScope extends AScope {
|
|||
}};
|
||||
|
||||
|
||||
// "/", ObelusFn,
|
||||
// "mod", ModFn,
|
||||
// "now", NowFn,;
|
||||
|
||||
|
|
|
@ -36,6 +36,9 @@ public class SNumber {
|
|||
public IOps divide(Object x);
|
||||
public IOps divide(Double x);
|
||||
public IOps divide(Long x);
|
||||
public IOps mod(Object x);
|
||||
public IOps mod(Double x);
|
||||
public IOps mod(Long x);
|
||||
|
||||
public Object value();
|
||||
}
|
||||
|
@ -123,6 +126,25 @@ public class SNumber {
|
|||
return y.divide(this.v);
|
||||
}
|
||||
|
||||
public IOps mod(Object x) throws SereneException {
|
||||
if (x instanceof Long) {
|
||||
return this.mod((long) x);
|
||||
}
|
||||
else if (x instanceof Double) {
|
||||
return this.mod((Double) x);
|
||||
}
|
||||
throw new SereneException("Can't cast anything beside Long and Double");
|
||||
}
|
||||
|
||||
public IOps mod(Long x) {
|
||||
return new LongNumber(this.v % x);
|
||||
}
|
||||
|
||||
public IOps mod(Double x) {
|
||||
DoubleNumber y = new DoubleNumber(x);
|
||||
return y.mod(this.v);
|
||||
}
|
||||
|
||||
public Object value() {
|
||||
return this.v;
|
||||
}
|
||||
|
@ -207,6 +229,24 @@ public class SNumber {
|
|||
return new DoubleNumber(x.doubleValue() / this.v);
|
||||
}
|
||||
|
||||
public IOps mod(Object x) throws SereneException {
|
||||
if (x instanceof Long) {
|
||||
return this.mod((long) x);
|
||||
}
|
||||
else if (x instanceof Double) {
|
||||
return this.mod((Double) x);
|
||||
}
|
||||
throw new SereneException("Can't cast anything beside Long and Double");
|
||||
}
|
||||
|
||||
public IOps mod(Long x) {
|
||||
return new DoubleNumber(this.v % x);
|
||||
}
|
||||
|
||||
public IOps mod(Double x) {
|
||||
return new DoubleNumber(this.v % x);
|
||||
}
|
||||
|
||||
public Object value() {
|
||||
return this.v;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* Serene (simple) - A PoC lisp to collect data on Serenes concepts
|
||||
* Copyright (C) 2019-2020 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
package serene.simple.builtin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import serene.simple.IScope;
|
||||
import serene.simple.SNumber;
|
||||
import serene.simple.SereneException;
|
||||
|
||||
|
||||
public class ModFn extends AFn {
|
||||
public String fnName() {
|
||||
return "mod";
|
||||
};
|
||||
|
||||
public Object eval(IScope scope) throws SereneException{
|
||||
List<Object> args = this.arguments();
|
||||
|
||||
if (args.size() != 2) {
|
||||
throw new SereneException(
|
||||
"ArityError: You need 2 parameters for this functions.");
|
||||
}
|
||||
|
||||
SNumber.IOps result = SNumber.createNumber(args.get(0));
|
||||
result = result.mod(args.get(1));
|
||||
|
||||
return result.value();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue