Minor cleaning in the eval pacakge, added unit tests for the sexpression package
This commit is contained in:
parent
7b7556cc65
commit
62a509eb4b
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>APPLY</code> represents the APPLY function in Lisp.
|
||||
|
@ -20,9 +11,8 @@ public class APPLY extends LispFunction {
|
|||
* Call APPLY with the specified argument list.
|
||||
*
|
||||
* @param argList
|
||||
* the list of arguments to be sent to APPLY (MUST BE A PROPER LIST)
|
||||
* @return
|
||||
* the result of evaluating APPLY on <code>argList</code>
|
||||
* the list of arguments to be sent to APPLY (MUST BE A PROPER LIST)
|
||||
* @return the result of evaluating APPLY on <code>argList</code>
|
||||
*/
|
||||
public static SExpression apply(Cons argList) {
|
||||
return new APPLY().call(argList);
|
||||
|
@ -38,16 +28,15 @@ public class APPLY extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("APPLY"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to APPLY: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to APPLY: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
||||
SExpression car = argList.getCar(); // function name
|
||||
SExpression car = argList.getCar(); // function name
|
||||
Cons cdr = (Cons) argList.getCdr();
|
||||
SExpression cadr = cdr.getCar(); // argument list
|
||||
SExpression cadr = cdr.getCar(); // argument list
|
||||
|
||||
// make sure the second argument is a list
|
||||
if (cadr.listp()) {
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>ATOM</code> represents the ATOM function in Lisp.
|
||||
|
@ -27,9 +17,8 @@ public class ATOM extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("ATOM"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to ATOM: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to ATOM: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>CAR</code> represents the CAR function in Lisp.
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>CDR</code> represents the CDR function in Lisp.
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>COND</code> represents the COND form in Lisp.
|
||||
|
@ -22,8 +13,8 @@ public class COND extends LispFunction {
|
|||
return Nil.getUniqueInstance();
|
||||
}
|
||||
|
||||
SExpression argCar = argList.getCar(); // first clause
|
||||
Cons argCdr = (Cons) argList.getCdr(); // list of remaining clauses
|
||||
SExpression argCar = argList.getCar(); // first clause
|
||||
Cons argCdr = (Cons) argList.getCdr(); // list of remaining clauses
|
||||
|
||||
// make sure the first clause is a list and is not NIL
|
||||
if (argCar.consp()) {
|
||||
|
@ -58,16 +49,13 @@ public class COND extends LispFunction {
|
|||
return Nil.getUniqueInstance();
|
||||
}
|
||||
|
||||
throw new RuntimeException("COND: clause " + argCar +
|
||||
" should be a list");
|
||||
throw new RuntimeException("COND: clause " + argCar + " should be a list");
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the arguments passed to this Lisp function should be
|
||||
* evaluated.
|
||||
* Determine if the arguments passed to this Lisp function should be evaluated.
|
||||
*
|
||||
* @return
|
||||
* <code>false</code>
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
public boolean evaluateArguments() {
|
||||
return false;
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>CONS</code> represents the CONS function in Lisp.
|
||||
|
@ -26,9 +17,8 @@ public class CONS extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("CONS"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to CONS: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to CONS: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
|
|
@ -1,18 +1,9 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>DEFUN</code> represents the DEFUN form in Lisp.
|
||||
*/
|
||||
|
@ -28,16 +19,15 @@ public class DEFUN extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength < MIN_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("DEFUN"), argList);
|
||||
String errMsg = "too few arguments given to DEFUN: " +
|
||||
originalSExpr;
|
||||
String errMsg = "too few arguments given to DEFUN: " + originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
||||
SExpression name = argList.getCar(); // name of the function
|
||||
SExpression name = argList.getCar(); // name of the function
|
||||
|
||||
// make sure the function name is a symbol
|
||||
if (! name.symbolp()) {
|
||||
if (!name.symbolp()) {
|
||||
throw new RuntimeException("DEFUN: " + name + " is not a symbol");
|
||||
}
|
||||
|
||||
|
@ -45,14 +35,13 @@ public class DEFUN extends LispFunction {
|
|||
SExpression cadr = cdr.getCar();
|
||||
|
||||
// make sure the list of arguments (lambda list) is a proper list
|
||||
if (! cadr.listp()) {
|
||||
if (!cadr.listp()) {
|
||||
throw new RuntimeException("DEFUN: " + cadr + " is not a list");
|
||||
} else if (EVAL.isDotted((Cons) cadr)) {
|
||||
throw new RuntimeException("DEFUN: " + cadr +
|
||||
" is not a proper list");
|
||||
throw new RuntimeException("DEFUN: " + cadr + " is not a proper list");
|
||||
}
|
||||
|
||||
Cons lambdaList = (Cons) cadr; // lambda list of the function
|
||||
Cons lambdaList = (Cons) cadr; // lambda list of the function
|
||||
|
||||
// list of S-expressions making up the body of the function
|
||||
Cons body = (Cons) cdr.getCdr();
|
||||
|
@ -61,23 +50,19 @@ public class DEFUN extends LispFunction {
|
|||
|
||||
// give a warning if this function has already been defined
|
||||
if (functionTable.containsKey(name.toString())) {
|
||||
System.out.println("WARNING: redefining function " +
|
||||
name.toString());
|
||||
System.out.println("WARNING: redefining function " + name.toString());
|
||||
}
|
||||
|
||||
// place the function in the function table
|
||||
functionTable.put(name.toString(),
|
||||
new UserDefinedFunction(name.toString(), lambdaList, body));
|
||||
functionTable.put(name.toString(), new UserDefinedFunction(name.toString(), lambdaList, body));
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the arguments passed to this Lisp function should be
|
||||
* evaluated.
|
||||
* Determine if the arguments passed to this Lisp function should be evaluated.
|
||||
*
|
||||
* @return
|
||||
* <code>false</code>
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
public boolean evaluateArguments() {
|
||||
return false;
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>DIVIDE</code> represents the '/' function in Lisp.
|
||||
|
@ -22,8 +12,7 @@ public class DIVIDE extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol("/"), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to /: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to /: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression argFirst = argList.getCar();
|
||||
|
@ -44,8 +33,7 @@ public class DIVIDE extends LispFunction {
|
|||
// make sure that the next argument is a number as well
|
||||
if (argSecond.numberp()) {
|
||||
LispNumber num2 = (LispNumber) argSecond;
|
||||
LispNumber quotient = new LispNumber(num1.getValue() /
|
||||
num2.getValue());
|
||||
LispNumber quotient = new LispNumber(num1.getValue() / num2.getValue());
|
||||
SExpression argCddr = argRest.getCdr();
|
||||
|
||||
if (argCddr.consp()) {
|
||||
|
@ -54,7 +42,7 @@ public class DIVIDE extends LispFunction {
|
|||
|
||||
return quotient;
|
||||
}
|
||||
|
||||
|
||||
throw new RuntimeException("/: " + argSecond + " is not a number");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>EQ</code> represents the EQ function in Lisp.
|
||||
|
@ -27,20 +17,18 @@ public class EQ extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("EQ"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to EQ: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to EQ: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
||||
SExpression argOne = argList.getCar(); // first argument
|
||||
SExpression argOne = argList.getCar(); // first argument
|
||||
Cons cdr = (Cons) argList.getCdr();
|
||||
SExpression argTwo = cdr.getCar(); // second argumnet
|
||||
SExpression argTwo = cdr.getCar(); // second argumnet
|
||||
|
||||
if (argOne.atomp() && argTwo.atomp()) {
|
||||
return ((argOne.toString().equals(argTwo.toString()))
|
||||
? Symbol.T : Nil.getUniqueInstance());
|
||||
return ((argOne.toString().equals(argTwo.toString())) ? Symbol.T : Nil.getUniqueInstance());
|
||||
}
|
||||
|
||||
return ((argOne == argTwo) ? Symbol.T : Nil.getUniqueInstance());
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>EQUAL</code> represents the EQUAL function in Lisp.
|
||||
|
@ -27,16 +17,15 @@ public class EQUAL extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("EQUAL"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to EQUAL: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to EQUAL: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
||||
SExpression argOne = argList.getCar(); // first argument
|
||||
SExpression argOne = argList.getCar(); // first argument
|
||||
Cons cdr = (Cons) argList.getCdr();
|
||||
SExpression argTwo = cdr.getCar(); // second argumnet
|
||||
SExpression argTwo = cdr.getCar(); // second argumnet
|
||||
|
||||
if (argOne.consp() && argTwo.consp()) {
|
||||
Cons listOne = (Cons) argOne;
|
||||
|
@ -46,17 +35,13 @@ public class EQUAL extends LispFunction {
|
|||
SExpression listOneCdr = listOne.getCdr();
|
||||
SExpression listTwoCdr = listTwo.getCdr();
|
||||
|
||||
SExpression carEqual =
|
||||
call(new Cons(listOneCar, LIST.makeList(listTwoCar)));
|
||||
SExpression cdrEqual =
|
||||
call(new Cons(listOneCdr, LIST.makeList(listTwoCdr)));
|
||||
SExpression carEqual = call(new Cons(listOneCar, LIST.makeList(listTwoCar)));
|
||||
SExpression cdrEqual = call(new Cons(listOneCdr, LIST.makeList(listTwoCdr)));
|
||||
|
||||
return (((carEqual == Symbol.T) && (cdrEqual == Symbol.T))
|
||||
? Symbol.T : Nil.getUniqueInstance());
|
||||
return (((carEqual == Symbol.T) && (cdrEqual == Symbol.T)) ? Symbol.T : Nil.getUniqueInstance());
|
||||
}
|
||||
|
||||
return ((argOne.toString().equals(argTwo.toString()))
|
||||
? Symbol.T : Nil.getUniqueInstance());
|
||||
return ((argOne.toString().equals(argTwo.toString())) ? Symbol.T : Nil.getUniqueInstance());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,17 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>EQUALSP</code> represents the '=' function in Lisp.
|
||||
|
@ -23,8 +12,7 @@ public class EQUALSP extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol("="), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to =: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to =: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression firstArg = argList.getCar();
|
||||
|
@ -50,7 +38,7 @@ public class EQUALSP extends LispFunction {
|
|||
|
||||
return Nil.getUniqueInstance();
|
||||
}
|
||||
|
||||
|
||||
throw new RuntimeException("=: " + secondArg + " is not a number");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +1,16 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>EVAL</code> represents the EVAL function in Lisp.
|
||||
*/
|
||||
public class EVAL extends LispFunction {
|
||||
|
||||
// A table to contain all the built-in and user-defined Lisp functions.
|
||||
private static HashMap<String, LispFunction> functionTable =
|
||||
new HashMap<String, LispFunction>();
|
||||
private static HashMap<String, LispFunction> functionTable = new HashMap<String, LispFunction>();
|
||||
|
||||
static {
|
||||
// place all of the built-in functions into the function table
|
||||
|
@ -63,8 +52,7 @@ public class EVAL extends LispFunction {
|
|||
/**
|
||||
* Retrieve the function table.
|
||||
*
|
||||
* @return
|
||||
* the function table
|
||||
* @return the function table
|
||||
*/
|
||||
public static HashMap<String, LispFunction> getFunctionTable() {
|
||||
return functionTable;
|
||||
|
@ -74,10 +62,8 @@ public class EVAL extends LispFunction {
|
|||
* Look up a function by its name.
|
||||
*
|
||||
* @param functionName
|
||||
* the name of the function to look up
|
||||
* @return
|
||||
* the function with the name <code>functionName</code> if it exists; null
|
||||
* otherwise
|
||||
* the name of the function to look up
|
||||
* @return the function with the name <code>functionName</code> if it exists; null otherwise
|
||||
*/
|
||||
public static LispFunction lookupFunction(String functionName) {
|
||||
return functionTable.get(functionName);
|
||||
|
@ -87,9 +73,8 @@ public class EVAL extends LispFunction {
|
|||
* Look up a symbol's value using its name.
|
||||
*
|
||||
* @param symbolName
|
||||
* the name of the symbol to look up (must not be null)
|
||||
* @return
|
||||
* the value of <code>symbolName</code> if it has one; null otherwise
|
||||
* the name of the symbol to look up (must not be null)
|
||||
* @return the value of <code>symbolName</code> if it has one; null otherwise
|
||||
*/
|
||||
public static SExpression lookupSymbol(String symbolName) {
|
||||
if (symbolName.equals("NIL")) {
|
||||
|
@ -107,10 +92,8 @@ public class EVAL extends LispFunction {
|
|||
* Determine if the given list is dotted.
|
||||
*
|
||||
* @param list
|
||||
* the list to be tested (must not be null)
|
||||
* @return
|
||||
* <code>true</code> if <code>list</code> is dotted; <code>false</code>
|
||||
* otherwise
|
||||
* the list to be tested (must not be null)
|
||||
* @return <code>true</code> if <code>list</code> is dotted; <code>false</code> otherwise
|
||||
*/
|
||||
public static boolean isDotted(Cons list) {
|
||||
if (list.nullp()) {
|
||||
|
@ -131,9 +114,8 @@ public class EVAL extends LispFunction {
|
|||
* Evaluate the given S-expression.
|
||||
*
|
||||
* @param sexpr
|
||||
* the S-expression to evaluate
|
||||
* @return
|
||||
* the value of <code>sexpr</code>
|
||||
* the S-expression to evaluate
|
||||
* @return the value of <code>sexpr</code>
|
||||
*/
|
||||
public static SExpression eval(SExpression sexpr) {
|
||||
Cons expList = LIST.makeList(sexpr);
|
||||
|
@ -152,9 +134,8 @@ public class EVAL extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("EVAL"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to EVAL: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to EVAL: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
@ -166,7 +147,7 @@ public class EVAL extends LispFunction {
|
|||
return evaluateList((Cons) arg);
|
||||
}
|
||||
|
||||
return arg; // 'arg' is NIL
|
||||
return arg; // 'arg' is NIL
|
||||
}
|
||||
|
||||
if (arg.symbolp()) {
|
||||
|
@ -179,7 +160,7 @@ public class EVAL extends LispFunction {
|
|||
throw new RuntimeException("variable " + arg + " has no value");
|
||||
}
|
||||
|
||||
return arg; // 'arg' is a NUMBER or a STRING
|
||||
return arg; // 'arg' is a NUMBER or a STRING
|
||||
}
|
||||
|
||||
// Evaluate the specified list.
|
||||
|
@ -212,8 +193,7 @@ public class EVAL extends LispFunction {
|
|||
|
||||
// make sure the list of arguments is not dotted
|
||||
if (isDotted(args)) {
|
||||
throw new RuntimeException("argument list given to " + car +
|
||||
" is dotted: " + list);
|
||||
throw new RuntimeException("argument list given to " + car + " is dotted: " + list);
|
||||
}
|
||||
|
||||
// determine if we should evaluate the arguments that will be
|
||||
|
@ -226,15 +206,14 @@ public class EVAL extends LispFunction {
|
|||
}
|
||||
|
||||
// the list of arguments is not a list!
|
||||
throw new RuntimeException("argument list given to " + car +
|
||||
" is dotted: " + list);
|
||||
throw new RuntimeException("argument list given to " + car + " is dotted: " + list);
|
||||
}
|
||||
|
||||
// Evaluate a list of arguments for a function.
|
||||
//
|
||||
// Parameters: arguments - a list of arguments for a function
|
||||
// Returns: a list consisting of the values of the S-expressions found in
|
||||
// 'arguments'
|
||||
// 'arguments'
|
||||
// Precondition: 'arguments' must not be null.
|
||||
private Cons evaluateArgList(Cons arguments) {
|
||||
if (arguments.nullp()) {
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
public class EXIT extends LispFunction {
|
||||
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>FUNCALL</code> represents the FUNCALL function in Lisp.
|
||||
|
@ -21,8 +12,7 @@ public class FUNCALL extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol("FUNCALL"), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to FUNCALL: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to FUNCALL: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression cdr = argList.getCdr();
|
||||
|
|
|
@ -1,17 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>GREATERP</code> represents the '>' function in Lisp.
|
||||
|
@ -23,8 +12,7 @@ public class GREATERP extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol(">"), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to >: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to >: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression firstArg = argList.getCar();
|
||||
|
@ -50,7 +38,7 @@ public class GREATERP extends LispFunction {
|
|||
|
||||
return Nil.getUniqueInstance();
|
||||
}
|
||||
|
||||
|
||||
throw new RuntimeException(">: " + secondArg + " is not a number");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LAMBDA</code> represents the LAMBDA form in Lisp.
|
||||
|
@ -20,10 +11,9 @@ public class LAMBDA extends LispFunction {
|
|||
* Determine if the given S-expression is a lambda expression.
|
||||
*
|
||||
* @param sexpr
|
||||
* the S-expression to test (must not be null)
|
||||
* @return
|
||||
* <code>true</code> if <code>sexpr</code> is a valid lambda expression;
|
||||
* <code>false</code> otherwise
|
||||
* the S-expression to test (must not be null)
|
||||
* @return <code>true</code> if <code>sexpr</code> is a valid lambda expression;
|
||||
* <code>false</code> otherwise
|
||||
*/
|
||||
public static boolean isLambdaExpression(SExpression sexpr) {
|
||||
if (sexpr.consp()) {
|
||||
|
@ -36,23 +26,21 @@ public class LAMBDA extends LispFunction {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create an internal representation of a user-defined function from the
|
||||
* specified lambda expression.
|
||||
* Create an internal representation of a user-defined function from the specified lambda
|
||||
* expression.
|
||||
*
|
||||
* @param lexpr
|
||||
* the lambda expression to create the function from (must not be null)
|
||||
* @return
|
||||
* an internal representation of a user-defined function created from
|
||||
* <code>lexpr</code>
|
||||
* the lambda expression to create the function from (must not be null)
|
||||
* @return an internal representation of a user-defined function created from <code>lexpr</code>
|
||||
* @throws RuntimeException
|
||||
* Indicates that <code>lexpr</code> is not a valid lambda expression.
|
||||
* Indicates that <code>lexpr</code> is not a valid lambda expression.
|
||||
*/
|
||||
public static UserDefinedFunction createFunction(Cons lexpr) {
|
||||
LAMBDA lambda = new LAMBDA();
|
||||
SExpression cdr = lexpr.getCdr();
|
||||
|
||||
// make sure lexpr is a proper list
|
||||
if (! cdr.consp()) {
|
||||
if (!cdr.consp()) {
|
||||
throw new RuntimeException("invalid lambda expression");
|
||||
} else if (EVAL.isDotted((Cons) cdr)) {
|
||||
throw new RuntimeException("dotted lambda expression " + lexpr);
|
||||
|
@ -73,8 +61,7 @@ public class LAMBDA extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength < MIN_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("LAMBDA"), argList);
|
||||
String errMsg = "too few arguments given to LAMBDA: " +
|
||||
originalSExpr;
|
||||
String errMsg = "too few arguments given to LAMBDA: " + originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
@ -82,11 +69,10 @@ public class LAMBDA extends LispFunction {
|
|||
SExpression car = argList.getCar();
|
||||
|
||||
// make sure the list of arguments is a proper list
|
||||
if (! car.listp()) {
|
||||
if (!car.listp()) {
|
||||
throw new RuntimeException("LAMBDA: " + car + " is not a list");
|
||||
} else if (EVAL.isDotted((Cons) car)) {
|
||||
throw new RuntimeException("LAMBDA: " + car +
|
||||
" must be a proper list");
|
||||
throw new RuntimeException("LAMBDA: " + car + " must be a proper list");
|
||||
}
|
||||
|
||||
Cons lambdaList = (Cons) car;
|
||||
|
@ -98,11 +84,9 @@ public class LAMBDA extends LispFunction {
|
|||
}
|
||||
|
||||
/**
|
||||
* Determine if the arguments passed to this Lisp function should be
|
||||
* evaluated.
|
||||
* Determine if the arguments passed to this Lisp function should be evaluated.
|
||||
*
|
||||
* @return
|
||||
* <code>false</code>
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
public boolean evaluateArguments() {
|
||||
return false;
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LENGTH</code> represents the LENGTH function in Lisp.
|
||||
|
|
|
@ -1,17 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LESSP</code> represents the '<' function in Lisp.
|
||||
|
@ -23,8 +12,7 @@ public class LESSP extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol("<"), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to <: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to <: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression firstArg = argList.getCar();
|
||||
|
@ -50,7 +38,7 @@ public class LESSP extends LispFunction {
|
|||
|
||||
return Nil.getUniqueInstance();
|
||||
}
|
||||
|
||||
|
||||
throw new RuntimeException("<: " + secondArg + " is not a number");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LET</code> represents the LET form in Lisp.
|
||||
|
@ -49,22 +40,19 @@ public class LET extends LispFunction {
|
|||
// Add a list of variables and their values to the specified symbol table.
|
||||
//
|
||||
// Parameters: environment - the symbol table to add the variables and
|
||||
// their values to (must not be null)
|
||||
// vars - a list of variable/value pairs (must be either a
|
||||
// proper list of pairs or NIL)
|
||||
// their values to (must not be null)
|
||||
// vars - a list of variable/value pairs (must be either a
|
||||
// proper list of pairs or NIL)
|
||||
// Throws: RuntimeException - Indicates that 'vars' is not a proper list or
|
||||
// that it contains an member that is not a
|
||||
// variable/value pair.
|
||||
// that it contains an member that is not a
|
||||
// variable/value pair.
|
||||
// Precondition: 'environment' and 'vars' must not be null.
|
||||
// Postcondition: All of the variables in 'vars' have been placed into
|
||||
// 'environment' with their values.
|
||||
private void addVariablesToTable(SymbolTable environment,
|
||||
SExpression vars) {
|
||||
// 'environment' with their values.
|
||||
private void addVariablesToTable(SymbolTable environment, SExpression vars) {
|
||||
// makes sure the list of variable/value pairs is a list
|
||||
if (! vars.listp()) {
|
||||
throw new RuntimeException("LET: " + vars +
|
||||
" is not a properly formatted" +
|
||||
" variable/value pair list");
|
||||
if (!vars.listp()) {
|
||||
throw new RuntimeException("LET: " + vars + " is not a properly formatted" + " variable/value pair list");
|
||||
}
|
||||
|
||||
// add all variables in 'vars' to 'environment'
|
||||
|
@ -73,10 +61,9 @@ public class LET extends LispFunction {
|
|||
SExpression varListCar = varList.getCar();
|
||||
|
||||
// make sure this variable/value pair is a list
|
||||
if (! varListCar.consp()) {
|
||||
throw new RuntimeException("LET: " + varListCar +
|
||||
" is not a properly formatted" +
|
||||
" variable/value pair");
|
||||
if (!varListCar.consp()) {
|
||||
throw new RuntimeException("LET: " + varListCar + " is not a properly formatted"
|
||||
+ " variable/value pair");
|
||||
}
|
||||
|
||||
Cons varSpec = (Cons) varListCar;
|
||||
|
@ -84,9 +71,8 @@ public class LET extends LispFunction {
|
|||
SExpression varSpecCdr = varSpec.getCdr();
|
||||
|
||||
// make sure this variable pair has a value associated with it
|
||||
if (! varSpecCdr.consp()) {
|
||||
throw new RuntimeException("LET: illegal variable " +
|
||||
"specification " + varSpec);
|
||||
if (!varSpecCdr.consp()) {
|
||||
throw new RuntimeException("LET: illegal variable " + "specification " + varSpec);
|
||||
}
|
||||
|
||||
Cons varValue = (Cons) varSpecCdr;
|
||||
|
@ -94,9 +80,8 @@ public class LET extends LispFunction {
|
|||
|
||||
// make sure there are no more members of this variable/value pair
|
||||
// and that 'symbol' is actually a symbol
|
||||
if ((! varValue.getCdr().nullp()) || (! symbol.symbolp())) {
|
||||
throw new RuntimeException("LET: illegal variable " +
|
||||
"specification " + varSpec);
|
||||
if ((!varValue.getCdr().nullp()) || (!symbol.symbolp())) {
|
||||
throw new RuntimeException("LET: illegal variable " + "specification " + varSpec);
|
||||
}
|
||||
|
||||
environment.put(symbol.toString(), value);
|
||||
|
@ -106,11 +91,9 @@ public class LET extends LispFunction {
|
|||
}
|
||||
|
||||
/**
|
||||
* Determine if the arguments passed to this Lisp function should be
|
||||
* evaluated.
|
||||
* Determine if the arguments passed to this Lisp function should be evaluated.
|
||||
*
|
||||
* @return
|
||||
* <code>false</code>
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
public boolean evaluateArguments() {
|
||||
return false;
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LIST</code> represents the LIST function in Lisp.
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LISTP</code> represents the LISTP function in Lisp.
|
||||
|
@ -27,9 +17,8 @@ public class LISTP extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("LISTP"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to LISTP: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to LISTP: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispString;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import java.io.*;
|
||||
import parser.LispParser;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>LOAD</code> represents the LOAD function in Lisp.
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
|
||||
|
@ -19,13 +12,12 @@ public class LambdaExpression extends SExpression {
|
|||
private UserDefinedFunction function;
|
||||
|
||||
/**
|
||||
* Create a new FUNCTION with the specified lambda expression and
|
||||
* internal representation.
|
||||
* Create a new FUNCTION with the specified lambda expression and internal representation.
|
||||
*
|
||||
* @param lexpr
|
||||
* the lambda expression of this FUNCTION
|
||||
* the lambda expression of this FUNCTION
|
||||
* @param function
|
||||
* the internal representation of this FUNCTION
|
||||
* the internal representation of this FUNCTION
|
||||
*/
|
||||
public LambdaExpression(Cons lexpr, UserDefinedFunction function) {
|
||||
this.lexpr = lexpr;
|
||||
|
@ -35,8 +27,7 @@ public class LambdaExpression extends SExpression {
|
|||
/**
|
||||
* Test if this S-expression is a FUNCTION.
|
||||
*
|
||||
* @return
|
||||
* <code>true</code>
|
||||
* @return <code>true</code>
|
||||
*/
|
||||
public boolean functionp() {
|
||||
return true;
|
||||
|
@ -45,8 +36,7 @@ public class LambdaExpression extends SExpression {
|
|||
/**
|
||||
* Retrieve the lambda expression of this FUNCTION.
|
||||
*
|
||||
* @return
|
||||
* the lambda expression of this FUNCTION
|
||||
* @return the lambda expression of this FUNCTION
|
||||
*/
|
||||
public Cons getLExpression() {
|
||||
return lexpr;
|
||||
|
@ -55,8 +45,7 @@ public class LambdaExpression extends SExpression {
|
|||
/**
|
||||
* Retrieve the internal representation of this FUNCTION.
|
||||
*
|
||||
* @return
|
||||
* the user-defined function of this FUNCTION
|
||||
* @return the user-defined function of this FUNCTION
|
||||
*/
|
||||
public UserDefinedFunction getFunction() {
|
||||
return function;
|
||||
|
@ -65,8 +54,7 @@ public class LambdaExpression extends SExpression {
|
|||
/**
|
||||
* Returns a string representation of this FUNCTION.
|
||||
*
|
||||
* @return
|
||||
* a string representation of this FUNCTION
|
||||
* @return a string representation of this FUNCTION
|
||||
*/
|
||||
public String toString() {
|
||||
return lexpr.toString();
|
||||
|
|
|
@ -1,18 +1,11 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
|
||||
/**
|
||||
* A <code>LispFunction</code> is an internal representation of a built-in
|
||||
* function in the Lisp programming language.
|
||||
* A <code>LispFunction</code> is an internal representation of a built-in function in the Lisp
|
||||
* programming language.
|
||||
*/
|
||||
public abstract class LispFunction {
|
||||
|
||||
|
@ -20,24 +13,20 @@ public abstract class LispFunction {
|
|||
* Call this Lisp function with the given list of arguments.
|
||||
*
|
||||
* @param argList
|
||||
* the list of arguments to pass to this function (MUST BE A PROPER LIST)
|
||||
* @return
|
||||
* the resulting S-expression of calling this function with the specified
|
||||
* arguments
|
||||
* the list of arguments to pass to this function (MUST BE A PROPER LIST)
|
||||
* @return the resulting S-expression of calling this function with the specified arguments
|
||||
* @throws RuntimeException
|
||||
* Indicates that an incorrect number of arguments has been passed to this
|
||||
* function or that one of the arguments is not of the expected type.
|
||||
* Indicates that an incorrect number of arguments has been passed to this function
|
||||
* or that one of the arguments is not of the expected type.
|
||||
*/
|
||||
public abstract SExpression call(Cons argList);
|
||||
|
||||
/**
|
||||
* Determine if the arguments passed to this Lisp function should be
|
||||
* evaluated. A subclass should override this method to return
|
||||
* <code>false</code> if it does not want its arguments to be evaluated
|
||||
* prior to being passed.
|
||||
* Determine if the arguments passed to this Lisp function should be evaluated. A subclass
|
||||
* should override this method to return <code>false</code> if it does not want its arguments to
|
||||
* be evaluated prior to being passed.
|
||||
*
|
||||
* @return
|
||||
* <code>true</code>
|
||||
* @return <code>true</code>
|
||||
*/
|
||||
public boolean evaluateArguments() {
|
||||
return true;
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>MINUS</code> represents the '-' function in Lisp.
|
||||
|
@ -22,8 +12,7 @@ public class MINUS extends LispFunction {
|
|||
if (argList.nullp()) {
|
||||
Cons originalSExpr = new Cons(new Symbol("-"), argList);
|
||||
|
||||
throw new RuntimeException("too few arguments given to -: " +
|
||||
originalSExpr);
|
||||
throw new RuntimeException("too few arguments given to -: " + originalSExpr);
|
||||
}
|
||||
|
||||
SExpression argFirst = argList.getCar();
|
||||
|
@ -36,7 +25,7 @@ public class MINUS extends LispFunction {
|
|||
if (argRest.nullp()) {
|
||||
// there is only one argument, so return the additive
|
||||
// inverse of the number
|
||||
return new LispNumber(- num1.getValue());
|
||||
return new LispNumber(-num1.getValue());
|
||||
}
|
||||
|
||||
SExpression argSecond = argRest.getCar();
|
||||
|
@ -44,8 +33,7 @@ public class MINUS extends LispFunction {
|
|||
// make sure that the next argument is a number as well
|
||||
if (argSecond.numberp()) {
|
||||
LispNumber num2 = (LispNumber) argSecond;
|
||||
LispNumber difference = new LispNumber(num1.getValue() -
|
||||
num2.getValue());
|
||||
LispNumber difference = new LispNumber(num1.getValue() - num2.getValue());
|
||||
SExpression argCddr = argRest.getCdr();
|
||||
|
||||
if (argCddr.consp()) {
|
||||
|
@ -54,7 +42,7 @@ public class MINUS extends LispFunction {
|
|||
|
||||
return difference;
|
||||
}
|
||||
|
||||
|
||||
throw new RuntimeException("-: " + argSecond + " is not a number");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>MULTIPLY</code> represents the '*' function in Lisp.
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.Nil;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>NULL</code> represents the NULL function in Lisp.
|
||||
|
@ -27,9 +17,8 @@ public class NULL extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("NULL"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to NULL: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to NULL: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.LispNumber;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>PLUS</code> represents the '+' function in Lisp.
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>PRINT</code> represents the PRINT function in Lisp.
|
||||
|
@ -26,9 +17,8 @@ public class PRINT extends LispFunction {
|
|||
// make sure we have received the proper number of arguments
|
||||
if (argListLength != NUM_ARGS) {
|
||||
Cons originalSExpr = new Cons(new Symbol("PRINT"), argList);
|
||||
String errMsg = "too " +
|
||||
((argListLength > NUM_ARGS) ? "many" : "few") +
|
||||
" arguments given to PRINT: " + originalSExpr;
|
||||
String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to PRINT: "
|
||||
+ originalSExpr;
|
||||
|
||||
throw new RuntimeException(errMsg);
|
||||
}
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 1
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>QUOTE</code> represents the QUOTE form in Lisp.
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>SETF</code> represents the SETF form in Lisp.
|
||||
|
|
|
@ -1,15 +1,6 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.Cons;
|
||||
import sexpression.SExpression;
|
||||
import sexpression.Symbol;
|
||||
import sexpression.*;
|
||||
|
||||
/**
|
||||
* <code>SYMBOL_FUNCTION</code> represents the SYMBOL-FUNCTION function in
|
||||
|
|
|
@ -1,16 +1,9 @@
|
|||
/*
|
||||
* Name: Mike Cifelli
|
||||
* Course: CIS 443 - Programming Languages
|
||||
* Assignment: Lisp Interpreter 2
|
||||
*/
|
||||
|
||||
package eval;
|
||||
|
||||
import parser.*;
|
||||
import sexpression.SExpression;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import sexpression.SExpression;
|
||||
|
||||
/**
|
||||
* A <code>SymbolTable</code> maps symbol names to values.
|
||||
*/
|
||||
|
@ -30,7 +23,7 @@ public class SymbolTable {
|
|||
* Create a new symbol table with the specified parent.
|
||||
*
|
||||
* @param parent
|
||||
* the parent of this symbol table
|
||||
* the parent of this symbol table
|
||||
*/
|
||||
public SymbolTable(SymbolTable parent) {
|
||||
this.table = new HashMap<String, SExpression>();
|
||||
|
@ -41,39 +34,34 @@ public class SymbolTable {
|
|||
* Determine if the specified symbol name is in this symbol table.
|
||||
*
|
||||
* @param symbolName
|
||||
* the name of the symbol to look up
|
||||
* @return
|
||||
* <code>true</code> if the symbol is in this symbol table;
|
||||
* <code>false</code> otherwise
|
||||
* the name of the symbol to look up
|
||||
* @return <code>true</code> if the symbol is in this symbol table; <code>false</code> otherwise
|
||||
*/
|
||||
public boolean contains(String symbolName) {
|
||||
return table.containsKey(symbolName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified symbol name is mapped in this
|
||||
* symbol table.
|
||||
* Returns the value to which the specified symbol name is mapped in this symbol table.
|
||||
*
|
||||
* @param symbolName
|
||||
* the name of the symbol whose associated value is to be returned
|
||||
* @return
|
||||
* the value to which this symbol table maps <code>symbolName</code>, or
|
||||
* null if no mapping exists
|
||||
* the name of the symbol whose associated value is to be returned
|
||||
* @return the value to which this symbol table maps <code>symbolName</code>, or null if no
|
||||
* mapping exists
|
||||
*/
|
||||
public SExpression get(String symbolName) {
|
||||
return table.get(symbolName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates the specified symbol name with the specified value in this
|
||||
* symbol table. If the symbol table previously contained a mapping for
|
||||
* this symbol name, the old value has been replaced.
|
||||
* Associates the specified symbol name with the specified value in this symbol table. If the
|
||||
* symbol table previously contained a mapping for this symbol name, the old value has been
|
||||
* replaced.
|
||||
*
|
||||
* @param symbolName
|
||||
* the name of the symbol with which the specified value is to be
|
||||
* associated
|
||||
* the name of the symbol with which the specified value is to be associated
|
||||
* @param value
|
||||
* the value to be associated with the specified symbol name
|
||||
* the value to be associated with the specified symbol name
|
||||
*/
|
||||
public void put(String symbolName, SExpression value) {
|
||||
table.put(symbolName, value);
|
||||
|
@ -82,8 +70,7 @@ public class SymbolTable {
|
|||
/**
|
||||
* Returns the parent of this symbol table.
|
||||
*
|
||||
* @return
|
||||
* the parent of this symbol table
|
||||
* @return the parent of this symbol table
|
||||
*/
|
||||
public SymbolTable getParent() {
|
||||
return parent;
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package sexpression;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SExpressionTester {
|
||||
|
||||
private void assertSExpressionMatchesString(String expected, SExpression sExpression) {
|
||||
assertEquals(expected, sExpression.toString());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {}
|
||||
|
||||
@Test
|
||||
public void testNilToString() {
|
||||
String input = "NIL";
|
||||
|
||||
assertSExpressionMatchesString(input, Nil.getUniqueInstance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToString() {
|
||||
String input = "12";
|
||||
|
||||
assertSExpressionMatchesString(input, new LispNumber(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberValueToString() {
|
||||
String expected = "12";
|
||||
|
||||
assertSExpressionMatchesString(expected, new LispNumber(12));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringToString() {
|
||||
String input = "\"hi\"";
|
||||
|
||||
assertSExpressionMatchesString(input, new LispString(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSymbolToString() {
|
||||
String input = "symbol";
|
||||
|
||||
assertSExpressionMatchesString(input.toUpperCase(), new Symbol(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleConsToString() {
|
||||
String expected = "(1)";
|
||||
Cons cons = new Cons(new LispNumber(1), Nil.getUniqueInstance());
|
||||
|
||||
assertSExpressionMatchesString(expected, cons);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexConsToString() {
|
||||
String expected = "(1 A \"string\")";
|
||||
Cons list = new Cons(new LispNumber(1),
|
||||
new Cons(new Symbol("a"),
|
||||
new Cons(new LispString("\"string\""), Nil.getUniqueInstance())));
|
||||
|
||||
assertSExpressionMatchesString(expected, list);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue