Search in sources :

Example 46 with PyObject

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

the class PyNodeVisitorType method generic_visit.

@DefinePyFunction(name = "generic_visit")
public void generic_visit(PyObject self, PyObject node) {
    PyObject visit = this.runtime.getattr(self, "visit");
    PyObject astModule = this.runtime.moduleOrThrow("ast");
    PyObject iter_fields = this.runtime.getattr(astModule, "iter_fields");
    PyObject list = this.runtime.typeOrThrow("builtins.list");
    PyObject ast = this.runtime.typeOrThrow("_ast.AST");
    PyObject iter_fieldsResult = iter_fields.call(node);
    this.runtime.iter(iter_fieldsResult, fvs -> {
        PyObject[] fieldAndValue = new PyObject[2];
        this.runtime.iterIndex(fvs, (fv, i) -> fieldAndValue[i] = fv);
        PyObject value = fieldAndValue[1];
        if (this.runtime.isInstance(value, list)) {
            this.runtime.iter(value, item -> {
                if (this.runtime.isInstance(item, ast)) {
                    visit.call(self, item);
                }
            });
        } else if (this.runtime.isInstance(value, ast)) {
            visit.call(self, value);
        }
    });
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 47 with PyObject

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

the class PyBuiltinsModule method issubclass.

@DefinePyFunction(name = "issubclass")
public PyObject issubclass(PyObject clazz, PyObject classInfo) {
    if (!clazz.isType() || (!classInfo.isType() && !(classInfo instanceof PyTupleType))) {
        throw this.runtime.newRaiseTypeError("issubclass() arg 2 must be a type or tuple of types");
    }
    List<PyObject> types = new ArrayList<>();
    types.addAll(clazz.getTypes());
    for (PyObject type : types) {
        if (type == classInfo) {
            return this.runtime.True();
        }
    }
    return this.runtime.False();
}
Also used : ArrayList(java.util.ArrayList) PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 48 with PyObject

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

the class PyInterpretClassObject method call.

@Override
public PyObject call(PyObject... args) {
    PyObject object = this.runtime.getattr(this, __new__).call();
    this.runtime.getattr(object, __init__).call(args);
    return object;
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) AbstractPyObject(org.cafebabepy.runtime.AbstractPyObject)

Example 49 with PyObject

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

the class InteractiveConsole method interact.

public void interact() {
    InteractiveParser parser = new InteractiveParser(this.runtime);
    InterpretEvaluator evaluator = new InterpretEvaluator(this.runtime);
    Terminal terminal = TerminalFactory.get();
    terminal.setEchoEnabled(false);
    try {
        terminal.init();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    try (ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, terminal)) {
        this.consoleReader = consoleReader;
        printBanner();
        StringBuilder buffer = new StringBuilder();
        while (status != Status.QUIT) {
            consoleReader.flush();
            String line = readLine();
            if (line == null) {
                break;
            }
            if (buffer.length() > 0) {
                buffer.append(System.lineSeparator());
            }
            buffer.append(line);
            PyObject ast;
            try {
                ast = parser.parse(buffer.toString());
                if (ast.isNone()) {
                    this.status = Status.INCOMPLETE;
                    continue;
                }
            } catch (RaiseException e) {
                e.printStackTrace();
                this.consoleReader.println(e.getException().toJava(String.class));
                this.status = Status.READLINE;
                buffer.setLength(0);
                continue;
            }
            this.status = Status.READLINE;
            buffer.setLength(0);
            PyObject result;
            try {
                result = evaluator.evalMainModule(ast);
                if (!result.isNone()) {
                    if (result instanceof PyStrObject) {
                        this.consoleReader.println(result.toString());
                    } else {
                        this.consoleReader.println(result.toJava(String.class));
                    }
                }
            } catch (RaiseException e) {
                PyObject exception = e.getException();
                this.consoleReader.println(exception.toJava(String.class));
                this.consoleReader.println(e.getMessage());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) InterpretEvaluator(org.cafebabepy.evaluter.Interpret.InterpretEvaluator) PyStrObject(org.cafebabepy.runtime.object.java.PyStrObject) RaiseException(org.cafebabepy.runtime.RaiseException) InteractiveParser(org.cafebabepy.parser.InteractiveParser) Terminal(jline.Terminal) PyObject(org.cafebabepy.runtime.PyObject) IOException(java.io.IOException) RaiseException(org.cafebabepy.runtime.RaiseException)

Example 50 with PyObject

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

the class CafeBabePyAstCreateVisitor method createAssign.

private PyObject createAssign(List<PythonParser.Testlist_star_exprContext> testlist_star_exprContextList) {
    int count = testlist_star_exprContextList.size() - 1;
    PyObject[] targetArray = new PyObject[count];
    for (int i = 0; i < count; i++) {
        PythonParser.Testlist_star_exprContext testlist_star_exprContext = testlist_star_exprContextList.get(i);
        PyObject target = visitTestlist_star_expr(testlist_star_exprContext);
        toStore(target, 0);
        targetArray[i] = target;
    }
    PyObject targets = this.runtime.list(targetArray);
    PyObject value = visitTestlist_star_expr(testlist_star_exprContextList.get(testlist_star_exprContextList.size() - 1));
    return this.runtime.newPyObject("_ast.Assign", targets, value);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) 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