use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyNodeVisitorType method generic_visit.
@DefinePyFunction(name = "generic_visit")
public void generic_visit(PyObject self, PyObject node) {
PyObject visit = this.runtime.getattr(self, "visit");
PyObject astModule = this.runtime.moduleOrThrow("ast");
PyObject iter_fields = this.runtime.getattr(astModule, "iter_fields");
PyObject list = this.runtime.typeOrThrow("builtins.list");
PyObject ast = this.runtime.typeOrThrow("_ast.AST");
PyObject iter_fieldsResult = iter_fields.call(node);
this.runtime.iter(iter_fieldsResult, fvs -> {
PyObject[] fieldAndValue = new PyObject[2];
this.runtime.iterIndex(fvs, (fv, i) -> fieldAndValue[i] = fv);
PyObject value = fieldAndValue[1];
if (this.runtime.isInstance(value, list)) {
this.runtime.iter(value, item -> {
if (this.runtime.isInstance(item, ast)) {
visit.call(self, item);
}
});
} else if (this.runtime.isInstance(value, ast)) {
visit.call(self, value);
}
});
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyBuiltinsModule method issubclass.
@DefinePyFunction(name = "issubclass")
public PyObject issubclass(PyObject clazz, PyObject classInfo) {
if (!clazz.isType() || (!classInfo.isType() && !(classInfo instanceof PyTupleType))) {
throw this.runtime.newRaiseTypeError("issubclass() arg 2 must be a type or tuple of types");
}
List<PyObject> types = new ArrayList<>();
types.addAll(clazz.getTypes());
for (PyObject type : types) {
if (type == classInfo) {
return this.runtime.True();
}
}
return this.runtime.False();
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class PyInterpretClassObject method call.
@Override
public PyObject call(PyObject... args) {
PyObject object = this.runtime.getattr(this, __new__).call();
this.runtime.getattr(object, __init__).call(args);
return object;
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class InteractiveConsole method interact.
public void interact() {
InteractiveParser parser = new InteractiveParser(this.runtime);
InterpretEvaluator evaluator = new InterpretEvaluator(this.runtime);
Terminal terminal = TerminalFactory.get();
terminal.setEchoEnabled(false);
try {
terminal.init();
} catch (Exception e) {
e.printStackTrace();
return;
}
try (ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, terminal)) {
this.consoleReader = consoleReader;
printBanner();
StringBuilder buffer = new StringBuilder();
while (status != Status.QUIT) {
consoleReader.flush();
String line = readLine();
if (line == null) {
break;
}
if (buffer.length() > 0) {
buffer.append(System.lineSeparator());
}
buffer.append(line);
PyObject ast;
try {
ast = parser.parse(buffer.toString());
if (ast.isNone()) {
this.status = Status.INCOMPLETE;
continue;
}
} catch (RaiseException e) {
e.printStackTrace();
this.consoleReader.println(e.getException().toJava(String.class));
this.status = Status.READLINE;
buffer.setLength(0);
continue;
}
this.status = Status.READLINE;
buffer.setLength(0);
PyObject result;
try {
result = evaluator.evalMainModule(ast);
if (!result.isNone()) {
if (result instanceof PyStrObject) {
this.consoleReader.println(result.toString());
} else {
this.consoleReader.println(result.toJava(String.class));
}
}
} catch (RaiseException e) {
PyObject exception = e.getException();
this.consoleReader.println(exception.toJava(String.class));
this.consoleReader.println(e.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.
the class CafeBabePyAstCreateVisitor method createAssign.
private PyObject createAssign(List<PythonParser.Testlist_star_exprContext> testlist_star_exprContextList) {
int count = testlist_star_exprContextList.size() - 1;
PyObject[] targetArray = new PyObject[count];
for (int i = 0; i < count; i++) {
PythonParser.Testlist_star_exprContext testlist_star_exprContext = testlist_star_exprContextList.get(i);
PyObject target = visitTestlist_star_expr(testlist_star_exprContext);
toStore(target, 0);
targetArray[i] = target;
}
PyObject targets = this.runtime.list(targetArray);
PyObject value = visitTestlist_star_expr(testlist_star_exprContextList.get(testlist_star_exprContextList.size() - 1));
return this.runtime.newPyObject("_ast.Assign", targets, value);
}
Aggregations