> function has been added

This commit is contained in:
Sameer Rahmani 2019-12-31 21:19:17 +00:00
parent 516a606aab
commit 5b1ab5120c
5 changed files with 100 additions and 1 deletions

View File

@ -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);

View File

@ -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<Object> xs);
public Object value();
}
@ -145,6 +152,28 @@ public class SNumber {
return y.mod(this.v);
}
public boolean gt(boolean acc, List<Object> 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<Object> 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;
}

View File

@ -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;

View File

@ -22,7 +22,6 @@ package serene.simple.builtin;
import java.util.List;
import serene.simple.IScope;
import serene.simple.SNumber;
import serene.simple.SereneException;

View File

@ -0,0 +1,49 @@
/**
* 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 GtFn extends AFn {
public String fnName() {
return ">";
};
public Object eval(IScope scope) throws SereneException{
List<Object> 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()));
}
}