use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalReturn.
private PyObject evalReturn(PyObject context, PyObject node) {
PyObject value = this.runtime.getattr(node, "value");
PyObject evalValue = eval(context, value);
throw new InterpretReturn(evalValue);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method unpack.
private void unpack(PyObject context, PyObject target, PyObject evalValue, Map<String, PyObject> assignMap) {
PyObject targetType = target.getType();
PyObject targets;
if (targetType instanceof PyNameType) {
PyObject id = this.runtime.getattr(target, "id");
assignMap.put(id.toJava(String.class), evalValue);
return;
} else if (targetType instanceof PyAttributeType) {
PyObject attr = this.runtime.getattr(target, "attr");
PyObject attributeContext = eval(context, target);
attributeContext.getScope().put(attr.toJava(String.class), evalValue);
return;
} else if (targetType instanceof PyStarredType) {
PyObject value = this.runtime.getattr(target, "value");
unpack(context, value, evalValue, assignMap);
return;
} else if (targetType instanceof PyListType) {
targets = this.runtime.getattr(target, "elts");
} else {
throw this.runtime.newRaiseTypeError("Invalid '" + targetType.getFullName() + "' type");
}
List<PyObject> targetPyList = this.runtime.toList(targets);
List<PyObject> evalValuePyList = this.runtime.toList(evalValue);
int notStarredFirstCount = 0;
int notStarredLastCount = 0;
boolean throughStar;
throughStar = false;
for (int i = 0; i < targetPyList.size(); i++) {
PyObject childTargetType = targetPyList.get(i).getType();
if (childTargetType instanceof PyStarredType) {
if (throughStar) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "two starred expressions in assignment");
}
throughStar = true;
} else {
if (throughStar) {
notStarredLastCount++;
} else {
notStarredFirstCount++;
}
}
}
int targetMinCount = notStarredFirstCount + notStarredLastCount;
if (targetMinCount > evalValuePyList.size()) {
throw this.runtime.newRaiseException("builtins.ValueError", "not enough values to unpack (expected " + targetMinCount + ", got " + evalValuePyList.size() + ")");
}
int valueIndex = 0;
PyObject starredTarget = null;
List<PyObject> starredValueList = new ArrayList<>();
for (int i = 0; i < targetPyList.size(); i++) {
PyObject t = targetPyList.get(i);
PyObject tt = t.getType();
if (tt instanceof PyStarredType) {
starredTarget = t;
int count = evalValuePyList.size() - notStarredLastCount;
while (valueIndex < count) {
starredValueList.add(evalValuePyList.get(valueIndex));
valueIndex++;
}
} else {
if (starredTarget != null) {
PyObject value = this.runtime.list(starredValueList);
unpack(context, starredTarget, value, assignMap);
starredTarget = null;
starredValueList = null;
}
unpack(context, t, evalValuePyList.get(valueIndex), assignMap);
valueIndex++;
}
}
if (starredTarget != null) {
PyObject value = this.runtime.list(starredValueList);
unpack(context, starredTarget, value, assignMap);
}
if (valueIndex < evalValuePyList.size()) {
throw this.runtime.newRaiseException("builtins.ValueError", "too many values to unpack (expected " + targetPyList.size() + ")");
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalSuite.
private PyObject evalSuite(PyObject context, PyObject node) {
PyObject body = this.runtime.getattr(node, "body");
PyObject[] result = new PyObject[1];
result[0] = this.runtime.None();
this.runtime.iter(body, b -> {
result[0] = eval(context, body);
});
return result[0];
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalDict.
private PyObject evalDict(PyObject context, PyObject node) {
PyObject keys = this.runtime.getattr(node, "keys");
PyObject values = this.runtime.getattr(node, "values");
List<PyObject> keyList = new ArrayList<>();
List<PyObject> valueList = new ArrayList<>();
this.runtime.iter(keys, keyList::add);
this.runtime.iter(values, valueList::add);
LinkedHashMap<PyObject, PyObject> map = new LinkedHashMap<>();
for (int i = 0; i < keyList.size(); i++) {
PyObject key = eval(context, keyList.get(i));
PyObject value = eval(context, valueList.get(i));
if (key.isNone()) {
Map<PyObject, PyObject> vs = value.toJava(Map.class);
// TODO Error処理
map.putAll(vs);
} else {
map.put(key, value);
}
}
return this.runtime.dict(map);
}
Aggregations