3
3
import com .annimon .ownlang .exceptions .TypeException ;
4
4
import com .annimon .ownlang .lib .*;
5
5
import java .util .List ;
6
+ import java .util .regex .Pattern ;
6
7
7
8
/**
8
9
*
9
10
* @author aNNiMON
10
11
*/
11
12
public final class ContainerAccessExpression implements Expression , Accessible {
12
-
13
+
14
+ private static final Pattern PATTERN_SIMPLE_INDEX = Pattern .compile ("^\" [a-zA-Z$_]\\ w*\" " );
15
+
13
16
public final Expression root ;
14
17
public final List <Expression > indices ;
15
- private boolean rootIsVariable ;
18
+ private final boolean [] simpleIndices ;
19
+ private final boolean rootIsVariable ;
16
20
17
21
public ContainerAccessExpression (String variable , List <Expression > indices ) {
18
22
this (new VariableExpression (variable ), indices );
@@ -22,6 +26,7 @@ public ContainerAccessExpression(Expression root, List<Expression> indices) {
22
26
rootIsVariable = root instanceof VariableExpression ;
23
27
this .root = root ;
24
28
this .indices = indices ;
29
+ simpleIndices = precomputeSimpleIndices ();
25
30
}
26
31
27
32
public boolean rootIsVariable () {
@@ -55,21 +60,12 @@ public Value set(Value value) {
55
60
final Value container = getContainer ();
56
61
final Value lastIndex = lastIndex ();
57
62
switch (container .type ()) {
58
- case Types .ARRAY :
59
- ((ArrayValue ) container ).set (lastIndex .asInt (), value );
60
- return value ;
61
-
62
- case Types .MAP :
63
- ((MapValue ) container ).set (lastIndex , value );
64
- return value ;
65
-
66
- case Types .CLASS :
67
- ((ClassInstanceValue ) container ).set (lastIndex , value );
68
- return value ;
69
-
70
- default :
71
- throw new TypeException ("Array or map expected. Got " + container .type ());
63
+ case Types .ARRAY -> ((ArrayValue ) container ).set (lastIndex .asInt (), value );
64
+ case Types .MAP -> ((MapValue ) container ).set (lastIndex , value );
65
+ case Types .CLASS -> ((ClassInstanceValue ) container ).set (lastIndex , value );
66
+ default -> throw new TypeException ("Array or map expected. Got " + container .type ());
72
67
}
68
+ return value ;
73
69
}
74
70
75
71
public Value getContainer () {
@@ -111,8 +107,30 @@ public <R, T> R accept(ResultVisitor<R, T> visitor, T t) {
111
107
return visitor .visit (this , t );
112
108
}
113
109
110
+ private boolean [] precomputeSimpleIndices () {
111
+ final boolean [] result = new boolean [indices .size ()];
112
+ int i = 0 ;
113
+ for (Expression index : indices ) {
114
+ String indexStr = index .toString ();
115
+ result [i ] = PATTERN_SIMPLE_INDEX .matcher (indexStr ).matches ();
116
+ i ++;
117
+ }
118
+ return result ;
119
+ }
120
+
114
121
@ Override
115
122
public String toString () {
116
- return root .toString () + indices ;
123
+ final var sb = new StringBuilder (root .toString ());
124
+ int i = 0 ;
125
+ for (Expression index : indices ) {
126
+ String indexStr = index .toString ();
127
+ if (simpleIndices [i ]) {
128
+ sb .append ('.' ).append (indexStr , 1 , indexStr .length () - 1 );
129
+ } else {
130
+ sb .append ('[' ).append (indexStr ).append (']' );
131
+ }
132
+ i ++;
133
+ }
134
+ return sb .toString ();
117
135
}
118
136
}
0 commit comments