2016-12-19 13:05:53 -05:00
|
|
|
package function;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2016-12-19 13:05:53 -05:00
|
|
|
import function.builtin.*;
|
2016-12-15 11:19:03 -05:00
|
|
|
import sexpression.*;
|
2016-12-19 13:05:53 -05:00
|
|
|
import table.SymbolTable;
|
2016-12-15 11:19:03 -05:00
|
|
|
|
|
|
|
public class UserDefinedFunction extends LispFunction {
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
private String name;
|
|
|
|
private Cons body;
|
2016-12-20 12:03:37 -05:00
|
|
|
private Cons lambdaExpression;
|
2016-12-07 16:38:26 -05:00
|
|
|
private SymbolTable environment;
|
2016-12-20 12:03:37 -05:00
|
|
|
private ArrayList<String> formalParameters;
|
2016-12-22 16:55:25 -05:00
|
|
|
private ArgumentValidator argumentValidator;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-15 11:19:03 -05:00
|
|
|
public UserDefinedFunction(String name, Cons lambdaList, Cons body) {
|
2016-12-07 16:38:26 -05:00
|
|
|
this.name = name;
|
|
|
|
this.body = body;
|
2016-12-20 12:03:37 -05:00
|
|
|
this.lambdaExpression = new Cons(new Symbol(name), new Cons(lambdaList, body));
|
2016-12-07 16:38:26 -05:00
|
|
|
this.environment = SETF.getEnvironment();
|
|
|
|
|
2016-12-22 16:55:25 -05:00
|
|
|
for (this.formalParameters = new ArrayList<>(); lambdaList.consp(); lambdaList = (Cons) lambdaList.getCdr())
|
2016-12-20 12:03:37 -05:00
|
|
|
this.formalParameters.add(lambdaList.getCar().toString());
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-20 12:03:37 -05:00
|
|
|
this.argumentValidator = new ArgumentValidator(this.name);
|
|
|
|
this.argumentValidator.setExactNumberOfArguments(this.formalParameters.size());
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public SExpression call(Cons argList) {
|
2016-12-20 12:03:37 -05:00
|
|
|
argumentValidator.validate(argList);
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
// push a new symbol table onto this function's environment (for its
|
|
|
|
// parameters)
|
|
|
|
environment = new SymbolTable(environment);
|
|
|
|
|
|
|
|
// bind the values of the arguments to the formal parameter names
|
2016-12-20 12:03:37 -05:00
|
|
|
for (String param : formalParameters) {
|
2016-12-07 16:38:26 -05:00
|
|
|
SExpression currentArg = argList.getCar();
|
|
|
|
|
|
|
|
environment.put(param, currentArg);
|
|
|
|
|
|
|
|
argList = (Cons) argList.getCdr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the environment of the S-expression that called this function
|
|
|
|
// (the current environment)
|
|
|
|
SymbolTable currentEnvironment = SETF.getEnvironment();
|
|
|
|
|
|
|
|
// replace the current environment with the environment of this
|
|
|
|
// function
|
|
|
|
SETF.setEnvironment(environment);
|
|
|
|
|
|
|
|
Cons currentSExpression = body;
|
|
|
|
SExpression retval = null;
|
|
|
|
|
|
|
|
// evaluate all the S-expressions making up this function's body
|
|
|
|
while (currentSExpression.consp()) {
|
|
|
|
retval = EVAL.eval(currentSExpression.getCar());
|
|
|
|
currentSExpression = (Cons) currentSExpression.getCdr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace the environment of the S-expression that called this
|
|
|
|
// function
|
|
|
|
SETF.setEnvironment(currentEnvironment);
|
|
|
|
|
|
|
|
// remove the bindings of the arguments to the formal parameter names
|
|
|
|
// in the environment of this function
|
|
|
|
environment = new SymbolTable(environment.getParent());
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-12-20 12:03:37 -05:00
|
|
|
public Cons getLambdaExpression() {
|
|
|
|
return lambdaExpression;
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|