Skip to content

Commit

Permalink
chore: Parse negatives in SSA parser (#6510)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Nov 13, 2024
1 parent f81c649 commit 0fc0c53
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/noirc_evaluator/src/ssa/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,17 @@ impl<'a> Parser<'a> {
}

fn eat_int(&mut self) -> ParseResult<Option<FieldElement>> {
let negative = self.eat(Token::Dash)?;

if matches!(self.token.token(), Token::Int(..)) {
let token = self.bump()?;
match token.into_token() {
Token::Int(int) => Ok(Some(int)),
Token::Int(mut int) => {
if negative {
int = -int;
}
Ok(Some(int))
}
_ => unreachable!(),
}
} else {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl<'a> Lexer<'a> {
Some(']') => self.single_char_token(Token::RightBracket),
Some('&') => self.single_char_token(Token::Ampersand),
Some('-') if self.peek_char() == Some('>') => self.double_char_token(Token::Arrow),
Some('-') => self.single_char_token(Token::Dash),
Some(ch) if ch.is_ascii_alphanumeric() || ch == '_' => self.eat_alpha_numeric(ch),
Some(char) => Err(LexerError::UnexpectedCharacter {
char,
Expand Down
11 changes: 11 additions & 0 deletions compiler/noirc_evaluator/src/ssa/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,14 @@ fn test_slice() {
";
assert_ssa_roundtrip(src);
}

#[test]
fn test_negative() {
let src = "
acir(inline) fn main f0 {
b0():
return Field -1
}
";
assert_ssa_roundtrip(src);
}
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/ssa/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub(crate) enum Token {
Equal,
/// &
Ampersand,
/// -
Dash,
Eof,
}

Expand Down Expand Up @@ -90,6 +92,7 @@ impl Display for Token {
Token::Arrow => write!(f, "->"),
Token::Equal => write!(f, "=="),
Token::Ampersand => write!(f, "&"),
Token::Dash => write!(f, "-"),
Token::Eof => write!(f, "(end of stream)"),
}
}
Expand Down

0 comments on commit 0fc0c53

Please sign in to comment.