Search in sources :

Example 56 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitComparison.

@Override
public PyObject visitComparison(PythonParser.ComparisonContext ctx) {
    int count = ctx.getChildCount();
    if (count == 1) {
        return visitExpr(ctx.expr(0));
    }
    List<PythonParser.ExprContext> exprContextList = ctx.expr();
    List<PyObject> comparatorList = new ArrayList<>(exprContextList.size());
    for (PythonParser.ExprContext exprContext : exprContextList) {
        PyObject comparator = visitExpr(exprContext);
        comparatorList.add(comparator);
    }
    List<PythonParser.Comp_opContext> comp_opContextList = ctx.comp_op();
    List<PyObject> opList = new ArrayList<>(comp_opContextList.size());
    for (PythonParser.Comp_opContext comp_opContext : comp_opContextList) {
        PyObject op = visitComp_op(comp_opContext);
        opList.add(op);
    }
    if (comparatorList.size() == 1) {
        return comparatorList.get(0);
    }
    PyObject left = comparatorList.get(0);
    comparatorList.remove(0);
    PyObject ops = this.runtime.list(opList);
    PyObject comparators = this.runtime.list(comparatorList);
    return this.runtime.newPyObject("_ast.Compare", left, ops, comparators);
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 57 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitSimple_stmt.

@Override
public PyObject visitSimple_stmt(PythonParser.Simple_stmtContext ctx) {
    List<PyObject> small_stmtList = new ArrayList<>();
    for (PythonParser.Small_stmtContext small_stmtContext : ctx.small_stmt()) {
        PyObject small_stmt = visitSmall_stmt(small_stmtContext);
        small_stmtList.add(small_stmt);
    }
    return this.runtime.list(small_stmtList);
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 58 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTest.

@Override
public PyObject visitTest(PythonParser.TestContext ctx) {
    List<PythonParser.Or_testContext> or_testContext = ctx.or_test();
    PyObject test = visitOr_test(or_testContext.get(0));
    if (or_testContext.size() == 2) {
        PyObject ifTest = visitOr_test(or_testContext.get(1));
        PyObject elseTest = visitTest(ctx.test());
        return this.runtime.newPyObject("_ast.IfExp", ifTest, test, elseTest);
    } else {
        return test;
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 59 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitExpr_stmt.

@Override
public PyObject visitExpr_stmt(PythonParser.Expr_stmtContext ctx) {
    List<PythonParser.Testlist_star_exprContext> testlist_star_exprContextList = ctx.testlist_star_expr();
    PythonParser.AnnassignContext annassignContext = ctx.annassign();
    if (annassignContext != null) {
        PyObject testlist_star_expr = visitTestlist_star_expr(testlist_star_exprContextList.get(0));
        return createAnnasign(testlist_star_expr, annassignContext);
    } else if (testlist_star_exprContextList.size() >= 2) {
        return createAssign(testlist_star_exprContextList);
    }
    PyObject children = visitChildren(ctx);
    PyObject expr_stmt = this.runtime.newPyObject("_ast.Expr", children);
    return expr_stmt;
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 60 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTuple.

private PyObject visitTuple(PythonParser.Testlist_compContext testlist_compContext) {
    if (testlist_compContext != null) {
        PyObject resultVisit = visitTestlist_comp(testlist_compContext);
        List<PyObject> resultVisitList = this.runtime.toList(resultVisit);
        if (resultVisitList.size() == 1) {
            return resultVisitList.get(0);
        } else {
            return visitAtomToTestlist_comp(resultVisit, "_ast.GeneratorExp", "_ast.Tuple");
        }
    } else {
        PyObject load = this.runtime.newPyObject("_ast.Load");
        return this.runtime.newPyObject("_ast.Tuple", this.runtime.list(), load);
    }
}
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