public static SignedUnaryNode newSignedUnaryNode(ParserRuleContext ctx, BaseNode expr) { return new SignedUnaryNode( ctx, expr ); }
@Override public DirectCompilerResult visit(SignedUnaryNode n) { DirectCompilerResult result = n.getExpression().accept(this); if (n.getSign() == SignedUnaryNode.Sign.NEGATIVE) { return DirectCompilerResult.of( Expressions.negate(result.getExpression()), result.resultType, result.getFieldDeclarations()); } else { return result; } }
@Override public Object evaluate(EvaluationContext ctx) { if (expression == null) return null; BigDecimal result = EvalHelper.getBigDecimalOrNull( expression.evaluate( ctx ) ); if ( result == null ) { ctx.notifyEvt( astEvent(Severity.WARN, Msg.createMessage(Msg.NEGATING_A_NULL))); return null; } else if ( Sign.NEGATIVE == sign ) { return BigDecimal.valueOf( -1 ).multiply( result ); } else { return result; } }
@Test public void testNegativeFloatLiteral() { String inputExpression = "-10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }
@Test public void testPositiveIntegerLiteral() { String inputExpression = "+10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testNegativeIntegerLiteral() { String inputExpression = "-10"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.NEGATIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10" ) ); }
@Test public void testPositiveFloatLiteral() { String inputExpression = "+10.5"; BaseNode number = parse( inputExpression ); assertThat( number, is( instanceOf( SignedUnaryNode.class ) ) ); assertThat( number.getResultType(), is( BuiltInType.NUMBER ) ); assertLocation( inputExpression, number ); SignedUnaryNode sun = (SignedUnaryNode) number; assertThat( sun.getSign(), is( SignedUnaryNode.Sign.POSITIVE ) ); assertThat( sun.getExpression(), is( instanceOf( NumberNode.class ) ) ); assertThat( sun.getExpression().getText(), is( "10.5" ) ); }