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 for parsing and analysing negative values #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tarunbhm
Copy link
Contributor

@tarunbhm tarunbhm commented May 29, 2024

Summary by CodeRabbit

  • New Features

    • Introduced support for handling negative numbers in formula parsing and analysis.
    • Added a new public function to initialize FormulaConfig with specific parameters.
  • Bug Fixes

    • Improved the handling and simplification of arithmetic expressions involving negative numbers.
  • Documentation

    • Clarified the purpose of the formula field in the README.
  • Tests

    • Added multiple tests to validate scenarios of multiplication, division, exponentiation, and negation operations.
  • Enhancements

    • Updated the parser and analyzer to better handle and simplify expressions with negative values.
    • Enhanced the LaTeX generator to format negative expressions correctly.

@tarunbhm tarunbhm linked an issue May 29, 2024 that may be closed by this pull request
Copy link

coderabbitai bot commented May 29, 2024

Walkthrough

The recent changes introduce a new module for simplifying expressions, handle negative values more effectively, and adjust the parser to support unsigned integers and negative expressions. Tests were added to ensure correct handling of various arithmetic operations. Updates to the FormulaConfig struct allow for more flexible initialization, and the latex_generator now properly formats negative expressions.

Changes

Files Change Summary
README.md Clarified the purpose of the formula field regarding numeric literals.
src/analyzer.rs Added simplify_expr module, updated analyze function, and added tests.
src/analyzer/analyze_rounding.rs Updated visit function to handle Expr::Negative.
src/analyzer/ast.rs Changed Number type to u32, added Negative variant to Expr.
src/analyzer/simplify_expr.rs Introduced functions to simplify expressions, handle negative values.
src/parser/arithmetic.lalrpop Adjusted grammar rules for handling negative numbers and identifiers.
src/parser/formula_config.rs Added new function for FormulaConfig struct initialization.
src/parser/input.rs Added Expr::Negative pattern to visit function.
src/printer/latex_generator.rs Added case for Expr::Negative in visit function, formatting output.

Poem

In the land of code where numbers dance,
We’ve given negatives a fairer chance.
Simplified expressions, clear and bright,
Parsing integers with all our might.
With formulas new and tests in sight,
Our code now shines, a beacon of light.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 8

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between d7cab44 and 80f6414.
Files selected for processing (9)
  • README.md (1 hunks)
  • src/analyzer.rs (2 hunks)
  • src/analyzer/analyze_rounding.rs (1 hunks)
  • src/analyzer/ast.rs (2 hunks)
  • src/analyzer/simplify_expr.rs (1 hunks)
  • src/parser/arithmetic.lalrpop (2 hunks)
  • src/parser/formula_config.rs (1 hunks)
  • src/parser/input.rs (1 hunks)
  • src/printer/latex_generator.rs (1 hunks)
Additional Context Used
LanguageTool (3)
README.md (3)

Near line 14: Possible missing article found.
Context: ...direction of the substracted element is inverse of the expected rounding) - `A * B => r...
Rule ID: AI_HYDRA_LEO_MISSING_THE


Near line 25: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...to generate user configuration file. - Run roundme analyze to analyze the config...
Rule ID: ENGLISH_WORD_REPEAT_BEGINNING_RULE


Near line 40: The past participle is required after “to be”, alternatively you could omit the “be”.
Context: ...``` - formula contains the formula to be analyze. All the numeric literals and identifie...
Rule ID: BE_VBP_IN

Markdownlint (10)
README.md (10)

23: Expected: 0 or 2; Actual: 1
Trailing spaces


24: Expected: 0 or 2; Actual: 1
Trailing spaces


5: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


34: null
Fenced code blocks should be surrounded by blank lines


39: null
Fenced code blocks should be surrounded by blank lines


50: null
Fenced code blocks should be surrounded by blank lines


55: null
Fenced code blocks should be surrounded by blank lines


6: null
Lists should be surrounded by blank lines


40: null
Lists should be surrounded by blank lines


15: null
Spaces inside code span elements

Additional comments not posted (9)
src/parser/arithmetic.lalrpop (1)

