Skip to content

Commit

Permalink
Updated JPL0 to match Lisp integrations in EBot
Browse files Browse the repository at this point in the history
  • Loading branch information
EnKrypt committed Apr 8, 2012
1 parent dcc9bc4 commit c5df313
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 56 deletions.
Binary file modified Lisp.class
Binary file not shown.
174 changes: 118 additions & 56 deletions Lisp.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
/*
@author EnKrypt
*/
// **************************************************************************
// * *
// * 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 3 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 || 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, see <http://www.gnu.org/licenses/>. *
// * *
// * (C) Arvind Kumar 2011 . *
// * (C) James McClain 2011 . *
// **************************************************************************

import java.io.*;
import java.util.*;
Expand Down Expand Up @@ -30,6 +45,10 @@ public static void main(String args[])throws IOException{
}
}
public static String dot(String code){
Pattern lisp;
Matcher now;
Pattern quo;
Matcher nowa;
quo=Pattern.compile("'[^'\"]*\"");
nowa=quo.matcher(code);
code=unquote(code);
Expand All @@ -39,16 +58,22 @@ public static String dot(String code){
String m=now.group(0);
m=m.replace("(","");
m=m.replace(")","");
m=m.replaceAll(" +"," ");
String[] arg=m.split(" ");
String res=eval(arg);
result = now.replaceFirst(res);
return dot(result);
}
String[] cfin=code.split(" ");
return cfin[cfin.length-1];
if (cfin.length != 0)
return cfin[cfin.length-1];
return "";
}
public static String eval(String arg[]){
arg=requote(arg);
// for(int i=0;i<arg.length;++i){
// System.out.println(i+" : "+arg[i]);
// }
if (arg[0].equalsIgnoreCase("add")){
int cres=0;
for (int i=1;i<arg.length;i++){
Expand All @@ -71,9 +96,9 @@ else if (arg[0].equalsIgnoreCase("mul")){
return ""+cres;
}
else if (arg[0].equalsIgnoreCase("div")){
int cres=Integer.parseInt(arg[1]);
double cres=Double.parseDouble(arg[1]);
for (int i=2;i<arg.length;i++){
cres/=Integer.parseInt(arg[i]);
cres/=Double.parseDouble(arg[i]);
}
return ""+cres;
}
Expand All @@ -86,11 +111,10 @@ else if (arg[0].equalsIgnoreCase("cat")){
}
else if (arg[0].equalsIgnoreCase("eval")){
String cres="";
for (int i=1;i<arg.length;i++){
cres+=evaldot(arg[i])+" ";
}
String[] cfn=cres.split(" ");
return cfn[cfn.length-1];
arg[0] = "";
cres = combine(arg," ");
cres=dot(cres);
return cres;
}
else if (arg[0].equalsIgnoreCase("slice")){
String cres="";
Expand All @@ -112,7 +136,7 @@ else if (arg[0].equalsIgnoreCase("gt")){
String cres="";
int flag=1;
for (int i=2;i<arg.length;i++){
if (Math.max(Integer.parseInt(arg[i-1]),Integer.parseInt(arg[i]))!=Integer.parseInt(arg[i-1])){
if ((Math.max(Integer.parseInt(arg[i-1]),Integer.parseInt(arg[i]))!=Integer.parseInt(arg[i-1]))||Integer.parseInt(arg[i])==Integer.parseInt(arg[i-1])){
flag=0;
}
}
Expand All @@ -122,7 +146,7 @@ else if (arg[0].equalsIgnoreCase("lt")){
String cres="";
int flag=1;
for (int i=2;i<arg.length;i++){
if (Math.min(Integer.parseInt(arg[i-1]),Integer.parseInt(arg[i]))!=Integer.parseInt(arg[i-1])){
if ((Math.min(Integer.parseInt(arg[i-1]),Integer.parseInt(arg[i]))!=Integer.parseInt(arg[i-1]))||Integer.parseInt(arg[i])==Integer.parseInt(arg[i-1])){
flag=0;
}
}
Expand All @@ -140,17 +164,17 @@ else if (arg[0].equalsIgnoreCase("and")){
}
else if (arg[0].equalsIgnoreCase("or")){
String cres="";
int flag=1;
int flag=0;
for (int i=1;i<arg.length;i++){
if (!arg[i].equalsIgnoreCase("0")){
flag=0;
flag=1;
}
}
return flag+"";
}
else if (arg[0].equalsIgnoreCase("not")){
String cres="";
if (arg.length==2&&arg[1].equalsIgnoreCase("0")){
if (arg[1].equalsIgnoreCase("0")){
return "1";
}
else{
Expand All @@ -165,17 +189,26 @@ else if (arg[0].equalsIgnoreCase("print")){
System.out.println(cres);
return "";
}
else if (arg[0].equalsIgnoreCase("read")){
else if (arg[0].equalsIgnoreCase("read")&&arg.length==2){
String cres="";
for (int i=1;i<arg.length;i++){
cres+=arg[i]+" ";
}
System.out.print(cres);
try{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
cres=b.readLine();
if (cres.equals(" ")){
try{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
cres=b.readLine();
}
catch(Exception e) {}
}
else{
try{
System.out.print(cres);
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
cres=b.readLine();
}
catch(Exception e) {}
}
catch(Exception e) {}
return cres;
}
else if (arg[0].equalsIgnoreCase("pass")){
Expand All @@ -184,10 +217,10 @@ else if (arg[0].equalsIgnoreCase("pass")){
}
else if (arg[0].equalsIgnoreCase("if")){
if (arg[1].equalsIgnoreCase("0")){
return evaldot(arg[3]);
return arg[3];
}
else{
return evaldot(arg[2]);
return arg[2];
}
}
else if (arg[0].equalsIgnoreCase("var")&&arg.length==3){
Expand All @@ -204,8 +237,41 @@ else if (arg[0].equalsIgnoreCase("var")&&arg.length==2){
}
return ""+setvar;
}
else if (arg[0].equalsIgnoreCase("lambda")){
return "runlambda '"+arg[1]+"\" '"+arg[2]+"\"";
}
else if (arg[0].equalsIgnoreCase("runlambda")){
arg[1] = arg[1].replaceFirst("^\\(","");
arg[1] = arg[1].replaceFirst("\\)$","");
arg[1] = arg[1].replaceAll(" +"," ");
String[] lambdaArg = arg[1].split(" ");
String to_eval;
to_eval = arg[2];
to_eval = to_eval.replaceAll("\\("," ( ");
to_eval = to_eval.replaceAll("\\)"," ) ");
to_eval = to_eval.replaceAll("'"," ' ");
to_eval = to_eval.replaceAll("\""," \" ");
int i;
int q;
int argNum = 3;
String[] to_eval_array;
for(i=0,q=0; i < lambdaArg.length;++i,++argNum){
to_eval_array = to_eval.split(" ");
for(q=0;q < to_eval_array.length;++q){
if(to_eval_array[q].equals(lambdaArg[i])) {
to_eval_array[q] = arg[argNum];
}
}
to_eval = combine(to_eval_array," ");
}
to_eval = to_eval.replaceAll(" \\( ","(");
to_eval = to_eval.replaceAll(" \\) ",")");
to_eval = to_eval.replaceAll(" ' ","'");
to_eval = to_eval.replaceAll(" \" ","\"");
return "(eval '"+to_eval+"\")";
}
else{
return "";
return "";
}
}
public static String[] requote(String[] ar) {
Expand All @@ -221,36 +287,32 @@ public static String[] requote(String[] ar) {
return ar;
}
public static String unquote(String ar) {
quo=Pattern.compile("'[^'\"]*\"");
nowa=quo.matcher(ar);
while(nowa.find()) {
String m=nowa.group(0);
m = m.replaceAll("^.","---");
m = m.replaceAll(".$","~~~");
m = m.replaceAll("\\(","{");
m = m.replaceAll("\\)","}");
m = m.replaceAll(" ","<>");
String result = nowa.replaceFirst(m);
return unquote(result);
}
return ar;
}
public static String evaldot(String code){
equo=Pattern.compile("'[^'\"]*\"");
enowa=equo.matcher(code);
code=unquote(code);
elisp=Pattern.compile("\\([^()]*\\)");
enow=elisp.matcher(code);
while (enow.find()){
String m=enow.group(0);
m=m.replace("(","");
m=m.replace(")","");
String[] arg=m.split(" ");
String res=eval(arg);
eresult = enow.replaceFirst(res);
return evaldot(eresult);
}
String[] cfin=code.split(" ");
return cfin[cfin.length-1];
Pattern lisp;
Matcher now;
Pattern quo;
Matcher nowa;
quo=Pattern.compile("'[^'\"]*\"");
nowa=quo.matcher(ar);
while(nowa.find()) {
String m=nowa.group(0);
m = m.replaceAll("^.","---");
m = m.replaceAll(".$","~~~");
m = m.replaceAll("\\(","{");
m = m.replaceAll("\\)","}");
m = m.replaceAll(" ","<>");
String result = nowa.replaceFirst(m);
return unquote(result);
}
return ar;
}
public static String combine(String[] s, String glue) {
int k=s.length;
if (k==0)
return null;
StringBuilder out=new StringBuilder();
out.append(s[0]);
for (int x=1;x<k;++x)
out.append(glue).append(s[x]);
return out.toString();
}
}
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
All thanks to JQ and PiMaster for motivating me to do this :3
JPL0 has also been integrated into EBot (http://github.com/EnKrypt/EBot/) for enhanced Bot communication

Standard Functions (to be implemented):
https://docs.google.com/document/d/10iZQ-L6S4k4P38m9rlO6fwcs8ItR_IYRdmZ6ZFIDb5E/edit
Expand Down

0 comments on commit c5df313

Please sign in to comment.