45 lines
1.5 KiB
Java
45 lines
1.5 KiB
Java
package serene.simple;
|
|
|
|
import java.util.function.Function;
|
|
|
|
public class FnSpecialForm extends SpecialForm {
|
|
public FnSpecialForm(ListNode<Node> paramsAndBody) {
|
|
super(paramsAndBody);
|
|
}
|
|
|
|
@Override
|
|
public Object eval(final Scope parentScope) {
|
|
final ListNode<Node> formalParams = (ListNode<Node>) this.node.rest().first();
|
|
final ListNode<Node> body = this.node.rest().rest();
|
|
|
|
return new Function<Object, Object>() {
|
|
@Override
|
|
public Object apply(Object arguments) {
|
|
Object[] args = (Object[]) arguments;
|
|
|
|
Scope scope = new Scope(parentScope);
|
|
if (args.length != formalParams.length) {
|
|
throw new RuntimeException(String.format("Wrong number of arguments. Expected: %s, Got: %s.",
|
|
formalParams.length,
|
|
args.length));
|
|
}
|
|
// Map parameter values to formal parameter names
|
|
int i = 0;
|
|
for (Node param : formalParams) {
|
|
SymbolNode paramSymbol = (SymbolNode) param;
|
|
scope.insertSymbol(paramSymbol.name, args[i]);
|
|
i++;
|
|
}
|
|
|
|
// Evaluate body
|
|
Object output = null;
|
|
for (Node node : body) {
|
|
output = node.eval(scope);
|
|
}
|
|
|
|
return output;
|
|
}
|
|
};
|
|
}
|
|
}
|