Rewrites the switch to an if expression. @param cc compilation context @return new or original expression @throws QueryException query exception
(final CompileContext cc)
| 168 | * @throws QueryException query exception |
| 169 | */ |
| 170 | private Expr toIf(final CompileContext cc) throws QueryException { |
| 171 | if(groups.length != 2) return this; |
| 172 | |
| 173 | final SeqType st = cond.seqType(); |
| 174 | final boolean string = st.type.isStringOrUntyped(), dec = st.type.instanceOf(BasicType.DECIMAL); |
| 175 | if(!st.one() || !(string || dec)) return this; |
| 176 | |
| 177 | final Expr[] exprs = groups[0].exprs; |
| 178 | for(int e = exprs.length - 1; e >= 1; e--) { |
| 179 | final SeqType est = exprs[e].seqType(); |
| 180 | if(!est.oneOrMore() || !( |
| 181 | string && est.type.isStringOrUntyped() || |
| 182 | dec && est.type.instanceOf(BasicType.DECIMAL) |
| 183 | )) return this; |
| 184 | } |
| 185 | |
| 186 | final Expr list = List.get(cc, groups[0].info, Arrays.copyOfRange(exprs, 1, exprs.length)); |
| 187 | final CmpG cmp = new CmpG(groups[0].info, cond, list, CmpOp.EQ); |
| 188 | return new If(info, cmp.optimize(cc), groups[0].rtrn(), groups[1].rtrn()).optimize(cc); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Iter iter(final QueryContext qc) throws QueryException { |
no test coverage detected