use of org.cafebabepy.runtime.CafeBabePyException in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalBinOp.
private PyObject evalBinOp(PyObject context, PyObject node) {
PyObject left = this.runtime.getattr(node, "left");
PyObject evalLeft = eval(context, left);
PyObject right = this.runtime.getattr(node, "right");
PyObject evalRight = eval(context, right);
PyObject addType = this.runtime.typeOrThrow("_ast.Add");
PyObject subType = this.runtime.typeOrThrow("_ast.Sub");
PyObject modType = this.runtime.typeOrThrow("_ast.Mod");
PyObject multType = this.runtime.typeOrThrow("_ast.Mult");
PyObject op = this.runtime.getattr(node, "op");
if (this.runtime.isInstance(op, addType)) {
return this.runtime.add(evalLeft, evalRight);
} else if (this.runtime.isInstance(op, subType)) {
return this.runtime.sub(evalLeft, evalRight);
} else if (this.runtime.isInstance(op, modType)) {
return this.runtime.mod(evalLeft, evalRight);
} else if (this.runtime.isInstance(op, multType)) {
return this.runtime.mul(evalLeft, evalRight);
}
throw new CafeBabePyException("operator '" + op.getName() + "' not found");
}
use of org.cafebabepy.runtime.CafeBabePyException in project cafebabepy by cafebabepy.
the class AbstractAbstractCafeBabePyAny method getBases.
@Override
public List<PyObject> getBases() {
if (this.bases == null) {
synchronized (this) {
if (this.bases == null) {
String[] baseNames = getBaseNames();
List<PyObject> bases = new ArrayList<>(baseNames.length);
for (String baseName : getBaseNames()) {
PyObject base = this.runtime.type(baseName, false).orElseThrow(() -> new CafeBabePyException("type '" + getName() + "' parent '" + baseName + "' is not found"));
bases.add(base);
}
this.bases = Collections.unmodifiableList(Collections.synchronizedList(bases));
}
}
}
return this.bases;
}
use of org.cafebabepy.runtime.CafeBabePyException in project cafebabepy by cafebabepy.
the class PyJavaFunctionObject method call.
@Override
public PyObject call(PyObject... args) {
PyObject target = this.targetType;
Class<?> methodClass = this.method.getDeclaringClass();
for (PyObject type : target.getTypes()) {
Class<?> typeClass = type.getClass();
if (methodClass.isAssignableFrom(typeClass)) {
target = type;
break;
}
}
try {
Object[] compArgs;
Class<?>[] paramClasses = this.method.getParameterTypes();
if (paramClasses.length == 0) {
compArgs = new Object[0];
} else {
if (paramClasses.length == 1 && paramClasses[0].isArray()) {
compArgs = new Object[] { args };
} else if (paramClasses[paramClasses.length - 1].isArray()) {
// Split argument and variable argument from args array
if (args.length > 0) {
if (args.length - paramClasses.length >= 0) {
// Last argument is variable argument
compArgs = new Object[paramClasses.length];
PyObject[] lastArrayArg = new PyObject[args.length - compArgs.length + 1];
System.arraycopy(args, 0, compArgs, 0, compArgs.length - 1);
for (int i = 0; i < lastArrayArg.length; i++) {
lastArrayArg[i] = args[i + compArgs.length - 1];
}
compArgs[paramClasses.length - 1] = lastArrayArg;
} else {
compArgs = new Object[args.length + 1];
for (int i = 0; i < args.length; i++) {
if (args[i].getClass().isArray()) {
compArgs[i] = new PyObject[] { args[i] };
} else {
compArgs[i] = args[i];
}
}
compArgs[compArgs.length - 1] = new PyObject[0];
}
} else {
compArgs = new Object[] { new PyObject[0] };
}
} else if (paramClasses.length != args.length) {
throw this.runtime.newRaiseException("builtins.TypeError", this.name + "() takes at most " + paramClasses.length + " arguments (" + args.length + " given)");
} else {
compArgs = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Class<?> paramClass = args[i].getClass();
if (paramClass.isArray()) {
compArgs[i] = new PyObject[] { args[i] };
} else {
compArgs[i] = args[i];
}
}
}
}
Object result = this.method.invoke(target, compArgs);
if (result == null) {
return this.runtime.None();
} else if (result instanceof PyObject) {
return (PyObject) result;
} else {
// FIXME Java object to Python object
throw new UnsupportedOperationException("FIXME Java object to Python object !!!!!!!!");
}
} catch (IllegalAccessException | IllegalArgumentException e) {
// FIXME CPython message???
throw new CafeBabePyException("Not accessible method " + this.method.getDeclaringClass().getName() + "#" + this.method.getName() + Arrays.stream(this.method.getParameterTypes()).map(Class::getName).collect(Collectors.joining(", ", " (", ")")), e);
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
}
throw new CafeBabePyException(targetException);
}
}
use of org.cafebabepy.runtime.CafeBabePyException in project cafebabepy by cafebabepy.
the class AbstractAbstractCafeBabePyAny method defineClassMember.
private void defineClassMember(Class<?> clazz) {
// Check duplicate
Set<String> defineClassMemberNamesSet = new HashSet<>();
for (Method method : clazz.getMethods()) {
// Same class method only
if (clazz != method.getDeclaringClass()) {
continue;
}
DefinePyFunction definePyFunction = method.getAnnotation(DefinePyFunction.class);
if (definePyFunction == null) {
continue;
}
if (defineClassMemberNamesSet.contains(definePyFunction.name())) {
throw new CafeBabePyException("Duplicate '" + definePyFunction.name() + "' function");
}
PyJavaFunctionObject f = new PyJavaFunctionObject(getRuntime(), this, definePyFunction.name(), method);
if (__call__.equals(f.getName())) {
f.getScope().put(__call__, f);
}
getScope().put(f.getName(), f);
defineClassMemberNamesSet.add(definePyFunction.name());
}
}
Aggregations