Search in sources :

Example 1 with FunctionReserved

use of org.elasticsearch.painless.node.SFunction.FunctionReserved in project elasticsearch by elastic.

the class Walker method visitConstructorfuncref.

@Override
public ANode visitConstructorfuncref(ConstructorfuncrefContext ctx) {
    if (!ctx.decltype().LBRACE().isEmpty()) {
        // array constructors are special: we need to make a synthetic method
        // taking integer as argument and returning a new instance, and return a ref to that.
        Location location = location(ctx);
        String arrayType = ctx.decltype().getText();
        SReturn code = new SReturn(location, new ENewArray(location, arrayType, Arrays.asList(new EVariable(location, "size")), false));
        String name = nextLambda();
        globals.addSyntheticMethod(new SFunction(new FunctionReserved(), location, arrayType, name, Arrays.asList("int"), Arrays.asList("size"), Arrays.asList(code), true));
        return new EFunctionRef(location(ctx), "this", name);
    }
    return new EFunctionRef(location(ctx), ctx.decltype().getText(), ctx.NEW().getText());
}
Also used : EVariable(org.elasticsearch.painless.node.EVariable) EFunctionRef(org.elasticsearch.painless.node.EFunctionRef) SFunction(org.elasticsearch.painless.node.SFunction) ENewArray(org.elasticsearch.painless.node.ENewArray) FunctionReserved(org.elasticsearch.painless.node.SFunction.FunctionReserved) EString(org.elasticsearch.painless.node.EString) SReturn(org.elasticsearch.painless.node.SReturn) Location(org.elasticsearch.painless.Location)

Example 2 with FunctionReserved

use of org.elasticsearch.painless.node.SFunction.FunctionReserved in project elasticsearch by elastic.

the class Walker method visitFunction.

@Override
public ANode visitFunction(FunctionContext ctx) {
    reserved.push(new FunctionReserved());
    String rtnType = ctx.decltype().getText();
    String name = ctx.ID().getText();
    List<String> paramTypes = new ArrayList<>();
    List<String> paramNames = new ArrayList<>();
    List<AStatement> statements = new ArrayList<>();
    for (DecltypeContext decltype : ctx.parameters().decltype()) {
        paramTypes.add(decltype.getText());
    }
    for (TerminalNode id : ctx.parameters().ID()) {
        paramNames.add(id.getText());
    }
    for (StatementContext statement : ctx.block().statement()) {
        statements.add((AStatement) visit(statement));
    }
    return new SFunction((FunctionReserved) reserved.pop(), location(ctx), rtnType, name, paramTypes, paramNames, statements, false);
}
Also used : AStatement(org.elasticsearch.painless.node.AStatement) SFunction(org.elasticsearch.painless.node.SFunction) FunctionReserved(org.elasticsearch.painless.node.SFunction.FunctionReserved) ArrayList(java.util.ArrayList) EString(org.elasticsearch.painless.node.EString) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) DecltypeContext(org.elasticsearch.painless.antlr.PainlessParser.DecltypeContext) StatementContext(org.elasticsearch.painless.antlr.PainlessParser.StatementContext)

Example 3 with FunctionReserved

use of org.elasticsearch.painless.node.SFunction.FunctionReserved in project elasticsearch by elastic.

the class Walker method visitLambda.

@Override
public ANode visitLambda(LambdaContext ctx) {
    reserved.push(new FunctionReserved());
    List<String> paramTypes = new ArrayList<>();
    List<String> paramNames = new ArrayList<>();
    List<AStatement> statements = new ArrayList<>();
    for (LamtypeContext lamtype : ctx.lamtype()) {
        if (lamtype.decltype() == null) {
            paramTypes.add("def");
        } else {
            paramTypes.add(lamtype.decltype().getText());
        }
        paramNames.add(lamtype.ID().getText());
    }
    if (ctx.expression() != null) {
        // single expression
        AExpression expression = (AExpression) visit(ctx.expression());
        statements.add(new SReturn(location(ctx), expression));
    } else {
        for (StatementContext statement : ctx.block().statement()) {
            statements.add((AStatement) visit(statement));
        }
    }
    String name = nextLambda();
    return new ELambda(name, (FunctionReserved) reserved.pop(), location(ctx), paramTypes, paramNames, statements);
}
Also used : AStatement(org.elasticsearch.painless.node.AStatement) LamtypeContext(org.elasticsearch.painless.antlr.PainlessParser.LamtypeContext) ELambda(org.elasticsearch.painless.node.ELambda) FunctionReserved(org.elasticsearch.painless.node.SFunction.FunctionReserved) AExpression(org.elasticsearch.painless.node.AExpression) ArrayList(java.util.ArrayList) EString(org.elasticsearch.painless.node.EString) SReturn(org.elasticsearch.painless.node.SReturn) StatementContext(org.elasticsearch.painless.antlr.PainlessParser.StatementContext)

Aggregations

EString (org.elasticsearch.painless.node.EString)3 FunctionReserved (org.elasticsearch.painless.node.SFunction.FunctionReserved)3 ArrayList (java.util.ArrayList)2 StatementContext (org.elasticsearch.painless.antlr.PainlessParser.StatementContext)2 AStatement (org.elasticsearch.painless.node.AStatement)2 SFunction (org.elasticsearch.painless.node.SFunction)2 SReturn (org.elasticsearch.painless.node.SReturn)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 Location (org.elasticsearch.painless.Location)1 DecltypeContext (org.elasticsearch.painless.antlr.PainlessParser.DecltypeContext)1 LamtypeContext (org.elasticsearch.painless.antlr.PainlessParser.LamtypeContext)1 AExpression (org.elasticsearch.painless.node.AExpression)1 EFunctionRef (org.elasticsearch.painless.node.EFunctionRef)1 ELambda (org.elasticsearch.painless.node.ELambda)1 ENewArray (org.elasticsearch.painless.node.ENewArray)1 EVariable (org.elasticsearch.painless.node.EVariable)1