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

Introduce negative numbers parsing #342

Merged

Conversation

AzraelSec
Copy link
Contributor

@AzraelSec AzraelSec commented May 30, 2024

Hello, Warden Team 👋🏻

I'd like to contribute to this project by opening a PR to make the shield DSL handle negative integers. I didn't comment on the related issue before picking it up since I briefly chatted with @Pitasi about this. Please forgive me if this was not the expected workflow to follow.

The current PR defines the abstraction to handle the negative sign as a prefix expression (with the highest priority between the operators) instead of changing the lexer to manually recognize negative integers. This way, you can write more complex expressions (e.g. --1, -(-1), -var [when/if constants/variables will be available]) without additional costs.

Also, I used a small REPL tool to manually test the feature (as an additional step to write unit tests), and I decided to include it as part of the current PR. I'm happy to drop it if you don't think this might be useful.

I'm happy to discuss the proposed changes. Let me know if anything does not match what you expect. Thanks!

Closes #328

Summary by CodeRabbit

  • New Features

    • Introduced a negative prefix operator to handle negative expressions and integers, enhancing arithmetic capabilities.
  • Bug Fixes

    • Improved handling and evaluation of prefix expressions in various components.
  • Tests

    • Added new test cases for evaluating boolean and arithmetic expressions involving negative numbers.
    • Added tests for parsing negative integers.
  • Documentation

    • Updated documentation to reflect changes in handling prefix expressions.
  • Refactor

    • Enhanced internal processing of prefix expressions for better performance and accuracy.

Copy link
Contributor

coderabbitai bot commented May 30, 2024

Walkthrough

Walkthrough

The changes introduce a new negative prefix operator in the Shield project to handle negative expressions and integers. This enhancement involves updating various components, including the parser, evaluator, and abstract syntax tree (AST), to correctly parse, evaluate, and process negative numbers. Additionally, new test cases ensure the functionality works as intended.

Changes

Files/Path(s) Change Summary
CHANGELOG.md Added a summary of the new negative prefix operator feature.
cmd/shield_repl/main.go Provided a REPL for the Shield language interpreter to read, evaluate, and print expressions.
proto/shield/ast/ast.proto Added a new PrefixExpression message type to handle prefix expressions.
shield/ast/ast.go Added functions NewPrefixExpression and UnwrapPrefixExpression for handling prefix expressions in the AST.
shield/ast/stringify.go Updated Stringify function to handle PrefixExpression and return formatted strings.
shield/internal/evaluator/evaluator.go Added evalPrefixExpression and evalPrefixSubOperator functions to evaluate prefix expressions.
shield/internal/evaluator/evaluator_test.go Added test cases for evaluating boolean and arithmetic expressions involving negative numbers.
shield/internal/metadata/metadata.go Updated processNode function to handle PrefixExpression by processing the right expression.
shield/internal/metadata/metadata_test.go Added test cases to TestExtractMetadata for handling negative numbers.
shield/internal/parser/parser.go Introduced parsing logic for prefix expressions and added a PREFIX constant and parsePrefixExpression method.
shield/internal/parser/parser_test.go Added TestNegativeInteger to test parsing of negative integers.
shield/internal/preprocess/preprocess.go Added preprocessPrefixExpression function to handle prefix expressions during preprocessing.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant REPL
    participant Parser
    participant Evaluator
    participant AST
    participant Metadata
    participant Preprocessor

    User->>REPL: Enter expression with negative number
    REPL->>Parser: Parse expression
    Parser->>AST: Create PrefixExpression node
    Parser->>Preprocessor: Preprocess PrefixExpression
    Preprocessor->>AST: Process right side of PrefixExpression
    AST->>Evaluator: Evaluate PrefixExpression
    Evaluator->>AST: Evaluate right side of PrefixExpression
    Evaluator->>Metadata: Update metadata
    Metadata->>Evaluator: Return updated metadata
    Evaluator->>REPL: Return evaluated result
    REPL->>User: Display result
Loading

Assessment against linked issues

