sieve/compiler/grammar/expr/
mod.rs

1/*
2 * Copyright (c) 2020-2023, Stalwart Labs Ltd.
3 *
4 * This file is part of the Stalwart Sieve Interpreter.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 * in the LICENSE file at the top-level directory of this distribution.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 *
19 * You can be released from the requirements of the AGPLv3 license by
20 * purchasing a commercial license. Please contact licensing@stalw.art
21 * for more details.
22*/
23
24use std::sync::Arc;
25
26use serde::{Deserialize, Serialize};
27
28use crate::compiler::{Number, VariableType};
29
30pub mod parser;
31pub mod tokenizer;
32
33#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
34pub(crate) enum Expression {
35    Variable(VariableType),
36    Constant(Constant),
37    BinaryOperator(BinaryOperator),
38    UnaryOperator(UnaryOperator),
39    JmpIf { val: bool, pos: u32 },
40    Function { id: u32, num_args: u32 },
41    ArrayAccess,
42    ArrayBuild(u32),
43}
44
45#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
46pub(crate) enum Constant {
47    Integer(i64),
48    Float(f64),
49    String(Arc<String>),
50}
51
52impl Eq for Constant {}
53
54impl From<Number> for Constant {
55    fn from(value: Number) -> Self {
56        match value {
57            Number::Integer(i) => Constant::Integer(i),
58            Number::Float(f) => Constant::Float(f),
59        }
60    }
61}
62
63impl From<String> for Constant {
64    fn from(value: String) -> Self {
65        Constant::String(value.into())
66    }
67}
68
69#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
70pub(crate) enum BinaryOperator {
71    Add,
72    Subtract,
73    Multiply,
74    Divide,
75
76    And,
77    Or,
78    Xor,
79
80    Eq,
81    Ne,
82    Lt,
83    Le,
84    Gt,
85    Ge,
86}
87
88#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
89pub(crate) enum UnaryOperator {
90    Not,
91    Minus,
92}
93
94#[derive(Debug, PartialEq, Eq, Clone)]
95pub(crate) enum Token {
96    Variable(VariableType),
97    Function {
98        name: String,
99        id: u32,
100        num_args: u32,
101    },
102    Number(Number),
103    String(String),
104    BinaryOperator(BinaryOperator),
105    UnaryOperator(UnaryOperator),
106    OpenParen,
107    CloseParen,
108    OpenBracket,
109    CloseBracket,
110    Comma,
111}