Search in sources :

Example 1 with PyObject

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

the class PyObjectType method __setattr__.

@DefinePyFunction(name = __setattr__)
public void __setattr__(PyObject self, PyObject name, PyObject value) {
    PyObject strType = this.runtime.typeOrThrow("builtins.str");
    if (!this.runtime.isInstance(self, strType)) {
        throw this.runtime.newRaiseTypeError("attribute name must be string, not '" + name.getFullName() + "'");
    }
    self.getScope().put(name.toJava(String.class), value);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 2 with PyObject

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

the class PyRangeType method __init__.

@DefinePyFunction(name = __init__)
public void __init__(PyObject self, PyObject... args) {
    if (args.length == 0) {
        throw this.runtime.newRaiseTypeError("range expected 1 arguments, got 0");
    } else if (3 < args.length) {
        throw this.runtime.newRaiseTypeError("range expected at most 3 arguments, got " + args.length);
    }
    PyObject start;
    PyObject stop;
    PyObject step;
    if (args.length == 1) {
        start = this.runtime.number(0);
        stop = getInt(args[0]);
        step = this.runtime.number(1);
    } else if (args.length == 2) {
        start = getInt(args[0]);
        stop = getInt(args[1]);
        step = this.runtime.number(1);
    } else {
        start = getInt(args[0]);
        stop = getInt(args[1]);
        step = getInt(args[2]);
    }
    self.getScope().put("start", start);
    self.getScope().put("stop", stop);
    self.getScope().put("step", step);
    int stepInt = ((PyIntObject) step).getIntValue();
    if (stepInt <= 0) {
        throw this.runtime.newRaiseException("builtins.ValueError", "range() arg 3 must not be zero");
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 3 with PyObject

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

the class PyRangeType method getInt.

private PyObject getInt(PyObject object) {
    if (object instanceof PyIntObject) {
        return object;
    }
    PyObject indexMethod = this.runtime.getattrOptional(object, __index__).orElseThrow(() -> this.runtime.newRaiseTypeError("'" + object.getFullName() + "' object cannot be interpreted as an integer"));
    PyObject intObject = indexMethod.call(object);
    if (intObject instanceof PyIntObject) {
        return intObject;
    }
    throw this.runtime.newRaiseTypeError("__index__ returned non-int (type " + intObject.getFullName() + ")");
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject)

Example 4 with PyObject

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

the class PyRangeType method __iter__.

@DefinePyFunction(name = __iter__)
public PyObject __iter__(PyObject self) {
    if (!this.runtime.isInstance(self, this)) {
        throw this.runtime.newRaiseTypeError("descriptor '__iter__' requires a 'range' object but received a '" + self.getType().getFullName() + "'");
    }
    PyObject start = this.runtime.getattr(self, "start");
    PyObject stop = this.runtime.getattr(self, "stop");
    PyObject step = this.runtime.getattr(self, "step");
    int startInt = ((PyIntObject) start).getIntValue();
    int stopInt = ((PyIntObject) stop).getIntValue();
    int stepInt = ((PyIntObject) step).getIntValue();
    return new PyRangeIteratorObject(this.runtime, startInt, stopInt, stepInt);
}
Also used : PyRangeIteratorObject(org.cafebabepy.runtime.object.iterator.PyRangeIteratorObject) PyObject(org.cafebabepy.runtime.PyObject) PyIntObject(org.cafebabepy.runtime.object.java.PyIntObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 5 with PyObject

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

the class PyTypeType method __call__.

@DefinePyFunction(name = __call__)
public PyObject __call__(PyObject self, PyObject... args) {
    PyObject object = this.runtime.getattr(self, __new__).call();
    this.runtime.getattr(object, __init__).call(args);
    return object;
}
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