|
@@ -7,6 +7,13 @@ import org.openzen.zenscript.codemodel.FunctionParameter;
|
7
|
7
|
import org.openzen.zenscript.codemodel.HighLevelDefinition;
|
8
|
8
|
import org.openzen.zenscript.codemodel.Modifiers;
|
9
|
9
|
import org.openzen.zenscript.codemodel.expression.*;
|
|
10
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.CharSwitchValue;
|
|
11
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.EnumConstantSwitchValue;
|
|
12
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.IntSwitchValue;
|
|
13
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.StringSwitchValue;
|
|
14
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
|
|
15
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValueVisitor;
|
|
16
|
+import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
|
10
|
17
|
import org.openzen.zenscript.codemodel.member.FieldMember;
|
11
|
18
|
import org.openzen.zenscript.codemodel.member.IDefinitionMember;
|
12
|
19
|
import org.openzen.zenscript.codemodel.type.BasicTypeID;
|
|
@@ -116,54 +123,35 @@ public class CompilerUtils {
|
116
|
123
|
}
|
117
|
124
|
|
118
|
125
|
|
119
|
|
- public static int getKeyForSwitch(Expression expression) {
|
120
|
|
- if (expression.type instanceof BasicTypeID)
|
121
|
|
- switch ((BasicTypeID) expression.type) {
|
122
|
|
- case BOOL:
|
123
|
|
- if (expression instanceof ConstantBoolExpression)
|
124
|
|
- return ((ConstantBoolExpression) expression).value ? 1 : 0;
|
125
|
|
- break;
|
126
|
|
- case BYTE:
|
127
|
|
- if (expression instanceof ConstantByteExpression)
|
128
|
|
- return ((ConstantByteExpression) expression).value;
|
129
|
|
- break;
|
130
|
|
- case SBYTE:
|
131
|
|
- if (expression instanceof ConstantSByteExpression)
|
132
|
|
- return ((ConstantSByteExpression) expression).value;
|
133
|
|
- break;
|
134
|
|
- case SHORT:
|
135
|
|
- if(expression instanceof ConstantShortExpression)
|
136
|
|
- return ((ConstantShortExpression) expression).value;
|
137
|
|
- break;
|
138
|
|
- case USHORT:
|
139
|
|
- if (expression instanceof ConstantUShortExpression)
|
140
|
|
- return ((ConstantUShortExpression) expression).value;
|
141
|
|
- break;
|
142
|
|
- case INT:
|
143
|
|
- if (expression instanceof ConstantIntExpression)
|
144
|
|
- return ((ConstantIntExpression) expression).value;
|
145
|
|
- break;
|
146
|
|
- case UINT:
|
147
|
|
- if (expression instanceof ConstantUIntExpression)
|
148
|
|
- return ((ConstantUIntExpression) expression).value;
|
149
|
|
- break;
|
150
|
|
- case LONG:
|
151
|
|
- if(expression instanceof ConstantLongExpression)
|
152
|
|
- return (int)((ConstantLongExpression) expression).value;
|
153
|
|
- break;
|
154
|
|
- case ULONG:
|
155
|
|
- if(expression instanceof ConstantULongExpression)
|
156
|
|
- return (int)((ConstantULongExpression) expression).value;
|
157
|
|
- break;
|
158
|
|
- case CHAR:
|
159
|
|
- if(expression instanceof ConstantCharExpression)
|
160
|
|
- return ((ConstantCharExpression) expression).value;
|
161
|
|
- break;
|
162
|
|
- case STRING:
|
163
|
|
- if(expression instanceof ConstantStringExpression)
|
164
|
|
- return ((ConstantStringExpression) expression).value.hashCode();
|
165
|
|
- break;
|
166
|
|
- }
|
167
|
|
- throw new RuntimeException("Cannot switch over expression " + expression);
|
|
126
|
+ public static int getKeyForSwitch(SwitchValue expression) {
|
|
127
|
+ return expression.accept(new SwitchKeyVisitor());
|
168
|
128
|
}
|
|
129
|
+
|
|
130
|
+ private static class SwitchKeyVisitor implements SwitchValueVisitor<Integer> {
|
|
131
|
+
|
|
132
|
+ @Override
|
|
133
|
+ public Integer acceptInt(IntSwitchValue value) {
|
|
134
|
+ return value.value;
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ @Override
|
|
138
|
+ public Integer acceptChar(CharSwitchValue value) {
|
|
139
|
+ return (int)value.value;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ @Override
|
|
143
|
+ public Integer acceptString(StringSwitchValue value) {
|
|
144
|
+ return value.value.hashCode();
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ @Override
|
|
148
|
+ public Integer acceptEnumConstant(EnumConstantSwitchValue value) {
|
|
149
|
+ return value.constant.value;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ @Override
|
|
153
|
+ public Integer acceptVariantOption(VariantOptionSwitchValue value) {
|
|
154
|
+ throw new UnsupportedOperationException("Not there yet");
|
|
155
|
+ }
|
|
156
|
+ }
|
169
|
157
|
}
|