2016-12-19 13:05:53 -05:00
|
|
|
package function.builtin;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-19 13:05:53 -05:00
|
|
|
import function.*;
|
2017-01-14 18:01:14 -05:00
|
|
|
import function.builtin.cons.LENGTH;
|
2016-12-15 15:33:48 -05:00
|
|
|
import sexpression.*;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <code>SYMBOL_FUNCTION</code> represents the SYMBOL-FUNCTION function in
|
|
|
|
* Lisp.
|
|
|
|
*/
|
|
|
|
public class SYMBOL_FUNCTION extends LispFunction {
|
|
|
|
|
|
|
|
// The number of arguments that SYMBOL-FUNCTION takes.
|
|
|
|
private static final int NUM_ARGS = 1;
|
|
|
|
|
|
|
|
public SExpression call(Cons argList) {
|
|
|
|
// retrieve the number of arguments passed to SYMBOL-FUNCTION
|
|
|
|
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("SYMBOL-FUNCTION"),
|
|
|
|
argList);
|
|
|
|
String errMsg = "too " +
|
|
|
|
((argListLength > NUM_ARGS) ? "many" : "few") +
|
|
|
|
" arguments given to SYMBOL-FUNCTION: " +
|
|
|
|
originalSExpr;
|
|
|
|
|
|
|
|
throw new RuntimeException(errMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SExpression arg = argList.getCar();
|
|
|
|
|
|
|
|
// make sure the argument is a symbol
|
|
|
|
if (arg.symbolp()) {
|
|
|
|
LispFunction function = EVAL.lookupFunction(arg.toString());
|
|
|
|
|
|
|
|
// make sure the function actually exists
|
|
|
|
if (function != null) {
|
2016-12-15 11:19:03 -05:00
|
|
|
if (function instanceof UserDefinedFunction) {
|
2016-12-07 16:38:26 -05:00
|
|
|
// this is a user-defined function
|
|
|
|
|
2016-12-15 11:19:03 -05:00
|
|
|
UserDefinedFunction udFunction = (UserDefinedFunction) function;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-20 12:03:37 -05:00
|
|
|
return udFunction.getLambdaExpression();
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is a built-in function
|
|
|
|
|
|
|
|
return new Symbol("SUBR-" + arg.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException("SYMBOL-FUNCTION: undefined function " +
|
|
|
|
arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException("SYMBOL-FUNCTION: " + arg +
|
|
|
|
" is not a symbol");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|