-
Notifications
You must be signed in to change notification settings - Fork 101
feat(operators): Add AND, OR, and NOT bitwise operators #1504
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
Open
asauzeau
wants to merge
2
commits into
vectordotdev:main
Choose a base branch
from
asauzeau:bitwise_operators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Added `and`(&), `or`(^), and `not`(~) bitwise operators. | ||
|
|
||
| author: asauzeau |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
lib/tests/tests/expressions/arithmetic/bitwise_and/integer.vrl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # result: 5 | ||
|
|
||
| 21 & 45 |
3 changes: 3 additions & 0 deletions
3
lib/tests/tests/expressions/arithmetic/bitwise_or/integer.vrl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # result: 61 | ||
|
|
||
| 21 ^ 45 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # result: [-76, 40, -26] | ||
|
|
||
| [~75, ~~40, ~~~25] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| use std::fmt; | ||
|
|
||
| use crate::compiler::state::{TypeInfo, TypeState}; | ||
| use crate::compiler::{ | ||
| Context, Expression, Span, | ||
| expression::{Expr, Resolved}, | ||
| parser::Node, | ||
| value::{Kind, VrlValueArithmetic}, | ||
| }; | ||
| use crate::diagnostic::{DiagnosticMessage, Label, Note, Urls}; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq)] | ||
| pub struct BitwiseNot { | ||
| inner: Box<Expr>, | ||
| } | ||
|
|
||
| pub(crate) type Result = std::result::Result<BitwiseNot, Error>; | ||
|
|
||
| impl BitwiseNot { | ||
| /// Creates a new `BitwiseNot` expression. | ||
| /// | ||
| /// # Errors | ||
| /// Returns an `Error` if the provided expression's type is not integer or bytes. | ||
| /// | ||
| /// # Arguments | ||
| /// * `node` - The node representing the expression. | ||
| /// * `not_span` - The span of the `bitwise not` operator. | ||
| /// * `state` - The current type state. | ||
| /// | ||
| /// # Returns | ||
| /// A `Result` containing the new `BitwiseNot` expression or an error. | ||
| /// | ||
| /// # Errors | ||
| /// - `NonInteger`: If operand is not of type integer. | ||
| pub fn new(node: Node<Expr>, not_span: Span, state: &TypeState) -> Result { | ||
| let (expr_span, expr) = node.take(); | ||
| let type_def = expr.type_info(state).result; | ||
|
|
||
| if !type_def.is_integer() && !type_def.is_bytes() { | ||
| return Err(Error { | ||
| variant: ErrorVariant::NonInteger(type_def.into()), | ||
| not_span, | ||
| expr_span, | ||
| }); | ||
| } | ||
|
|
||
| Ok(Self { | ||
| inner: Box::new(expr), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl Expression for BitwiseNot { | ||
| fn resolve(&self, ctx: &mut Context) -> Resolved { | ||
| Ok(self.inner.resolve(ctx)?.try_bitwise_not()?) | ||
| } | ||
|
|
||
| fn type_info(&self, state: &TypeState) -> TypeInfo { | ||
| let mut state = state.clone(); | ||
| let mut inner_def = self.inner.apply_type_info(&mut state); | ||
| if inner_def.is_integer() { | ||
| inner_def = inner_def.infallible().with_kind(Kind::integer()); | ||
| } else { | ||
| inner_def = inner_def.fallible().with_kind(Kind::integer()); | ||
| } | ||
| TypeInfo::new(state, inner_def) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for BitwiseNot { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "~{}", self.inner) | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Error { | ||
| pub(crate) variant: ErrorVariant, | ||
|
|
||
| not_span: Span, | ||
| expr_span: Span, | ||
| } | ||
|
|
||
| #[derive(thiserror::Error, Debug)] | ||
| pub(crate) enum ErrorVariant { | ||
| #[error("non-integer bitwise negation")] | ||
| NonInteger(Kind), | ||
| } | ||
|
|
||
| impl fmt::Display for Error { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{:#}", self.variant) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for Error { | ||
| fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
| Some(&self.variant) | ||
| } | ||
| } | ||
|
|
||
| impl DiagnosticMessage for Error { | ||
| fn code(&self) -> usize { | ||
| use ErrorVariant::NonInteger; | ||
|
|
||
| match &self.variant { | ||
| NonInteger(..) => 670, | ||
| } | ||
| } | ||
|
|
||
| fn labels(&self) -> Vec<Label> { | ||
| use ErrorVariant::NonInteger; | ||
|
|
||
| match &self.variant { | ||
| NonInteger(kind) => vec![ | ||
| Label::primary("bitwise negation only works on integers", self.not_span), | ||
| Label::context( | ||
| format!("this expression resolves to {kind}"), | ||
| self.expr_span, | ||
| ), | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| fn notes(&self) -> Vec<Note> { | ||
| use ErrorVariant::NonInteger; | ||
|
|
||
| match &self.variant { | ||
| NonInteger(..) => { | ||
| vec![ | ||
| Note::CoerceValue, | ||
| Note::SeeDocs( | ||
| "type coercion".to_owned(), | ||
| Urls::func_docs("#coerce-functions"), | ||
| ), | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------------- | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use crate::compiler::{TypeDef, expression::Literal}; | ||
| use crate::test_type_def; | ||
|
|
||
| use super::*; | ||
|
|
||
| test_type_def![bitwise_not_integer { | ||
| expr: |_| BitwiseNot { | ||
| inner: Box::new(Literal::from(10).into()) | ||
| }, | ||
| want: TypeDef::integer().infallible(), | ||
| }]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice addition. Did you have a chance to run the fuzzer 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.
Yes I tested it for about ten minutes and didn't experience any crashes. I'll try to run it longer.