y_lang/ast/
if_statement.rs

1use pest::iterators::Pair;
2
3use super::{Block, Expression, Position, Rule};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct If<T> {
7    pub condition: Box<Expression<T>>,
8    pub if_block: Block<T>,
9    pub else_block: Option<Block<T>>,
10    pub position: Position,
11    pub info: T,
12}
13
14impl If<()> {
15    pub fn from_pair(pair: Pair<Rule>, file: &str) -> If<()> {
16        assert_eq!(pair.as_rule(), Rule::ifStmt);
17
18        let (line, col) = pair.line_col();
19
20        let mut inner = pair.into_inner();
21        let condition = Expression::from_pair(inner.next().unwrap(), file);
22        let if_block = inner.next().unwrap();
23        let else_block = inner.next().map(|block| Block::from_pair(block, file));
24
25        If {
26            condition: Box::new(condition),
27            if_block: Block::from_pair(if_block, file),
28            else_block,
29            position: (file.to_owned(), line, col),
30            info: (),
31        }
32    }
33}