Search in sources :

Example 11 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitAtom_expr.

@Override
public PyObject visitAtom_expr(PythonParser.Atom_exprContext ctx) {
    PythonParser.AtomContext atomContext = ctx.atom();
    PyObject atom = visitAtom(atomContext);
    List<PythonParser.TrailerContext> trailerContextList = ctx.trailer();
    int count = trailerContextList.size();
    if (count == 0) {
        return atom;
    }
    PyObject expr = null;
    for (int i = 0; i < count; i++) {
        PythonParser.TrailerContext trailerContext = trailerContextList.get(i);
        PyObject trailer = visitTrailer(trailerContext);
        if (this.runtime.isInstance(trailer, "_ast.Call")) {
            if (expr == null) {
                trailer.getScope().put("func", atom);
            } else {
                trailer.getScope().put("func", expr);
            }
        } else if (this.runtime.isInstance(trailer, "_ast.Attribute")) {
            if (expr == null) {
                trailer.getScope().put("value", atom);
            } else {
                trailer.getScope().put("value", expr);
            }
        }
        expr = trailer;
    }
    return expr;
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 12 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTfpdef.

@Override
public PyObject visitTfpdef(PythonParser.TfpdefContext ctx) {
    PyObject arg = this.runtime.str(ctx.NAME().getText());
    PyObject annotation = this.runtime.None();
    PythonParser.TestContext testContext = ctx.test();
    if (testContext != null) {
        annotation = visitTest(testContext);
    }
    return this.runtime.newPyObject("_ast.arg", arg, annotation);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 13 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitComp_if.

@Override
public PyObject visitComp_if(PythonParser.Comp_ifContext ctx) {
    PyObject test_nocond = visitTest_nocond(ctx.test_nocond());
    PythonParser.Comp_iterContext comp_iterContext = ctx.comp_iter();
    if (comp_iterContext != null) {
        List<PyObject> comp_iterList = new ArrayList<>();
        PyObject comp_iter = visitComp_iter(comp_iterContext);
        comp_iterList.add(test_nocond);
        this.runtime.iter(comp_iter, comp_iterList::add);
        return this.runtime.list(comp_iterList);
    } else {
        return this.runtime.list(test_nocond);
    }
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 14 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitComp_for.

@Override
public PyObject visitComp_for(PythonParser.Comp_forContext ctx) {
    PyObject target = visitExprlist(ctx.exprlist());
    PyObject iter = visitOr_test(ctx.or_test());
    PyObject ifs = this.runtime.None();
    PyObject is_async = this.runtime.None();
    List<PyObject> comp_iterList = new ArrayList<>();
    PyObject comprehension = this.runtime.newPyObject("_ast.comprehension", target, iter, ifs, is_async);
    PythonParser.Comp_iterContext comp_iterContext = ctx.comp_iter();
    if (comp_iterContext != null) {
        List<PyObject> comprehensionList = new ArrayList<>();
        List<PyObject> ifsList = new ArrayList<>();
        PyObject comp_iter = visitComp_iter(comp_iterContext);
        this.runtime.iter(comp_iter, comp_iterList::add);
        comprehensionList.add(comprehension);
        PyObject type = this.runtime.typeOrThrow("_ast.comprehension");
        for (int i = 0; i < comp_iterList.size(); i++) {
            PyObject e = comp_iterList.get(i);
            if (!this.runtime.isInstance(e, type)) {
                ifsList.add(e);
            } else {
                comprehensionList.add(e);
            }
        }
        ifs = this.runtime.list(ifsList);
        comprehension.getScope().put("ifs", ifs);
        return this.runtime.list(comprehensionList);
    } else {
        return this.runtime.list(comprehension);
    }
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 15 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitSuite.

@Override
public PyObject visitSuite(PythonParser.SuiteContext ctx) {
    PythonParser.Simple_stmtContext simple_stmtContext = ctx.simple_stmt();
    if (simple_stmtContext != null) {
        PyObject body = visitSimple_stmt(simple_stmtContext);
        return this.runtime.newPyObject("_ast.Suite", body);
    } else {
        List<PyObject> stmtList = new ArrayList<>();
        for (PythonParser.StmtContext stmtContext : ctx.stmt()) {
            PyObject stmt = visitStmt(stmtContext);
            stmtList.add(stmt);
        }
        return this.runtime.list(stmtList);
    }
}
Also used : ArrayList(java.util.ArrayList) 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