Search in sources :

Example 1 with PyObjectScope

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

the class PyInterpretFunctionObject method call.

@Override
public PyObject call(PyObject... args) {
    PyObject lexicalContext = new PyLexicalScopeProxyObject(this.context);
    PyObjectScope scope = lexicalContext.getScope();
    this.runtime.getattrOptional(this.args, "args").ifPresent(argslist -> this.runtime.iterIndex(argslist, (a, i) -> {
        PyObject arg = this.runtime.getattr(a, "arg");
        scope.put(arg.toJava(String.class), args[i]);
    }));
    return evaluator.eval(lexicalContext, body);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) Python(org.cafebabepy.runtime.Python) AbstractPyObjectObject(org.cafebabepy.runtime.object.AbstractPyObjectObject) PyLexicalScopeProxyObject(org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject) ProtocolNames.__call__(org.cafebabepy.util.ProtocolNames.__call__) PyObjectScope(org.cafebabepy.runtime.PyObjectScope) PyLexicalScopeProxyObject(org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject) PyObject(org.cafebabepy.runtime.PyObject) PyObjectScope(org.cafebabepy.runtime.PyObjectScope)

Example 2 with PyObjectScope

use of org.cafebabepy.runtime.PyObjectScope 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)

Aggregations

PyObject (org.cafebabepy.runtime.PyObject)2 PyObjectScope (org.cafebabepy.runtime.PyObjectScope)2 Python (org.cafebabepy.runtime.Python)1 AbstractPyObjectObject (org.cafebabepy.runtime.object.AbstractPyObjectObject)1 PyLexicalScopeProxyObject (org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject)1 ProtocolNames.__call__ (org.cafebabepy.util.ProtocolNames.__call__)1