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