2016-12-19 13:05:53 -05:00
|
|
|
package function.builtin;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-15 15:33:48 -05:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
2016-12-14 13:09:41 -05:00
|
|
|
|
2016-12-19 13:05:53 -05:00
|
|
|
import function.LispFunction;
|
2017-01-14 18:01:14 -05:00
|
|
|
import function.builtin.cons.LENGTH;
|
2016-12-15 15:33:48 -05:00
|
|
|
import parser.LispParser;
|
|
|
|
import sexpression.*;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <code>LOAD</code> represents the LOAD function in Lisp.
|
|
|
|
*/
|
|
|
|
public class LOAD extends LispFunction {
|
|
|
|
// The number of arguments that LOAD takes.
|
|
|
|
private static final int NUM_ARGS = 1;
|
|
|
|
|
|
|
|
public SExpression call(Cons argList) {
|
|
|
|
// retrieve the number of arguments passed to LOAD
|
|
|
|
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("LOAD"), argList);
|
|
|
|
String errMsg = "too " +
|
|
|
|
((argListLength > NUM_ARGS) ? "many" : "few") +
|
|
|
|
" arguments given to LOAD: " + originalSExpr;
|
|
|
|
|
|
|
|
throw new RuntimeException(errMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
SExpression argCar = argList.getCar();
|
|
|
|
|
|
|
|
// make sure the argument is a string
|
|
|
|
if (! argCar.stringp()) {
|
|
|
|
throw new RuntimeException("LOAD: " + argCar + " is not a string");
|
|
|
|
}
|
|
|
|
|
|
|
|
LispString quotedName = (LispString) argCar;
|
|
|
|
String fileName = quotedName.toString();
|
|
|
|
|
|
|
|
// remove the surrounding quotes from the file name
|
|
|
|
fileName = fileName.substring(1, (fileName.length() - 1));
|
|
|
|
|
|
|
|
return processFile(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate all the S-expressions found in the file with the specified
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// Parameters: fileName - the name of the file to be evaluated
|
|
|
|
// Returns: 'T' if the file was processed successfully; 'NIL' otherwise
|
|
|
|
private SExpression processFile(String fileName) {
|
|
|
|
LispParser parser = null;
|
|
|
|
|
|
|
|
// attempt to create a new 'LispParser' on the specified file
|
|
|
|
try {
|
|
|
|
parser = new LispParser(new FileInputStream(fileName), fileName);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
System.out.println("LOAD: could not open " + fileName);
|
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
return Nil.getInstance();
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to evaluate all the S-expressions contained in the file
|
2016-12-14 13:09:41 -05:00
|
|
|
while (! parser.isEof()) {
|
2016-12-07 16:38:26 -05:00
|
|
|
try {
|
2016-12-14 13:09:41 -05:00
|
|
|
SExpression sexpr = parser.getNextSExpression();
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
EVAL.eval(sexpr);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
System.out.println("LOAD: " + e.getMessage());
|
|
|
|
|
2016-12-29 13:32:45 -05:00
|
|
|
return Nil.getInstance();
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// success!
|
|
|
|
return Symbol.T;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|