Painless Script语法

语法


官方文档:https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html

字面量

整数

包括十进制、八进制、十六进制

l 代表 long, f 代表float, d 代表 double ,默认是int

INTEGER: '-'? ( '0' | [1-9] [0-9]* ) [lLfFdD]?;
OCTAL: '-'? '0' [0-7]+ [lL]?;
HEX: '-'? '0' [xX] [0-9a-fA-F]+ [lL]?;
0     // integer literal of 0
0D    // double literal of 0.0
1234L // long literal of 1234
-90F  // float literal of -90.0
-022  // integer literal of -18 specified in octal
0xF2A // integer literal of 3882
浮点数

f 代表 float 代表 d 代表 double 默认是 double

DECIMAL: '-'? ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? ( [eE] [+\-]? [0-9]+ )? [fFdD]?;
0.0      // double value of 0.0
1E6      // double value of 1000000
0.977777 // double value of 0.97777
-126.34  // double value of -126.34
89.9F    // float value of 89.9
字符串

可以使用单引号或者双引号

STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' ) | ( '\'' ( '\\\'' | '\\\\' | ~[\\'] )*? '\'' );
"double-quoted String literal"
'single-quoted String literal'
"\"double-quoted String with escaped double-quotes\" and backslash: \\"
'\'single-quoted String with escaped single-quotes\' and backslash \\'
"double-quoted String with non-escaped 'single-quotes'"
'single-quoted String with non-escaped "double-quotes"'
字符

无法直接定义一个char,必须通过强制转换,如果强制转换多个字符的字符串,将会抛出异常

(char)"C"
(char)'c'

变量

变量名

变量名必须以字母或下划线开始。不能使用关键字或类型作为标识符。

ID: [_a-zA-Z] [_a-zA-Z-0-9]*;
_
a
Z
id
list
list0
MAP25
_map25
变量申明

使用之前必须先申明 type-name identifier-name

declaration : type ID assignment? (',' ID assignment?)*;
type: ID ('.' ID)* ('[' ']')*;
assignment: '=' expression;
int x;        // Declare a variable with type int and id x
List y;       // Declare a variable with type List and id y
int x, y, z;  // Declare variables with type int and ids x, y, and z
int x, y = 5, z;   //Declare int x; store default int 0 to x; declare int y; store int 5 to y; declare int z; store default int 0 to z;
def[] d;      // Declare the variable d with type def[]
Map[][] m;    //Declare Map[][] m; store default null to m
int i = 10;   // Declare the int variable i and set it to the int literal 10
变量赋值

基本跟其他语言一致

assignment: ID '=' expression
int i;   // Declare an int i
i = 10;  // Set the int i to the int literal 10

int i = 10;     // Declare the int variable i and set it the int literal 1
double j = 2.0; // Declare the double variable j and set it to the double literal 2.0
                
int i = 10; // Declare the int variable i and set it to the int literal 10
int j = i;  // Declare the int variable j and set it to the int variable i

ArrayList l = new ArrayList();  // Declare an ArrayList variable l and set it to a newly allocated ArrayList
Map m = new HashMap();          // Declare a Map variable m and set it   to a newly allocated HashMap

List l = new ArrayList(); // Declare List variable l and set it a newly allocated ArrayList
List k = l;               // Declare List variable k and set it to the value of the List variable l
List m;                   // Declare List variable m and set it the default value null
m = k;                    // Set the value of List variable m to the value of List variable k
int[] ia1;                      //Declare int[] ia1; store default null to ia1    
ia1 = new int[2];               //Allocate 1-d int array instance with length [2] → 1-d int array reference; store 1-d int array reference to ia1        
ia1[0] = 1;                     //Load from ia1 → 1-d int array reference; store int 1 to index [0] of 1-d int array reference 
int[] ib1 = ia1;                //Declare int[] ib1; load from ia1 → 1-d int array reference; store 1-d int array reference to ib1; (note ia1 and ib1 refer to the same instance known as a shallow copy) 
int[][] ic2 = new int[2][5];    //Declare int[][] ic2; allocate 2-d int array instance with length [2, 5] → 2-d int array reference; store 2-d int array reference to ic2
ic2[1][3] = 2;                  //Load from ic2 → 2-d int array reference; store int 2 to index [1, 3] of 2-d int array reference
ic2[0] = ia1;                   //Load from ia1 → 1-d int array reference; load from ic2 → 2-d int array reference; store 1-d int array reference to index [0] of 2-d int array reference; (note ia1, ib1, and index [0] of ia2 refer to the same instance)

你可能感兴趣的:(Painless Script语法)