Skip to content

Commit

Permalink
Parse unary reference and dereference operators
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed Apr 22, 2023
1 parent 4129759 commit 32bf1b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/dreammaker/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum UnaryOp {
PostIncr,
PreDecr,
PostDecr,
Reference,
Dereference,
}

impl UnaryOp {
Expand All @@ -49,6 +51,8 @@ impl UnaryOp {
PostIncr => write!(f, "{}++", self.expr),
PreDecr => write!(f, "--{}", self.expr),
PostDecr => write!(f, "{}--", self.expr),
Reference => write!(f, "&{}", self.expr),
Dereference => write!(f, "*{}", self.expr),
}
}
}
Expand All @@ -65,6 +69,8 @@ impl UnaryOp {
BitNot => "~",
PreIncr | PostIncr => "++",
PreDecr | PostDecr => "--",
Reference => "&",
Dereference => "*",
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/dreammaker/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,8 @@ impl<'ctx, 'an, 'inp> Parser<'ctx, 'an, 'inp> {
Token::Punct(Punctuation::BitNot) => unary_ops.push(Spanned::new(self.location, Follow::Unary(UnaryOp::BitNot))),
Token::Punct(Punctuation::PlusPlus) => unary_ops.push(Spanned::new(self.location, Follow::Unary(UnaryOp::PreIncr))),
Token::Punct(Punctuation::MinusMinus) => unary_ops.push(Spanned::new(self.location, Follow::Unary(UnaryOp::PreDecr))),
Token::Punct(Punctuation::BitAnd) => unary_ops.push(Spanned::new(self.location, Follow::Unary(UnaryOp::Reference))),
Token::Punct(Punctuation::Mul) => unary_ops.push(Spanned::new(self.location, Follow::Unary(UnaryOp::Dereference))),
other => {
self.put_back(other);
break;
Expand Down
14 changes: 14 additions & 0 deletions crates/dreammaker/tests/expression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,17 @@ fn bitop_precedence() {
}
);
}

#[test]
fn pointer_ops() {
assert_eq!(
parse_expr("*&1"),
Expression::Base {
term: Box::new(Spanned::new(Default::default(), Term::Int(1))),
follow: vec![
Spanned::new(Default::default(), Follow::Unary(UnaryOp::Reference)),
Spanned::new(Default::default(), Follow::Unary(UnaryOp::Dereference)),
].into_boxed_slice(),
}
)
}

0 comments on commit 32bf1b3

Please sign in to comment.