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
193
194
195
196
197
198
199
200
//! # Expression types
//! Expression types for Semantic analyzer result state.

use super::types::Type;
use super::{FunctionCall, PrimitiveValue, ValueName};
use crate::ast;
use crate::types::semantic::ExtendedExpression;
#[cfg(feature = "codec")]
use serde::{Deserialize, Serialize};

/// # Expression result
/// Contains analyzing results of expression:
/// - `expr_type` - result type of expression
/// - `expr_value` - result value of expression
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ExpressionResult {
    /// Result type of expression
    pub expr_type: Type,
    /// Result value of expression
    pub expr_value: ExpressionResultValue,
}

/// # Expression Result Value
/// Result value of expression analyze has to kind:
/// - Primitive value
/// - Register that contain result of expression
///   evaluation or call.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum ExpressionResultValue {
    PrimitiveValue(PrimitiveValue),
    Register(u64),
}

/// `ExtendedExpressionValue` represent simplified string
/// data of custom extended `ExpressionValue`. For now
/// used only for display errors.
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ExtendedExpressionValue(String);

/// Expression value kinds:
/// - value name - initialized through let-binding
/// - primitive value - most primitive value, like integers etc.
/// - struct value - value of struct type
/// - function call - call of function with params
/// - expression - contains other expression
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum ExpressionValue {
    ValueName(ValueName),
    PrimitiveValue(PrimitiveValue),
    StructValue(ExpressionStructValue),
    FunctionCall(FunctionCall),
    Expression(Box<Expression>),
    ExtendedExpression(ExtendedExpressionValue),
}

impl ToString for ExpressionValue {
    fn to_string(&self) -> String {
        match self {
            Self::ValueName(val) => val.clone().to_string(),
            Self::PrimitiveValue(val) => val.clone().to_string(),
            Self::StructValue(st_val) => st_val.clone().to_string(),
            Self::FunctionCall(fn_call) => fn_call.clone().to_string(),
            Self::Expression(val) => val.to_string(),
            Self::ExtendedExpression(val) => val.clone().0,
        }
    }
}

impl<E: ExtendedExpression> From<ast::ExpressionValue<'_, E>> for ExpressionValue {
    fn from(value: ast::ExpressionValue<'_, E>) -> Self {
        match value {
            ast::ExpressionValue::ValueName(v) => Self::ValueName(v.into()),
            ast::ExpressionValue::PrimitiveValue(v) => Self::PrimitiveValue(v.into()),
            ast::ExpressionValue::StructValue(v) => Self::StructValue(v.into()),
            ast::ExpressionValue::FunctionCall(v) => Self::FunctionCall(v.into()),
            ast::ExpressionValue::Expression(v) => {
                Self::Expression(Box::new(v.as_ref().clone().into()))
            }
            ast::ExpressionValue::ExtendedExpression(expr) => {
                Self::ExtendedExpression(ExtendedExpressionValue(format!("{expr:?}")))
            }
        }
    }
}

/// Expression value of struct type. It's represent access to
/// struct attributes of values with struct type
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ExpressionStructValue {
    /// Value name for structure value
    pub name: ValueName,
    /// Value attribute for structure value
    pub attribute: ValueName,
}

impl ToString for ExpressionStructValue {
    fn to_string(&self) -> String {
        self.name.to_string()
    }
}

impl From<ast::ExpressionStructValue<'_>> for ExpressionStructValue {
    fn from(value: ast::ExpressionStructValue<'_>) -> Self {
        Self {
            name: value.name.into(),
            attribute: value.attribute.into(),
        }
    }
}

/// Basic  expression operations - calculations and
/// logic operations
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum ExpressionOperations {
    Plus,
    Minus,
    Multiply,
    Divide,
    ShiftLeft,
    ShiftRight,
    And,
    Or,
    Xor,
    Eq,
    NotEq,
    Great,
    Less,
    GreatEq,
    LessEq,
}

impl From<ast::ExpressionOperations> for ExpressionOperations {
    fn from(value: ast::ExpressionOperations) -> Self {
        match value {
            ast::ExpressionOperations::Plus => Self::Plus,
            ast::ExpressionOperations::Minus => Self::Minus,
            ast::ExpressionOperations::Multiply => Self::Multiply,
            ast::ExpressionOperations::Divide => Self::Divide,
            ast::ExpressionOperations::ShiftLeft => Self::ShiftLeft,
            ast::ExpressionOperations::ShiftRight => Self::ShiftRight,
            ast::ExpressionOperations::And => Self::And,
            ast::ExpressionOperations::Or => Self::Or,
            ast::ExpressionOperations::Xor => Self::Xor,
            ast::ExpressionOperations::Eq => Self::Eq,
            ast::ExpressionOperations::NotEq => Self::NotEq,
            ast::ExpressionOperations::Great => Self::Great,
            ast::ExpressionOperations::Less => Self::Less,
            ast::ExpressionOperations::GreatEq => Self::GreatEq,
            ast::ExpressionOperations::LessEq => Self::LessEq,
        }
    }
}

/// # Expression
/// Basic expression entity representation. It contains
/// `ExpressionValue` and optional operations with other
/// expressions. So it's represent flat tree.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct Expression {
    /// Expression value
    pub expression_value: ExpressionValue,
    /// Optional expression operation under other `Expression`
    pub operation: Option<(ExpressionOperations, Box<Expression>)>,
}

impl ToString for Expression {
    fn to_string(&self) -> String {
        self.expression_value.to_string()
    }
}

impl<E: ExtendedExpression> From<ast::Expression<'_, E>> for Expression {
    fn from(value: ast::Expression<'_, E>) -> Self {
        Self {
            expression_value: value.expression_value.into(),
            operation: value
                .operation
                .map(|(op, expr)| (op.into(), Box::new(expr.as_ref().clone().into()))),
        }
    }
}