com.github.dakusui.logias.lisp.func.util.Op Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of logiaslisp Show documentation
Show all versions of logiaslisp Show documentation
A JSON based Lisp processor.
The newest version!
package com.github.dakusui.logias.lisp.func.util;
import static java.lang.String.format;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import com.github.dakusui.logias.lisp.Context;
import com.github.dakusui.logias.lisp.func.Func;
import com.github.dakusui.logias.lisp.s.Literal;
import com.github.dakusui.logias.lisp.s.Sexp;
public abstract class Op extends Func {
@Override
public Sexp invoke(Context context, Sexp... params) {
if (params == null) {
throw new RuntimeException();
}
if (params.length < 2) {
throw new RuntimeException(format("This function takes at least 2 parameters. But only <%s> given. (%s:%s)", params.length, this.getClass().getSimpleName(), Arrays.toString(params)));
}
List unexpectedSexpList = new LinkedList();
List parameterList = new LinkedList();
for (Sexp s : params) {
if (!s.isAtom() || !(s instanceof Literal)) {
unexpectedSexpList.add(s);
} else {
parameterList.add((Literal) s);
}
}
if (unexpectedSexpList.size() > 0) {
throw new RuntimeException(format("A literal is expected, but non-atoms:%s are given.", unexpectedSexpList));
}
return calc(parameterList.toArray(new Literal[0]));
}
protected abstract Sexp calc(Literal... p1);
}