use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitFile_input.
@Override
public PyObject visitFile_input(PythonParser.File_inputContext ctx) {
List<PyObject> bodyList = new ArrayList<>();
for (PythonParser.StmtContext stmtContext : ctx.stmt()) {
PyObject body = visitStmt(stmtContext);
if (this.runtime.isInstance(body, "builtins.list")) {
this.runtime.iter(body, bodyList::add);
} else {
bodyList.add(body);
}
}
PyObject body = this.runtime.list(bodyList);
PyObject module = this.runtime.newPyObject("_ast.Module", body);
return module;
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitClassdef.
@Override
public PyObject visitClassdef(PythonParser.ClassdefContext ctx) {
PyObject name = this.runtime.str(ctx.NAME().getText());
PythonParser.ArglistContext arglistContext = ctx.arglist();
PyObject bases;
if (arglistContext != null) {
bases = visitArglist(arglistContext);
} else {
bases = this.runtime.list();
}
// TODO 何これ?
PyObject keywords = this.runtime.list();
PythonParser.SuiteContext suiteContext = ctx.suite();
PyObject body = visitSuite(suiteContext);
// TODO 何これ?
PyObject decorator_list = this.runtime.None();
return this.runtime.newPyObject("_ast.ClassDef", name, bases, keywords, body, decorator_list);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitTestlist_comp.
@Override
public PyObject visitTestlist_comp(PythonParser.Testlist_compContext ctx) {
List<PyObject> list = new ArrayList<>();
PythonParser.Comp_forContext comp_forContext = ctx.comp_for();
if (comp_forContext == null) {
int count = ctx.getChildCount();
for (int i = 0; i < count; i++) {
ParseTree c = ctx.getChild(i);
PyObject element = c.accept(this);
if (element != null) {
list.add(element);
}
}
} else {
list.add(ctx.getChild(0).accept(this));
PyObject comp_for = visitComp_for(comp_forContext);
this.runtime.iter(comp_for, list::add);
}
return this.runtime.list(list);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitSingle_input.
@Override
public PyObject visitSingle_input(PythonParser.Single_inputContext ctx) {
PyObject body;
PythonParser.Simple_stmtContext simple_stmtContext = ctx.simple_stmt();
PythonParser.Compound_stmtContext compound_stmtContext = ctx.compound_stmt();
if (simple_stmtContext != null) {
body = this.runtime.list(visitSimple_stmt(simple_stmtContext));
} else if (compound_stmtContext != null) {
body = this.runtime.list(visitCompound_stmt(compound_stmtContext));
} else {
body = this.runtime.list();
}
return this.runtime.newPyObject("_ast.Interactive", body);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitFactor.
@Override
public PyObject visitFactor(PythonParser.FactorContext ctx) {
PythonParser.PowerContext powerContext = ctx.power();
if (powerContext != null) {
return visitPower(powerContext);
} else {
PythonParser.FactorContext factorContext = ctx.factor();
PyObject factor = visitFactor(factorContext);
PyObject op;
switch(ctx.getChild(0).getText()) {
case "-":
op = this.runtime.newPyObject("_ast.USub");
break;
case "+":
op = this.runtime.newPyObject("_ast.UAdd");
break;
case "~":
op = this.runtime.newPyObject("_ast.Invert");
break;
default:
throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid factor");
}
return this.runtime.newPyObject("_ast.UnaryOp", op, factor);
}
}
Aggregations