From 5b1ab5120c0e7366c5c484c6dd08f518775763fc Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Tue, 31 Dec 2019 21:19:17 +0000 Subject: [PATCH] > function has been added --- src/main/java/serene/simple/RootScope.java | 4 ++ src/main/java/serene/simple/SNumber.java | 42 ++++++++++++++++ .../java/serene/simple/SereneException.java | 5 ++ src/main/java/serene/simple/builtin/EqFn.java | 1 - src/main/java/serene/simple/builtin/GtFn.java | 49 +++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/main/java/serene/simple/builtin/GtFn.java diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index ca65c33..ac64d8b 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -36,6 +36,10 @@ public class RootScope extends AScope { put("/", new ObelusFn()); put("mod", new ModFn()); put("=", new EqFn()); + put(">", new GtFn()); + // put("<", new LtFn()); + // put("<=", new LteFn()); + // put(">=", new GteFn()); put("System", System.class); put("Boolean", Boolean.class); put("String", String.class); diff --git a/src/main/java/serene/simple/SNumber.java b/src/main/java/serene/simple/SNumber.java index c74aa7f..6272478 100644 --- a/src/main/java/serene/simple/SNumber.java +++ b/src/main/java/serene/simple/SNumber.java @@ -19,6 +19,11 @@ */ package serene.simple; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import serene.simple.SereneException; @@ -40,6 +45,8 @@ public class SNumber { public IOps mod(Double x); public IOps mod(Long x); + public boolean gt(boolean acc, List xs); + public Object value(); } @@ -145,6 +152,28 @@ public class SNumber { return y.mod(this.v); } + public boolean gt(boolean acc, List xs) { + for(Object x: xs) { + if (!(x instanceof Number)) { + throw new SereneException("Only number parameters can be use with '>' function."); + } + + if (!(this.v > (x instanceof Long ? (long) x : (double) x))) { + return false; + } + } + + return true; + } + + public boolean gt(Long x) { + return this.v > x; + } + + public boolean gt(Double x) { + return this.v > x; + } + public Object value() { return this.v; } @@ -250,6 +279,19 @@ public class SNumber { return new DoubleNumber(this.v % x); } + public boolean gt(boolean acc, List xs) { + if (xs.size() > 1) { + IOps next = SNumber.createNumber(xs.get(0)); + return next.gt(this.v > (double) next.value(), xs.subList(1, xs.size())); + + } + else if (xs.size() == 1){ + IOps next = SNumber.createNumber(xs.get(0)); + return this.v > (double) next.value(); + } + return acc; + } + public Object value() { return this.v; } diff --git a/src/main/java/serene/simple/SereneException.java b/src/main/java/serene/simple/SereneException.java index cda5a6c..60aa26a 100644 --- a/src/main/java/serene/simple/SereneException.java +++ b/src/main/java/serene/simple/SereneException.java @@ -20,6 +20,11 @@ package serene.simple; +/** + * Serene does not have a well desinged exception system that + * reports the location of the error. It simply raise an exception. + * Since it an experiment such a design would be an overkill. + **/ public class SereneException extends RuntimeException { private static final long serialVersionUID = 1L; diff --git a/src/main/java/serene/simple/builtin/EqFn.java b/src/main/java/serene/simple/builtin/EqFn.java index 59435f4..12455c1 100644 --- a/src/main/java/serene/simple/builtin/EqFn.java +++ b/src/main/java/serene/simple/builtin/EqFn.java @@ -22,7 +22,6 @@ package serene.simple.builtin; import java.util.List; import serene.simple.IScope; -import serene.simple.SNumber; import serene.simple.SereneException; diff --git a/src/main/java/serene/simple/builtin/GtFn.java b/src/main/java/serene/simple/builtin/GtFn.java new file mode 100644 index 0000000..069ec47 --- /dev/null +++ b/src/main/java/serene/simple/builtin/GtFn.java @@ -0,0 +1,49 @@ +/** + * 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 GtFn extends AFn { + public String fnName() { + return ">"; + }; + + public Object eval(IScope scope) throws SereneException{ + List args = this.arguments(); + + if (args.size() == 0) { + throw new SereneException( + "ArityError: You need 1 or more parameters for this functions."); + } + + if (args.size() == 1) { + return true; + } + + SNumber.IOps result = SNumber.createNumber(args.get(0)); + return result.gt(true, args.subList(1, args.size())); + } +}