Search in sources :

Example 36 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class InterpretEvaluator method evalClassDef.

private PyObject evalClassDef(PyObject context, PyObject node) {
    PyObject name = this.runtime.getattr(node, "name");
    PyObject bases = this.runtime.getattr(node, "bases");
    PyObject keywords = this.runtime.getattr(node, "keywords");
    PyObject body = this.runtime.getattr(node, "body");
    PyObject decorator_list = this.runtime.getattr(node, "decorator_list");
    List<PyObject> baseList = new ArrayList<>();
    Set<String> duplicateCheckSet = new LinkedHashSet<>();
    this.runtime.iter(bases, base -> {
        boolean exists = !duplicateCheckSet.add(base.toJava(String.class));
        if (exists) {
            throw this.runtime.newRaiseTypeError("duplicate base class " + base.getName());
        }
        baseList.add(base);
    });
    String n = name.toJava(String.class);
    if (!context.isModule()) {
        n = context.getName() + "." + n;
    }
    PyObject clazz = new PyInterpretClassObject(this.runtime, context, n, baseList);
    context.getScope().put(name.toJava(String.class), clazz);
    eval(clazz, body);
    return this.runtime.None();
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 37 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class InterpretEvaluator method evalBinOp.

private PyObject evalBinOp(PyObject context, PyObject node) {
    PyObject left = this.runtime.getattr(node, "left");
    PyObject evalLeft = eval(context, left);
    PyObject right = this.runtime.getattr(node, "right");
    PyObject evalRight = eval(context, right);
    PyObject addType = this.runtime.typeOrThrow("_ast.Add");
    PyObject subType = this.runtime.typeOrThrow("_ast.Sub");
    PyObject modType = this.runtime.typeOrThrow("_ast.Mod");
    PyObject multType = this.runtime.typeOrThrow("_ast.Mult");
    PyObject op = this.runtime.getattr(node, "op");
    if (this.runtime.isInstance(op, addType)) {
        return this.runtime.add(evalLeft, evalRight);
    } else if (this.runtime.isInstance(op, subType)) {
        return this.runtime.sub(evalLeft, evalRight);
    } else if (this.runtime.isInstance(op, modType)) {
        return this.runtime.mod(evalLeft, evalRight);
    } else if (this.runtime.isInstance(op, multType)) {
        return this.runtime.mul(evalLeft, evalRight);
    }
    throw new CafeBabePyException("operator '" + op.getName() + "' not found");
}
Also used : CafeBabePyException(org.cafebabepy.runtime.CafeBabePyException) PyObject(org.cafebabepy.runtime.PyObject)

Example 38 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class InterpretEvaluator method evalFor.

private PyObject evalFor(PyObject context, PyObject node) {
    PyObject target = this.runtime.getattr(node, "target");
    PyObject iter = this.runtime.getattr(node, "iter");
    PyObject body = this.runtime.getattr(node, "body");
    PyObject orelse = this.runtime.getattr(node, "orelse");
    PyObject evalIter = eval(context, iter);
    this.runtime.iter(evalIter, next -> {
        assign(context, target, next);
        eval(context, body);
    });
    eval(context, orelse);
    return this.runtime.None();
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 39 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class InterpretEvaluator method evalCall.

private PyObject evalCall(PyObject context, PyObject node) {
    PyObject func = this.runtime.getattr(node, "func");
    PyObject funcEval = eval(context, func);
    PyObject args = this.runtime.getattr(node, "args");
    PyObject[] argArray;
    if (args.isNone()) {
        argArray = new PyObject[0];
    } else {
        // FIXME array direct
        List<PyObject> argList = new ArrayList<>();
        this.runtime.iter(args, arg -> {
            PyObject evalArg = eval(context, arg);
            argList.add(evalArg);
        });
        argArray = new PyObject[argList.size()];
        argList.toArray(argArray);
    }
    PyObject result;
    try {
        result = funcEval.call(argArray);
    } catch (InterpretReturn re) {
        result = re.getValue();
    }
    return result;
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 40 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class AbstractAbstractCafeBabePyAny method getBases.

@Override
public List<PyObject> getBases() {
    if (this.bases == null) {
        synchronized (this) {
            if (this.bases == null) {
                String[] baseNames = getBaseNames();
                List<PyObject> bases = new ArrayList<>(baseNames.length);
                for (String baseName : getBaseNames()) {
                    PyObject base = this.runtime.type(baseName, false).orElseThrow(() -> new CafeBabePyException("type '" + getName() + "' parent '" + baseName + "' is not found"));
                    bases.add(base);
                }
                this.bases = Collections.unmodifiableList(Collections.synchronizedList(bases));
            }
        }
    }
    return this.bases;
}
Also used : CafeBabePyException(org.cafebabepy.runtime.CafeBabePyException) PyObject(org.cafebabepy.runtime.PyObject) AbstractPyObject(org.cafebabepy.runtime.AbstractPyObject)

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