Parsing and lexing bitwise operators.
This commit is contained in:
parent
f0c84fe0c8
commit
b0660c9e5a
@ -23,6 +23,9 @@ pub enum BinaryOperation {
|
|||||||
Subtract,
|
Subtract,
|
||||||
LeftShift,
|
LeftShift,
|
||||||
RightShift,
|
RightShift,
|
||||||
|
BitwiseAnd,
|
||||||
|
BitwiseXor,
|
||||||
|
BitwiseOr,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BinaryExpression {
|
pub struct BinaryExpression {
|
||||||
@ -168,6 +171,9 @@ impl BinaryExpression {
|
|||||||
}
|
}
|
||||||
BinaryOperation::LeftShift => todo!(),
|
BinaryOperation::LeftShift => todo!(),
|
||||||
BinaryOperation::RightShift => todo!(),
|
BinaryOperation::RightShift => todo!(),
|
||||||
|
BinaryOperation::BitwiseAnd => todo!(),
|
||||||
|
BinaryOperation::BitwiseXor => todo!(),
|
||||||
|
BinaryOperation::BitwiseOr => todo!(),
|
||||||
}
|
}
|
||||||
|
|
||||||
diagnostics_result!(diagnostics)
|
diagnostics_result!(diagnostics)
|
||||||
@ -198,6 +204,9 @@ impl BinaryExpression {
|
|||||||
}
|
}
|
||||||
BinaryOperation::LeftShift => todo!(),
|
BinaryOperation::LeftShift => todo!(),
|
||||||
BinaryOperation::RightShift => todo!(),
|
BinaryOperation::RightShift => todo!(),
|
||||||
|
BinaryOperation::BitwiseAnd => todo!(),
|
||||||
|
BinaryOperation::BitwiseXor => todo!(),
|
||||||
|
BinaryOperation::BitwiseOr => todo!(),
|
||||||
};
|
};
|
||||||
IrOperation::Binary(ir_binary_operation)
|
IrOperation::Binary(ir_binary_operation)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,12 @@ impl<'a> Lexer<'a> {
|
|||||||
Token::new(self.position, self.position + 2, TokenKind::LeftShift)
|
Token::new(self.position, self.position + 2, TokenKind::LeftShift)
|
||||||
} else if chunk.starts_with(">>") {
|
} else if chunk.starts_with(">>") {
|
||||||
Token::new(self.position, self.position + 2, TokenKind::RightShift)
|
Token::new(self.position, self.position + 2, TokenKind::RightShift)
|
||||||
|
} else if chunk.starts_with("&") {
|
||||||
|
Token::new(self.position, self.position + 1, TokenKind::Ampersand)
|
||||||
|
} else if chunk.starts_with("^") {
|
||||||
|
Token::new(self.position, self.position + 1, TokenKind::Caret)
|
||||||
|
} else if chunk.starts_with("|") {
|
||||||
|
Token::new(self.position, self.position + 1, TokenKind::Bar)
|
||||||
} else if chunk.starts_with("=") {
|
} else if chunk.starts_with("=") {
|
||||||
Token::new(self.position, self.position + 1, TokenKind::Equals)
|
Token::new(self.position, self.position + 1, TokenKind::Equals)
|
||||||
} else if chunk.starts_with(",") {
|
} else if chunk.starts_with(",") {
|
||||||
|
|||||||
@ -662,7 +662,58 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
fn expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
||||||
self.shift_expression()
|
self.bitwise_or_expression()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bitwise_or_expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
||||||
|
let mut result = self.bitwise_xor_expression()?;
|
||||||
|
while self.current.is_some() && self.peek_current(TokenKind::Bar) {
|
||||||
|
self.advance(); // |
|
||||||
|
let rhs = self.bitwise_xor_expression()?;
|
||||||
|
let source_range =
|
||||||
|
SourceRange::new(result.source_range().start(), rhs.source_range().end());
|
||||||
|
result = Expression::Binary(BinaryExpression::new(
|
||||||
|
result,
|
||||||
|
rhs,
|
||||||
|
BinaryOperation::BitwiseOr,
|
||||||
|
source_range,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bitwise_xor_expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
||||||
|
let mut result = self.bitwise_and_expression()?;
|
||||||
|
while self.current.is_some() && self.peek_current(TokenKind::Caret) {
|
||||||
|
self.advance(); // ^
|
||||||
|
let rhs = self.bitwise_and_expression()?;
|
||||||
|
let source_range =
|
||||||
|
SourceRange::new(result.source_range().start(), rhs.source_range().end());
|
||||||
|
result = Expression::Binary(BinaryExpression::new(
|
||||||
|
result,
|
||||||
|
rhs,
|
||||||
|
BinaryOperation::BitwiseXor,
|
||||||
|
source_range,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bitwise_and_expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
||||||
|
let mut result = self.shift_expression()?;
|
||||||
|
while self.current.is_some() && self.peek_current(TokenKind::Ampersand) {
|
||||||
|
self.advance(); // &
|
||||||
|
let rhs = self.shift_expression()?;
|
||||||
|
let source_range =
|
||||||
|
SourceRange::new(result.source_range().start(), rhs.source_range().end());
|
||||||
|
result = Expression::Binary(BinaryExpression::new(
|
||||||
|
result,
|
||||||
|
rhs,
|
||||||
|
BinaryOperation::BitwiseAnd,
|
||||||
|
source_range,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shift_expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
fn shift_expression(&mut self) -> Result<Expression, Vec<Diagnostic>> {
|
||||||
@ -1067,6 +1118,32 @@ mod smoke_tests {
|
|||||||
fn simple_right_shift() {
|
fn simple_right_shift() {
|
||||||
smoke_test("fn main() 4 >> 1 end");
|
smoke_test("fn main() 4 >> 1 end");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_bitwise_and() {
|
||||||
|
smoke_test("fn main() 2 & 1 end");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_bitwise_xor() {
|
||||||
|
smoke_test("fn main() 1 ^ 2 end");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simple_bitwise_or() {
|
||||||
|
smoke_test("fn main() 1 | 2 end");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ops_left_to_right() {
|
||||||
|
smoke_test(
|
||||||
|
"
|
||||||
|
fn main()
|
||||||
|
1 | 2 ^ 3 & 4 << 5 >> 7 + 8 - 9 * 10 / 11 % 12
|
||||||
|
end
|
||||||
|
",
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -47,6 +47,9 @@ pub enum TokenKind {
|
|||||||
Modulo,
|
Modulo,
|
||||||
LeftShift,
|
LeftShift,
|
||||||
RightShift,
|
RightShift,
|
||||||
|
Ampersand,
|
||||||
|
Caret,
|
||||||
|
Bar,
|
||||||
Class,
|
Class,
|
||||||
Dot,
|
Dot,
|
||||||
SelfKw,
|
SelfKw,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user