diff --git a/src/error/package.html b/src/error/package.html deleted file mode 100644 index b8e7489..0000000 --- a/src/error/package.html +++ /dev/null @@ -1,3 +0,0 @@ -
- Provides a class for managing errors in the Lisp Interpreter. - diff --git a/src/eval/DEFUN.java b/src/eval/DEFUN.java index ffa5bf7..a98b024 100644 --- a/src/eval/DEFUN.java +++ b/src/eval/DEFUN.java @@ -67,7 +67,7 @@ public class DEFUN extends LispFunction { // place the function in the function table functionTable.put(name.toString(), - new UDFunction(name.toString(), lambdaList, body)); + new UserDefinedFunction(name.toString(), lambdaList, body)); return name; } diff --git a/src/eval/LAMBDA.java b/src/eval/LAMBDA.java index ebe5721..f7d8f71 100644 --- a/src/eval/LAMBDA.java +++ b/src/eval/LAMBDA.java @@ -47,7 +47,7 @@ public class LAMBDA extends LispFunction { * @throws RuntimeException * Indicates thatlexpr
is not a valid lambda expression.
*/
- public static UDFunction createFunction(Cons lexpr) {
+ public static UserDefinedFunction createFunction(Cons lexpr) {
LAMBDA lambda = new LAMBDA();
SExpression cdr = lexpr.getCdr();
@@ -92,7 +92,7 @@ public class LAMBDA extends LispFunction {
Cons lambdaList = (Cons) car;
Cons body = (Cons) argList.getCdr();
Cons lexpr = new Cons(new Symbol("LAMBDA"), argList);
- UDFunction function = new UDFunction(":LAMBDA", lambdaList, body);
+ UserDefinedFunction function = new UserDefinedFunction(":LAMBDA", lambdaList, body);
return new LambdaExpression(lexpr, function);
}
diff --git a/src/eval/LambdaExpression.java b/src/eval/LambdaExpression.java
index 2b787c7..57b3856 100644
--- a/src/eval/LambdaExpression.java
+++ b/src/eval/LambdaExpression.java
@@ -16,7 +16,7 @@ import sexpression.SExpression;
public class LambdaExpression extends SExpression {
private Cons lexpr;
- private UDFunction function;
+ private UserDefinedFunction function;
/**
* Create a new FUNCTION with the specified lambda expression and
@@ -27,7 +27,7 @@ public class LambdaExpression extends SExpression {
* @param function
* the internal representation of this FUNCTION
*/
- public LambdaExpression(Cons lexpr, UDFunction function) {
+ public LambdaExpression(Cons lexpr, UserDefinedFunction function) {
this.lexpr = lexpr;
this.function = function;
}
@@ -58,7 +58,7 @@ public class LambdaExpression extends SExpression {
* @return
* the user-defined function of this FUNCTION
*/
- public UDFunction getFunction() {
+ public UserDefinedFunction getFunction() {
return function;
}
diff --git a/src/eval/SYMBOL_FUNCTION.java b/src/eval/SYMBOL_FUNCTION.java
index 02ff8c5..d64fd9c 100644
--- a/src/eval/SYMBOL_FUNCTION.java
+++ b/src/eval/SYMBOL_FUNCTION.java
@@ -44,10 +44,10 @@ public class SYMBOL_FUNCTION extends LispFunction {
// make sure the function actually exists
if (function != null) {
- if (function instanceof UDFunction) {
+ if (function instanceof UserDefinedFunction) {
// this is a user-defined function
- UDFunction udFunction = (UDFunction) function;
+ UserDefinedFunction udFunction = (UserDefinedFunction) function;
return udFunction.getLexpr();
}
diff --git a/src/eval/UDFunction.java b/src/eval/UserDefinedFunction.java
similarity index 88%
rename from src/eval/UDFunction.java
rename to src/eval/UserDefinedFunction.java
index 9828a8e..4cc5fb4 100644
--- a/src/eval/UDFunction.java
+++ b/src/eval/UserDefinedFunction.java
@@ -1,23 +1,10 @@
-/*
- * 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.ArrayList;
-/**
- * A UDFunction
is an internal representation of a user-defined
- * function in the Lisp programming language.
- */
-public class UDFunction extends LispFunction {
+import sexpression.*;
+
+public class UserDefinedFunction extends LispFunction {
// the number of arguments that this user-defined function takes.
private final int NUM_ARGS;
@@ -40,7 +27,7 @@ public class UDFunction extends LispFunction {
* @param body
* the body of this user-defined function (MUST BE A PROPER LIST)
*/
- public UDFunction(String name, Cons lambdaList, Cons body) {
+ public UserDefinedFunction(String name, Cons lambdaList, Cons body) {
this.name = name;
this.body = body;
this.lexpr = new Cons(new Symbol(name), new Cons(lambdaList, body));
diff --git a/src/eval/package.html b/src/eval/package.html
deleted file mode 100644
index e9c971d..0000000
--- a/src/eval/package.html
+++ /dev/null
@@ -1,4 +0,0 @@
-
- Provides functions and forms to be used during the evaluation of an
- S-expression.
-
diff --git a/src/file/FilePosition.java b/src/file/FilePosition.java
index f8576e7..e65837b 100644
--- a/src/file/FilePosition.java
+++ b/src/file/FilePosition.java
@@ -1,7 +1,5 @@
package file;
-import java.util.Objects;
-
public class FilePosition {
private String fileName;
@@ -11,29 +9,25 @@ public class FilePosition {
public FilePosition(String fileName) {
this.fileName = fileName;
}
-
+
public String getFileName() {
return fileName;
}
-
+
public int getLineNumber() {
return lineNumber;
}
-
+
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
-
+
public int getColumnNumber() {
return columnNumber;
}
-
+
public void setColumnNumber(int columnNumber) {
this.columnNumber = columnNumber;
}
- public boolean isEqual(FilePosition otherFilePosition) {
- return Objects.equals(this.fileName, otherFilePosition.fileName) && (this.lineNumber == otherFilePosition.lineNumber)
- && (this.columnNumber == otherFilePosition.columnNumber);
- }
}
\ No newline at end of file
diff --git a/src/main/LispInterpreter.java b/src/main/LispInterpreter.java
index 3236019..10ae185 100644
--- a/src/main/LispInterpreter.java
+++ b/src/main/LispInterpreter.java
@@ -16,11 +16,10 @@ import java.io.*;
import java.text.MessageFormat;
/**
- * LispInterpreter
is an interpreter for the Lisp programming
- * language. It takes the name of a file as a command-line argument, evaluates
- * the S-expressions found in the file and then prints the results to the
- * console. If no file name is provided at the command-line, this program will
- * read from standard input.
+ * This is an interpreter for the Lisp programming language. It takes the name of a file as a
+ * command-line argument, evaluates the s-expressions found in the file and then prints the results
+ * to the console. If no file name is provided at the command-line, this program will read from
+ * standard input.
*/
public class LispInterpreter {
@@ -31,15 +30,9 @@ public class LispInterpreter {
public static final String ANSI_GREEN = "\u001B[32m";
/**
- * Evaluate the S-expressions found in the file given as a command-line
- * argument and print the results to the console. If no file name was given,
- * retrieve the S-expressions from standard input.
- *
- * @param args
- * the command-line arguments:
- * args[0]
- file name (optional)true
- */
public boolean atomp() {
return true;
}
- /**
- * Returns a string representation of this ATOM.
- *
- * @return
- * a string representation of this ATOM
- */
public String toString() {
return text;
}
diff --git a/src/sexpression/Cons.java b/src/sexpression/Cons.java
index ae749da..00f5190 100644
--- a/src/sexpression/Cons.java
+++ b/src/sexpression/Cons.java
@@ -1,88 +1,35 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This class represents a Lisp CONS cell in the PL-Lisp implementation.
- */
public class Cons extends SExpression {
private SExpression car;
private SExpression cdr;
- /**
- * Create a new CONS cell with the specified car and cdr.
- *
- * @param car
- * the car of this CONS cell
- * @param cdr
- * the cdr of this CONS cell
- */
public Cons(SExpression car, SExpression cdr) {
this.car = car;
this.cdr = cdr;
}
- /**
- * Retrieve the car of this CONS cell.
- *
- * @return
- * the car of this CONS cell
- */
public SExpression getCar() {
return car;
}
- /**
- * Retrieve the cdr of this CONS cell.
- *
- * @return
- * the cdr of this CONS cell
- */
public SExpression getCdr() {
return cdr;
}
- /**
- * Set the car of this CONS cell to the specified value.
- *
- * @param newCar
- * the value to assign to the car of this CONS cell
- */
public void setCar(SExpression newCar) {
car = newCar;
}
- /**
- * Set the cdr of this CONS cell to the specified value.
- *
- * @param newCdr
- * the value to assign to the cdr of this CONS cell
- */
public void setCdr(SExpression newCdr) {
cdr = newCdr;
}
- /**
- * Test if this S-expression is a CONS cell.
- *
- * @return
- * true
- */
public boolean consp() {
return true;
}
- /**
- * Returns a string representation of this CONS cell.
- *
- * @return
- * a string representation of this CONS cell
- */
public String toString() {
return ("(" + toStringAux());
}
@@ -94,17 +41,12 @@ public class Cons extends SExpression {
// this method. When used in conjunction with the 'toString' method of this
// class, this method provides a means for creating the correct string
// representation of a list.
- //
- // Returns: a string representation of the car of a CONS cell followed by
- // its cdr
private String toStringAux() {
- if (cdr.nullp()) {
+ if (cdr.nullp())
return (car.toString() + ")");
- } else if (cdr.consp()) {
+ else if (cdr.consp())
return (car.toString() + " " + ((Cons) cdr).toStringAux());
- }
- // the cdr of this CONS cell is not a list
return (car.toString() + " . " + cdr.toString() + ")");
}
diff --git a/src/sexpression/LispNumber.java b/src/sexpression/LispNumber.java
index eaa5abc..68d9853 100644
--- a/src/sexpression/LispNumber.java
+++ b/src/sexpression/LispNumber.java
@@ -1,65 +1,29 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This class represents a NUMBER in the PL-Lisp implementation.
- */
public class LispNumber extends Atom {
private int value;
- /**
- * Create a new NUMBER with the specified text.
- *
- * @param text
- * the text representing this NUMBER
- * @throws IllegalArgumentException
- * Indicates that text
does not represent a valid integer.
- */
public LispNumber(String text) {
super(text.replaceFirst("^0+(?!$)", ""));
try {
this.value = Integer.parseInt(text);
} catch (NumberFormatException e) {
- throw new IllegalArgumentException(text +
- " is not a valid integer");
+ throw new IllegalArgumentException(text + " is not a valid integer");
}
}
- /**
- * Create a new NUMBER with the specified value.
- *
- * @param value
- * the integer value of this NUMBER
- */
public LispNumber(int value) {
super(Integer.toString(value));
this.value = value;
}
- /**
- * Test if this S-expression is a NUMBER.
- *
- * @return
- * true
- */
public boolean numberp() {
return true;
}
- /**
- * Retrieve the integer value of this NUMBER.
- *
- * @return
- * the integer value of this NUMBER
- */
public int getValue() {
return value;
}
diff --git a/src/sexpression/LispString.java b/src/sexpression/LispString.java
index b559cee..3a148d1 100644
--- a/src/sexpression/LispString.java
+++ b/src/sexpression/LispString.java
@@ -1,32 +1,11 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This class represents a STRING in the PL-Lisp implementation.
- */
public class LispString extends Atom {
- /**
- * Create a new STRING with the specified text.
- *
- * @param text
- * the text representing this STRING
- */
public LispString(String text) {
super(text);
}
- /**
- * Test if this S-expression is a STRING.
- *
- * @return
- * true
- */
public boolean stringp() {
return true;
}
diff --git a/src/sexpression/Nil.java b/src/sexpression/Nil.java
index b831e2e..ab2c529 100644
--- a/src/sexpression/Nil.java
+++ b/src/sexpression/Nil.java
@@ -1,102 +1,48 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This class represents NIL in the PL-Lisp implementation.
- */
public class Nil extends Cons {
private static Nil uniqueInstance = new Nil();
- /**
- * Retrieve the single unique instance of NIL.
- *
- * @return
- * the single unique instance of NIL
- */
public static Nil getUniqueInstance() {
return uniqueInstance;
}
- // We are using the Singleton pattern, so the constructor for 'Nil' is
- // private.
private Nil() {
super(null, null);
- // the car and cdr of NIL both refer to NIL
super.setCar(this);
super.setCdr(this);
}
- /**
- * Test if this S-expression is NULL.
- *
- * @return
- * true
- */
public boolean nullp() {
return true;
}
- /**
- * Test if this S-expression is an ATOM.
- *
- * @return
- * true
- */
public boolean atomp() {
return true;
}
- /**
- * Test if this S-expression is a CONS cell.
- *
- * @return
- * false
- */
public boolean consp() {
return false;
}
- /**
- * Test if this S-expression is a SYMBOL.
- *
- * @return
- * true
- */
public boolean symbolp() {
return true;
}
/**
- * Set the car of this CONS cell to the specified value. This method does
- * nothing (the car of NIL can not be changed).
- *
- * @param newCar
- * the value to assign to the car of this CONS cell
+ * Set the car of this CONS cell to the specified value. This method does nothing (the car of
+ * NIL can not be changed).
*/
public void setCar(SExpression newCar) {}
/**
- * Set the cdr of this CONS cell to the specified value. This method does
- * nothing (the cdr of NIL can not be changed).
- *
- * @param newCdr
- * the value to assign to the cdr of this CONS cell
+ * Set the cdr of this CONS cell to the specified value. This method does nothing (the cdr of
+ * NIL can not be changed).
*/
public void setCdr(SExpression newCdr) {}
- /**
- * Returns a string representation of NIL.
- *
- * @return
- * a string representation of NIL
- */
public String toString() {
return "NIL";
}
diff --git a/src/sexpression/SExpression.java b/src/sexpression/SExpression.java
index a40c7bb..28a7c4e 100644
--- a/src/sexpression/SExpression.java
+++ b/src/sexpression/SExpression.java
@@ -1,120 +1,49 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This is the base class for memory in the PL-Lisp implementation.
- */
-public class SExpression {
+public abstract class SExpression {
// for mark and sweep garbage collection
private boolean marked = false;
- /**
- * Determine if this SExpression
is marked.
- *
- * @return
- * true
if this SExpression
is marked;
- * false
otherwise
- */
public final boolean isMarked() {
return marked;
}
- /**
- * Set the marked status of this S-expression to the specified value.
- *
- * @param value
- * the value to assign to this S-expression's marked status
- */
public final void setMarked(final boolean value) {
marked = value;
}
- // Lisp type predicates;
- // by default, all return false (an SExpression effectively has NO type);
+ // Lisp type predicates...
// overridden in subclasses to describe their Lisp type
- /**
- * Test if this S-expression is NULL.
- *
- * @return
- * false
- */
public boolean nullp() {
return false;
}
- /**
- * Test if this S-expression is an ATOM.
- *
- * @return
- * false
- */
public boolean atomp() {
return false;
}
- /**
- * Test if this S-expression is a CONS cell.
- *
- * @return
- * false
- */
public boolean consp() {
return false;
}
- /**
- * Test if this S-expression is a LIST.
- *
- * @return
- * the value of (consp() || nullp())
- */
public boolean listp() {
return (consp() || nullp());
}
- /**
- * Test if this S-expression is a NUMBER.
- *
- * @return
- * false
- */
public boolean numberp() {
return false;
}
- /**
- * Test if this S-expression is a SYMBOL.
- *
- * @return
- * false
- */
public boolean symbolp() {
return false;
}
- /**
- * Test if this S-expression is a FUNCTION.
- *
- * @return
- * false
- */
public boolean functionp() {
return false;
}
- /**
- * Test if this S-expression is a STRING.
- *
- * @return
- * false
- */
public boolean stringp() {
return false;
}
diff --git a/src/sexpression/Symbol.java b/src/sexpression/Symbol.java
index 381966d..4b5e157 100644
--- a/src/sexpression/Symbol.java
+++ b/src/sexpression/Symbol.java
@@ -1,35 +1,17 @@
-/*
- * Name: Mike Cifelli
- * Course: CIS 443 - Programming Languages
- * Assignment: Lisp Parser
- */
-
package sexpression;
-/**
- * This class represents a SYMBOL in the PL-Lisp implementation.
- */
public class Symbol extends Atom {
- /** This SYMBOL represents TRUE in the PL-Lisp implementation. */
public static final Symbol T = new Symbol("T");
+
+ public static Symbol createQuote() {
+ return new Symbol("QUOTE");
+ }
- /**
- * Create a new SYMBOL with the specified text.
- *
- * @param text
- * the text representing this SYMBOL
- */
public Symbol(String text) {
super(text.toUpperCase());
}
- /**
- * Test if this S-expression is a SYMBOL.
- *
- * @return
- * true
- */
public boolean symbolp() {
return true;
}
diff --git a/src/token/Eof.java b/src/token/Eof.java
index 5e7ff95..6e8b22b 100644
--- a/src/token/Eof.java
+++ b/src/token/Eof.java
@@ -13,7 +13,7 @@ public class Eof extends Token {
}
@Override
- public SExpression sExpr(Supplier