Search in sources :

Example 41 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class PyRangeIteratorObject method next.

public final PyObject next() {
    if (hasNext()) {
        int number = this.start + this.step * this.i;
        PyObject result = this.runtime.number(number);
        this.i++;
        return result;
    } else {
        throw this.runtime.newRaiseException("builtins.StopIteration");
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 42 with PyObject

use of org.cafebabepy.runtime.PyObject 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);
    }
}
Also used : CafeBabePyException(org.cafebabepy.runtime.CafeBabePyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PyObject(org.cafebabepy.runtime.PyObject) AbstractPyObjectObject(org.cafebabepy.runtime.object.AbstractPyObjectObject) PyObject(org.cafebabepy.runtime.PyObject)

Example 43 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class PyObjectType method __getattribute__.

@DefinePyFunction(name = __getattribute__)
public PyObject __getattribute__(PyObject self, PyObject name) {
    String n = name.toJava(String.class);
    Optional<PyObject> resultOpt = self.getScope().get(n);
    if (resultOpt.isPresent()) {
        return resultOpt.get();
    }
    boolean isParent = false;
    resultOpt = getFromTypes(self, n);
    if (!resultOpt.isPresent()) {
        resultOpt = getFromType(self, n);
        if (!resultOpt.isPresent()) {
            resultOpt = getFromParent(self, n);
            if (!resultOpt.isPresent()) {
                if (self.isModule()) {
                    throw this.runtime.newRaiseException("builtins.AttributeError", "module '" + self.getName() + "' has no attribute '" + name + "'");
                } else if (self.isType()) {
                    throw this.runtime.newRaiseException("builtins.AttributeError", "type object '" + self.getName() + "' has no attribute '" + name + "'");
                } else {
                    throw this.runtime.newRaiseException("builtins.AttributeError", "'" + self.getName() + "' object has no attribute '" + name + "'");
                }
            } else {
                // Module
                isParent = true;
            }
        }
    }
    PyObject result = resultOpt.get();
    if (!result.isCallable() || isParent) {
        return result;
    } else {
        synchronized (this) {
            // if (this.methodMap == null) {
            // this.methodMap = new LinkedHashMap<>();
            // }
            // PyObject method = this.methodMap.get(name);
            PyObject method = null;
            if (method == null) {
                // method = newPyObject("types.MethodType", result, object);
                method = new PyMethodTypeObject(this.runtime, self, result);
            // this.methodMap.put(name, method);
            }
            return method;
        }
    }
}
Also used : PyMethodTypeObject(org.cafebabepy.runtime.object.proxy.PyMethodTypeObject) PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 44 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class PyObjectType method getFromParent.

public Optional<PyObject> getFromParent(PyObject object, String name) {
    Optional<PyObjectScope> parentOpt = object.getScope().getParent();
    while (parentOpt.isPresent()) {
        PyObjectScope parent = parentOpt.get();
        Optional<PyObject> result = parent.get(name);
        if (result.isPresent()) {
            return result;
        }
        parentOpt = parent.getParent();
    }
    return Optional.empty();
}
Also used : PyObjectScope(org.cafebabepy.runtime.PyObjectScope) PyObject(org.cafebabepy.runtime.PyObject)

Example 45 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class PySuperType method __getattribute__.

@DefinePyFunction(name = __getattribute__)
public PyObject __getattribute__(PyObject self, PyObject name) {
    PyObject strType = this.runtime.typeOrThrow("builtins.str");
    if (!this.runtime.isInstance(name, strType)) {
        throw this.runtime.newRaiseTypeError("attribute name must be string, not '" + name.getFullName() + "'");
    }
    PyObject object = self.getScope().get("_proxy_object", false).orElseThrow(() -> getRuntime().newRaiseException("builtins.AttributeError", "'" + self.getFullName() + "' object has no attribute '_proxy_object'"));
    PyObject selfProxy;
    List<PyObject> types = object.getType().getTypes();
    if (types.size() == 1) {
        selfProxy = types.get(0);
    } else {
        selfProxy = types.get(1);
    }
    return this.runtime.getattr(selfProxy, name.toJava(String.class));
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Aggregations

PyObject (org.cafebabepy.runtime.PyObject)69 PythonParser (org.cafebabepy.parser.antlr.PythonParser)20 ArrayList (java.util.ArrayList)10 DefinePyFunction (org.cafebabepy.runtime.module.DefinePyFunction)9 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 CafeBabePyException (org.cafebabepy.runtime.CafeBabePyException)3 PyIntObject (org.cafebabepy.runtime.object.java.PyIntObject)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 AbstractPyObject (org.cafebabepy.runtime.AbstractPyObject)2 PyObjectScope (org.cafebabepy.runtime.PyObjectScope)2 Python (org.cafebabepy.runtime.Python)2 AbstractPyObjectObject (org.cafebabepy.runtime.object.AbstractPyObjectObject)2 PyLexicalScopeProxyObject (org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 Terminal (jline.Terminal)1 ConsoleReader (jline.console.ConsoleReader)1 InterpretEvaluator (org.cafebabepy.evaluter.Interpret.InterpretEvaluator)1 InteractiveParser (org.cafebabepy.parser.InteractiveParser)1