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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! # Types
//! Semantic analyzer type.
//! Contains basic entities:
//! - Semantic types system
//! - Semantic basic elements types
//! - Block state types
//! - Error types

#![allow(clippy::module_inception)]
/// Block state types
pub mod block_state;
/// Condition types
pub mod condition;
/// Error types
pub mod error;
/// Expression types
pub mod expression;
/// Basic semantic types
pub mod semantic;
/// Types for type system
pub mod types;

use self::condition::{IfStatement, LoopBodyStatement};
use self::expression::{Expression, ExpressionOperations};
use self::types::Type;
use crate::ast;
use crate::ast::GetName;
use crate::types::semantic::ExtendedExpression;
#[cfg(feature = "codec")]
use serde::{Deserialize, Serialize};

/// Value name type
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ValueName(String);

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

impl From<String> for ValueName {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for ValueName {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl ToString for ValueName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Inner value name type
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct InnerValueName(String);

impl From<ValueName> for InnerValueName {
    fn from(value: ValueName) -> Self {
        Self(value.0)
    }
}

impl From<String> for InnerValueName {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl From<&str> for InnerValueName {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl ToString for InnerValueName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Label name type
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct LabelName(String);

impl From<String> for LabelName {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl ToString for LabelName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Function name type
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct FunctionName(String);

impl From<String> for FunctionName {
    fn from(value: String) -> Self {
        Self(value)
    }
}
impl From<ast::FunctionName<'_>> for FunctionName {
    fn from(value: ast::FunctionName<'_>) -> Self {
        Self(value.to_string())
    }
}

impl ToString for FunctionName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Constant name type
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ConstantName(String);

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

impl From<String> for ConstantName {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl ToString for ConstantName {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

/// Constant value can contain other constant or primitive value
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum ConstantValue {
    Constant(ConstantName),
    Value(PrimitiveValue),
}

impl From<ast::ConstantValue<'_>> for ConstantValue {
    fn from(value: ast::ConstantValue<'_>) -> Self {
        match value {
            ast::ConstantValue::Constant(v) => Self::Constant(v.into()),
            ast::ConstantValue::Value(v) => Self::Value(v.into()),
        }
    }
}

/// Constant expression represent expression operation between
/// constant values, and represent flat tree
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ConstantExpression {
    /// Constant value for expression operation
    pub value: ConstantValue,
    /// Optional expression operation and next constant expression entry point
    pub operation: Option<(ExpressionOperations, Box<ConstantExpression>)>,
}

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

/// # Constant
/// Can contain: name, type
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct Constant {
    /// Constant name
    pub name: ConstantName,
    /// Constant type
    pub constant_type: Type,
    /// Constant value represented through constant expression
    pub constant_value: ConstantExpression,
}

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

/// # Values
/// Can contain inner data: name, type, memory allocation status:
/// - alloca - stack allocation
/// - malloc - malloc allocation
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct Value {
    /// Inner value name
    pub inner_name: InnerValueName,
    /// Inner value type
    pub inner_type: Type,
    /// Mutability flag
    pub mutable: bool,
    /// Stack allocation flag
    pub alloca: bool,
    /// Memory allocation flag
    pub malloc: bool,
}

/// # Function
/// Function declaration analyze contains:
/// - function name
/// - function type
/// - parameters of functions (with types only)
///
/// It used to detect functions in state and
/// their parameters to use in normal execution
/// flog.
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct Function {
    /// Inner function name
    pub inner_name: FunctionName,
    /// Inner (return) type
    pub inner_type: Type,
    /// Function parameters typesz
    pub parameters: Vec<Type>,
}

/// Parameter name type for Functions parameter
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct ParameterName(String);

impl From<ast::ParameterName<'_>> for ParameterName {
    fn from(value: ast::ParameterName<'_>) -> Self {
        Self(value.to_string())
    }
}

/// Function parameter one of the basic entity for `FunctionStatement`
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct FunctionParameter {
    /// Function parameter name
    pub name: ParameterName,
    /// Function parameter type
    pub parameter_type: Type,
}

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

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

/// # Function statement
/// Function statement is basic type that represent function itself.
/// The basic function struct elements:
/// - function name
/// - function parameters
/// - function result type
/// - function body statements
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct FunctionStatement {
    /// Function name
    pub name: FunctionName,
    /// Function parameters
    pub parameters: Vec<FunctionParameter>,
    /// Function result type
    pub result_type: Type,
    /// Function body statements
    pub body: Vec<BodyStatement>,
}

impl<E: ExtendedExpression> From<ast::FunctionStatement<'_, E>> for FunctionStatement {
    fn from(value: ast::FunctionStatement<'_, E>) -> Self {
        Self {
            name: value.name.into(),
            parameters: value.parameters.iter().map(|v| v.clone().into()).collect(),
            result_type: value.result_type.into(),
            body: value.body.iter().map(|v| v.clone().into()).collect(),
        }
    }
}

/// # Body statement
/// Statement of body. Body is basic entity for functions and
/// represent basic functions elements.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum BodyStatement {
    LetBinding(LetBinding),
    Binding(Binding),
    FunctionCall(FunctionCall),
    If(IfStatement),
    Loop(Vec<LoopBodyStatement>),
    Expression(Expression),
    Return(Expression),
}

impl<E: ExtendedExpression> From<ast::BodyStatement<'_, E>> for BodyStatement {
    fn from(value: ast::BodyStatement<'_, E>) -> Self {
        match value {
            ast::BodyStatement::LetBinding(v) => Self::LetBinding(v.into()),
            ast::BodyStatement::Binding(v) => Self::Binding(v.into()),
            ast::BodyStatement::FunctionCall(v) => Self::FunctionCall(v.into()),
            ast::BodyStatement::If(v) => Self::If(v.into()),
            ast::BodyStatement::Loop(v) => Self::Loop(v.iter().map(|v| v.clone().into()).collect()),
            ast::BodyStatement::Expression(v) => Self::Expression(v.into()),
            ast::BodyStatement::Return(v) => Self::Return(v.into()),
        }
    }
}

/// # Let binding
/// Value initialization through binding.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct LetBinding {
    /// Value name
    pub name: ValueName,
    /// Value mutability flag
    pub mutable: bool,
    /// Value type
    pub value_type: Option<Type>,
    /// Value bind expression
    pub value: Box<Expression>,
}

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

