use of org.cafebabepy.runtime.module.DefinePyFunction in project cafebabepy by cafebabepy.
the class PyNodeVisitorType method visit.
@DefinePyFunction(name = "visit")
public PyObject visit(PyObject self, PyObject node) {
String method = "visit_" + node.getName();
PyObject visitor = this.runtime.getattrOptional(self, method).orElse(this.runtime.getattr(this, "generic_visit"));
return visitor.call(self, node);
}
use of org.cafebabepy.runtime.module.DefinePyFunction 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;
}
}
}
use of org.cafebabepy.runtime.module.DefinePyFunction 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));
}
use of org.cafebabepy.runtime.module.DefinePyFunction in project cafebabepy by cafebabepy.
the class PyNodeVisitorType method generic_visit.
@DefinePyFunction(name = "generic_visit")
public void generic_visit(PyObject self, PyObject node) {
PyObject visit = this.runtime.getattr(self, "visit");
PyObject astModule = this.runtime.moduleOrThrow("ast");
PyObject iter_fields = this.runtime.getattr(astModule, "iter_fields");
PyObject list = this.runtime.typeOrThrow("builtins.list");
PyObject ast = this.runtime.typeOrThrow("_ast.AST");
PyObject iter_fieldsResult = iter_fields.call(node);
this.runtime.iter(iter_fieldsResult, fvs -> {
PyObject[] fieldAndValue = new PyObject[2];
this.runtime.iterIndex(fvs, (fv, i) -> fieldAndValue[i] = fv);
PyObject value = fieldAndValue[1];
if (this.runtime.isInstance(value, list)) {
this.runtime.iter(value, item -> {
if (this.runtime.isInstance(item, ast)) {
visit.call(self, item);
}
});
} else if (this.runtime.isInstance(value, ast)) {
visit.call(self, value);
}
});
}
use of org.cafebabepy.runtime.module.DefinePyFunction in project cafebabepy by cafebabepy.
the class PyBuiltinsModule method issubclass.
@DefinePyFunction(name = "issubclass")
public PyObject issubclass(PyObject clazz, PyObject classInfo) {
if (!clazz.isType() || (!classInfo.isType() && !(classInfo instanceof PyTupleType))) {
throw this.runtime.newRaiseTypeError("issubclass() arg 2 must be a type or tuple of types");
}
List<PyObject> types = new ArrayList<>();
types.addAll(clazz.getTypes());
for (PyObject type : types) {
if (type == classInfo) {
return this.runtime.True();
}
}
return this.runtime.False();
}
Aggregations