30-31: Ensure the new grammar rules for negative numbers and expressions are thoroughly tested.

Also applies to: 33-34, 35-35

src/parser/formula_config.rs (1)

30-34: The new constructor method for FormulaConfig enhances flexibility but ensure it is covered by unit tests.

Verification successful

The search results indicate multiple instances of FormulaConfig::new in src/analyzer.rs, but it is unclear if these are within unit tests. To ensure the constructor method is adequately tested, we need to verify if these instances are part of test functions.


The instances of FormulaConfig::new are indeed within test functions in src/analyzer.rs. This confirms that the new constructor method for FormulaConfig is covered by unit tests.

  • Instances of FormulaConfig::new are found within multiple test functions in src/analyzer.rs.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the new constructor method for `FormulaConfig`.

# Test: Search for unit tests related to the new constructor method. Expect: At least one test.
rg --type rust 'test.*new'

Length of output: 28



Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the new constructor method for `FormulaConfig`.

# Step 1: Locate the definition of the `FormulaConfig` struct.
echo "Searching for the definition of the 'FormulaConfig' struct..."
rg --type rust 'struct FormulaConfig'

# Step 2: Search for any instantiation of `FormulaConfig` in test files.
echo "Searching for instantiations of 'FormulaConfig' in test files..."
rg --type rust 'FormulaConfig::new'

Length of output: 1105



Script:

#!/bin/bash
# Description: Verify that the instances of `FormulaConfig::new` in `src/analyzer.rs` are within unit tests.

# Step 1: Search for test functions in `src/analyzer.rs`.
echo "Searching for test functions in 'src/analyzer.rs'..."
rg --type rust '#\[test\]' src/analyzer.rs

# Step 2: Extract lines around the instances of `FormulaConfig::new` to check if they are within test functions.
echo "Extracting context around 'FormulaConfig::new' instances in 'src/analyzer.rs'..."
rg --type rust -C 5 'FormulaConfig::new' src/analyzer.rs

Length of output: 3089

src/analyzer/simplify_expr.rs (2)

57-65: Assess the simplify_div function for correctness, especially in handling negative values and rounding.


47-55: Ensure the simplify_mul function handles rounding correctly and is covered by unit tests.

Verification successful

The search results indicate that there are multiple test functions related to Opcode::Mul and other operations in the src/analyzer.rs and src/analyzer/ast.rs files. These tests likely cover the functionality of the simplify_mul function indirectly through the expression simplification process.

  • src/analyzer.rs contains tests such as test_mul_up, test_mul_down, test_negative, and test_double_negative, which likely involve multiplication operations.
  • src/analyzer/ast.rs contains tests like test_display_opcode_mul_init, test_display_opcode_mul_up, test_display_opcode_mul_down, and test_display_opcode_mul_unknown, which test different rounding modes for multiplication.

Given this context, it appears that the simplify_mul function is covered by these tests.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the `simplify_mul` function.

# Test: Search for unit tests related to `simplify_mul`. Expect: At least one test.
rg --type rust 'test.*simplify_mul'

Length of output: 37



Script:

#!/bin/bash
# Locate the `simplify_mul` function definition to confirm its location.
rg --type rust 'fn simplify_mul'

# Search for any test files or modules that might contain relevant tests.
fd --type f --extension rs --exec rg 'simplify_mul' {}

Length of output: 384



Script:

#!/bin/bash
# Extract the context of `simplify_mul` usage within `src/analyzer/simplify_expr.rs`.
rg --context 5 'simplify_mul' src/analyzer/simplify_expr.rs

# Search for test files or modules that might contain relevant tests, focusing on `Opcode::Mul`.
fd --type f --extension rs --exec rg 'Opcode::Mul' {}

Length of output: 1554



Script:

#!/bin/bash
# Search for test functions that might cover the expression simplification process.
rg --type rust 'fn test' --context 5

# Look for any test files or modules that might contain relevant tests.
fd --type f --extension rs --exec rg 'test' {}

Length of output: 11002

src/analyzer.rs (2)

