Search in sources :

Example 16 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitFile_input.

@Override
public PyObject visitFile_input(PythonParser.File_inputContext ctx) {
    List<PyObject> bodyList = new ArrayList<>();
    for (PythonParser.StmtContext stmtContext : ctx.stmt()) {
        PyObject body = visitStmt(stmtContext);
        if (this.runtime.isInstance(body, "builtins.list")) {
            this.runtime.iter(body, bodyList::add);
        } else {
            bodyList.add(body);
        }
    }
    PyObject body = this.runtime.list(bodyList);
    PyObject module = this.runtime.newPyObject("_ast.Module", body);
    return module;
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 17 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitClassdef.

@Override
public PyObject visitClassdef(PythonParser.ClassdefContext ctx) {
    PyObject name = this.runtime.str(ctx.NAME().getText());
    PythonParser.ArglistContext arglistContext = ctx.arglist();
    PyObject bases;
    if (arglistContext != null) {
        bases = visitArglist(arglistContext);
    } else {
        bases = this.runtime.list();
    }
    // TODO 何これ?
    PyObject keywords = this.runtime.list();
    PythonParser.SuiteContext suiteContext = ctx.suite();
    PyObject body = visitSuite(suiteContext);
    // TODO 何これ?
    PyObject decorator_list = this.runtime.None();
    return this.runtime.newPyObject("_ast.ClassDef", name, bases, keywords, body, decorator_list);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 18 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitTestlist_comp.

@Override
public PyObject visitTestlist_comp(PythonParser.Testlist_compContext ctx) {
    List<PyObject> list = new ArrayList<>();
    PythonParser.Comp_forContext comp_forContext = ctx.comp_for();
    if (comp_forContext == null) {
        int count = ctx.getChildCount();
        for (int i = 0; i < count; i++) {
            ParseTree c = ctx.getChild(i);
            PyObject element = c.accept(this);
            if (element != null) {
                list.add(element);
            }
        }
    } else {
        list.add(ctx.getChild(0).accept(this));
        PyObject comp_for = visitComp_for(comp_forContext);
        this.runtime.iter(comp_for, list::add);
    }
    return this.runtime.list(list);
}
Also used : ArrayList(java.util.ArrayList) PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 19 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitSingle_input.

@Override
public PyObject visitSingle_input(PythonParser.Single_inputContext ctx) {
    PyObject body;
    PythonParser.Simple_stmtContext simple_stmtContext = ctx.simple_stmt();
    PythonParser.Compound_stmtContext compound_stmtContext = ctx.compound_stmt();
    if (simple_stmtContext != null) {
        body = this.runtime.list(visitSimple_stmt(simple_stmtContext));
    } else if (compound_stmtContext != null) {
        body = this.runtime.list(visitCompound_stmt(compound_stmtContext));
    } else {
        body = this.runtime.list();
    }
    return this.runtime.newPyObject("_ast.Interactive", body);
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) PyObject(org.cafebabepy.runtime.PyObject)

Example 20 with PyObject

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

the class CafeBabePyAstCreateVisitor method visitFactor.

@Override
public PyObject visitFactor(PythonParser.FactorContext ctx) {
    PythonParser.PowerContext powerContext = ctx.power();
    if (powerContext != null) {
        return visitPower(powerContext);
    } else {
        PythonParser.FactorContext factorContext = ctx.factor();
        PyObject factor = visitFactor(factorContext);
        PyObject op;
        switch(ctx.getChild(0).getText()) {
            case "-":
                op = this.runtime.newPyObject("_ast.USub");
                break;
            case "+":
                op = this.runtime.newPyObject("_ast.UAdd");
                break;
            case "~":
                op = this.runtime.newPyObject("_ast.Invert");
                break;
            default:
                throw this.runtime.newRaiseException("builtins.SyntaxError", "Invalid factor");
        }
        return this.runtime.newPyObject("_ast.UnaryOp", op, factor);
    }
}
Also used : PythonParser(org.cafebabepy.parser.antlr.PythonParser) 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