transcendental-lisp/eval/SETF.java

113 lines
2.9 KiB
Java
Raw Normal View History

2016-12-07 14:16:45 -05:00
/*
* Name: Mike Cifelli
* Course: CIS 443 - Programming Languages
* Assignment: Lisp Interpreter 2
*/
package eval;
import parser.*;
/**
* <code>SETF</code> represents the SETF form in Lisp.
*/
public class SETF extends LispFunction {
private static SymbolTable environment = new SymbolTable();
/**
* Look up the value of a symbol using its name.
*
* @param symbolName
* the name of the symbol to look up
* @return
* the value of <code>symbolName</code> if it has one; null otherwise
*/
public static SExpression lookup(String symbolName) {
SymbolTable current = environment;
while (current != null) {
if (current.contains(symbolName)) {
return current.get(symbolName);
}
current = current.getParent();
}
return null;
}
/**
* Set the current environment to the specified value.
*
* @param newEnvironment
* the value to set the environment to
*/
public static void setEnvironment(SymbolTable newEnvironment) {
environment = newEnvironment;
}
/**
* Retrieve the current environment.
*
* @return
* the current environment
*/
public static SymbolTable getEnvironment() {
return environment;
}
// The number of arguments that SETF takes.
private static final int NUM_ARGS = 2;
public SExpression call(Cons argList) {
// retrieve the number of arguments passed to SETF
int argListLength = LENGTH.getLength(argList);
// make sure we have received the proper number of arguments
if (argListLength != NUM_ARGS) {
Cons originalSExpr = new Cons(new Symbol("SETF"), argList);
String errMsg = "too " +
((argListLength > NUM_ARGS) ? "many" : "few") +
" arguments given to SETF: " + originalSExpr;
throw new RuntimeException(errMsg);
}
SExpression symbol = argList.getCar();
// make sure the first argument is a symbol
if (! symbol.symbolp()) {
throw new RuntimeException("SETF: " + symbol + " is not a symbol");
}
Cons cdr = (Cons) argList.getCdr();
SExpression value = EVAL.eval(cdr.getCar());
SymbolTable current = environment;
// set 'current' to the symbol table that contains 'symbol' or the
// global symbol table if 'symbol' is not in the environment
while ((! current.contains(symbol.toString())) &&
(current.getParent() != null)) {
current = current.getParent();
}
current.put(symbol.toString(), value);
return value;
}
/**
* Determine if the arguments passed to this Lisp function should be
* evaluated.
*
* @return
* <code>false</code>
*/
public boolean evaluateArguments() {
return false;
}
}