2-2: The integration of simplify_expr and its usage in analyze function are well-implemented. The changes are consistent with the PR's objectives to handle negative values.

Also applies to: 17-21


24-147: The test cases are comprehensive and effectively validate the handling of negative values and rounding directions. Good coverage of different scenarios.

src/analyzer/ast.rs (2)

10-10: Changing the Number variant from i32 to u32 is a thoughtful change, ensuring that negative numbers are handled explicitly using the Negative variant.


13-14: The addition of the Negative variant to the Expr enum and its handling in the Display implementation are well-executed. This change supports the explicit handling of negative values, aligning with the PR's objectives.

Also applies to: 68-68

src/printer/latex_generator.rs (1)

28-28: The handling of Expr::Negative in the LaTeX generator is correctly implemented, ensuring that negative values are properly formatted in the output documents.

Num: i32 = {
r"[0-9]+" =>? i32::from_str(<>)
Num: u32 = {
r"[0-9]+" =>? u32::from_str(<>)
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Add tests for parsing large numbers to ensure error handling is properly tested.

  • The parser code includes error handling for large numbers using u32::from_str and maps errors to a "number is too big" message.
  • However, there are no tests specifically for parsing large numbers.

Consider adding tests to cover this scenario.

Analysis chain

Validate the error handling for large numbers during parsing.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the error handling for large numbers is correctly implemented and tested.

# Test: Search for unit tests related to error handling for large numbers. Expect: At least one test.
rg --type rust 'test.*"number is too big"'

Length of output: 44



Script:

#!/bin/bash
# Description: Verify the presence of general number parsing tests and error handling for large numbers.

# Step 1: Search for general number parsing tests.
rg --type rust 'test.*number'

# Step 2: Examine the parser code for error handling related to large numbers.
rg --type rust 'u32::from_str' src/parser/arithmetic.lalrpop

# Step 3: Check for any relevant documentation or comments in the parser code.
rg --type rust 'large number' src/parser/arithmetic.lalrpop

Length of output: 241



Script:

#!/bin/bash
# Description: Further investigate the handling of number parsing and error handling for large numbers.

# Step 1: Extract and review the `test_display_number` function.
rg --type rust 'fn test_display_number' -A 20

# Step 2: Search for error handling mechanisms in the parser code where `u32::from_str` is used.
rg --type rust 'u32::from_str' -A 5 src/parser/arithmetic.lalrpop

# Step 3: Search for any additional tests related to number parsing.
rg --type rust 'test.*parse.*number'

Length of output: 1235


Tip

Codebase Verification

Ensure the error handling for identifier parsing is robust and tested. Currently, there are no unit tests covering the error message "number is too big" in the arithmetic parser.

  • Add unit tests to cover the error handling for the Num and Id rules in src/parser/arithmetic.lalrpop.
Analysis chain

Ensure the error handling for identifier parsing is robust and tested.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the error handling for identifier parsing is robust and tested.

# Test: Search for unit tests related to error handling for identifiers. Expect: At least one test.
rg --type rust 'test.*"number is too big"'

Length of output: 44



Script:

#!/bin/bash
# Description: Verify that the error handling for identifier parsing is robust and tested.

# Test: Search for unit tests related to parsing errors in general.
rg --type rust 'test.*parse_error'

# Test: Search for any error messages related to parsing numbers.
rg --type rust 'number is too big'

# Inspect the parser code for error handling mechanisms.
rg --type rust 'from_str' src/parser

Length of output: 223



Script:

#!/bin/bash
# Description: Verify that the error handling for identifier parsing in the arithmetic parser is robust and tested.

# Test: Search for unit tests related to the arithmetic parser.
rg --type rust 'test.*arithmetic'

# Inspect the arithmetic parser code for error handling mechanisms.
rg --type rust 'Result' src/parser/arithmetic.lalrpop

Length of output: 89



Script:

#!/bin/bash
# Description: Verify that the error handling for identifier parsing in the arithmetic parser is robust and tested.

# Test: Search for any other related tests or error handling mechanisms.
rg --type rust 'test.*' src/parser

# Check for comments or documentation related to error handling in the arithmetic parser.
rg --type rust 'error' src/parser/arithmetic.lalrpop

Length of output: 167



Script:

#!/bin/bash
# Description: Verify the context of the error message "number is too big" in the arithmetic parser and search for any indirect references or tests.

# Inspect the specific lines in src/parser/arithmetic.lalrpop where the error message "number is too big" is used.
rg --type rust 'number is too big' src/parser/arithmetic.lalrpop -A 5 -B 5

# Search the entire codebase for any indirect references or tests related to the error message "number is too big".
rg --type rust 'number is too big'

Length of output: 412

README.md Outdated
@@ -37,7 +37,7 @@ round_up: true
less_than_one: ["a * b"] # optional
greater_than_one: ["c"] # optional
```
- `formula` contains the formula to be analyze
- `formula` contains the formula to be analyze. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula
Copy link

Choose a reason for hiding this comment

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

Correct grammatical error in the documentation to enhance clarity.

- formula contains the formula to be analyze.
+ formula contains the formula to be analyzed.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- `formula` contains the formula to be analyze. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula
- `formula` contains the formula to be analyze. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula
+ `formula` contains the formula to be analyzed. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula

@@ -89,7 +89,7 @@ fn find_less_greater_than_one(expr: &Expr, formula_config: &mut FormulaConfig) {

fn visit(expr: &Expr, formula_config: &mut FormulaConfig) {
match expr {
Expr::Number(_) | Expr::Id(_) | Expr::Error => (),
Expr::Number(_) | Expr::Id(_) | Expr::Negative(_) | Expr::Error => (),
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

There are no unit tests specifically covering the Expr::Negative variant in the visit function. The search results did not show any test functions referencing Expr::Negative or the visit function in a way that would indicate coverage of this new variant.

  • Ensure that unit tests are added to cover the new handling of Expr::Negative in the visit function.
Analysis chain

Ensure the new handling of Expr::Negative in the visit function is covered by unit tests.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the new handling of `Expr::Negative` in the `visit` function.

# Test: Search for unit tests related to `Expr::Negative`. Expect: At least one test.
rg --type rust 'test.*Expr::Negative'

Length of output: 39



Script:

#!/bin/bash
# Description: Search for the `Expr::Negative` variant in the codebase to locate its usage.

# Search for the definition and usage of `Expr::Negative`.
rg 'Expr::Negative'

# Search for test files that might be testing the `visit` function or `Expr` variants.
rg --type rust 'fn visit'

# Search for any tests that might be indirectly testing `Expr::Negative`.
rg --type rust 'Negative'

Length of output: 2920



Script:

#!/bin/bash
# Description: Search for test functions in the codebase to locate any indirect tests for `Expr::Negative`.

# Search for test functions in the codebase.
rg --type rust '#\[test\]'

# Search for test modules in the codebase.
rg --type rust 'mod test'

Length of output: 930



Script:

#!/bin/bash
# Description: Search within the test functions for references to `Expr::Negative` or the `visit` function.

# Search for references to `Expr::Negative` within test functions.
rg --type rust 'Expr::Negative' src/analyzer.rs src/printer/latex_generator.rs src/analyzer/ast.rs

# Search for references to the `visit` function within test functions.
rg --type rust 'visit' src/analyzer.rs src/printer/latex_generator.rs src/analyzer/ast.rs

Length of output: 878

Comment on lines +5 to +23
fn is_negative_value(expr: Box<Expr>) -> (bool, Box<Expr>) {
match *expr {
Expr::Negative(ne) => match *ne {
Expr::Negative(internal) => is_negative_value(internal),
_ => (true, ne),
}
// Need to handle this case separately because in case of nested negatives the
// inner most operation will not be simplified
Expr::Op(l, o, r) => {
let simplified_internal_expr = simplify_sign(Box::new(Expr::Op(l, o, r)));

match *simplified_internal_expr {
Expr::Negative(ne) => (true, ne),
_ => (false, simplified_internal_expr),
}
},
_ => (false, expr),
}
}
Copy link

Choose a reason for hiding this comment

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

Review the logic in is_negative_value for potential simplification or optimization.

Consider simplifying the nested match statements to improve readability and potentially enhance performance.

Comment on lines +36 to +45
fn simplify_sub(lnv: (bool, Box<Expr>), rnv: (bool, Box<Expr>)) -> Box<Expr> {
let expr = match (lnv.0, rnv.0) {
(true, false) => Expr::Negative(Box::new(Expr::Op(lnv.1, Opcode::Add, rnv.1))),
(true, true) => Expr::Op(rnv.1, Opcode::Sub, lnv.1),
(false, true) => Expr::Op(lnv.1, Opcode::Add, rnv.1),
(false, false) => Expr::Op(lnv.1, Opcode::Sub, rnv.1),
};

Box::new(expr)
}
Copy link

Choose a reason for hiding this comment

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

Check the logic in simplify_sub for potential errors in handling negative values.

Consider reviewing the handling of negative values to ensure that the function behaves as expected under all conditions.

Comment on lines +25 to +34
fn simplify_add(lnv: (bool, Box<Expr>), rnv: (bool, Box<Expr>)) -> Box<Expr> {
let expr = match (lnv.0, rnv.0) {
(true, false) => Expr::Op(rnv.1, Opcode::Sub, lnv.1),
(true, true) => Expr::Negative(Box::new(Expr::Op(lnv.1, Opcode::Add, rnv.1))),
(false, true) => Expr::Op(lnv.1, Opcode::Sub, rnv.1),
(false, false) => Expr::Op(lnv.1, Opcode::Add, rnv.1),
};

Box::new(expr)
}
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The simplify_add function is referenced within the match statement handling different Opcode variants. However, there are no direct or indirect tests found for simplify_add in the test files.

  • Please add unit tests for the simplify_add function to ensure its correctness.
Analysis chain

Validate the correctness of the simplify_add function and ensure it is covered by unit tests.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the `simplify_add` function.

# Test: Search for unit tests related to `simplify_add`. Expect: At least one test.
rg --type rust 'test.*simplify_add'

Length of output: 37



Script:

#!/bin/bash
# Locate the definition of the `simplify_add` function to confirm its location.
ast-grep --lang rust --pattern 'fn simplify_add($_, $_) -> $_ { $$$ }'

# Search for any references to `simplify_add` in the codebase to identify potential indirect tests.
rg 'simplify_add'

Length of output: 1054



Script:

#!/bin/bash
# Locate the context in which `simplify_add` is referenced within `src/analyzer/simplify_expr.rs`.
rg 'simplify_add' src/analyzer/simplify_expr.rs -A 5 -B 5

# Search for any test files or modules that might contain tests for `simplify_add`.
rg 'simplify_add' --type rust --glob '*test*'

Length of output: 1001

fn visit(expr: &Expr, rounding_direction: bool, formula_config: &mut FormulaConfig) -> Result<()> {
match expr {
Expr::Number(_) | Expr::Id(_) | Expr::Error => (),
Expr::Negative(nexpr) => visit(nexpr, !rounding_direction, formula_config)?,
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search results indicate that there are no direct unit tests for Expr::Negative in the test files. This suggests that the new handling of Expr::Negative in the visit function is not explicitly covered by unit tests.

  • Location of visit function: src/analyzer/analyze_rounding.rs at line 79.
  • References to Expr::Negative: Found in multiple files, but no direct unit tests.

To ensure the new handling of Expr::Negative in the visit function is properly tested, consider adding unit tests that specifically cover this case.

  • Files to update:
    • Add unit tests in the relevant test files to cover Expr::Negative.
Analysis chain

Ensure the new handling of Expr::Negative in the visit function is covered by unit tests.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that there are unit tests covering the new handling of `Expr::Negative` in the `visit` function.

# Test: Search for unit tests related to `Expr::Negative`. Expect: At least one test.
rg --type rust 'test.*Expr::Negative'

Length of output: 39



Script:

#!/bin/bash
# Description: Verify the presence of unit tests covering the new handling of `Expr::Negative` in the `visit` function.

# Step 1: Locate the definition of the `visit` function.
ast-grep --lang rust --pattern 'fn visit($_, $_, $_) -> $_ { $$$ }'

# Step 2: Search for references to `Expr::Negative` in test files.
rg --type rust 'Expr::Negative'

Length of output: 2840

@tarunbhm tarunbhm requested a review from montyly May 29, 2024 13:57
@tarunbhm tarunbhm self-assigned this May 29, 2024
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 80f6414 and 81a8bf2.

Files selected for processing (3)
  • README.md (1 hunks)
  • src/analyzer.rs (2 hunks)
  • src/analyzer/simplify_expr.rs (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/analyzer/simplify_expr.rs
Additional context used
LanguageTool
README.md

[uncategorized] ~14-~14: Possible missing article found.
Context: ...direction of the substracted element is inverse of the expected rounding) - `A * B => r...


[style] ~25-~25: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...to generate user configuration file. - Run roundme analyze to analyze the config...


[grammar] ~40-~40: The past participle is required after “to be”, alternatively you could omit the “be”.
Context: ...``` - formula contains the formula to be analyze. All the numeric literals and identifie...

Markdownlint
README.md

23-23: Expected: 0 or 2; Actual: 1
Trailing spaces


24-24: Expected: 0 or 2; Actual: 1
Trailing spaces


5-5: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


34-34: null
Fenced code blocks should be surrounded by blank lines


39-39: null
Fenced code blocks should be surrounded by blank lines


50-50: null
Fenced code blocks should be surrounded by blank lines


55-55: null
Fenced code blocks should be surrounded by blank lines


6-6: null
Lists should be surrounded by blank lines


40-40: null
Lists should be surrounded by blank lines


15-15: null
Spaces inside code span elements

Additional comments not posted (2)
src/analyzer.rs (2)

2-2: The addition of the simplify_expr module is well-integrated.


23-23: The refactored analyze function correctly simplifies the AST before further processing.

README.md Outdated
@@ -37,7 +37,7 @@ round_up: true
less_than_one: ["a * b"] # optional
greater_than_one: ["c"] # optional
```
- `formula` contains the formula to be analyze
- `formula` contains the formula to be analyze. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula. The negative sign of the base in the exponential operations are ignored.
Copy link

Choose a reason for hiding this comment

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

Correct grammatical error in the documentation to enhance clarity.

- formula contains the formula to be analyze.
+ formula contains the formula to be analyzed.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
- `formula` contains the formula to be analyze. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula. The negative sign of the base in the exponential operations are ignored.
- `formula` contains the formula to be analyzed. All the numeric literals and identifiers are parsed as positive values, use the `-` sign to include negative values in the formula. The negative sign of the base in the exponential operations are ignored.

src/analyzer.rs Outdated
Comment on lines 17 to 19
println!("parsed : {ast}");
let simplified_ast = simplify_expr::simplify_sign(ast);
println!("simplified: {simplified_ast}");
Copy link

Choose a reason for hiding this comment

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

Adding debug print statements can be useful for development but should be removed or replaced with proper logging before merging to production.

-    println!("parsed    : {ast}");
-    println!("simplified: {simplified_ast}");
+    // Consider replacing with a logging framework
+    // log::debug!("parsed    : {ast}");
+    // log::debug!("simplified: {simplified_ast}");

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
println!("parsed : {ast}");
let simplified_ast = simplify_expr::simplify_sign(ast);
println!("simplified: {simplified_ast}");
// Consider replacing with a logging framework
// log::debug!("parsed : {ast}");
// log::debug!("simplified: {simplified_ast}");
let simplified_ast = simplify_expr::simplify_sign(ast);

@@ -13,7 +14,148 @@ pub fn analyze(formula_config: &mut FormulaConfig) -> anyhow::Result<Box<Expr>>
anyhow::anyhow!("Error occured while parsing the formula {}: {}", formula, e)
})?;

