From 7b7556cc65ea1b2976798b6c4205d46b45939b87 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Thu, 15 Dec 2016 11:19:03 -0500 Subject: [PATCH] Started refactoring the sexpression package and cleaned up some unit test code --- src/error/package.html | 3 - src/eval/DEFUN.java | 2 +- src/eval/LAMBDA.java | 4 +- src/eval/LambdaExpression.java | 6 +- src/eval/SYMBOL_FUNCTION.java | 4 +- ...Function.java => UserDefinedFunction.java} | 21 +----- src/eval/package.html | 4 - src/file/FilePosition.java | 16 ++-- src/main/LispInterpreter.java | 23 ++---- src/main/package.html | 3 - src/parser/LispParser.java | 5 +- src/parser/package.html | 4 - src/scanner/package.html | 3 - src/sexpression/Atom.java | 29 +------ src/sexpression/Cons.java | 62 +-------------- src/sexpression/LispNumber.java | 38 +--------- src/sexpression/LispString.java | 21 ------ src/sexpression/Nil.java | 62 +-------------- src/sexpression/SExpression.java | 75 +------------------ src/sexpression/Symbol.java | 26 +------ src/token/Eof.java | 2 +- src/token/Identifier.java | 2 +- src/token/LeftParenthesis.java | 4 +- src/token/Number.java | 2 +- src/token/QuoteMark.java | 6 +- src/token/QuotedString.java | 2 +- src/token/RightParenthesis.java | 4 +- src/token/Token.java | 11 ++- test/file/FilePositionTrackerTester.java | 66 ++++------------ 29 files changed, 73 insertions(+), 437 deletions(-) delete mode 100644 src/error/package.html rename src/eval/{UDFunction.java => UserDefinedFunction.java} (88%) delete mode 100644 src/eval/package.html delete mode 100644 src/main/package.html delete mode 100644 src/parser/package.html delete mode 100644 src/scanner/package.html 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 that lexpr 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: - * + * 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. */ public static void main(String[] args) { LispParser parser = null; @@ -94,7 +87,7 @@ public class LispInterpreter { @Override public int getSeverity() { - return 0; + return 0; } @Override diff --git a/src/main/package.html b/src/main/package.html deleted file mode 100644 index bfdbdab..0000000 --- a/src/main/package.html +++ /dev/null @@ -1,3 +0,0 @@ - - Provides test drivers for the various stages of the Lisp Interpreter. - diff --git a/src/parser/LispParser.java b/src/parser/LispParser.java index 435f96c..e28ff58 100644 --- a/src/parser/LispParser.java +++ b/src/parser/LispParser.java @@ -9,8 +9,7 @@ import token.Eof; import token.Token; /** - * Converts a stream of bytes into internal representations of Lisp - * S-expressions. + * Converts a stream of bytes into internal representations of Lisp s-expressions. */ public class LispParser { @@ -50,7 +49,7 @@ public class LispParser { isNextTokenStored = false; - return nextToken.sExpr(scanner::getNextToken); + return nextToken.parseSExpression(scanner::getNextToken); } private void throwDelayedExceptionIfNecessary() { diff --git a/src/parser/package.html b/src/parser/package.html deleted file mode 100644 index ed4dbdd..0000000 --- a/src/parser/package.html +++ /dev/null @@ -1,4 +0,0 @@ - - Provides the classes necessary for creating an internal representation of - the Lisp programming language. - diff --git a/src/scanner/package.html b/src/scanner/package.html deleted file mode 100644 index 5a0c31e..0000000 --- a/src/scanner/package.html +++ /dev/null @@ -1,3 +0,0 @@ - - Provides the classes necessary to perform a lexical analysis of the Lisp programming language. - diff --git a/src/sexpression/Atom.java b/src/sexpression/Atom.java index 3fd4596..2c9d9e5 100644 --- a/src/sexpression/Atom.java +++ b/src/sexpression/Atom.java @@ -1,44 +1,17 @@ -/* - * Name: Mike Cifelli - * Course: CIS 443 - Programming Languages - * Assignment: Lisp Parser - */ - package sexpression; -/** - * This class represents an ATOM in the PL-Lisp implementation. - */ -public class Atom extends SExpression { +public abstract class Atom extends SExpression { private String text; - /** - * Create a new ATOM with the specified text. - * - * @param text - * the text representing this ATOM - */ public Atom(String text) { this.text = text; } - /** - * Test if this S-expression is an ATOM. - * - * @return - * 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 getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { throw new EofEncounteredException(this); } diff --git a/src/token/Identifier.java b/src/token/Identifier.java index 7c6a2e9..f425de4 100644 --- a/src/token/Identifier.java +++ b/src/token/Identifier.java @@ -13,7 +13,7 @@ public class Identifier extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { return new Symbol(getText()); } diff --git a/src/token/LeftParenthesis.java b/src/token/LeftParenthesis.java index 758443a..b3fc350 100644 --- a/src/token/LeftParenthesis.java +++ b/src/token/LeftParenthesis.java @@ -12,10 +12,10 @@ public class LeftParenthesis extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { Token nextToken = getNextToken.get(); - return nextToken.sExprTail(getNextToken); + return nextToken.parseSExpressionTail(getNextToken); } } diff --git a/src/token/Number.java b/src/token/Number.java index 314c433..dad0d82 100644 --- a/src/token/Number.java +++ b/src/token/Number.java @@ -13,7 +13,7 @@ public class Number extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { return new LispNumber(getText()); } diff --git a/src/token/QuoteMark.java b/src/token/QuoteMark.java index d7b12bb..64ee3f4 100644 --- a/src/token/QuoteMark.java +++ b/src/token/QuoteMark.java @@ -12,11 +12,11 @@ public class QuoteMark extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { Token nextToken = getNextToken.get(); - SExpression argument = nextToken.sExpr(getNextToken); + SExpression argument = nextToken.parseSExpression(getNextToken); - return new Cons(new Symbol("QUOTE"), new Cons(argument, Nil.getUniqueInstance())); + return new Cons(Symbol.createQuote(), new Cons(argument, Nil.getUniqueInstance())); } } diff --git a/src/token/QuotedString.java b/src/token/QuotedString.java index a0d9d69..22b08b9 100644 --- a/src/token/QuotedString.java +++ b/src/token/QuotedString.java @@ -13,7 +13,7 @@ public class QuotedString extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { return new LispString(getText()); } diff --git a/src/token/RightParenthesis.java b/src/token/RightParenthesis.java index bd329e1..135788c 100644 --- a/src/token/RightParenthesis.java +++ b/src/token/RightParenthesis.java @@ -14,12 +14,12 @@ public class RightParenthesis extends Token { } @Override - public SExpression sExpr(Supplier getNextToken) { + public SExpression parseSExpression(Supplier getNextToken) { throw new StartsWithRightParenthesisException(this); } @Override - public SExpression sExprTail(Supplier getNextToken) { + public SExpression parseSExpressionTail(Supplier getNextToken) { return Nil.getUniqueInstance(); } diff --git a/src/token/Token.java b/src/token/Token.java index f8f5480..b2dd533 100644 --- a/src/token/Token.java +++ b/src/token/Token.java @@ -35,16 +35,15 @@ public abstract class Token { return position.getColumnNumber(); } - // sExpr ::= NUMBER | IDENTIFIER | STRING | QUOTE_MARK sExpr | - // LEFT_PAREN sExprTail - public abstract SExpression sExpr(Supplier getNextToken); + // sExpr ::= NUMBER | IDENTIFIER | STRING | QUOTE_MARK sExpr | LEFT_PAREN sExprTail + public abstract SExpression parseSExpression(Supplier getNextToken); // sExprTail ::= RIGHT_PAREN | sExpr sExprTail - public SExpression sExprTail(Supplier getNextToken) { - SExpression car = sExpr(getNextToken); + public SExpression parseSExpressionTail(Supplier getNextToken) { + SExpression car = parseSExpression(getNextToken); Token nextToken = getNextToken.get(); - SExpression cdr = nextToken.sExprTail(getNextToken); + SExpression cdr = nextToken.parseSExpressionTail(getNextToken); return new Cons(car, cdr); } diff --git a/test/file/FilePositionTrackerTester.java b/test/file/FilePositionTrackerTester.java index 5af16f2..7f22503 100644 --- a/test/file/FilePositionTrackerTester.java +++ b/test/file/FilePositionTrackerTester.java @@ -1,8 +1,9 @@ package file; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.Objects; + import org.junit.Before; import org.junit.Test; @@ -20,81 +21,46 @@ public class FilePositionTrackerTester { return position; } + private void assertTrackerPositionEquals(FilePosition expectedPosition) { + assertTrue(arePositionsEqual(expectedPosition, trackerUnderTest.getCurrentPosition())); + } + + private boolean arePositionsEqual(FilePosition position1, FilePosition position2) { + return Objects.equals(position1.getFileName(), position2.getFileName()) + && Objects.equals(position1.getLineNumber(), position2.getLineNumber()) + && Objects.equals(position1.getColumnNumber(), position2.getColumnNumber()); + } + @Before public void setUp() throws Exception { trackerUnderTest = new FilePositionTracker(FILE_NAME); } - @Test - public void filePositionEquality_CorrectlyReturnsTrue() { - FilePosition positionOne = createFilePosition(5, 9); - FilePosition positionTwo = createFilePosition(5, 9); - - assertTrue(positionOne.isEqual(positionTwo)); - } - - @Test - public void filePositionEquality_CorrectlyReturnsFalseWithDifferentLine() { - FilePosition positionOne = createFilePosition(5, 9); - FilePosition positionTwo = createFilePosition(8, 9); - - assertFalse(positionOne.isEqual(positionTwo)); - } - - @Test - public void filePositionEquality_CorrectlyReturnsFalseWithDifferentColumn() { - FilePosition positionOne = createFilePosition(5, 9); - FilePosition positionTwo = createFilePosition(5, 10); - - assertFalse(positionOne.isEqual(positionTwo)); - } - - @Test - public void filePositionEquality_CorrectlyReturnsFalseWithDifferentFileName() { - FilePosition positionOne = new FilePosition("FileOne"); - positionOne.setLineNumber(5); - positionOne.setColumnNumber(9); - - FilePosition positionTwo = new FilePosition("FileTwo"); - positionTwo.setLineNumber(5); - positionTwo.setColumnNumber(9); - - assertFalse(positionOne.isEqual(positionTwo)); - } - @Test public void noMovement_ReturnsInitialPosition() { - FilePosition expectedPosition = createFilePosition(1, 0); - - assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition())); + assertTrackerPositionEquals(createFilePosition(1, 0)); } @Test public void advanceOneColumn_ReturnsCorrectPosition() { - FilePosition expectedPosition = createFilePosition(1, 1); - trackerUnderTest.incrementColumn(); - assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition())); + assertTrackerPositionEquals(createFilePosition(1, 1)); } @Test public void advanceOneLine_ReturnsCorrectPosition() { - FilePosition expectedPosition = createFilePosition(2, 0); - trackerUnderTest.incrementLine(); - assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition())); + assertTrackerPositionEquals(createFilePosition(2, 0)); } @Test public void advanceOneLine_ResetsColumn() { - FilePosition expectedPosition = createFilePosition(2, 0); - trackerUnderTest.incrementColumn(); trackerUnderTest.incrementLine(); - assertTrue(expectedPosition.isEqual(trackerUnderTest.getCurrentPosition())); + assertTrackerPositionEquals(createFilePosition(2, 0)); } }