From b19016f2f0d4e9849019e44ba9a4a93033446497 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 31 Dec 2019 01:47:41 +0000 Subject: [PATCH] mod function has been added --- src/main/java/serene/simple/RootScope.java | 3 +- src/main/java/serene/simple/SNumber.java | 40 ++++++++++++++++ .../java/serene/simple/builtin/ModFn.java | 47 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/main/java/serene/simple/builtin/ModFn.java diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index bd9353b..ca63fad 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -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,; diff --git a/src/main/java/serene/simple/SNumber.java b/src/main/java/serene/simple/SNumber.java index 8a40639..958fcd4 100644 --- a/src/main/java/serene/simple/SNumber.java +++ b/src/main/java/serene/simple/SNumber.java @@ -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; } diff --git a/src/main/java/serene/simple/builtin/ModFn.java b/src/main/java/serene/simple/builtin/ModFn.java new file mode 100644 index 0000000..213c346 --- /dev/null +++ b/src/main/java/serene/simple/builtin/ModFn.java @@ -0,0 +1,47 @@ +/** + * Serene (simple) - A PoC lisp to collect data on Serenes concepts + * Copyright (C) 2019-2020 Sameer Rahmani + * + * 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 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(); + } +}