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

feat(shield): add arithmetic operations #330

Merged
merged 1 commit into from
May 30, 2024
Merged

Conversation

optifat
Copy link
Contributor

@optifat optifat commented May 29, 2024

Add basic arithmetic operations (closes #260)

No dealing with overflows and parsing negative numbers

Summary by CodeRabbit

  • New Features

    • Introduced support for basic arithmetic operations (+, -, *, /) on integers.
  • Bug Fixes

    • Enhanced lexer and parser to correctly handle new arithmetic operators.
  • Tests

    • Added comprehensive tests for arithmetic expressions, ensuring correct operator precedence and functionality.
  • Documentation

    • Updated CHANGELOG to reflect new arithmetic capabilities and other recent enhancements.

Copy link
Contributor

coderabbitai bot commented May 29, 2024

Walkthrough

Walkthrough

The changes introduce basic arithmetic operators (+, -, *, /) to the Shield language, enabling it to perform integer arithmetic operations. This includes updates to the lexer, parser, evaluator, and corresponding test files to support these new operators, ensuring they are correctly tokenized, parsed, and evaluated.

Changes

Files Change Summary
proto/shield/token/token.proto Added new values ADD, SUB, MUL, DIV, TRUE, and FALSE to the Type enum.
shield/internal/evaluator/evaluator.go Enhanced evalInfixExpression to handle arithmetic operations for integer objects.
shield/internal/evaluator/evaluator_test.go Added TestArithmeticInteger to test arithmetic expressions including precedence handling.
shield/internal/lexer/lexer.go Extended NextToken to tokenize arithmetic operators +, -, *, /.
shield/internal/lexer/lexer_test.go Updated TestNextToken to include arithmetic operators in the input string.
shield/internal/parser/parser.go Introduced new token types and precedences for arithmetic operators, and registered infix parsing for these operators.
shield/internal/parser/parser_test.go Updated TestParser to include test cases for arithmetic expressions, ensuring correct operator precedence.
CHANGELOG.md Documented the addition of basic integer arithmetic operators to the Shield language.

Assessment against linked issues

Objective Addressed Explanation
Add basic math operators +, -, *, / to the Shield language (#260)
Avoid dealing with floating numbers and use integers for arithmetic operations (#260)

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

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between ac4b933 and b12508b.
Files ignored due to path filters (2)
  • api/shield/token/token.pulsar.go is excluded by !api/**
  • shield/token/token.pb.go is excluded by !**/*.pb.go
Files selected for processing (7)
  • proto/shield/token/token.proto (1 hunks)
  • shield/internal/evaluator/evaluator.go (1 hunks)
  • shield/internal/evaluator/evaluator_test.go (1 hunks)
  • shield/internal/lexer/lexer.go (1 hunks)
  • shield/internal/lexer/lexer_test.go (2 hunks)
  • shield/internal/parser/parser.go (2 hunks)
  • shield/internal/parser/parser_test.go (1 hunks)
Additional Context Used
Path-based Instructions (6)
shield/internal/lexer/lexer_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/evaluator/evaluator_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/lexer/lexer.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/evaluator/evaluator.go (1)

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

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

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

Additional comments not posted (10)
proto/shield/token/token.proto (1)

27-30: The addition of arithmetic operation types in the enum is correctly implemented.

shield/internal/lexer/lexer_test.go (2)

10-10: The test input string has been correctly updated to include arithmetic operators.


30-33: The test cases for the new arithmetic tokens are correctly added and follow the expected structure.

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

42-61: The new test function TestArithmeticInteger is well-structured and covers a variety of arithmetic operations, ensuring that the basic arithmetic functionality works as expected.

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

52-59: The NextToken method has been correctly extended to handle the new arithmetic operators. Each case correctly creates a new token with the appropriate type.

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

137-152: The test cases for arithmetic operations in TestParser are correctly structured to ensure the parser handles operator precedence as expected.

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

88-96: The evalInfixExpression function has been correctly updated to handle arithmetic operations between integers. Each case correctly computes the result based on the operator.

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

17-18: The addition of ADD_SUB and MUL_DIV constants is correctly implemented to handle the precedence of new arithmetic operations.


25-28: The update to the precedences map correctly associates the new arithmetic tokens with their respective precedence levels.


64-67: The registration of infix parse functions for arithmetic operations in the New method is correctly implemented.

@optifat
Copy link
Contributor Author

optifat commented May 29, 2024

I'll merge comparison operators from main branch and add some new combined tests like 2 + 2 <= 5

This comment has been minimized.

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

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between b12508b and 51dabed.
Files ignored due to path filters (2)
  • api/shield/token/token.pulsar.go is excluded by !api/**
  • shield/token/token.pb.go is excluded by !**/*.pb.go
Files selected for processing (7)
  • proto/shield/token/token.proto (1 hunks)
  • shield/internal/evaluator/evaluator.go (1 hunks)
  • shield/internal/evaluator/evaluator_test.go (2 hunks)
  • shield/internal/lexer/lexer.go (1 hunks)
  • shield/internal/lexer/lexer_test.go (2 hunks)
  • shield/internal/parser/parser.go (3 hunks)
  • shield/internal/parser/parser_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (7)
  • proto/shield/token/token.proto
  • shield/internal/evaluator/evaluator.go
  • shield/internal/evaluator/evaluator_test.go
  • shield/internal/lexer/lexer.go
  • shield/internal/lexer/lexer_test.go
  • shield/internal/parser/parser.go
  • shield/internal/parser/parser_test.go

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: 1

Outside diff range and nitpick comments (4)
CHANGELOG.md (4)

Line range hint 106-106: Correct grammatical number for better readability.

The phrase "queries performance" should be "query performance" to correctly match the singular form of "performance".

- ...nd `x/warden`'s store design to improve queries performance:     * (x/intent) [#117](ht...
+ ...nd `x/warden`'s store design to improve query performance:     * (x/intent) [#117](ht...

Line range hint 109-109: Add missing article for grammatical correctness.

Consider adding "the" before "owner" to complete the phrase.

- ...otocol/wardenprotocol/pull/122) Improve `SpacesByOwner` query by adding a new "ow...
+ ...otocol/wardenprotocol/pull/122) Improve `SpacesByOwner` query by adding the new "ow...

Line range hint 57-57: Correct indentation for list items.

The indentation for list items should be consistent throughout the document. Here, it should be reduced to 2 spaces instead of 4.

-     * (x/intent) [#117](https://github.com/warden-protocol/wardenprotocol/pull/117) Pin the intent definition when an Action is created instead of just referencing it.
+   * (x/intent) [#117](https://github.com/warden-protocol/wardenprotocol/pull/117) Pin the intent definition when an Action is created instead of just referencing it.

Also applies to: 70-70, 82-82, 83-83, 107-107, 108-108, 109-109, 114-114, 117-117


Line range hint 41-41: Remove excessive blank lines for cleaner formatting.

Multiple consecutive blank lines should be reduced to a single blank line to maintain a clean and professional format in the document.

- 
-
+ 

Also applies to: 128-128, 133-133, 134-134

Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 51dabed and 7790b64.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional Context Used
LanguageTool (2)
CHANGELOG.md (2)

Near line 106: 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...
Rule ID: AI_EN_LECTOR_REPLACEMENT_NOUN_NUMBER


Near line 109: You might be missing the article “the” here.
Context: ...otocol/wardenprotocol/pull/122) Improve SpacesByOwner query by adding a new "ow...
Rule ID: AI_EN_LECTOR_MISSING_DETERMINER_THE

Markdownlint (14)
CHANGELOG.md (14)

57: Expected: 2; Actual: 4
Unordered list indentation


70: Expected: 2; Actual: 4
Unordered list indentation


82: Expected: 2; Actual: 4
Unordered list indentation


83: Expected: 2; Actual: 4
Unordered list indentation


107: Expected: 2; Actual: 4
Unordered list indentation


108: Expected: 2; Actual: 4
Unordered list indentation


109: Expected: 2; Actual: 4
Unordered list indentation


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


117: Expected: 2; Actual: 4
Unordered list indentation


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


128: Expected: 1; Actual: 2
Multiple consecutive blank lines


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


134: Expected: 1; Actual: 3
Multiple consecutive blank lines


114: null
Bare URL used

Path-based Instructions (1)
CHANGELOG.md (1)

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

@@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* (shield) Add new operators: `<`, `>`, `<=`, `>=`, `==`, `!=` for comparing integers to each others.
* (shield) Add support for `string` objects
* (shield) Add `contains(elem, array)` builtin function for checking if `elem` is contained in the `array`
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure consistent punctuation in list items.

Consider adding a period at the end of the entry for consistency with other entries in the list.

- * (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
+ * (shield) Add `+`, `-`, `*`, `/` math  operators for basic integer arithmetic.

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
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
* (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic.

@optifat optifat changed the title shield: add arithmetic operations feat(shield): add arithmetic operations May 29, 2024
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.

can you squash all these commit into a single one? thanks

add tests for arithmetic and comparison combinations

update CHANGELOG
@optifat optifat force-pushed the shield-math-operators branch from 7790b64 to 9b8a9bc Compare May 30, 2024 12:50
@optifat
Copy link
Contributor Author

optifat commented May 30, 2024

can you squash all these commit into a single one? thanks

done as requested

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

Line range hint 57-57: Correct the indentation of unordered list items.

-    * (wardend) analyzers smart contract: a way for 3rd party builders to provide metadata to shield's intents during new signature requests
+  * (wardend) analyzers smart contract: a way for 3rd party builders to provide metadata to shield's intents during new signature requests
-    * can be used to pass an Ethereum unsigned transaction, and will return the correct DataForSigning
+  * can be used to pass an Ethereum unsigned transaction, and will return the correct DataForSigning

Also applies to: 70-70, 82-82, 83-83, 107-107, 108-108, 109-109, 114-114, 117-117


Line range hint 41-41: Reduce multiple consecutive blank lines to a single blank line.

- 

+

Also applies to: 128-128, 133-133, 134-134


Line range hint 114-114: Avoid using bare URLs. Provide a descriptive link text.

- * (cosmwasm) [#156](https://github.com/warden-protocol/wardenprotocol/pull/156) Add support for smart contracts by integrating CosmWasm
+ * (cosmwasm) [#156](Add support for smart contracts by integrating CosmWasm)[https://github.com/warden-protocol/wardenprotocol/pull/156]
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7790b64 and 9b8a9bc.

Files ignored due to path filters (3)
  • api/shield/token/token.pulsar.go is excluded by !api/**
  • shield/token/token.pb.go is excluded by !**/*.pb.go
  • warden/x/warden/types/v1beta2/signature.pb.go is excluded by !**/*.pb.go
Files selected for processing (8)
  • CHANGELOG.md (1 hunks)
  • proto/shield/token/token.proto (1 hunks)
  • shield/internal/evaluator/evaluator.go (1 hunks)
  • shield/internal/evaluator/evaluator_test.go (2 hunks)
  • shield/internal/lexer/lexer.go (1 hunks)
  • shield/internal/lexer/lexer_test.go (2 hunks)
  • shield/internal/parser/parser.go (3 hunks)
  • shield/internal/parser/parser_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (7)
  • proto/shield/token/token.proto
  • shield/internal/evaluator/evaluator.go
  • shield/internal/evaluator/evaluator_test.go
  • shield/internal/lexer/lexer.go
  • shield/internal/lexer/lexer_test.go
  • shield/internal/parser/parser.go
  • shield/internal/parser/parser_test.go
Additional context used
Path-based instructions (1)
CHANGELOG.md (1)

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

Markdownlint
CHANGELOG.md

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


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


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


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


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


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


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


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


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


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


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


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


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


114-114: null
Bare URL used

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

50-50: Ensure consistent punctuation in list items.

Consider adding a period at the end of the entry for consistency with other entries in the list.

- * (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic
+ * (shield) Add `+`, `-`, `*`, `/` math operators for basic integer arithmetic.

@Pitasi Pitasi merged commit 1e8c8e1 into main May 30, 2024
8 of 9 checks passed
@Pitasi Pitasi deleted the shield-math-operators branch May 30, 2024 13:35
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.

Add math operators to Shield language
2 participants