use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitDictOrSet.
private PyObject visitDictOrSet(PythonParser.DictorsetmakerContext dictorsetmakerContext) {
if (dictorsetmakerContext != null) {
if (dictorsetmakerContext.comp_for() == null) {
List<PyObject> keys = new ArrayList<>();
List<PyObject> values = new ArrayList<>();
boolean doubleStar = false;
PyObject test = null;
int count = dictorsetmakerContext.getChildCount();
for (int i = 0; i < count; i++) {
ParseTree c = dictorsetmakerContext.getChild(i);
if ("**".equals(c.getText())) {
doubleStar = true;
} else {
if (doubleStar) {
keys.add(this.runtime.None());
values.add(c.accept(this));
} else {
PyObject element = c.accept(this);
if (element != null) {
if (test == null) {
test = element;
} else {
keys.add(test);
values.add(element);
test = null;
}
}
}
doubleStar = false;
}
}
return this.runtime.newPyObject("_ast.Dict", this.runtime.list(keys), this.runtime.list(values));
}
}
return this.runtime.newPyObject("_ast.Dict", this.runtime.list(), this.runtime.list());
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitStar_expr.
@Override
public PyObject visitStar_expr(PythonParser.Star_exprContext ctx) {
PythonParser.ExprContext exprContext = ctx.expr();
PyObject value = visitExpr(exprContext);
PyObject context = this.runtime.newPyObject("_ast.Load");
return this.runtime.newPyObject("_ast.Starred", value, context);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitNumber.
@Override
public PyObject visitNumber(PythonParser.NumberContext ctx) {
String text = ctx.getChild(0).getText();
PyObject number = this.runtime.number(Integer.parseInt(text));
PyObject object = this.runtime.newPyObject("_ast.Num", number);
return object;
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitIf_stmt.
@Override
public PyObject visitIf_stmt(PythonParser.If_stmtContext ctx) {
PyObject test = null;
PyObject body = null;
PyObject orElse = this.runtime.None();
List<PythonParser.TestContext> testContextList = ctx.test();
List<PythonParser.SuiteContext> suiteContextList = ctx.suite();
int testIndex = testContextList.size() - 1;
int suiteIndex = suiteContextList.size() - 1;
int count = ctx.getChildCount();
for (int i = count - 1; i >= 0; i--) {
String name = ctx.getChild(i).getText();
switch(name) {
case "if":
test = visitTest(testContextList.get(testIndex));
body = visitSuite(suiteContextList.get(suiteIndex));
testIndex--;
suiteIndex--;
break;
case "elif":
PyObject elifTest = visitTest(testContextList.get(testIndex));
PyObject elifBody = visitSuite(suiteContextList.get(suiteIndex));
orElse = this.runtime.newPyObject("_ast.If", elifTest, elifBody, orElse);
testIndex--;
suiteIndex--;
break;
case "else":
orElse = visitSuite(suiteContextList.get(suiteIndex));
suiteIndex--;
break;
}
}
return this.runtime.newPyObject("_ast.If", test, body, orElse);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitArith_expr.
@Override
public PyObject visitArith_expr(PythonParser.Arith_exprContext ctx) {
List<PythonParser.TermContext> termContextList = ctx.term();
int termIndex = 0;
PyObject term = visitTerm(termContextList.get(termIndex));
termIndex++;
int count = ctx.getChildCount();
for (int i = 1; i < count; i += 2) {
String op = ctx.getChild(i).getText();
PyObject operator;
switch(op) {
case "+":
operator = this.runtime.newPyObject("_ast.Add");
break;
case "-":
operator = this.runtime.newPyObject("_ast.Sub");
break;
default:
throw this.runtime.newRaiseException("builtins.SyntaxError", "op '" + op + "' is not found");
}
PyObject rightTerm = visitTerm(termContextList.get(termIndex));
termIndex++;
term = this.runtime.newPyObject("_ast.BinOp", term, operator, rightTerm);
}
return term;
}
Aggregations