Search in sources :

Example 51 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class CafeBabePyAstCreateVisitor method visitDictOrSet.

private PyObject visitDictOrSet(PythonParser.DictorsetmakerContext dictorsetmakerContext) {
    if (dictorsetmakerContext != null) {
        if (dictorsetmakerContext.comp_for() == null) {
            List<PyObject> keys = new ArrayList<>();
            List<PyObject> values = new ArrayList<>();
            boolean doubleStar = false;
            PyObject test = null;
            int count = dictorsetmakerContext.getChildCount();
            for (int i = 0; i < count; i++) {
                ParseTree c = dictorsetmakerContext.getChild(i);
                if ("**".equals(c.getText())) {
                    doubleStar = true;
                } else {
                    if (doubleStar) {
                        keys.add(this.runtime.None());
                        values.add(c.accept(this));
                    } else {
                        PyObject element = c.accept(this);
                        if (element != null) {
                            if (test == null) {
                                test = element;
                            } else {
                                keys.add(test);
                                values.add(element);
                                test = null;
                            }
                        }
                    }
                    doubleStar = false;
                }
            }
            return this.runtime.newPyObject("_ast.Dict", this.runtime.list(keys), this.runtime.list(values));
        }
    }
    return this.runtime.newPyObject("_ast.Dict", this.runtime.list(), this.runtime.list());
}
Also used : ArrayList(java.util.ArrayList) PyObject(org.cafebabepy.runtime.PyObject) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 52 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class CafeBabePyAstCreateVisitor method visitStar_expr.

@Override
public PyObject visitStar_expr(PythonParser.Star_exprContext ctx) {
    PythonParser.ExprContext exprContext = ctx.expr();
    PyObject value = visitExpr(exprContext);
    PyObject context = this.runtime.newPyObject("_ast.Load");
    return this.runtime.newPyObject("_ast.Starred", value, context);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 53 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class CafeBabePyAstCreateVisitor method visitNumber.

@Override
public PyObject visitNumber(PythonParser.NumberContext ctx) {
    String text = ctx.getChild(0).getText();
    PyObject number = this.runtime.number(Integer.parseInt(text));
    PyObject object = this.runtime.newPyObject("_ast.Num", number);
    return object;
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 54 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class CafeBabePyAstCreateVisitor method visitIf_stmt.

@Override
public PyObject visitIf_stmt(PythonParser.If_stmtContext ctx) {
    PyObject test = null;
    PyObject body = null;
    PyObject orElse = this.runtime.None();
    List<PythonParser.TestContext> testContextList = ctx.test();
    List<PythonParser.SuiteContext> suiteContextList = ctx.suite();
    int testIndex = testContextList.size() - 1;
    int suiteIndex = suiteContextList.size() - 1;
    int count = ctx.getChildCount();
    for (int i = count - 1; i >= 0; i--) {
        String name = ctx.getChild(i).getText();
        switch(name) {
            case "if":
                test = visitTest(testContextList.get(testIndex));
                body = visitSuite(suiteContextList.get(suiteIndex));
                testIndex--;
                suiteIndex--;
                break;
            case "elif":
                PyObject elifTest = visitTest(testContextList.get(testIndex));
                PyObject elifBody = visitSuite(suiteContextList.get(suiteIndex));
                orElse = this.runtime.newPyObject("_ast.If", elifTest, elifBody, orElse);
                testIndex--;
                suiteIndex--;
                break;
            case "else":
                orElse = visitSuite(suiteContextList.get(suiteIndex));
                suiteIndex--;
                break;
        }
    }
    return this.runtime.newPyObject("_ast.If", test, body, orElse);
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 55 with PyObject

use of org.cafebabepy.runtime.PyObject in project cafebabepy by cafebabepy.

the class CafeBabePyAstCreateVisitor method visitArith_expr.

@Override
public PyObject visitArith_expr(PythonParser.Arith_exprContext ctx) {
    List<PythonParser.TermContext> termContextList = ctx.term();
    int termIndex = 0;
    PyObject term = visitTerm(termContextList.get(termIndex));
    termIndex++;
    int count = ctx.getChildCount();
    for (int i = 1; i < count; i += 2) {
        String op = ctx.getChild(i).getText();
        PyObject operator;
        switch(op) {
            case "+":
                operator = this.runtime.newPyObject("_ast.Add");
                break;
            case "-":
                operator = this.runtime.newPyObject("_ast.Sub");
                break;
            default:
                throw this.runtime.newRaiseException("builtins.SyntaxError", "op '" + op + "' is not found");
        }
        PyObject rightTerm = visitTerm(termContextList.get(termIndex));
        termIndex++;
        term = this.runtime.newPyObject("_ast.BinOp", term, operator, rightTerm);
    }
    return term;
}
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