diff --git a/src/eval/APPLY.java b/src/eval/APPLY.java index cad1663..941f456 100644 --- a/src/eval/APPLY.java +++ b/src/eval/APPLY.java @@ -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.*; /** * APPLY 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 argList + * the list of arguments to be sent to APPLY (MUST BE A PROPER LIST) + * @return the result of evaluating APPLY on argList */ 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()) { diff --git a/src/eval/ATOM.java b/src/eval/ATOM.java index 0b5ca1c..84fcc8c 100644 --- a/src/eval/ATOM.java +++ b/src/eval/ATOM.java @@ -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.*; /** * ATOM 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); } diff --git a/src/eval/CAR.java b/src/eval/CAR.java index ca49f98..e2be950 100644 --- a/src/eval/CAR.java +++ b/src/eval/CAR.java @@ -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.*; /** * CAR represents the CAR function in Lisp. diff --git a/src/eval/CDR.java b/src/eval/CDR.java index 932a4f2..0d21a1f 100644 --- a/src/eval/CDR.java +++ b/src/eval/CDR.java @@ -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.*; /** * CDR represents the CDR function in Lisp. diff --git a/src/eval/COND.java b/src/eval/COND.java index 6fdc096..ef25a67 100644 --- a/src/eval/COND.java +++ b/src/eval/COND.java @@ -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.*; /** * COND 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 - * false + * @return false */ public boolean evaluateArguments() { return false; diff --git a/src/eval/CONS.java b/src/eval/CONS.java index 7c4e592..48747e6 100644 --- a/src/eval/CONS.java +++ b/src/eval/CONS.java @@ -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.*; /** * CONS 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); } diff --git a/src/eval/DEFUN.java b/src/eval/DEFUN.java index a98b024..aeb1bc6 100644 --- a/src/eval/DEFUN.java +++ b/src/eval/DEFUN.java @@ -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.*; + /** * DEFUN 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 - * false + * @return false */ public boolean evaluateArguments() { return false; diff --git a/src/eval/DIVIDE.java b/src/eval/DIVIDE.java index a1f9c5d..72c27f3 100644 --- a/src/eval/DIVIDE.java +++ b/src/eval/DIVIDE.java @@ -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.*; /** * DIVIDE 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"); } diff --git a/src/eval/EQ.java b/src/eval/EQ.java index 5939a75..8aff2dc 100644 --- a/src/eval/EQ.java +++ b/src/eval/EQ.java @@ -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.*; /** * EQ 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()); diff --git a/src/eval/EQUAL.java b/src/eval/EQUAL.java index 4cf0c1d..2414f5b 100644 --- a/src/eval/EQUAL.java +++ b/src/eval/EQUAL.java @@ -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.*; /** * EQUAL 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()); } } diff --git a/src/eval/EQUALSP.java b/src/eval/EQUALSP.java index f5c13c8..323cd4f 100644 --- a/src/eval/EQUALSP.java +++ b/src/eval/EQUALSP.java @@ -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.*; /** * EQUALSP 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"); } diff --git a/src/eval/EVAL.java b/src/eval/EVAL.java index 512c8ab..18ad8d4 100644 --- a/src/eval/EVAL.java +++ b/src/eval/EVAL.java @@ -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.*; + /** * EVAL 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 functionTable = - new HashMap(); + private static HashMap functionTable = new HashMap(); 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 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 functionName if it exists; null - * otherwise + * the name of the function to look up + * @return the function with the name functionName 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 symbolName if it has one; null otherwise + * the name of the symbol to look up (must not be null) + * @return the value of symbolName 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 - * true if list is dotted; false - * otherwise + * the list to be tested (must not be null) + * @return true if list is dotted; false 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 sexpr + * the S-expression to evaluate + * @return the value of sexpr */ 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()) { diff --git a/src/eval/EXIT.java b/src/eval/EXIT.java index b057b41..775f352 100644 --- a/src/eval/EXIT.java +++ b/src/eval/EXIT.java @@ -1,9 +1,6 @@ package eval; -import parser.*; -import sexpression.Cons; -import sexpression.SExpression; -import sexpression.Symbol; +import sexpression.*; public class EXIT extends LispFunction { diff --git a/src/eval/FUNCALL.java b/src/eval/FUNCALL.java index 50c8bdd..5e130a2 100644 --- a/src/eval/FUNCALL.java +++ b/src/eval/FUNCALL.java @@ -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.*; /** * FUNCALL 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(); diff --git a/src/eval/GREATERP.java b/src/eval/GREATERP.java index a1cda2c..1589785 100644 --- a/src/eval/GREATERP.java +++ b/src/eval/GREATERP.java @@ -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.*; /** * GREATERP 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"); } diff --git a/src/eval/LAMBDA.java b/src/eval/LAMBDA.java index f7d8f71..34d04b0 100644 --- a/src/eval/LAMBDA.java +++ b/src/eval/LAMBDA.java @@ -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.*; /** * LAMBDA 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 - * true if sexpr is a valid lambda expression; - * false otherwise + * the S-expression to test (must not be null) + * @return true if sexpr is a valid lambda expression; + * false 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 - * lexpr + * the lambda expression to create the function from (must not be null) + * @return an internal representation of a user-defined function created from lexpr * @throws RuntimeException - * Indicates that lexpr is not a valid lambda expression. + * Indicates that lexpr 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 - * false + * @return false */ public boolean evaluateArguments() { return false; diff --git a/src/eval/LENGTH.java b/src/eval/LENGTH.java index b201333..84947e4 100644 --- a/src/eval/LENGTH.java +++ b/src/eval/LENGTH.java @@ -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.*; /** * LENGTH represents the LENGTH function in Lisp. diff --git a/src/eval/LESSP.java b/src/eval/LESSP.java index 866946f..6d20a34 100644 --- a/src/eval/LESSP.java +++ b/src/eval/LESSP.java @@ -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.*; /** * LESSP 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"); } diff --git a/src/eval/LET.java b/src/eval/LET.java index f489a6e..fc35e96 100644 --- a/src/eval/LET.java +++ b/src/eval/LET.java @@ -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.*; /** * LET 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 - * false + * @return false */ public boolean evaluateArguments() { return false; diff --git a/src/eval/LIST.java b/src/eval/LIST.java index 182bbdb..207927d 100644 --- a/src/eval/LIST.java +++ b/src/eval/LIST.java @@ -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.*; /** * LIST represents the LIST function in Lisp. diff --git a/src/eval/LISTP.java b/src/eval/LISTP.java index 19e412a..9e63d7e 100644 --- a/src/eval/LISTP.java +++ b/src/eval/LISTP.java @@ -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.*; /** * LISTP 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); } diff --git a/src/eval/LOAD.java b/src/eval/LOAD.java index f7d7621..47f6add 100644 --- a/src/eval/LOAD.java +++ b/src/eval/LOAD.java @@ -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.*; /** * LOAD represents the LOAD function in Lisp. diff --git a/src/eval/LambdaExpression.java b/src/eval/LambdaExpression.java index 57b3856..e7cc669 100644 --- a/src/eval/LambdaExpression.java +++ b/src/eval/LambdaExpression.java @@ -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 - * true + * @return true */ 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(); diff --git a/src/eval/LispFunction.java b/src/eval/LispFunction.java index b86870a..324e24f 100644 --- a/src/eval/LispFunction.java +++ b/src/eval/LispFunction.java @@ -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 LispFunction is an internal representation of a built-in - * function in the Lisp programming language. + * A LispFunction 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 - * false 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 false if it does not want its arguments to + * be evaluated prior to being passed. * - * @return - * true + * @return true */ public boolean evaluateArguments() { return true; diff --git a/src/eval/MINUS.java b/src/eval/MINUS.java index ec3e6b3..decd8eb 100644 --- a/src/eval/MINUS.java +++ b/src/eval/MINUS.java @@ -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.*; /** * MINUS 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"); } diff --git a/src/eval/MULTIPLY.java b/src/eval/MULTIPLY.java index c461a30..8472430 100644 --- a/src/eval/MULTIPLY.java +++ b/src/eval/MULTIPLY.java @@ -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.*; /** * MULTIPLY represents the '*' function in Lisp. diff --git a/src/eval/NULL.java b/src/eval/NULL.java index b1d1a0f..91c7d6a 100644 --- a/src/eval/NULL.java +++ b/src/eval/NULL.java @@ -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.*; /** * NULL 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); } diff --git a/src/eval/PLUS.java b/src/eval/PLUS.java index 33e8d42..09a58f2 100644 --- a/src/eval/PLUS.java +++ b/src/eval/PLUS.java @@ -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.*; /** * PLUS represents the '+' function in Lisp. diff --git a/src/eval/PRINT.java b/src/eval/PRINT.java index 407544c..d11f653 100644 --- a/src/eval/PRINT.java +++ b/src/eval/PRINT.java @@ -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.*; /** * PRINT 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); } diff --git a/src/eval/QUOTE.java b/src/eval/QUOTE.java index 53cd348..33f7faf 100644 --- a/src/eval/QUOTE.java +++ b/src/eval/QUOTE.java @@ -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.*; /** * QUOTE represents the QUOTE form in Lisp. diff --git a/src/eval/SETF.java b/src/eval/SETF.java index 4ddd8ec..711843a 100644 --- a/src/eval/SETF.java +++ b/src/eval/SETF.java @@ -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.*; /** * SETF represents the SETF form in Lisp. diff --git a/src/eval/SYMBOL_FUNCTION.java b/src/eval/SYMBOL_FUNCTION.java index d64fd9c..f768284 100644 --- a/src/eval/SYMBOL_FUNCTION.java +++ b/src/eval/SYMBOL_FUNCTION.java @@ -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.*; /** * SYMBOL_FUNCTION represents the SYMBOL-FUNCTION function in diff --git a/src/eval/SymbolTable.java b/src/eval/SymbolTable.java index 844095c..65966ee 100644 --- a/src/eval/SymbolTable.java +++ b/src/eval/SymbolTable.java @@ -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 SymbolTable 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(); @@ -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 - * true if the symbol is in this symbol table; - * false otherwise + * the name of the symbol to look up + * @return true if the symbol is in this symbol table; false 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 symbolName, 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 symbolName, 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; diff --git a/test/sexpression/SExpressionTester.java b/test/sexpression/SExpressionTester.java new file mode 100644 index 0000000..3216773 --- /dev/null +++ b/test/sexpression/SExpressionTester.java @@ -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); + } + +}