Search in sources :

Example 31 with PyObject

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

Example 32 with PyObject

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

Example 33 with PyObject

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

Example 34 with PyObject

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

Example 35 with PyObject

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);
}
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