Search in sources :

Example 26 with PyObject

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

Example 27 with PyObject

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

Example 28 with PyObject

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

Example 29 with PyObject

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

Example 30 with PyObject

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