use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitReturn_stmt.
@Override
public PyObject visitReturn_stmt(PythonParser.Return_stmtContext ctx) {
PyObject value = this.runtime.None();
PythonParser.TestlistContext testlistContext = ctx.testlist();
if (testlistContext != null) {
value = visitTestlist(testlistContext);
}
return this.runtime.newPyObject("_ast.Return", value);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitNot_test.
@Override
public PyObject visitNot_test(PythonParser.Not_testContext ctx) {
PythonParser.ComparisonContext comparisonContext = ctx.comparison();
if (comparisonContext != null) {
return visitComparison(comparisonContext);
} else {
PyObject notTest = visitNot_test(ctx.not_test());
PyObject op = this.runtime.newPyObject("_ast.Not");
return this.runtime.newPyObject("_ast.UnaryOp", op, notTest);
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitAtom.
@Override
public PyObject visitAtom(PythonParser.AtomContext ctx) {
TerminalNode nameNode = ctx.NAME();
if (nameNode != null) {
PyObject id = this.runtime.str(nameNode.getText());
// Default
PyObject load = this.runtime.newPyObject("_ast.Load");
return this.runtime.newPyObject("_ast.Name", id, load);
}
TerminalNode trueNode = ctx.TRUE();
if (trueNode != null) {
return this.runtime.True();
}
TerminalNode falseNode = ctx.FALSE();
if (falseNode != null) {
return this.runtime.False();
}
TerminalNode noneNode = ctx.NONE();
if (noneNode != null) {
return this.runtime.None();
}
TerminalNode ellipsisNode = ctx.ELLIPSIS();
if (ellipsisNode != null) {
return this.runtime.Ellipsis();
}
List<PythonParser.StrContext> strContextList = ctx.str();
if (!strContextList.isEmpty()) {
PyObject str;
if (strContextList.size() == 1) {
str = visitStr(strContextList.get(0));
} else {
str = visitStr(strContextList.get(0));
for (int i = 1; i < strContextList.size(); i++) {
PyObject rightStr = visitStr(strContextList.get(i));
str = this.runtime.add(str, rightStr);
}
}
return this.runtime.newPyObject("_ast.Str", str);
}
String open = ctx.getChild(0).getText();
String close = ctx.getChild(ctx.getChildCount() - 1).getText();
if ("[".equals(open)) {
if (!"]".equals(close)) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "invalid syntax");
}
// list
return visitList(ctx.testlist_comp());
} else if ("(".equals(open)) {
if (!")".equals(close)) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "invalid syntax");
}
// tuple or ()
return visitTuple(ctx.testlist_comp());
} else if ("{".equals(open)) {
if (!"}".equals(close)) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "invalid syntax");
}
// dict or set
return visitDictOrSet(ctx.dictorsetmaker());
}
return super.visitAtom(ctx);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalFunctionDef.
private PyObject evalFunctionDef(PyObject context, PyObject node) {
PyObject name = this.runtime.getattr(node, "name");
PyObject args = this.runtime.getattr(node, "args");
PyObject body = this.runtime.getattr(node, "body");
PyObject decorator_list = this.runtime.getattr(node, "decorator_list");
PyObject returns = this.runtime.getattr(node, "returns");
PyObject function = new PyInterpretFunctionObject(this.runtime, this, context, args, body);
context.getScope().put(name.toJava(String.class), function);
return this.runtime.None();
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InterpretEvaluator method evalAnnassign.
private PyObject evalAnnassign(PyObject context, PyObject node) {
PyObject target = this.runtime.getattr(node, "target");
PyObject value = this.runtime.getattr(node, "value");
if (!value.isNone()) {
PyObject id = this.runtime.getattr(target, "id");
PyObject evalValue = eval(context, value);
context.getScope().put(id.toJava(String.class), evalValue);
}
return this.runtime.None();
}
Aggregations