public static BetweenNode newBetweenNode(ParserRuleContext ctx, BaseNode value, BaseNode start, BaseNode end) { return new BetweenNode( ctx, value, start, end ); }
@Override public DirectCompilerResult visit(BetweenNode n) { DirectCompilerResult value = n.getValue().accept(this); DirectCompilerResult start = n.getStart().accept(this); DirectCompilerResult end = n.getEnd().accept(this); return DirectCompilerResult.of( Expressions.between( value.getExpression(), start.getExpression(), end.getExpression()), BuiltInType.BOOLEAN) .withFD(value) .withFD(start) .withFD(end); }
@Override public Object evaluate(EvaluationContext ctx) { boolean problem = false; if ( value == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "value")) ); problem = true; } if ( start == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "start")) ); problem = true; } if ( end == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.IS_NULL, "end")) ); problem = true; } if (problem) return null; Comparable e = (Comparable) o_e; if ( val == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.EVALUATED_TO_NULL, "value")) ); problem = true; } if ( s == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.EVALUATED_TO_NULL, "start")) ); problem = true; } if ( e == null ) { ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.EVALUATED_TO_NULL, "end")) ); problem = true; } if (problem) return null; ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "value", "start")) ); return null; ctx.notifyEvt( astEvent(Severity.ERROR, Msg.createMessage(Msg.X_TYPE_INCOMPATIBLE_WITH_Y_TYPE, "value", "end")) ); return null;
@Test public void testBetween() { String inputExpression = "x between 10+y and 3**z"; BaseNode between = parse( inputExpression ); assertThat( between, is( instanceOf( BetweenNode.class ) ) ); assertThat( between.getResultType(), is( BuiltInType.BOOLEAN ) ); assertThat( between.getText(), is( inputExpression ) ); BetweenNode btw = (BetweenNode) between; assertThat( btw.getValue(), is( instanceOf( NameRefNode.class ) ) ); assertThat( btw.getValue().getText(), is( "x" ) ); assertThat( btw.getStart(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( btw.getStart().getText(), is( "10+y" ) ); assertThat( btw.getEnd(), is( instanceOf( InfixOpNode.class ) ) ); assertThat( btw.getEnd().getText(), is( "3**z" ) ); }