diff --git a/src/main/java/serene/simple/IDoc.java b/src/main/java/serene/simple/IDoc.java new file mode 100644 index 0000000..9b2150b --- /dev/null +++ b/src/main/java/serene/simple/IDoc.java @@ -0,0 +1,25 @@ +/** + * 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; + + +public interface IDoc { + public String getDoc(); +} diff --git a/src/main/java/serene/simple/RootScope.java b/src/main/java/serene/simple/RootScope.java index cd48352..b06ff2d 100644 --- a/src/main/java/serene/simple/RootScope.java +++ b/src/main/java/serene/simple/RootScope.java @@ -45,13 +45,12 @@ public class RootScope extends AScope { put("not", new NotFn()); put("conj", new ConjFn()); put("cons", new ConjFn()); - // put("size", new SizeFn()); - // put("reverse", new SizeFn()); + put("count", new CountFn()); + put("reverse", new ReverseFn()); put("list", new ListFn()); put("first", new FirstFn()); put("rest", new RestFn()); - // put("doc", new DocFn()); - // put("reduce", new ReduceFn()); + put("doc", new DocFn()); put("System", System.class); put("Boolean", Boolean.class); put("String", String.class); diff --git a/src/main/java/serene/simple/builtin/CountFn.java b/src/main/java/serene/simple/builtin/CountFn.java new file mode 100644 index 0000000..66729e1 --- /dev/null +++ b/src/main/java/serene/simple/builtin/CountFn.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.ListNode; +import serene.simple.SereneException; + + +public class CountFn extends AFn { + public String fnName() { + return "count"; + }; + + public Object eval(IScope scope) throws SereneException { + List args = this.arguments(); + + if (args.size() != 1) { + throw new SereneException("'count' expect one argument only."); + } + else { + if (args.get(0) instanceof ListNode) { + return ((ListNode) args.get(0)).length; + } + throw new SereneException("'count' argument should be list."); + } + } +} diff --git a/src/main/java/serene/simple/builtin/DocFn.java b/src/main/java/serene/simple/builtin/DocFn.java new file mode 100644 index 0000000..ad920ae --- /dev/null +++ b/src/main/java/serene/simple/builtin/DocFn.java @@ -0,0 +1,52 @@ +/** + * 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.ArrayList; +import java.util.Collections; + +import serene.simple.IDoc; +import serene.simple.IScope; +import serene.simple.ListNode; +import serene.simple.SereneException; + + +public class DocFn extends AFn { + public String fnName() { + return "doc"; + }; + + public Object eval(IScope scope) throws SereneException{ + ArrayList args = (ArrayList) this.arguments(); + + if (args.size() != 1) { + throw new SereneException("'doc' expect 1 parameter only."); + } + + Object obj = args.get(0); + + if (obj instanceof IDoc) { + return ((IDoc) obj).getDoc(); + } + else { + return null; + } + } +} diff --git a/src/main/java/serene/simple/builtin/FirstFn.java b/src/main/java/serene/simple/builtin/FirstFn.java index 000099e..d6f5f2c 100644 --- a/src/main/java/serene/simple/builtin/FirstFn.java +++ b/src/main/java/serene/simple/builtin/FirstFn.java @@ -21,16 +21,22 @@ package serene.simple.builtin; import java.util.List; +import serene.simple.IDoc; import serene.simple.IScope; import serene.simple.ListNode; import serene.simple.SereneException; -public class FirstFn extends AFn { +public class FirstFn extends AFn implements IDoc{ public String fnName() { return "first"; }; + public String getDoc() { + return "`(first coll)`: Returns the first element of the given" + + " COLL."; + } + public Object eval(IScope scope) throws SereneException{ List args = this.arguments(); diff --git a/src/main/java/serene/simple/builtin/ListFn.java b/src/main/java/serene/simple/builtin/ListFn.java index 33efcb8..0930be6 100644 --- a/src/main/java/serene/simple/builtin/ListFn.java +++ b/src/main/java/serene/simple/builtin/ListFn.java @@ -22,16 +22,22 @@ package serene.simple.builtin; import java.util.ArrayList; import java.util.Collections; +import serene.simple.IDoc; import serene.simple.IScope; import serene.simple.ListNode; import serene.simple.SereneException; -public class ListFn extends AFn { +public class ListFn extends AFn implements IDoc{ public String fnName() { return "list"; }; + public String getDoc() { + return "`(list x y ...)`: Returns a list containing the passed" + + " parameters"; + } + public Object eval(IScope scope) throws SereneException{ ArrayList args = (ArrayList) this.arguments(); diff --git a/src/main/java/serene/simple/builtin/RestFn.java b/src/main/java/serene/simple/builtin/RestFn.java index 8baa4eb..6486466 100644 --- a/src/main/java/serene/simple/builtin/RestFn.java +++ b/src/main/java/serene/simple/builtin/RestFn.java @@ -21,12 +21,13 @@ package serene.simple.builtin; import java.util.List; +import serene.simple.IDoc; import serene.simple.IScope; import serene.simple.ListNode; import serene.simple.SereneException; -public class RestFn extends AFn { +public class RestFn extends AFn implements IDoc { public String fnName() { return "rest"; }; @@ -44,4 +45,8 @@ public class RestFn extends AFn { return ((ListNode) args.get(0)).rest(); } + + public String getDoc() { + return "`(rest coll)`: Returns a list of elements of COLL without the first."; + } } diff --git a/src/main/java/serene/simple/builtin/ReverseFn.java b/src/main/java/serene/simple/builtin/ReverseFn.java new file mode 100644 index 0000000..10befcd --- /dev/null +++ b/src/main/java/serene/simple/builtin/ReverseFn.java @@ -0,0 +1,61 @@ +/** + * 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.IDoc; +import serene.simple.IScope; +import serene.simple.ListNode; +import serene.simple.SereneException; + + +public class ReverseFn extends AFn implements IDoc { + public String fnName() { + return "reverse"; + }; + + public String getDoc() { + return "`(reverse coll)`: Returns a new list by reversing the COLL."; + } + + public Object eval(IScope scope) throws SereneException { + List args = this.arguments(); + + if (args.size() != 1) { + throw new SereneException("'reverse' excepts 1 argument."); + } + else { + if (args.get(0) instanceof ListNode) { + ListNode l = (ListNode) args.get(0); + int index = l.length; + + Object[] objs = new Object[index]; + for (Object x : l) { + System.out.println(index); + index--; + objs[index] = x; + } + return ListNode.list(objs); + } + throw new SereneException("'reverse' argument should be list."); + } + } +}