Skip to main content

cell_sheet_core/formula/
ast.rs

1use crate::model::col_label_to_index;
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct CellRef {
5    pub col: usize,
6    pub row: usize,
7    pub abs_col: bool,
8    pub abs_row: bool,
9}
10
11impl CellRef {
12    /// Create from display-style strings (col="A", row="1") where row is 1-indexed.
13    pub fn from_display(col: &str, row: &str, abs_col: bool, abs_row: bool) -> Option<Self> {
14        let col_idx = col_label_to_index(col)?;
15        let row_idx: usize = row.parse::<usize>().ok()?.checked_sub(1)?;
16        Some(CellRef {
17            col: col_idx,
18            row: row_idx,
19            abs_col,
20            abs_row,
21        })
22    }
23}
24
25#[derive(Debug, Clone, Copy, PartialEq)]
26pub enum Op {
27    Add,
28    Sub,
29    Mul,
30    Div,
31    Gt,
32    Gte,
33    Lt,
34    Lte,
35    Eq,
36    Neq,
37}
38
39#[derive(Debug, Clone, PartialEq)]
40pub enum Expr {
41    Number(f64),
42    Text(String),
43    Bool(bool),
44    CellRef(CellRef),
45    Range {
46        start: CellRef,
47        end: CellRef,
48    },
49    BinaryOp {
50        op: Op,
51        left: Box<Expr>,
52        right: Box<Expr>,
53    },
54    UnaryNeg(Box<Expr>),
55    FnCall {
56        name: String,
57        args: Vec<Expr>,
58    },
59}