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