use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyNodeVisitorType method visit.
@DefinePyFunction(name = "visit")
public PyObject visit(PyObject self, PyObject node) {
String method = "visit_" + node.getName();
PyObject visitor = this.runtime.getattrOptional(self, method).orElse(this.runtime.getattr(this, "generic_visit"));
return visitor.call(self, node);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyInterpretFunctionObject method call.
@Override
public PyObject call(PyObject... args) {
PyObject lexicalContext = new PyLexicalScopeProxyObject(this.context);
PyObjectScope scope = lexicalContext.getScope();
this.runtime.getattrOptional(this.args, "args").ifPresent(argslist -> this.runtime.iterIndex(argslist, (a, i) -> {
PyObject arg = this.runtime.getattr(a, "arg");
scope.put(arg.toJava(String.class), args[i]);
}));
return evaluator.eval(lexicalContext, body);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method visitFor_stmt.
@Override
public PyObject visitFor_stmt(PythonParser.For_stmtContext ctx) {
PyObject target = visitExprlist(ctx.exprlist());
PyObject iter = visitTestlist(ctx.testlist());
PyObject body;
PyObject orelse = this.runtime.None();
List<PythonParser.SuiteContext> suiteContextList = ctx.suite();
body = visitSuite(suiteContextList.get(0));
if (suiteContextList.size() == 2) {
orelse = visitSuite(suiteContextList.get(1));
}
return this.runtime.newPyObject("_ast.For", target, iter, body, orelse);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method createAnnasign.
private PyObject createAnnasign(PyObject testlist_star_expr, PythonParser.AnnassignContext annassignContext) {
PyObject list = visitAnnassign(annassignContext);
List<PyObject> testList = this.runtime.toList(list);
PyObject target = testlist_star_expr;
if (!this.runtime.isInstance(target, "_ast.Name")) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "illegal target for annotation");
}
target.getScope().put("ctx", this.runtime.newPyObject("_ast.Store"));
PyObject annotation = testList.get(0);
PyObject value = this.runtime.None();
if (testList.size() == 2) {
value = testList.get(1);
}
// FIXME simple value fixed 1???
PyObject simple = this.runtime.number(1);
return this.runtime.newPyObject("_ast.Annassign", target, annotation, value, simple);
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method toStore.
private void toStore(PyObject target, int attributeDepth) {
PyObject type = target.getType();
if (type instanceof PyStarredType) {
target.getScope().put("ctx", this.runtime.newPyObject("_ast.Store"));
PyObject value = this.runtime.getattr(target, "value");
if (this.runtime.isIterable(value)) {
this.runtime.iter(value, v -> toStore(v, 0));
} else {
toStore(value, 0);
}
} else if (type instanceof PyNameType) {
if (attributeDepth == 0) {
target.getScope().put("ctx", this.runtime.newPyObject("_ast.Store"));
}
} else if (type instanceof PyAttributeType) {
if (attributeDepth == 0) {
target.getScope().put("ctx", this.runtime.newPyObject("_ast.Store"));
}
PyObject value = this.runtime.getattr(target, "value");
toStore(value, attributeDepth + 1);
} else if (type instanceof PyListType) {
target.getScope().put("ctx", this.runtime.newPyObject("_ast.Store"));
PyObject elts = this.runtime.getattr(target, "elts");
this.runtime.iter(elts, elt -> toStore(elt, 0));
} else if (type instanceof PyNumType) {
throw this.runtime.newRaiseException("builtins.SyntaxError", "can't assign to literal");
}
}
Aggregations