Minor cleaning in the eval pacakge, added unit tests for the sexpression package

This commit is contained in:
Mike Cifelli 2016-12-15 15:33:48 -05:00
parent 7b7556cc65
commit 62a509eb4b
34 changed files with 244 additions and 546 deletions

View File

@ -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.
@ -21,8 +12,7 @@ public class APPLY extends LispFunction {
*
* @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>
* @return the result of evaluating APPLY on <code>argList</code>
*/
public static SExpression apply(Cons argList) {
return new APPLY().call(argList);
@ -38,9 +28,8 @@ 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);
}

View File

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

View File

@ -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.

View File

@ -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.

View File

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

View File

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

View File

@ -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,8 +19,7 @@ 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);
}
@ -48,8 +38,7 @@ public class DEFUN extends LispFunction {
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
@ -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;

View File

@ -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()) {

View File

@ -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,9 +17,8 @@ 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);
}
@ -39,8 +28,7 @@ public class EQ extends LispFunction {
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());

View File

@ -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,9 +17,8 @@ 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);
}
@ -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());
}
}

View File

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

View File

@ -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;
@ -75,9 +63,7 @@ public class EVAL extends LispFunction {
*
* @param functionName
* the name of the function to look up
* @return
* the function with the name <code>functionName</code> if it exists; null
* otherwise
* @return the function with the name <code>functionName</code> if it exists; null otherwise
*/
public static LispFunction lookupFunction(String functionName) {
return functionTable.get(functionName);
@ -88,8 +74,7 @@ public class EVAL extends LispFunction {
*
* @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
* @return the value of <code>symbolName</code> if it has one; null otherwise
*/
public static SExpression lookupSymbol(String symbolName) {
if (symbolName.equals("NIL")) {
@ -108,9 +93,7 @@ public class EVAL extends LispFunction {
*
* @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
* @return <code>true</code> if <code>list</code> is dotted; <code>false</code> otherwise
*/
public static boolean isDotted(Cons list) {
if (list.nullp()) {
@ -132,8 +115,7 @@ public class EVAL extends LispFunction {
*
* @param sexpr
* the S-expression to evaluate
* @return
* the value of <code>sexpr</code>
* @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);
}
@ -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,8 +206,7 @@ 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.

View File

@ -1,9 +1,6 @@
package eval;
import parser.*;
import sexpression.Cons;
import sexpression.SExpression;
import sexpression.Symbol;
import sexpression.*;
public class EXIT extends LispFunction {

View File

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

View File

@ -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 '&gt;' 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();

View File

@ -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.
@ -21,8 +12,7 @@ public class LAMBDA extends LispFunction {
*
* @param sexpr
* the S-expression to test (must not be null)
* @return
* <code>true</code> if <code>sexpr</code> is a valid lambda expression;
* @return <code>true</code> if <code>sexpr</code> is a valid lambda expression;
* <code>false</code> otherwise
*/
public static boolean isLambdaExpression(SExpression sexpr) {
@ -36,14 +26,12 @@ 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>
* @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.
*/
@ -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);
}
@ -85,8 +72,7 @@ public class LAMBDA extends LispFunction {
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;

View File

@ -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.

View File

@ -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 '&lt;' 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();

View File

@ -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.
@ -58,13 +49,10 @@ public class LET extends LispFunction {
// 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) {
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");
throw new RuntimeException("LET: " + vars + " is not a properly formatted" + " variable/value pair list");
}
// add all variables in 'vars' to 'environment'
@ -74,9 +62,8 @@ public class LET extends LispFunction {
// 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");
throw new RuntimeException("LET: " + varListCar + " is not a properly formatted"
+ " variable/value pair");
}
Cons varSpec = (Cons) varListCar;
@ -85,8 +72,7 @@ public class LET extends LispFunction {
// make sure this variable pair has a value associated with it
if (!varSpecCdr.consp()) {
throw new RuntimeException("LET: illegal variable " +
"specification " + varSpec);
throw new RuntimeException("LET: illegal variable " + "specification " + varSpec);
}
Cons varValue = (Cons) varSpecCdr;
@ -95,8 +81,7 @@ 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);
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;

View File

@ -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.

View File

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

View File

@ -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.

View File

@ -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,8 +12,7 @@ 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
@ -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();

View File

@ -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 {
@ -21,23 +14,19 @@ public abstract class LispFunction {
*
* @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
* @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;

View File

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

View File

@ -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.

View File

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

View File

@ -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.

View File

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

View File

@ -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.

View File

@ -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.

View File

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

View File

@ -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.
*/
@ -42,36 +35,31 @@ public class SymbolTable {
*
* @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
* @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
* @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
*/
@ -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;

View File

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