Search in sources :

Example 21 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTerm.

@Override
public PyObject visitTerm(PythonParser.TermContext ctx) {
    List<PythonParser.FactorContext> factorContextList = ctx.factor();
    int factorIndex = 0;
    PyObject factor = visitFactor(factorContextList.get(factorIndex));
    factorIndex++;
    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.Mult");
                break;
            case "%":
                operator = this.runtime.newPyObject("_ast.Mod");
                break;
            default:
                throw this.runtime.newRaiseException("builtins.SyntaxError", "op '" + op + "' is not found");
        }
        PyObject rightFactor = visitFactor(factorContextList.get(factorIndex));
        factorIndex++;
        factor = this.runtime.newPyObject("_ast.BinOp", factor, operator, rightFactor);
    }
    return factor;
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 22 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitFuncdef.

@Override
public PyObject visitFuncdef(PythonParser.FuncdefContext ctx) {
    PyObject name = this.runtime.str(ctx.NAME().getText());
    PyObject args = this.runtime.None();
    PythonParser.ParametersContext parametersContext = ctx.parameters();
    if (parametersContext != null) {
        args = visitParameters(parametersContext);
    }
    PyObject body = visitSuite(ctx.suite());
    // FIXME 未実装
    PyObject decorator_list = this.runtime.None();
    // 無視
    PyObject returns = this.runtime.None();
    PythonParser.TestContext testContext = ctx.test();
    if (testContext != null) {
        returns = visitTest(testContext);
    }
    return this.runtime.newPyObject("_ast.FunctionDef", name, args, body, decorator_list, returns);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 23 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitAtomToTestlist_comp.

private PyObject visitAtomToTestlist_comp(PyObject testList_Comp, String comp, String structure) {
    PyObject load = this.runtime.newPyObject("_ast.Load");
    List<PyObject> resultVisitList = this.runtime.toList(testList_Comp);
    int comprehensionCount = 0;
    PyObject type = this.runtime.typeOrThrow("_ast.comprehension");
    for (int i = 1; i < resultVisitList.size(); i++) {
        if (this.runtime.isInstance(resultVisitList.get(i), type)) {
            comprehensionCount++;
        }
    }
    if (0 < comprehensionCount) {
        if (resultVisitList.size() - 1 != comprehensionCount) {
            throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid comprehension");
        }
        PyObject elt = resultVisitList.get(0);
        PyObject generators = this.runtime.list(resultVisitList.subList(1, resultVisitList.size()));
        return this.runtime.newPyObject(comp, elt, generators);
    } else {
        return this.runtime.newPyObject(structure, testList_Comp, load);
    }
}
Also used : PyObject(org.cafebabepy.runtime.PyObject)

Example 24 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTrailer.

@Override
public PyObject visitTrailer(PythonParser.TrailerContext ctx) {
    if (ctx.NAME() != null) {
        PyObject attr = this.runtime.str(ctx.NAME().getText());
        PyObject load = this.runtime.newPyObject("_ast.Load");
        return this.runtime.newPyObject("_ast.Attribute", this.runtime.None(), attr, load);
    }
    String firstText = ctx.getChild(0).getText();
    String lastText = ctx.getChild(ctx.getChildCount() - 1).getText();
    if ("(".equals(firstText) && ")".equals(lastText)) {
        PythonParser.ArglistContext arglistContext = ctx.arglist();
        PyObject arglist = this.runtime.None();
        if (arglistContext != null) {
            arglist = visitArglist(arglistContext);
        }
        // FIXME keywords
        return this.runtime.newPyObject("_ast.Call", this.runtime.None(), arglist, this.runtime.None());
    }
    throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid ast");
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 25 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTypedargslist.

@Override
public PyObject visitTypedargslist(PythonParser.TypedargslistContext ctx) {
    List<PythonParser.TfpdefContext> tfpdefContextList = ctx.tfpdef();
    PyObject[] argArray = new PyObject[tfpdefContextList.size()];
    for (int i = 0; i < argArray.length; i++) {
        argArray[i] = visitTfpdef(tfpdefContextList.get(i));
    }
    PyObject args = this.runtime.list(argArray);
    PyObject vararg = this.runtime.None();
    PyObject kwonlyargs = this.runtime.None();
    PyObject kw_defaults = this.runtime.None();
    PyObject kwarg = this.runtime.None();
    PyObject defaults = this.runtime.None();
    return this.runtime.newPyObject("_ast.arguments", args, vararg, kwonlyargs, kw_defaults, kwarg, defaults);
}
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