transcendental-lisp/eval/CONS.java

45 lines
1.2 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 1
*/
package eval;
import parser.*;
/**
* <code>CONS</code> represents the CONS function in Lisp.
*/
public class CONS extends LispFunction {
// The number of arguments that CONS takes.
private static final int NUM_ARGS = 2;
public Cons call(Cons argList) {
// retrieve the number of arguments passed to CONS
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("CONS"), argList);
String errMsg = "too " +
((argListLength > NUM_ARGS) ? "many" : "few") +
" arguments given to CONS: " + originalSExpr;
throw new RuntimeException(errMsg);
}
// the car of the CONS cell we are going to create
SExpression argOne = argList.getCar();
Cons cdr = (Cons) argList.getCdr();
// the cdr of the CONS cell we are going to create
SExpression argTwo = cdr.getCar();
return new Cons(argOne, argTwo);
}
}