mylang_ast/
expr.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use mylang_token::{Locatable, Range};
use serde::{Deserialize, Serialize};

/// 式を表す抽象構文木
#[derive(Debug, Serialize, Deserialize)]
pub enum Expr {
    I32Lit(Range, i32),
    StrLit(Range, String),
    Add(Box<Expr>, Box<Expr>),
}

impl Locatable for Expr {
    fn locate(&self) -> Range {
        match self {
            Expr::I32Lit(r, _) | Expr::StrLit(r, _) => r.clone(),

            Expr::Add(lhs, rhs) => {
                let mut range = lhs.locate();
                let rhs_range = rhs.locate();
                range.concat(rhs_range);
                range
            }
        }
    }
}