Skip to content

Commit

Permalink
Implemented parsing differentiation between multiply/dereference and …
Browse files Browse the repository at this point in the history
…bitwise-and/address-of operators
  • Loading branch information
IsaacShelton committed Sep 12, 2024
1 parent e7febd0 commit 4b8fe61
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
18 changes: 11 additions & 7 deletions src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ impl<T: Text + Send> Lexer<T> {
'*' => {
if self.characters.eat('=') {
Has(TokenKind::MultiplyAssign.at(source))
} else {
} else if self.characters.peek().is_spacing() {
Has(TokenKind::Multiply.at(source))
} else {
Has(TokenKind::Dereference.at(source))
}
}
'/' => {
Expand Down Expand Up @@ -240,27 +242,29 @@ impl<T: Text + Send> Lexer<T> {
'~' => Has(TokenKind::BitComplement.at(source)),
'&' => {
if self.characters.eat('=') {
Has(TokenKind::AmpersandAssign.at(source))
Has(TokenKind::BitAndAssign.at(source))
} else if self.characters.eat('&') {
Has(TokenKind::And.at(source))
} else if self.characters.peek().is_spacing() {
Has(TokenKind::BitAnd.at(source))
} else {
Has(TokenKind::Ampersand.at(source))
Has(TokenKind::AddressOf.at(source))
}
}
'|' => {
if self.characters.eat('=') {
Has(TokenKind::PipeAssign.at(source))
Has(TokenKind::BitOrAssign.at(source))
} else if self.characters.eat('|') {
Has(TokenKind::Or.at(source))
} else {
Has(TokenKind::Pipe.at(source))
Has(TokenKind::BitOr.at(source))
}
}
'^' => {
if self.characters.eat('=') {
Has(TokenKind::CaretAssign.at(source))
Has(TokenKind::BitXorAssign.at(source))
} else {
Has(TokenKind::Caret.at(source))
Has(TokenKind::BitXor.at(source))
}
}
',' => Has(TokenKind::Comma.at(source)),
Expand Down
6 changes: 3 additions & 3 deletions src/parser/parse_expr/primary/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
TokenKind::LessThanEq => BasicBinaryOperator::LessThanEq.into(),
TokenKind::GreaterThan => BasicBinaryOperator::GreaterThan.into(),
TokenKind::GreaterThanEq => BasicBinaryOperator::GreaterThanEq.into(),
TokenKind::Ampersand => BasicBinaryOperator::BitwiseAnd.into(),
TokenKind::Pipe => BasicBinaryOperator::BitwiseOr.into(),
TokenKind::Caret => BasicBinaryOperator::BitwiseXor.into(),
TokenKind::BitAnd => BasicBinaryOperator::BitwiseAnd.into(),
TokenKind::BitOr => BasicBinaryOperator::BitwiseOr.into(),
TokenKind::BitXor => BasicBinaryOperator::BitwiseXor.into(),
TokenKind::LeftShift => BasicBinaryOperator::LeftShift.into(),
TokenKind::LogicalLeftShift => BasicBinaryOperator::LogicalLeftShift.into(),
TokenKind::RightShift => BasicBinaryOperator::RightShift.into(),
Expand Down
6 changes: 3 additions & 3 deletions src/parser/parse_stmt/parse_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
TokenKind::MultiplyAssign => Some(BasicBinaryOperator::Multiply),
TokenKind::DivideAssign => Some(BasicBinaryOperator::Divide),
TokenKind::ModulusAssign => Some(BasicBinaryOperator::Modulus),
TokenKind::AmpersandAssign => Some(BasicBinaryOperator::BitwiseAnd),
TokenKind::PipeAssign => Some(BasicBinaryOperator::BitwiseOr),
TokenKind::CaretAssign => Some(BasicBinaryOperator::BitwiseXor),
TokenKind::BitAndAssign => Some(BasicBinaryOperator::BitwiseAnd),
TokenKind::BitOrAssign => Some(BasicBinaryOperator::BitwiseOr),
TokenKind::BitXorAssign => Some(BasicBinaryOperator::BitwiseXor),
TokenKind::LeftShiftAssign => Some(BasicBinaryOperator::LeftShift),
TokenKind::RightShiftAssign => Some(BasicBinaryOperator::RightShift),
TokenKind::LogicalLeftShiftAssign => Some(BasicBinaryOperator::LogicalLeftShift),
Expand Down
5 changes: 5 additions & 0 deletions src/text/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl Character {
matches!(self, Character::At('+' | '-', _))
}

#[inline]
pub fn is_spacing(&self) -> bool {
matches!(self, Character::At(' ' | '\n' | '\t', _))
}

#[inline]
pub fn source(&self) -> Source {
match self {
Expand Down
48 changes: 27 additions & 21 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,27 @@ pub enum TokenKind {
Colon,
Hash,
Ellipsis,
AddressOf,
Dereference,
DeclareAssign,
Assign,
AddAssign,
SubtractAssign,
MultiplyAssign,
DivideAssign,
ModulusAssign,
AmpersandAssign,
PipeAssign,
CaretAssign,
BitAndAssign,
BitOrAssign,
BitXorAssign,
LeftShiftAssign,
RightShiftAssign,
LogicalLeftShiftAssign,
LogicalRightShiftAssign,
And,
Or,
Ampersand,
Pipe,
Caret,
BitAnd,
BitOr,
BitXor,
LeftShift,
RightShift,
LogicalLeftShift,
Expand Down Expand Up @@ -177,25 +179,27 @@ impl Display for TokenKind {
TokenKind::Colon => f.write_str("':'"),
TokenKind::Hash => f.write_str("'#'"),
TokenKind::Ellipsis => f.write_str("'...'"),
TokenKind::AddressOf => f.write_str("(address of) '&'"),
TokenKind::Dereference => f.write_str("(dereference) '*'"),
TokenKind::DeclareAssign => f.write_str("':='"),
TokenKind::Assign => f.write_str("'='"),
TokenKind::AddAssign => f.write_str("'+='"),
TokenKind::SubtractAssign => f.write_str("'-='"),
TokenKind::MultiplyAssign => f.write_str("'*='"),
TokenKind::DivideAssign => f.write_str("'/='"),
TokenKind::ModulusAssign => f.write_str("'%='"),
TokenKind::AmpersandAssign => f.write_str("'&='"),
TokenKind::PipeAssign => f.write_str("'|='"),
TokenKind::CaretAssign => f.write_str("'^='"),
TokenKind::BitAndAssign => f.write_str("'&='"),
TokenKind::BitOrAssign => f.write_str("'|='"),
TokenKind::BitXorAssign => f.write_str("'^='"),
TokenKind::LeftShiftAssign => f.write_str("'<<='"),
TokenKind::RightShiftAssign => f.write_str("'>>='"),
TokenKind::LogicalLeftShiftAssign => f.write_str("'<<<='"),
TokenKind::LogicalRightShiftAssign => f.write_str("'>>>='"),
TokenKind::And => f.write_str("'&&'"),
TokenKind::Or => f.write_str("'||'"),
TokenKind::Ampersand => f.write_str("'&'"),
TokenKind::Pipe => f.write_str("'|'"),
TokenKind::Caret => f.write_str("'^'"),
TokenKind::BitAnd => f.write_str("'&'"),
TokenKind::BitOr => f.write_str("'|'"),
TokenKind::BitXor => f.write_str("'^'"),
TokenKind::LeftShift => f.write_str("'<<'"),
TokenKind::RightShift => f.write_str("'>>'"),
TokenKind::LogicalLeftShift => f.write_str("'<<<'"),
Expand Down Expand Up @@ -234,9 +238,9 @@ impl TokenKind {
TokenKind::GreaterThanEq => 9,
TokenKind::Equals => 8,
TokenKind::NotEquals => 8,
TokenKind::Ampersand => 7,
TokenKind::Caret => 6,
TokenKind::Pipe => 5,
TokenKind::BitAnd => 7,
TokenKind::BitXor => 6,
TokenKind::BitOr => 5,
TokenKind::And => 4,
TokenKind::Or => 3,
TokenKind::DeclareAssign
Expand All @@ -245,9 +249,9 @@ impl TokenKind {
| TokenKind::MultiplyAssign
| TokenKind::DivideAssign
| TokenKind::ModulusAssign
| TokenKind::AmpersandAssign
| TokenKind::PipeAssign
| TokenKind::CaretAssign
| TokenKind::BitAndAssign
| TokenKind::BitOrAssign
| TokenKind::BitXorAssign
| TokenKind::LeftShiftAssign
| TokenKind::RightShiftAssign
| TokenKind::LogicalLeftShiftAssign
Expand Down Expand Up @@ -286,6 +290,8 @@ impl TokenKind {
| TokenKind::Colon
| TokenKind::Hash
| TokenKind::Ellipsis
| TokenKind::AddressOf
| TokenKind::Dereference
| TokenKind::Namespace
| TokenKind::Extend
| TokenKind::FatArrow => 0,
Expand All @@ -300,9 +306,9 @@ impl TokenKind {
| Self::MultiplyAssign
| Self::DivideAssign
| Self::ModulusAssign
| Self::AmpersandAssign
| Self::PipeAssign
| Self::CaretAssign
| Self::BitAndAssign
| Self::BitOrAssign
| Self::BitXorAssign
| Self::LeftShiftAssign
| Self::RightShiftAssign
| Self::LogicalLeftShiftAssign
Expand Down

0 comments on commit 4b8fe61

Please sign in to comment.