Skip to content

Commit 22622b3

Browse files
committed
Better container access formatting
1 parent ae94a1d commit 22622b3

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/ContainerAccessExpression.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import com.annimon.ownlang.exceptions.TypeException;
44
import com.annimon.ownlang.lib.*;
55
import java.util.List;
6+
import java.util.regex.Pattern;
67

78
/**
89
*
910
* @author aNNiMON
1011
*/
1112
public final class ContainerAccessExpression implements Expression, Accessible {
12-
13+
14+
private static final Pattern PATTERN_SIMPLE_INDEX = Pattern.compile("^\"[a-zA-Z$_]\\w*\"");
15+
1316
public final Expression root;
1417
public final List<Expression> indices;
15-
private boolean rootIsVariable;
18+
private final boolean[] simpleIndices;
19+
private final boolean rootIsVariable;
1620

1721
public ContainerAccessExpression(String variable, List<Expression> indices) {
1822
this(new VariableExpression(variable), indices);
@@ -22,6 +26,7 @@ public ContainerAccessExpression(Expression root, List<Expression> indices) {
2226
rootIsVariable = root instanceof VariableExpression;
2327
this.root = root;
2428
this.indices = indices;
29+
simpleIndices = precomputeSimpleIndices();
2530
}
2631

2732
public boolean rootIsVariable() {
@@ -55,21 +60,12 @@ public Value set(Value value) {
5560
final Value container = getContainer();
5661
final Value lastIndex = lastIndex();
5762
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());
7267
}
68+
return value;
7369
}
7470

7571
public Value getContainer() {
@@ -111,8 +107,30 @@ public <R, T> R accept(ResultVisitor<R, T> visitor, T t) {
111107
return visitor.visit(this, t);
112108
}
113109

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+
114121
@Override
115122
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();
117135
}
118136
}

0 commit comments

Comments
 (0)