impl<E: ExtendedExpression> From<ast::LetBinding<'_, E>> for LetBinding {
    fn from(value: ast::LetBinding<'_, E>) -> Self {
        Self {
            name: value.name.into(),
            mutable: value.mutable,
            value_type: value.value_type.map(Into::into),
            value: Box::new(value.value.as_ref().clone().into()),
        }
    }
}

/// Primitive value is most primitive and basic values entity.
/// It's basic elements for all other values elements.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "codec",
    derive(Serialize, Deserialize),
    serde(tag = "type", content = "content")
)]
pub enum PrimitiveValue {
    U8(u8),
    U16(u16),
    U32(u32),
    U64(u64),
    I8(i8),
    I16(i16),
    I32(i32),
    I64(i64),
    F32(f32),
    F64(f64),
    Bool(bool),
    String(String),
    Char(char),
    Ptr,
    None,
}

impl From<ast::PrimitiveValue> for PrimitiveValue {
    fn from(value: ast::PrimitiveValue) -> Self {
        match value {
            ast::PrimitiveValue::U8(v) => Self::U8(v),
            ast::PrimitiveValue::U16(v) => Self::U16(v),
            ast::PrimitiveValue::U32(v) => Self::U32(v),
            ast::PrimitiveValue::U64(v) => Self::U64(v),
            ast::PrimitiveValue::I8(v) => Self::I8(v),
            ast::PrimitiveValue::I16(v) => Self::I16(v),
            ast::PrimitiveValue::I32(v) => Self::I32(v),
            ast::PrimitiveValue::I64(v) => Self::I64(v),
            ast::PrimitiveValue::F32(v) => Self::F32(v),
            ast::PrimitiveValue::F64(v) => Self::F64(v),
            ast::PrimitiveValue::Bool(v) => Self::Bool(v),
            ast::PrimitiveValue::String(v) => Self::String(v),
            ast::PrimitiveValue::Char(v) => Self::Char(v),
            ast::PrimitiveValue::Ptr => Self::Ptr,
            ast::PrimitiveValue::None => Self::None,
        }
    }
}

impl ToString for PrimitiveValue {
    fn to_string(&self) -> String {
        match self {
            Self::U8(val) => val.clone().to_string(),
            Self::U16(val) => val.clone().to_string(),
            Self::U32(val) => val.clone().to_string(),
            Self::U64(val) => val.clone().to_string(),
            Self::I8(val) => val.clone().to_string(),
            Self::I16(val) => val.clone().to_string(),
            Self::I32(val) => val.clone().to_string(),
            Self::I64(val) => val.clone().to_string(),
            Self::F32(val) => val.clone().to_string(),
            Self::F64(val) => val.clone().to_string(),
            Self::Bool(val) => val.to_string(),
            Self::String(s) => s.clone(),
            Self::Char(c) => format!("{c}"),
            Self::Ptr => "ptr".to_string(),
            Self::None => "None".to_string(),
        }
    }
}

/// # Function call
/// Basic struct for function call representation
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct FunctionCall {
    /// Call function name
    pub name: FunctionName,
    /// Call function parameters contains expressions
    pub parameters: Vec<Expression>,
}

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

impl<E: ExtendedExpression> From<ast::FunctionCall<'_, E>> for FunctionCall {
    fn from(value: ast::FunctionCall<'_, E>) -> Self {
        Self {
            name: value.name.into(),
            parameters: value.parameters.iter().map(|v| v.clone().into()).collect(),
        }
    }
}

/// `Binding` represents mutable binding for previously bind values
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "codec", derive(Serialize, Deserialize))]
pub struct Binding {
    /// Binding value name
    pub name: ValueName,
    /// Value expression representation
    pub value: Box<Expression>,
}

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

impl<E: ExtendedExpression> From<ast::Binding<'_, E>> for Binding {
    fn from(value: ast::Binding<'_, E>) -> Self {
        Self {
            name: value.name.into(),
            value: Box::new(value.value.as_ref().clone().into()),
        }
    }
}