Search in sources :

Example 6 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) DefinePyFunction(org.cafebabepy.runtime.module.DefinePyFunction)

Example 7 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject) Python(org.cafebabepy.runtime.Python) AbstractPyObjectObject(org.cafebabepy.runtime.object.AbstractPyObjectObject) PyLexicalScopeProxyObject(org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject) ProtocolNames.__call__(org.cafebabepy.util.ProtocolNames.__call__) PyObjectScope(org.cafebabepy.runtime.PyObjectScope) PyLexicalScopeProxyObject(org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject) PyObject(org.cafebabepy.runtime.PyObject) PyObjectScope(org.cafebabepy.runtime.PyObjectScope)

Example 8 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 9 with PyObject

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);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 10 with PyObject

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");
    }
}
Also used : PythonParserBaseVisitor(org.cafebabepy.parser.antlr.PythonParserBaseVisitor) PyObject(org.cafebabepy.runtime.PyObject) List(java.util.List) Python(org.cafebabepy.runtime.Python) PythonParser(org.cafebabepy.parser.antlr.PythonParser) org.cafebabepy.runtime.module._ast(org.cafebabepy.runtime.module._ast) ParseTree(org.antlr.v4.runtime.tree.ParseTree) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ArrayList(java.util.ArrayList) PyObject(org.cafebabepy.runtime.PyObject)

Aggregations

PyObject (org.cafebabepy.runtime.PyObject)69 PythonParser (org.cafebabepy.parser.antlr.PythonParser)20 ArrayList (java.util.ArrayList)10 DefinePyFunction (org.cafebabepy.runtime.module.DefinePyFunction)9 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 CafeBabePyException (org.cafebabepy.runtime.CafeBabePyException)3 PyIntObject (org.cafebabepy.runtime.object.java.PyIntObject)3 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 AbstractPyObject (org.cafebabepy.runtime.AbstractPyObject)2 PyObjectScope (org.cafebabepy.runtime.PyObjectScope)2 Python (org.cafebabepy.runtime.Python)2 AbstractPyObjectObject (org.cafebabepy.runtime.object.AbstractPyObjectObject)2 PyLexicalScopeProxyObject (org.cafebabepy.runtime.object.proxy.PyLexicalScopeProxyObject)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 Terminal (jline.Terminal)1 ConsoleReader (jline.console.ConsoleReader)1 InterpretEvaluator (org.cafebabepy.evaluter.Interpret.InterpretEvaluator)1 InteractiveParser (org.cafebabepy.parser.InteractiveParser)1