'doc', 'reverse' and 'count' functions have been added

This commit is contained in:
Sameer Rahmani 2020-01-02 16:41:08 +00:00
parent d02bdaf158
commit 562fa43752
8 changed files with 208 additions and 7 deletions

View File

@ -0,0 +1,25 @@
/**
* 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;
public interface IDoc {
public String getDoc();
}

View File

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

View File

@ -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.ListNode;
import serene.simple.SereneException;
public class CountFn extends AFn {
public String fnName() {
return "count";
};
public Object eval(IScope scope) throws SereneException {
List<Object> args = this.arguments();
if (args.size() != 1) {
throw new SereneException("'count' expect one argument only.");
}
else {
if (args.get(0) instanceof ListNode) {
return ((ListNode<Object>) args.get(0)).length;
}
throw new SereneException("'count' argument should be list.");
}
}
}

View File

@ -0,0 +1,52 @@
/**
* 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.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<Object> args = (ArrayList<Object>) 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;
}
}
}

View File

@ -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<Object> args = this.arguments();

View File

@ -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<Object> args = (ArrayList<Object>) this.arguments();

View File

@ -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<Object>) args.get(0)).rest();
}
public String getDoc() {
return "`(rest coll)`: Returns a list of elements of COLL without the first.";
}
}

View File

@ -0,0 +1,61 @@
/**
* 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.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<Object> args = this.arguments();
if (args.size() != 1) {
throw new SereneException("'reverse' excepts 1 argument.");
}
else {
if (args.get(0) instanceof ListNode) {
ListNode<Object> l = (ListNode<Object>) 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.");
}
}
}