analyze_rounding::analyze(&ast, formula_config.round_up, formula_config)?;
println!("parsed : {ast}");
let simplified_ast = simplify_expr::simplify_sign(ast);
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Ensure that the simplify_sign function is thoroughly tested, especially since it manipulates the AST directly.

  • The simplify_sign function is not directly tested in the codebase.
  • The test module in src/analyzer.rs does not contain tests for simplify_sign.
Analysis chain

Ensure that the simplify_sign function is thoroughly tested, especially since it manipulates the AST directly.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Test: Search for the function usage in tests. Expect: At least one occurrence.
rg --type rust $'simplify_sign' ./src/analyzer/tests.rs

Length of output: 118



Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Test: Search for the function usage in all test files. Expect: At least one occurrence.
rg --type rust 'simplify_sign' --glob '*test*'

Length of output: 46



Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Step 1: Find the definition of `simplify_sign`.
ast-grep --lang rust --pattern 'fn simplify_sign($_) { $$$ }'

# Step 2: Search for the usage of `simplify_sign` across the repository.
rg 'simplify_sign'

Length of output: 525



Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Step 1: Find test files related to `src/analyzer/simplify_expr.rs` and `src/analyzer.rs`.
test_files=$(fd --type f --glob '*test*.rs')

# Step 2: Search for tests involving `simplify_sign` in the identified test files.
for test_file in $test_files; do
    echo "Searching in $test_file"
    rg 'simplify_sign' "$test_file"
