use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalListComp.
private PyObject evalListComp(PyObject context, PyObject node) {
PyObject elt = this.runtime.getattr(node, "elt");
PyObject generators = this.runtime.getattr(node, "generators");
List<PyObject> generatorList = this.runtime.toList(generators);
List<PyObject> resultList = new ArrayList<>();
PyLexicalScopeProxyObject lexicalContext = new PyLexicalScopeProxyObject(context);
evalGenerators(lexicalContext, elt, generatorList, resultList);
return this.runtime.list(resultList);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method assign.
private void assign(PyObject context, PyObject target, PyObject evalValue) {
if (target instanceof PyNameType) {
PyObject id = this.runtime.getattr(target, "id");
context.getScope().put(id.toJava(String.class), evalValue);
} else {
LinkedHashMap<String, PyObject> assignMap = new LinkedHashMap<>();
unpack(context, target, evalValue, assignMap);
for (Map.Entry<String, PyObject> e : assignMap.entrySet()) {
context.getScope().put(e.getKey(), e.getValue());
}
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalUnaryOp.
private PyObject evalUnaryOp(PyObject context, PyObject node) {
PyObject op = this.runtime.getattr(node, "op");
PyObject operand = this.runtime.getattr(node, "operand");
PyObject evalOperand = eval(context, operand);
PyObject uAddType = this.runtime.typeOrThrow("_ast.UAdd");
PyObject uSubType = this.runtime.typeOrThrow("_ast.USub");
PyObject invertType = this.runtime.typeOrThrow("_ast.Invert");
PyObject notType = this.runtime.typeOrThrow("_ast.Not");
if (this.runtime.isInstance(op, uAddType)) {
PyObject pos = this.runtime.getattr(evalOperand, __pos__);
return pos.call(evalOperand);
} else if (this.runtime.isInstance(op, uSubType)) {
PyObject pos = this.runtime.getattr(evalOperand, __neg__);
return pos.call(evalOperand);
} else if (this.runtime.isInstance(op, invertType)) {
PyObject pos = this.runtime.getattr(evalOperand, __invert__);
return pos.call(evalOperand);
} else if (this.runtime.isInstance(op, notType)) {
PyObject bool = this.runtime.getattr(evalOperand, __bool__);
PyObject result = bool.call(evalOperand);
if (result.isTrue()) {
return this.runtime.False();
} else {
return this.runtime.True();
}
} else {
throw this.runtime.newRaiseTypeError("Unknown op");
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalName.
private PyObject evalName(PyObject context, PyObject node) {
PyObject ctx = this.runtime.getattr(node, "ctx");
PyObject ctxType = ctx.getType();
if (ctxType instanceof PyLoadType) {
PyObject id = this.runtime.getattr(node, "id");
String name = id.toJava(String.class);
return this.runtime.getattrOptional(context, name).orElseThrow(() -> this.runtime.newRaiseException("builtins.NameError", "name '" + name + "' is not defined"));
} else if (ctxType instanceof PyStoreType) {
return context;
}
// TODO どうする?
return node;
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalAttribute.
private PyObject evalAttribute(PyObject context, PyObject node) {
PyObject value = this.runtime.getattr(node, "value");
PyObject attr = this.runtime.getattr(node, "attr");
PyObject ctx = this.runtime.getattr(node, "ctx");
PyObject evalValue = eval(context, value);
PyObject ctxType = ctx.getType();
if (ctxType instanceof PyLoadType) {
return this.runtime.getattr(evalValue, attr.toJava(String.class));
} else if (ctxType instanceof PyStoreType) {
return evalValue;
}
// TODO どうする?
return node;
}
Aggregations