Objective Addressed Explanation
Correctly parse negative numbers (#328)
Fix lexer to include - sign in integer tokens
Add unit tests for negative integer parsing

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
Contributor

@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 (3)
shield/internal/metadata/metadata.go (1)

36-37: The handling of PrefixExpression in metadata processing is correctly implemented. Consider adding a comment explaining why only the right side of the prefix expression is processed.

shield/internal/metadata/metadata_test.go (1)

78-82: The new test case for prefix expressions in metadata extraction is correctly implemented. Consider adding more comprehensive tests to cover complex nested expressions.

CHANGELOG.md (1)

Line range hint 58-58: Correct the indentation for unordered list items to comply with markdown best practices.

- * (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each others.
+ * (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each other.

Also applies to: 74-74, 86-87, 111-113, 118-118, 121-121, 132-132, 137-138

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a58e1ae and 7f8d5ab.

Files ignored due to path filters (2)
  • api/shield/ast/ast.pulsar.go is excluded by !api/**
  • shield/ast/ast.pb.go is excluded by !**/*.pb.go
Files selected for processing (11)
  • CHANGELOG.md (1 hunks)
  • cmd/shield_repl/main.go (1 hunks)
  • proto/shield/ast/ast.proto (2 hunks)
  • shield/ast/ast.go (2 hunks)
  • shield/internal/evaluator/evaluator.go (2 hunks)
  • shield/internal/evaluator/evaluator_test.go (2 hunks)
  • shield/internal/metadata/metadata.go (1 hunks)
  • shield/internal/metadata/metadata_test.go (1 hunks)
  • shield/internal/parser/parser.go (3 hunks)
  • shield/internal/parser/parser_test.go (1 hunks)
  • shield/internal/preprocess/preprocess.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • shield/internal/evaluator/evaluator_test.go
Additional context used
Path-based instructions (9)
cmd/shield_repl/main.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

shield/internal/metadata/metadata.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

shield/internal/preprocess/preprocess.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

shield/internal/metadata/metadata_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

shield/ast/ast.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

shield/internal/parser/parser_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

shield/internal/parser/parser.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

shield/internal/evaluator/evaluator.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

Markdownlint
CHANGELOG.md

58-58: Expected: 2; Actual: 4
Unordered list indentation


74-74: Expected: 2; Actual: 4
Unordered list indentation


86-86: Expected: 2; Actual: 4
Unordered list indentation


87-87: Expected: 2; Actual: 4
Unordered list indentation


111-111: Expected: 2; Actual: 4
Unordered list indentation


112-112: Expected: 2; Actual: 4
Unordered list indentation


113-113: Expected: 2; Actual: 4
Unordered list indentation


118-118: Expected: 2; Actual: 4
Unordered list indentation


121-121: Expected: 2; Actual: 4
Unordered list indentation


41-41: Expected: 1; Actual: 2
Multiple consecutive blank lines


132-132: Expected: 1; Actual: 2
Multiple consecutive blank lines


137-137: Expected: 1; Actual: 2
Multiple consecutive blank lines


138-138: Expected: 1; Actual: 3
Multiple consecutive blank lines


118-118: null
Bare URL used

Additional comments not posted (9)
cmd/shield_repl/main.go (1)

13-42: The REPL integration looks well-implemented and correctly utilizes the new parsing and evaluation functionalities.

shield/internal/preprocess/preprocess.go (1)

17-18: The addition of preprocessPrefixExpression correctly extends the preprocessing logic to handle prefix expressions. The implementation is consistent with the handling of other expression types.

Also applies to: 37-41

proto/shield/ast/ast.proto (1)

19-19: The definition of PrefixExpression in the protobuf file is correctly implemented and essential for the AST representation of the new prefix expressions.

Also applies to: 61-65

shield/ast/ast.go (1)

43-47: The implementation of NewPrefixExpression and UnwrapPrefixExpression is correct and essential for the manipulation of prefix expressions within the AST.

Also applies to: 96-101

shield/internal/parser/parser_test.go (1)

103-114: The test for negative integers appears comprehensive and correctly checks the parsing of a negative integer as a prefix expression.

shield/internal/parser/parser.go (2)

71-71: Correctly registered the prefix parse function for handling the subtraction token as a prefix operator.


188-198: The implementation of parsePrefixExpression correctly sets up the prefix expression and recursively parses the right-hand side expression, adhering to the precedence rules.

shield/internal/evaluator/evaluator.go (1)

41-42: The implementation of evalPrefixExpression and evalPrefixSubOperator correctly handles the evaluation of prefix expressions, including the special case for the negative operator.

Also applies to: 73-96

CHANGELOG.md (1)

51-51: The changelog entry for the addition of the negative prefix operator is clear and informative.

Copy link
Contributor

@Pitasi Pitasi left a comment

Choose a reason for hiding this comment

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

Looking good!

I'll ask you to do a couple more little things but all the work looks good:

  • add support for PrefixExpression to stringify.go
  • add more test cases to TestParses (see comment) so we can see that priority among operators (InfixExpression, CallExperssion) is correct and stays like that

require.True(t, ok, "expression is not *ast.PrefixExpression. got=%T", expression)
require.Equal(t, "-", negative.Operator)
}

func TestParser(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add some tests here to ensure that the priority of operators is the expected one?

@AzraelSec AzraelSec force-pushed the introduce-negative-prefix-operator branch from 7f8d5ab to dfa9c4d Compare May 30, 2024 16:08
Copy link
Contributor

@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 (1)
CHANGELOG.md (1)

Line range hint 58-58: There are several markdown lint issues related to incorrect list indentation and multiple consecutive blank lines. Correcting these will improve the readability and maintain the standard formatting of the document.

- * (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each others.
+ * (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each other.

Also applies to: 75-75, 87-88, 112-114, 119-122, 133-133, 138-139

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7f8d5ab and dfa9c4d.

Files ignored due to path filters (2)
  • api/shield/ast/ast.pulsar.go is excluded by !api/**
  • shield/ast/ast.pb.go is excluded by !**/*.pb.go
Files selected for processing (12)
  • CHANGELOG.md (1 hunks)
  • cmd/shield_repl/main.go (1 hunks)
  • proto/shield/ast/ast.proto (2 hunks)
  • shield/ast/ast.go (2 hunks)
  • shield/ast/stringify.go (1 hunks)
  • shield/internal/evaluator/evaluator.go (2 hunks)
  • shield/internal/evaluator/evaluator_test.go (2 hunks)
  • shield/internal/metadata/metadata.go (1 hunks)
  • shield/internal/metadata/metadata_test.go (1 hunks)
  • shield/internal/parser/parser.go (3 hunks)
  • shield/internal/parser/parser_test.go (2 hunks)
  • shield/internal/preprocess/preprocess.go (2 hunks)
Files skipped from review as they are similar to previous changes (10)
  • cmd/shield_repl/main.go
  • proto/shield/ast/ast.proto
  • shield/ast/ast.go
  • shield/internal/evaluator/evaluator.go
  • shield/internal/evaluator/evaluator_test.go
  • shield/internal/metadata/metadata.go
  • shield/internal/metadata/metadata_test.go
  • shield/internal/parser/parser.go
  • shield/internal/parser/parser_test.go
  • shield/internal/preprocess/preprocess.go
Additional context used
Path-based instructions (2)
shield/ast/stringify.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

LanguageTool
CHANGELOG.md

[uncategorized] ~46-~46: The grammatical number of this noun doesn’t look right. Consider replacing it.
Context: ...tocol/225) Fix shield to handle grouped expression * (shield) Add new operators: <, >,...


[uncategorized] ~111-~111: The grammatical number of this noun doesn’t look right. Consider replacing it.
Context: ...nd x/warden's store design to improve queries performance: * (x/intent) [#117](ht...


[uncategorized] ~114-~114: You might be missing the article “the” here.
Context: ...otocol/wardenprotocol/pull/122) Improve SpacesByOwner query by adding a new "ow...

Markdownlint
CHANGELOG.md

58-58: Expected: 2; Actual: 4
Unordered list indentation


75-75: Expected: 2; Actual: 4
Unordered list indentation


87-87: Expected: 2; Actual: 4
Unordered list indentation


88-88: Expected: 2; Actual: 4
Unordered list indentation


112-112: Expected: 2; Actual: 4
Unordered list indentation


113-113: Expected: 2; Actual: 4
Unordered list indentation


114-114: Expected: 2; Actual: 4
Unordered list indentation


119-119: Expected: 2; Actual: 4
Unordered list indentation


122-122: Expected: 2; Actual: 4
Unordered list indentation


41-41: Expected: 1; Actual: 2
Multiple consecutive blank lines


133-133: Expected: 1; Actual: 2
Multiple consecutive blank lines


138-138: Expected: 1; Actual: 2
Multiple consecutive blank lines


139-139: Expected: 1; Actual: 3
Multiple consecutive blank lines


119-119: null
Bare URL used

Additional comments not posted (2)
shield/ast/stringify.go (1)

27-28: The implementation for Expression_PrefixExpression in the Stringify function correctly formats prefix expressions. This aligns well with the PR's objective to handle negative numbers and other prefix operations.

CHANGELOG.md (1)

51-51: The changelog entry for the addition of the negative prefix operator is clear and appropriately categorized under "Consensus Breaking Changes". This ensures users are aware of the significant update in functionality.

Copy link
Contributor

@Pitasi Pitasi left a comment

Choose a reason for hiding this comment

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

thanks a lot ❤️

@Pitasi Pitasi merged commit 50bcc4d into warden-protocol:main May 30, 2024
5 checks passed
@AzraelSec
Copy link
Contributor Author

It's been a pleasure to contribute! 😄

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

Successfully merging this pull request may close these issues.

shield: Correctly parse negative numbers
2 participants