done

Length of output: 47



Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Step 1: Search for test modules within `src/analyzer/simplify_expr.rs` and `src/analyzer.rs`.
rg '#\[cfg\(test\)\]' src/analyzer/simplify_expr.rs src/analyzer.rs

Length of output: 98



Script:

#!/bin/bash
# Description: Verify the presence of tests for `simplify_sign`.

# Step 1: Search for the usage of `simplify_sign` within the test module in `src/analyzer.rs`.
rg 'simplify_sign' src/analyzer.rs --context 10

Length of output: 718

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (2)
README.md (2)

Line range hint 23-24: Remove trailing spaces to comply with markdown best practices.

- Run `roundme init` to generate user configuration file. 
- Run `roundme analyze` to analyze the configuration file
+ Run `roundme init` to generate user configuration file.
+ Run `roundme analyze` to analyze the configuration file.

Line range hint 5-6: Ensure proper markdown formatting by adding necessary blank lines around headings, lists, and fenced code blocks.

+ 
## Features
- Recommends whether an arithmetic operation needs to round up or down
+ 
+ 
```yaml
formula: a * b / c 
round_up: true
less_than_one: ["a * b"] # optional
greater_than_one: ["c"] # optional
+ 
+ 
```bash
cargo install roundme
+ 
+ 

