Search in sources :

Example 61 with PyObject

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

Example 62 with PyObject

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

Example 63 with PyObject

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

Example 64 with PyObject

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

Example 65 with PyObject

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();
}
Also used : 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