aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: fe986da7843a630256ba094e03f368eac1630822 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[derive(Debug)]
pub struct Program {
    pub stanzas: Vec<Stanza>,
}

impl Program {
    pub fn new() -> Self {
        Self {
            stanzas: Vec::new(),
        }
    }

    pub fn from_str(mut self, i: &str) -> Result<Self, nom::error::Error<&str>> {
        use nom::Finish;
        let (remaining_input, stanzas) = crate::parser::parse_file(i).finish()?;
        assert!(remaining_input.trim().is_empty(), "{remaining_input}");
        self.stanzas = stanzas;
        Ok(self)
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct Stanza {
    pub pattern: Pattern,
    pub statements: Block,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Pattern {
    Begin,
    End,
    Node(NodePattern),
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct NodePattern {
    pub modifier: Modifier,
    pub kind: String,
}

#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
pub enum Modifier {
    #[default]
    Enter,
    Leave,
}

#[derive(Debug, Default, Eq, PartialEq, Clone)]
pub struct Block {
    pub body: Vec<Statement>,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Statement {
    Bare(Expr),
    Declaration(Declaration),
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Expr {
    Node,
    Unit,
    Lit(Literal),
    Ident(Identifier),
    FieldAccess(Box<Expr>, Identifier),
    Index(Box<Expr>, Box<Expr>),
    Bin(Box<Expr>, BinOp, Box<Expr>),
    Unary(Box<Expr>, UnaryOp),
    Call(Call),
    List(List),
    If(IfExpr),
    Block(Block),
}

impl Expr {
    pub fn boxed(self) -> Box<Expr> {
        Box::new(self)
    }

    pub fn as_ident(self) -> Option<Identifier> {
        match self {
            Self::Ident(i) => Some(i),
            _ => None,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum UnaryOp {
    Not,
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum BinOp {
    Arith(ArithOp),
    Cmp(CmpOp),
    Logic(LogicOp),
    // =
    Assign(AssignOp),
}

// + - * /
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum ArithOp {
    Add,
    Sub,
    Mul,
    Div,
    Mod,
}

// && ||
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum LogicOp {
    And,
    Or,
}

// == != > < >= <=
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum CmpOp {
    Eq,
    Neq,
    Gt,
    Lt,
    Gte,
    Lte,
}

// =, +=, -=, *=, /=
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct AssignOp {
    pub op: Option<ArithOp>,
}

pub type Identifier = String;

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Literal {
    Str(String),
    Int(i128),
    Bool(bool),
}

/// A function call
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Call {
    pub function: Identifier,
    pub parameters: Vec<Expr>,
}

impl From<Call> for Expr {
    fn from(expr: Call) -> Expr {
        Expr::Call(expr)
    }
}

/// A list construction expression
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct List {
    pub items: Vec<Expr>,
}

impl From<List> for Expr {
    fn from(list: List) -> Expr {
        Expr::List(list)
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Type {
    Unit,
    Integer,
    String,
    Boolean,
    Node,
    List,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Declaration {
    pub ty: Type,
    pub name: Identifier,
    pub init: Option<Box<Expr>>,
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct IfExpr {
    pub condition: Box<Expr>,
    pub then: Block,
    pub else_: Block,
}