Refactored the eval package

This commit is contained in:
Mike Cifelli 2016-12-19 13:05:53 -05:00
parent fbd2b3207c
commit 089e3bd520
40 changed files with 162 additions and 211 deletions

View File

@ -1,63 +0,0 @@
package eval;
import sexpression.Cons;
import sexpression.SExpression;
/**
* This class represents a Lisp FUNCTION in the PL-Lisp implementation.
*/
public class LambdaExpression extends SExpression {
private Cons lexpr;
private UserDefinedFunction function;
/**
* Create a new FUNCTION with the specified lambda expression and internal representation.
*
* @param lexpr
* the lambda expression of this FUNCTION
* @param function
* the internal representation of this FUNCTION
*/
public LambdaExpression(Cons lexpr, UserDefinedFunction function) {
this.lexpr = lexpr;
this.function = function;
}
/**
* Test if this S-expression is a FUNCTION.
*
* @return <code>true</code>
*/
public boolean functionp() {
return true;
}
/**
* Retrieve the lambda expression of this FUNCTION.
*
* @return the lambda expression of this FUNCTION
*/
public Cons getLExpression() {
return lexpr;
}
/**
* Retrieve the internal representation of this FUNCTION.
*
* @return the user-defined function of this FUNCTION
*/
public UserDefinedFunction getFunction() {
return function;
}
/**
* Returns a string representation of this FUNCTION.
*
* @return a string representation of this FUNCTION
*/
public String toString() {
return lexpr.toString();
}
}

View File

@ -1,33 +0,0 @@
package eval;
import sexpression.Cons;
import sexpression.SExpression;
/**
* A <code>LispFunction</code> is an internal representation of a built-in function in the Lisp
* programming language.
*/
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
* @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.
*/
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.
*/
public boolean evaluateArguments() {
return true;
}
}

View File

@ -1,79 +0,0 @@
package eval;
import java.util.HashMap;
import sexpression.SExpression;
/**
* A <code>SymbolTable</code> maps symbol names to values.
*/
public class SymbolTable {
private HashMap<String, SExpression> table;
private SymbolTable parent;
/**
* Create a new symbol table with no parent.
*/
public SymbolTable() {
this(null);
}
/**
* Create a new symbol table with the specified parent.
*
* @param parent
* the parent of this symbol table
*/
public SymbolTable(SymbolTable parent) {
this.table = new HashMap<String, SExpression>();
this.parent = parent;
}
/**
* Determine if the specified symbol name is in this symbol table.
*
* @param symbolName
* the name of the symbol to look up
* @return <code>true</code> if the symbol is in this symbol table; <code>false</code> otherwise
*/
public boolean contains(String symbolName) {
return table.containsKey(symbolName);
}
/**
* Returns the value to which the specified symbol name is mapped in this symbol table.
*
* @param symbolName
* the name of the symbol whose associated value is to be returned
* @return the value to which this symbol table maps <code>symbolName</code>, or null if no
* mapping exists
*/
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.
*
* @param symbolName
* 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
*/
public void put(String symbolName, SExpression value) {
table.put(symbolName, value);
}
/**
* Returns the parent of this symbol table.
*
* @return the parent of this symbol table
*/
public SymbolTable getParent() {
return parent;
}
}

View File

@ -1,9 +1,9 @@
package eval.argument;
package function;
import java.text.MessageFormat;
import error.LispException;
import eval.LENGTH;
import function.builtin.LENGTH;
import sexpression.*;
public class ArgumentValidator {

View File

@ -0,0 +1,18 @@
package function;
import sexpression.*;
public abstract class LispFunction {
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.
*/
public boolean evaluateArguments() {
return true;
}
}

View File

@ -1,8 +1,10 @@
package eval;
package function;
import java.util.ArrayList;
import function.builtin.*;
import sexpression.*;
import table.SymbolTable;
public class UserDefinedFunction extends LispFunction {

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.*;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,7 +1,8 @@
package eval;
package function.builtin;
import java.util.HashMap;
import function.*;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,7 +1,8 @@
package eval;
package function.builtin;
import java.util.HashMap;
import function.*;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
public class EXIT extends LispFunction {

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.*;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,6 +1,8 @@
package eval;
package function.builtin;
import function.*;
import sexpression.*;
import table.SymbolTable;
/**
* <code>LET</code> represents the LET form in Lisp.

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,8 +1,9 @@
package eval;
package function.builtin;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import function.LispFunction;
import parser.LispParser;
import sexpression.*;

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.LispFunction;
import sexpression.*;
/**

View File

@ -1,6 +1,8 @@
package eval;
package function.builtin;
import function.*;
import sexpression.*;
import table.SymbolTable;
/**
* <code>SETF</code> represents the SETF form in Lisp.

View File

@ -1,5 +1,6 @@
package eval;
package function.builtin;
import function.*;
import sexpression.*;
/**

View File

@ -4,7 +4,7 @@ import java.io.*;
import java.text.MessageFormat;
import error.*;
import eval.EVAL;
import function.builtin.EVAL;
import parser.LispParser;
import sexpression.SExpression;

View File

@ -0,0 +1,31 @@
package sexpression;
import function.UserDefinedFunction;
public class LambdaExpression extends SExpression {
private Cons lambdaExpression;
private UserDefinedFunction function;
public LambdaExpression(Cons lambdaExpression, UserDefinedFunction function) {
this.lambdaExpression = lambdaExpression;
this.function = function;
}
public boolean functionp() {
return true;
}
public Cons getLambdaExpression() {
return lambdaExpression;
}
public UserDefinedFunction getFunction() {
return function;
}
public String toString() {
return lambdaExpression.toString();
}
}

View File

@ -0,0 +1,43 @@
package table;
import java.util.HashMap;
import sexpression.SExpression;
public class SymbolTable {
private HashMap<String, SExpression> table;
private SymbolTable parent;
/**
* Create a new symbol table with no parent.
*/
public SymbolTable() {
this(null);
}
/**
* Create a new symbol table with the specified parent.
*/
public SymbolTable(SymbolTable parent) {
this.table = new HashMap<String, SExpression>();
this.parent = parent;
}
public boolean contains(String symbolName) {
return table.containsKey(symbolName);
}
public SExpression get(String symbolName) {
return table.get(symbolName);
}
public void put(String symbolName, SExpression value) {
table.put(symbolName, value);
}
public SymbolTable getParent() {
return parent;
}
}

View File

@ -1,11 +1,11 @@
package eval.argument;
package function;
import static org.junit.Assert.*;
import org.junit.*;
import error.ErrorManager;
import eval.argument.ArgumentValidator.*;
import function.ArgumentValidator.*;
import sexpression.*;
public class ArgumentValidatorTester {