(and or not) functions have been added
This commit is contained in:
parent
5b1ab5120c
commit
1788aaf866
|
@ -24,7 +24,22 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.Console;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
|
||||
/**
|
||||
* What is missing:
|
||||
* * A namespace functionality. Because creating and compiling dynamic classes
|
||||
* is a rabbit hole and tons of work which doesn't make sense for a toy
|
||||
* project.
|
||||
* * Unified function interface.
|
||||
* * Requiring different namespaces
|
||||
* * A sophisticated parser. My Reader implementation is really cheap that
|
||||
* suits a toy project. It might worth investigating on different solutions
|
||||
* including using a parser generator or a head of time read implementation.
|
||||
* * Primitive functions in Serene. I implemented lots of primitive functions
|
||||
* in java rather than Serene itself mostly because of two reasons. Lack of
|
||||
* macros and namespaces.
|
||||
* * Quality code. The general quality of this implementation is not great, I
|
||||
* sacrificed quality for time.
|
||||
*/
|
||||
public class Main {
|
||||
private static String licenseHeader = "\nSerene(simple), Copyright (C) 2019-2020 " +
|
||||
"Sameer Rahmani <lxsameer@gnu.org>\n" +
|
||||
|
|
|
@ -37,9 +37,12 @@ public class RootScope extends AScope {
|
|||
put("mod", new ModFn());
|
||||
put("=", new EqFn());
|
||||
put(">", new GtFn());
|
||||
// put("<", new LtFn());
|
||||
// put("<=", new LteFn());
|
||||
// put(">=", new GteFn());
|
||||
put(">=", new GteFn());
|
||||
put("<", new LtFn());
|
||||
put("<=", new LteFn());
|
||||
put("and", new AndFn());
|
||||
put("or", new OrFn());
|
||||
put("not", new NotFn());
|
||||
put("System", System.class);
|
||||
put("Boolean", Boolean.class);
|
||||
put("String", String.class);
|
||||
|
|
|
@ -19,11 +19,7 @@
|
|||
*/
|
||||
package serene.simple;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import serene.simple.SereneException;
|
||||
|
||||
|
||||
|
@ -46,8 +42,17 @@ public class SNumber {
|
|||
public IOps mod(Long x);
|
||||
|
||||
public boolean gt(boolean acc, List<Object> xs);
|
||||
public boolean gte(boolean acc, List<Object> xs);
|
||||
public boolean lt(boolean acc, List<Object> xs);
|
||||
public boolean lte(boolean acc, List<Object> xs);
|
||||
|
||||
public Object value();
|
||||
}
|
||||
|
||||
public abstract static class ANumber {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static class LongNumber implements IOps {
|
||||
|
@ -166,12 +171,46 @@ public class SNumber {
|
|||
return true;
|
||||
}
|
||||
|
||||
public boolean gt(Long x) {
|
||||
return this.v > x;
|
||||
public boolean gte(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(Double x) {
|
||||
return this.v > x;
|
||||
public boolean lt(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 lte(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 Object value() {
|
||||
|
@ -280,16 +319,59 @@ public class SNumber {
|
|||
}
|
||||
|
||||
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()));
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if (xs.size() == 1){
|
||||
IOps next = SNumber.createNumber(xs.get(0));
|
||||
return this.v > (double) next.value();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean gte(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 acc;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean lt(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 lte(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 Object value() {
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* 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.SereneException;
|
||||
|
||||
|
||||
public class AndFn extends AFn {
|
||||
public String fnName() {
|
||||
return "and";
|
||||
};
|
||||
|
||||
public Object eval(IScope scope) throws SereneException{
|
||||
List<Object> args = this.arguments();
|
||||
|
||||
if (args.size() == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.size() == 1) {
|
||||
return args.get(0);
|
||||
}
|
||||
|
||||
Object next = true;
|
||||
|
||||
for (Object x : args) {
|
||||
next = x;
|
||||
if (x != null && x != Boolean.FALSE)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
return next;
|
||||
|
||||
}
|
||||
}
|
|
@ -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 GteFn 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.gte(true, args.subList(1, args.size()));
|
||||
}
|
||||
}
|
|
@ -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 LtFn 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.lt(true, args.subList(1, args.size()));
|
||||
}
|
||||
}
|
|
@ -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 LteFn 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.lte(true, args.subList(1, args.size()));
|
||||
}
|
||||
}
|
|
@ -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.SereneException;
|
||||
|
||||
|
||||
public class NotFn extends AFn {
|
||||
public String fnName() {
|
||||
return "not";
|
||||
};
|
||||
|
||||
public Object eval(IScope scope) throws SereneException{
|
||||
List<Object> args = this.arguments();
|
||||
|
||||
if (args.size() != 1) {
|
||||
throw new SereneException(
|
||||
String.format("Wrong arity for 'not'. Got: %s", args.size()));
|
||||
}
|
||||
Object x = args.get(0);
|
||||
|
||||
if (x != null && x != Boolean.FALSE) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
* 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.SereneException;
|
||||
|
||||
|
||||
public class OrFn extends AFn {
|
||||
public String fnName() {
|
||||
return "or";
|
||||
};
|
||||
|
||||
public Object eval(IScope scope) throws SereneException{
|
||||
List<Object> args = this.arguments();
|
||||
|
||||
if (args.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (args.size() == 1) {
|
||||
return args.get(0);
|
||||
}
|
||||
|
||||
Object next = false;
|
||||
|
||||
for (Object x : args) {
|
||||
next = x;
|
||||
if (x != null || x != Boolean.FALSE)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue