Search in sources :

Example 1 with Location

use of org.elasticsearch.painless.Location 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 Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class NodeToStringTests method testECast.

public void testECast() {
    Location l = new Location(getTestName(), 0);
    AExpression child = new EConstant(l, "test");
    Cast cast = new Cast(Definition.STRING_TYPE, Definition.INT_OBJ_TYPE, true);
    assertEquals("(ECast Integer (EConstant String 'test'))", new ECast(l, child, cast).toString());
    l = new Location(getTestName(), 1);
    child = new EBinary(l, Operation.ADD, new EConstant(l, "test"), new EConstant(l, 12));
    cast = new Cast(Definition.INT_OBJ_TYPE, Definition.BOOLEAN_OBJ_TYPE, true);
    assertEquals("(ECast Boolean (EBinary (EConstant String 'test') + (EConstant Integer 12)))", new ECast(l, child, cast).toString());
}
Also used : Cast(org.elasticsearch.painless.Definition.Cast) Location(org.elasticsearch.painless.Location)

Example 3 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class NodeToStringTests method testPSubDefCall.

public void testPSubDefCall() {
    Location l = new Location(getTestName(), 0);
    PSubDefCall node = new PSubDefCall(l, "toString", emptyList());
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubDefCall (EVariable a) toString)", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubDefCall (EVariable a) toString))", new PSubNullSafeCallInvoke(l, node).toString());
    l = new Location(getTestName(), 0);
    node = new PSubDefCall(l, "equals", singletonList(new EVariable(l, "b")));
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubDefCall (EVariable a) equals (Args (EVariable b)))", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubDefCall (EVariable a) equals (Args (EVariable b))))", new PSubNullSafeCallInvoke(l, node).toString());
    l = new Location(getTestName(), 0);
    node = new PSubDefCall(l, "superWeird", Arrays.asList(new EVariable(l, "b"), new EVariable(l, "c"), new EVariable(l, "d")));
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubDefCall (EVariable a) superWeird (Args (EVariable b) (EVariable c) (EVariable d)))", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubDefCall (EVariable a) superWeird (Args (EVariable b) (EVariable c) (EVariable d))))", new PSubNullSafeCallInvoke(l, node).toString());
}
Also used : Location(org.elasticsearch.painless.Location)

Example 4 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class NodeToStringTests method testSSubEachArray.

public void testSSubEachArray() {
    Location l = new Location(getTestName(), 0);
    Variable v = new Variable(l, "test", Definition.INT_TYPE, 5, false);
    AExpression e = new ENewArray(l, "int", Arrays.asList(new EConstant(l, 1), new EConstant(l, 2), new EConstant(l, 3)), true);
    SBlock b = new SBlock(l, singletonList(new SReturn(l, new EConstant(l, 5))));
    SSubEachArray node = new SSubEachArray(l, v, e, b);
    assertEquals("(SSubEachArray int test (ENewArray int init (Args (EConstant Integer 1) (EConstant Integer 2) (EConstant Integer 3))) " + "(SBlock (SReturn (EConstant Integer 5))))", node.toString());
}
Also used : Variable(org.elasticsearch.painless.Locals.Variable) Location(org.elasticsearch.painless.Location)

Example 5 with Location

use of org.elasticsearch.painless.Location in project elasticsearch by elastic.

the class NodeToStringTests method testPSubCallInvoke.

public void testPSubCallInvoke() {
    Location l = new Location(getTestName(), 0);
    RuntimeClass c = Definition.getRuntimeClass(Integer.class);
    Method m = c.methods.get(new MethodKey("toString", 0));
    PSubCallInvoke node = new PSubCallInvoke(l, m, null, emptyList());
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubCallInvoke (EVariable a) toString)", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubCallInvoke (EVariable a) toString))", new PSubNullSafeCallInvoke(l, node).toString());
    l = new Location(getTestName(), 1);
    m = c.methods.get(new MethodKey("equals", 1));
    node = new PSubCallInvoke(l, m, null, singletonList(new EVariable(l, "b")));
    node.prefix = new EVariable(l, "a");
    assertEquals("(PSubCallInvoke (EVariable a) equals (Args (EVariable b)))", node.toString());
    assertEquals("(PSubNullSafeCallInvoke (PSubCallInvoke (EVariable a) equals (Args (EVariable b))))", new PSubNullSafeCallInvoke(l, node).toString());
}
Also used : Method(org.elasticsearch.painless.Definition.Method) RuntimeClass(org.elasticsearch.painless.Definition.RuntimeClass) Location(org.elasticsearch.painless.Location) MethodKey(org.elasticsearch.painless.Definition.MethodKey)

Aggregations

Location (org.elasticsearch.painless.Location)18 Struct (org.elasticsearch.painless.Definition.Struct)4 Token (org.antlr.v4.runtime.Token)2 Method (org.elasticsearch.painless.Definition.Method)2 MethodKey (org.elasticsearch.painless.Definition.MethodKey)2 Variable (org.elasticsearch.painless.Locals.Variable)2 Collections.emptyList (java.util.Collections.emptyList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 CharStream (org.antlr.v4.runtime.CharStream)1 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)1 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)1 Cast (org.elasticsearch.painless.Definition.Cast)1 Field (org.elasticsearch.painless.Definition.Field)1 RuntimeClass (org.elasticsearch.painless.Definition.RuntimeClass)1 FeatureTest (org.elasticsearch.painless.FeatureTest)1 EFunctionRef (org.elasticsearch.painless.node.EFunctionRef)1 ENewArray (org.elasticsearch.painless.node.ENewArray)1 EString (org.elasticsearch.painless.node.EString)1