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
//! # Semantic types
//! Semantic analyzer result state types.
//! It contains `SemanticStack` as Semantic results
//! Context data.

use super::condition::{Condition, LogicCondition};
use super::expression::{ExpressionOperations, ExpressionResult};
use super::types::StructTypes;
use super::{Constant, Function, FunctionStatement, LabelName, Value};

/// # Semantic stack
/// Semantic stack represent stack of Semantic Context
#[derive(Debug, Default, Clone, PartialEq)]
pub struct SemanticStack(Vec<SemanticStackContext>);

impl SemanticStack {
    /// Init Semantic stack
    pub fn new() -> Self {
        Self::default()
    }

    /// Push Context data to the stack
    pub fn push(&mut self, value: SemanticStackContext) {
        self.0.push(value);
    }

    /// Get all context stack data as array data
    pub fn get(self) -> Vec<SemanticStackContext> {
        self.0
    }

    /// Push Context to the stack as expression value data
    pub fn expression_value(&mut self, expression: Value) {
        self.push(SemanticStackContext::ExpressionValue { expression });
    }

    /// Push Context to the stack as expression const data
    pub fn expression_const(&mut self, expression: Constant) {
        self.push(SemanticStackContext::ExpressionConst { expression });
    }

    /// Push Context to the stack as expression struct value data
    pub fn expression_struct_value(&mut self, expression: Value, index: u32) {
        self.push(SemanticStackContext::ExpressionStructValue { expression, index });
    }

    /// Push Context to the stack as expression operation data
    pub fn expression_operation(
        &mut self,
        operation: ExpressionOperations,
        left_value: ExpressionResult,
        right_value: ExpressionResult,
    ) {
        self.push(SemanticStackContext::ExpressionOperation {
            operation,
            left_value,
            right_value,
        });
    }

    /// Push Context to the stack as function call data
    pub fn call(&mut self, call: Function, params: Vec<ExpressionResult>) {
        self.push(SemanticStackContext::Call { call, params });
    }

    /// Push Context to the stack as let-binding data
    pub fn let_binding(&mut self, let_decl: Value, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::LetBinding {
            let_decl,
            expr_result,
        });
    }

    /// Push Context to the stack as binding data
    pub fn binding(&mut self, val: Value, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::Binding { val, expr_result });
    }

    /// Push Context to the stack as function declaration data
    pub fn function_declaration(&mut self, fn_decl: FunctionStatement) {
        self.push(SemanticStackContext::FunctionDeclaration { fn_decl });
    }

    /// Push Context to the stack as constant data
    pub fn constant(&mut self, const_decl: Constant) {
        self.push(SemanticStackContext::Constant { const_decl });
    }

    /// Push Context to the stack as types data
    pub fn types(&mut self, type_decl: StructTypes) {
        self.push(SemanticStackContext::Types { type_decl });
    }

    /// Push Context to the stack as expression function return data
    pub fn expression_function_return(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::ExpressionFunctionReturn { expr_result });
    }

    /// Push Context to the stack as `expression function return with label` data
    pub fn expression_function_return_with_label(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::ExpressionFunctionReturnWithLabel { expr_result });
    }

    /// Push Context to the stack as `set label` data
    pub fn set_label(&mut self, label: LabelName) {
        self.push(SemanticStackContext::SetLabel { label });
    }

    /// Push Context to the stack as `jump to` data
    pub fn jump_to(&mut self, label: LabelName) {
        self.push(SemanticStackContext::JumpTo { label });
    }

    /// Push Context to the stack as `if condition expression` data
    pub fn if_condition_expression(
        &mut self,
        expr_result: ExpressionResult,
        label_if_begin: LabelName,
        label_if_end: LabelName,
    ) {
        self.push(SemanticStackContext::IfConditionExpression {
            expr_result,
            label_if_begin,
            label_if_end,
        });
    }

    /// Push Context to the stack as `condition expression` data
    pub fn condition_expression(
        &mut self,
        left_result: ExpressionResult,
        right_result: ExpressionResult,
        condition: Condition,
    ) {
        self.push(SemanticStackContext::ConditionExpression {
            left_result,
            right_result,
            condition,
        });
    }

    /// Push Context to the stack as `jump function return` data
    pub fn jump_function_return(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::JumpFunctionReturn { expr_result });
    }

    /// Push Context to the stack as `logic condition` data
    pub fn logic_condition(&mut self, logic_condition: LogicCondition) {
        self.push(SemanticStackContext::LogicCondition { logic_condition });
    }

    /// Push Context to the stack as `if condition logic` data
    pub fn if_condition_logic(&mut self, label_if_begin: LabelName, label_if_end: LabelName) {
        self.push(SemanticStackContext::IfConditionLogic {
            label_if_begin,
            label_if_end,
        });
    }
}

/// # Semantic stack Context
/// Context data of Semantic results
#[derive(Debug, Clone, PartialEq)]
pub enum SemanticStackContext {
    ExpressionValue {
        expression: Value,
    },
    ExpressionConst {
        expression: Constant,
    },
    ExpressionStructValue {
        expression: Value,
        index: u32,
    },
    ExpressionOperation {
        operation: ExpressionOperations,
        left_value: ExpressionResult,
        right_value: ExpressionResult,
    },
    Call {
        call: Function,
        params: Vec<ExpressionResult>,
    },
    LetBinding {
        let_decl: Value,
        expr_result: ExpressionResult,
    },
    Binding {
        val: Value,
        expr_result: ExpressionResult,
    },
    FunctionDeclaration {
        fn_decl: FunctionStatement,
    },
    Constant {
        const_decl: Constant,
    },
    Types {
        type_decl: StructTypes,
    },
    ExpressionFunctionReturn {
        expr_result: ExpressionResult,
    },
    ExpressionFunctionReturnWithLabel {
        expr_result: ExpressionResult,
    },
    SetLabel {
        label: LabelName,
    },
    JumpTo {
        label: LabelName,
    },
    IfConditionExpression {
        expr_result: ExpressionResult,
        label_if_begin: LabelName,
        label_if_end: LabelName,
    },
    ConditionExpression {
        left_result: ExpressionResult,
        right_result: ExpressionResult,
        condition: Condition,
    },
    JumpFunctionReturn {
        expr_result: ExpressionResult,
    },
    LogicCondition {
        logic_condition: LogicCondition,
    },
    IfConditionLogic {
        label_if_begin: LabelName,
        label_if_end: LabelName,
    },
}