Search in sources :

Example 66 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 67 with PyObject

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() + ")");
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 68 with PyObject

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];
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 69 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Aggregations

PyObject (org.cafebabepy.runtime.PyObject)69 PythonParser (org.cafebabepy.parser.antlr.PythonParser)20 ArrayList (java.util.ArrayList)10 DefinePyFunction (org.cafebabepy.runtime.module.DefinePyFunction)9 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 CafeBabePyException (org.cafebabepy.runtime.CafeBabePyException)3 PyIntObject (org.cafebabepy.runtime.object.java.PyIntObject)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 AbstractPyObject (org.cafebabepy.runtime.AbstractPyObject)2 PyObjectScope (org.cafebabepy.runtime.PyObjectScope)2 Python (org.cafebabepy.runtime.Python)2 AbstractPyObjectObject (org.cafebabepy.runtime.object.AbstractPyObjectObject)2 PyLexicalScopeProxyObject (org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 Terminal (jline.Terminal)1 ConsoleReader (jline.console.ConsoleReader)1 InterpretEvaluator (org.cafebabepy.evaluter.Interpret.InterpretEvaluator)1 InteractiveParser (org.cafebabepy.parser.InteractiveParser)1