Add parsing for bitwise or/and, xor and bitshifts

This commit is contained in:
Sofia 2025-07-28 11:53:50 +03:00
parent 63c54ae4da
commit 516833d26f
7 changed files with 77 additions and 9 deletions

10
examples/or_bitwise.reid Normal file
View File

@ -0,0 +1,10 @@
// Arithmetic, function calls and imports!
fn main() -> bool {
let bwand = (0xff & 0xf0) >> 4;
let bwor = (0x0f | 0x00) << 4;
let bwxor = (0xf0 | 0x0f);
return (bwxor == 255) && ((value == 15) || false) && (bwor == 240);
}

7
examples/test.reid Normal file
View File

@ -0,0 +1,7 @@
// Arithmetic, function calls and imports!
fn main() -> bool {
return 5.0 > -1;
}

View File

@ -88,6 +88,10 @@ pub enum Token {
LessThan, LessThan,
/// `&` /// `&`
Et, Et,
/// `|`
Pipe,
/// `^`
Hat,
/// `!` /// `!`
Exclamation, Exclamation,
@ -174,6 +178,8 @@ impl ToString for Token {
Token::GreaterThan => String::from('>'), Token::GreaterThan => String::from('>'),
Token::LessThan => String::from('<'), Token::LessThan => String::from('<'),
Token::Et => String::from('&'), Token::Et => String::from('&'),
Token::Pipe => String::from('|'),
Token::Hat => String::from('^'),
Token::Exclamation => String::from('!'), Token::Exclamation => String::from('!'),
Token::ParenOpen => String::from('('), Token::ParenOpen => String::from('('),
Token::ParenClose => String::from(')'), Token::ParenClose => String::from(')'),
@ -411,6 +417,8 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
'>' => Token::GreaterThan, '>' => Token::GreaterThan,
'<' => Token::LessThan, '<' => Token::LessThan,
'&' => Token::Et, '&' => Token::Et,
'|' => Token::Pipe,
'^' => Token::Hat,
'!' => Token::Exclamation, '!' => Token::Exclamation,
'(' => Token::ParenOpen, '(' => Token::ParenOpen,
')' => Token::ParenClose, ')' => Token::ParenClose,

View File

@ -116,7 +116,14 @@ pub enum BinaryOperator {
Div, Div,
Mod, Mod,
BitshiftRight,
BitshiftLeft,
And, And,
Or,
Xor,
BWAnd,
BWOr,
LT, LT,
LE, LE,
GT, GT,
@ -126,7 +133,7 @@ pub enum BinaryOperator {
} }
impl BinaryOperator { impl BinaryOperator {
pub fn get_precedence(&self) -> i8 { pub fn get_precedence(&self) -> u8 {
use BinaryOperator::*; use BinaryOperator::*;
match &self { match &self {
Minus => 5, Minus => 5,
@ -134,13 +141,20 @@ impl BinaryOperator {
Mult => 15, Mult => 15,
Div => 20, Div => 20,
Mod => 20, Mod => 20,
And => 100, BWAnd => 90,
LT => 100, BWOr => 90,
LE => 100, BWXor => 90,
GT => 100, BitshiftLeft => 100,
GE => 100, BitshiftRight => 100,
EQ => 100, And => 150,
NE => 100, Or => 150,
Xor => 150,
LT => 150,
LE => 150,
GT => 150,
GE => 150,
EQ => 150,
NE => 150,
} }
} }
} }

View File

@ -488,6 +488,10 @@ impl Parse for BinaryOperator {
stream.next(); stream.next();
BinaryOperator::And BinaryOperator::And
} }
(Some(Token::Pipe), Some(Token::Pipe)) => {
stream.next();
BinaryOperator::Or
}
(Some(Token::LessThan), Some(Token::Equals)) => { (Some(Token::LessThan), Some(Token::Equals)) => {
stream.next(); stream.next();
BinaryOperator::LE BinaryOperator::LE
@ -504,6 +508,18 @@ impl Parse for BinaryOperator {
stream.next(); stream.next();
BinaryOperator::NE BinaryOperator::NE
} }
(Some(Token::GreaterThan), Some(Token::GreaterThan)) => {
stream.next();
BinaryOperator::BitshiftRight
}
(Some(Token::LessThan), Some(Token::LessThan)) => {
stream.next();
BinaryOperator::BitshiftLeft
}
(Some(Token::Hat), _) => BinaryOperator::Xor,
(Some(Token::Et), _) => BinaryOperator::BWAnd,
(Some(Token::Pipe), _) => BinaryOperator::BWOr,
(Some(Token::LessThan), _) => BinaryOperator::LT, (Some(Token::LessThan), _) => BinaryOperator::LT,
(Some(Token::GreaterThan), _) => BinaryOperator::GT, (Some(Token::GreaterThan), _) => BinaryOperator::GT,

View File

@ -455,6 +455,12 @@ impl ast::BinaryOperator {
ast::BinaryOperator::GE => mir::BinaryOperator::Cmp(mir::CmpOperator::GE), ast::BinaryOperator::GE => mir::BinaryOperator::Cmp(mir::CmpOperator::GE),
ast::BinaryOperator::EQ => mir::BinaryOperator::Cmp(mir::CmpOperator::EQ), ast::BinaryOperator::EQ => mir::BinaryOperator::Cmp(mir::CmpOperator::EQ),
ast::BinaryOperator::NE => mir::BinaryOperator::Cmp(mir::CmpOperator::NE), ast::BinaryOperator::NE => mir::BinaryOperator::Cmp(mir::CmpOperator::NE),
ast::BinaryOperator::BitshiftRight => todo!(),
ast::BinaryOperator::BitshiftLeft => todo!(),
ast::BinaryOperator::Or => todo!(),
ast::BinaryOperator::Xor => todo!(),
ast::BinaryOperator::BWAnd => todo!(),
ast::BinaryOperator::BWOr => todo!(),
} }
} }
} }

View File

@ -135,7 +135,14 @@ impl TypeKind {
| TypeKind::U16 | TypeKind::U16
| TypeKind::U32 | TypeKind::U32
| TypeKind::U64 | TypeKind::U64
| TypeKind::U128 => Ok(other.clone()), | TypeKind::U128
| TypeKind::F16
| TypeKind::F32B
| TypeKind::F32
| TypeKind::F64
| TypeKind::F80
| TypeKind::F128
| TypeKind::F128PPC => Ok(other.clone()),
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())), _ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
}, },
(TypeKind::Vague(Vague::Decimal), other) | (other, TypeKind::Vague(Vague::Decimal)) => match other { (TypeKind::Vague(Vague::Decimal), other) | (other, TypeKind::Vague(Vague::Decimal)) => match other {