-
Notifications
You must be signed in to change notification settings - Fork 755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support unary expressions for constants #30693
Add support unary expressions for constants #30693
Conversation
import org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangConstant; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangGroupExpr; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef; | ||
import org.wso2.ballerinalang.compiler.tree.expressions.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use single class imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add the import statements
@@ -127,6 +120,18 @@ public void visit(BLangGroupExpr expr) { | |||
analyzeExpr(expr.expression); | |||
} | |||
|
|||
@Override | |||
public void visit(BLangUnaryExpr unaryExpr) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't add new line here
Codecov Report
@@ Coverage Diff @@
## master #30693 +/- ##
=========================================
Coverage 69.14% 69.14%
- Complexity 38174 38193 +19
=========================================
Files 2902 2902
Lines 159717 159753 +36
Branches 20040 20045 +5
=========================================
+ Hits 110438 110469 +31
+ Misses 42782 42779 -3
- Partials 6497 6505 +8
Continue to review full report at Codecov.
|
@@ -114,6 +114,16 @@ function testBitwiseConstExpressions() { | |||
assertEqual(ZERO_EXT_5, 0x0); | |||
} | |||
|
|||
const int CUI1 = -(10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add decimal and float test cases too
@@ -215,6 +222,25 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons | |||
return new BLangConstantValue(null, this.currentConstSymbol.type); | |||
} | |||
|
|||
private BLangConstantValue calculateConstValue(BLangConstantValue value, OperatorKind kind) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the unwanted newline in other places as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will reformat all
exprType = OperatorKind.ADD.equals(unaryExpr.operator) ? checkExpr(unaryExpr.expr, env, expType) : | ||
boolean decimalNegation = OperatorKind.SUB.equals(unaryExpr.operator) && expType.tag == TypeTags.DECIMAL; | ||
boolean isAdd = OperatorKind.ADD.equals(unaryExpr.operator); | ||
exprType = (decimalNegation || isAdd) ? checkExpr(unaryExpr.expr, env, expType) : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add a comment saying what we do here
|
||
private BLangConstantValue calculateBooleanComplement(BLangConstantValue value) { | ||
Object result = null; | ||
switch (this.currentConstSymbol.type.tag) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a single case, let's use a if
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment for line:386
|
||
private BLangConstantValue calculateBooleanComplement(BLangConstantValue value) { | ||
Object result = null; | ||
switch (this.currentConstSymbol.type.tag) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment for line:386
@@ -215,6 +218,26 @@ private BLangConstantValue calculateConstValue(BLangConstantValue lhs, BLangCons | |||
return new BLangConstantValue(null, this.currentConstSymbol.type); | |||
} | |||
|
|||
private BLangConstantValue calculateConstValue(BLangConstantValue value, OperatorKind kind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is only called for unary expressions right, shall we change the name of the method to reflect that, something like evaluateUnaryOperator
or something. In fact I don't see any value addition by having this bit of logic as a separate method. I think we can just inline this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it only calls for unary expressions. Will change the method name to reflect that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
break; | ||
case TypeTags.DECIMAL: | ||
BigDecimal valDecimal = new BigDecimal(String.valueOf(value.value), MathContext.DECIMAL128); | ||
BigDecimal negDecimal = new BigDecimal(String.valueOf(-1), MathContext.DECIMAL128); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purpose
Fixes #30019
Fixes #30320
Approach
Samples
const int MIN_VALUE = (-(10+5));
const int CUI3 = ~2;
const float CUF2 = +(10.0 + 2.0);
const boolean CUB = !(true);
Remarks
Check List