Skip to content
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

Merged
merged 19 commits into from
Jun 9, 2021

Conversation

rpjayasekara
Copy link
Contributor

@rpjayasekara rpjayasekara commented May 20, 2021

Purpose

Support unary expressions for constants

Fixes #30019
Fixes #30320

Approach

Describe how you are implementing the solutions along with the design details.

Samples

const int MIN_VALUE = (-(10+5));
const int CUI3 = ~2;
const float CUF2 = +(10.0 + 2.0);
const boolean CUB = !(true);

Remarks

List any other known issues, related PRs, TODO items, or any other notes related to the PR.

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@rdhananjaya rdhananjaya added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label May 20, 2021
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.*;
Copy link
Contributor

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

Copy link
Contributor Author

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) {

Copy link
Contributor

@rdulmina rdulmina May 20, 2021

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
Copy link

codecov bot commented May 21, 2021

Codecov Report

Merging #30693 (dfcf859) into master (5a5b376) will increase coverage by 0.00%.
The diff coverage is 78.94%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
.../compiler/semantics/analyzer/ConstantAnalyzer.java 88.67% <60.00%> (-3.16%) ⬇️
...iler/semantics/analyzer/ConstantValueResolver.java 80.31% <80.00%> (+0.55%) ⬆️
...alang/compiler/semantics/analyzer/TypeChecker.java 86.62% <100.00%> (+<0.01%) ⬆️
.../java/io/ballerina/projects/PackageResolution.java 87.37% <0.00%> (-2.03%) ⬇️
...lang/compiler/semantics/analyzer/CodeAnalyzer.java 71.54% <0.00%> (+0.04%) ⬆️
.../compiler/bir/codegen/interop/JMethodResolver.java 78.04% <0.00%> (+0.27%) ⬆️
...runtime/internal/scheduling/WorkerDataChannel.java 87.26% <0.00%> (+2.54%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5a5b376...dfcf859. Read the comment docs.

@@ -114,6 +114,16 @@ function testBitwiseConstExpressions() {
assertEqual(ZERO_EXT_5, 0x0);
}

const int CUI1 = -(10);
Copy link
Member

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) {

Copy link
Contributor

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

Copy link
Contributor Author

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) :
Copy link
Member

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) {
Copy link
Member

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

Copy link
Member

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) {
Copy link
Member

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) {
Copy link
Member

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.

Copy link
Contributor Author

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

@MaryamZi MaryamZi requested review from rdulmina and rdhananjaya June 8, 2021 03:59
@MaryamZi MaryamZi added this to the Ballerina Swan Lake - Beta2 milestone Jun 8, 2021
Copy link
Contributor

@rdulmina rdulmina left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rdhananjaya rdhananjaya merged commit 50c6020 into ballerina-platform:master Jun 9, 2021
@prakanth97 prakanth97 mentioned this pull request Sep 13, 2022
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

~ in const expressions Unary expression can be a constant expression
4 participants