mirror of
https://github.com/TheM1Stery/izanami.git
synced 2025-04-19 16:31:11 +00:00
refactor: remove specification of operators for ternary operator
I don't plan on adding other ternary operators. So we just gonna assume that we only have one ternary operator, which is ? !
This commit is contained in:
parent
50a9e062e0
commit
be15364e3b
@ -4,9 +4,7 @@ use crate::token::{LiteralType, Token};
|
||||
pub enum Expr {
|
||||
Ternary {
|
||||
first: Box<Expr>,
|
||||
first_op: Token,
|
||||
second: Box<Expr>,
|
||||
second_op: Token,
|
||||
third: Box<Expr>,
|
||||
},
|
||||
Binary {
|
||||
|
@ -50,15 +50,12 @@ impl Parser<'_> {
|
||||
let expr = self.equality()?;
|
||||
|
||||
if self.match_token(&[Question]) {
|
||||
let first_op = self.previous().clone();
|
||||
let second = self.expression()?;
|
||||
let second_op = self.consume(Colon, "Expected : after ternary operator ?")?;
|
||||
let _ = self.consume(Colon, "Expected : after ternary operator ?")?;
|
||||
let third = self.ternary()?;
|
||||
return Ok(Expr::Ternary {
|
||||
first: Box::new(expr),
|
||||
first_op,
|
||||
second: Box::new(second),
|
||||
second_op,
|
||||
third: Box::new(third),
|
||||
});
|
||||
}
|
||||
|
@ -13,14 +13,9 @@ pub fn pretty_print(expr: &Expr) -> String {
|
||||
Expr::Unary { op, right } => parenthesize(&op.lexeme, &[right]),
|
||||
Expr::Ternary {
|
||||
first,
|
||||
first_op,
|
||||
second,
|
||||
second_op,
|
||||
third,
|
||||
} => parenthesize(
|
||||
&format!("{}{}", first_op.lexeme, second_op.lexeme),
|
||||
&[first, second, third],
|
||||
),
|
||||
} => parenthesize("?:", &[first, second, third]),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user