Also applies to: 34-34, 39-39, 50-50, 55-55

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 81a8bf2 and fca4cca.

Files selected for processing (2)
  • README.md (1 hunks)
  • src/analyzer.rs (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/analyzer.rs
Additional context used
LanguageTool
README.md

[uncategorized] ~14-~14: Possible missing article found.
Context: ...direction of the substracted element is inverse of the expected rounding) - `A * B => r...


[style] ~25-~25: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...to generate user configuration file. - Run roundme analyze to analyze the config...

Markdownlint
README.md

23-23: Expected: 0 or 2; Actual: 1
Trailing spaces


24-24: Expected: 0 or 2; Actual: 1
Trailing spaces


5-5: Expected: 1; Actual: 0; Below
Headings should be surrounded by blank lines


34-34: null
Fenced code blocks should be surrounded by blank lines


39-39: null
Fenced code blocks should be surrounded by blank lines


50-50: null
Fenced code blocks should be surrounded by blank lines


55-55: null
Fenced code blocks should be surrounded by blank lines


6-6: null
Lists should be surrounded by blank lines


40-40: null
Lists should be surrounded by blank lines


15-15: null
Spaces inside code span elements

Additional comments not posted (1)
README.md (1)

40-40: Clarification on handling negative values in formulas aligns well with the PR objectives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: support negative values
1 participant