use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalGenerators.
private void evalGenerators(PyObject context, PyObject elt, List<PyObject> generators, List<PyObject> resultList) {
PyObject generator = generators.get(0);
PyObject target = this.runtime.getattr(generator, "target");
PyObject iter = this.runtime.getattr(generator, "iter");
PyObject ifs = this.runtime.getattr(generator, "ifs");
PyObject is_async = this.runtime.getattr(generator, "is_async");
PyObject evalIter = eval(context, iter);
List<PyObject> ifList;
if (ifs.isNone()) {
ifList = Collections.emptyList();
} else {
ifList = this.runtime.toList(ifs);
}
this.runtime.iter(evalIter, next -> {
assign(context, target, next);
for (int i = 0; i < ifList.size(); i++) {
PyObject result = eval(context, ifList.get(i));
if (result.isFalse()) {
return;
}
}
if (generators.size() == 1) {
resultList.add(eval(context, elt));
} else {
List<PyObject> gs = generators.subList(1, generators.size());
evalGenerators(context, elt, gs, resultList);
}
});
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalCompare.
private PyObject evalCompare(PyObject context, PyObject node) {
// 1 < 2 < 3 < 4 => 1 < 2 && 2 < 3 && 3 < 4
// 1 < 2 < 3 => 1 < 2 && 2 < 3
// 1 < 2 => 1 < 2
PyObject comparators = this.runtime.getattr(node, "comparators");
List<PyObject> comparatorList = getLinkedList(comparators);
PyObject left = this.runtime.getattr(node, "left");
comparatorList.add(0, left);
PyObject ops = this.runtime.getattr(node, "ops");
List<PyObject> opList = getLinkedList(ops);
PyObject eqType = this.runtime.typeOrThrow("_ast.Eq");
PyObject notEqType = this.runtime.typeOrThrow("_ast.NotEq");
PyObject ltType = this.runtime.typeOrThrow("_ast.Lt");
PyObject lteType = this.runtime.typeOrThrow("_ast.LtE");
PyObject gtType = this.runtime.typeOrThrow("_ast.Gt");
PyObject gteType = this.runtime.typeOrThrow("_ast.GtE");
boolean evalResult = true;
for (int i = 0; i < comparatorList.size() - 1; i++) {
PyObject evalLeft = eval(context, comparatorList.get(i));
PyObject evalRight = eval(context, comparatorList.get(i + 1));
PyObject op = opList.get(i);
if (this.runtime.isInstance(op, eqType)) {
evalResult &= this.runtime.eq(evalLeft, evalRight).isTrue();
} else if (this.runtime.isInstance(op, notEqType)) {
evalResult &= this.runtime.ne(evalLeft, evalRight).isTrue();
} else if (this.runtime.isInstance(op, ltType)) {
evalResult &= this.runtime.lt(evalLeft, evalRight).isTrue();
} else if (this.runtime.isInstance(op, lteType)) {
evalResult &= this.runtime.le(evalLeft, evalRight).isTrue();
} else if (this.runtime.isInstance(op, gtType)) {
evalResult &= this.runtime.gt(evalLeft, evalRight).isTrue();
} else if (this.runtime.isInstance(op, gteType)) {
evalResult &= this.runtime.ge(evalLeft, evalRight).isTrue();
} else {
throw this.runtime.newRaiseTypeError("Unknown AST '" + op.getType().getFullName() + "'");
}
if (!evalResult) {
break;
}
}
return this.runtime.bool(evalResult);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalAssign.
private PyObject evalAssign(PyObject context, PyObject node) {
PyObject targets = this.runtime.getattr(node, "targets");
PyObject value = this.runtime.getattr(node, "value");
PyObject evalValue = eval(context, value);
this.runtime.iter(targets, target -> assign(context, target, evalValue));
return this.runtime.None();
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalIfAndIfExp.
private PyObject evalIfAndIfExp(PyObject context, PyObject node) {
PyObject test = this.runtime.getattr(node, "test");
PyObject body = this.runtime.getattr(node, "body");
PyObject orElse = this.runtime.getattr(node, "orelse");
PyObject evalTest = eval(context, test);
if (evalTest.isTrue()) {
return eval(context, body);
} else {
return eval(context, orElse);
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalList.
private PyObject evalList(PyObject context, PyObject node) {
PyObject elts = this.runtime.getattr(node, "elts");
List<PyObject> elements = new ArrayList<>();
this.runtime.iter(elts, elt -> {
PyObject evalElt = eval(context, elt);
PyObject type = elt.getType();
if (type instanceof PyStarredType) {
this.runtime.iter(evalElt, elements::add);
} else {
elements.add(evalElt);
}
});
return this.runtime.list(elements);
}
Aggregations