use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalClassDef.
private PyObject evalClassDef(PyObject context, PyObject node) {
PyObject name = this.runtime.getattr(node, "name");
PyObject bases = this.runtime.getattr(node, "bases");
PyObject keywords = this.runtime.getattr(node, "keywords");
PyObject body = this.runtime.getattr(node, "body");
PyObject decorator_list = this.runtime.getattr(node, "decorator_list");
List<PyObject> baseList = new ArrayList<>();
Set<String> duplicateCheckSet = new LinkedHashSet<>();
this.runtime.iter(bases, base -> {
boolean exists = !duplicateCheckSet.add(base.toJava(String.class));
if (exists) {
throw this.runtime.newRaiseTypeError("duplicate base class " + base.getName());
}
baseList.add(base);
});
String n = name.toJava(String.class);
if (!context.isModule()) {
n = context.getName() + "." + n;
}
PyObject clazz = new PyInterpretClassObject(this.runtime, context, n, baseList);
context.getScope().put(name.toJava(String.class), clazz);
eval(clazz, body);
return this.runtime.None();
}
use of org.cafebabepy.runtime.PyObject 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.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalFor.
private PyObject evalFor(PyObject context, PyObject node) {
PyObject target = this.runtime.getattr(node, "target");
PyObject iter = this.runtime.getattr(node, "iter");
PyObject body = this.runtime.getattr(node, "body");
PyObject orelse = this.runtime.getattr(node, "orelse");
PyObject evalIter = eval(context, iter);
this.runtime.iter(evalIter, next -> {
assign(context, target, next);
eval(context, body);
});
eval(context, orelse);
return this.runtime.None();
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalCall.
private PyObject evalCall(PyObject context, PyObject node) {
PyObject func = this.runtime.getattr(node, "func");
PyObject funcEval = eval(context, func);
PyObject args = this.runtime.getattr(node, "args");
PyObject[] argArray;
if (args.isNone()) {
argArray = new PyObject[0];
} else {
// FIXME array direct
List<PyObject> argList = new ArrayList<>();
this.runtime.iter(args, arg -> {
PyObject evalArg = eval(context, arg);
argList.add(evalArg);
});
argArray = new PyObject[argList.size()];
argList.toArray(argArray);
}
PyObject result;
try {
result = funcEval.call(argArray);
} catch (InterpretReturn re) {
result = re.getValue();
}
return result;
}
use of org.cafebabepy.runtime.PyObject 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;
}
Aggregations