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
use crate::types::condition::{Condition, LogicCondition};
use crate::types::expression::{ExpressionOperations, ExpressionResult};
use crate::types::types::StructTypes;
use crate::types::{Constant, Function, FunctionStatement, LabelName, Value};

#[derive(Debug, Default, Clone, PartialEq)]
pub struct SemanticStack(Vec<SemanticStackContext>);

impl SemanticStack {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, value: SemanticStackContext) {
        self.0.push(value);
    }

    pub fn get(self) -> Vec<SemanticStackContext> {
        self.0
    }

    pub fn expression_value(&mut self, expression: Value) {
        self.push(SemanticStackContext::ExpressionValue { expression });
    }

    pub fn expression_const(&mut self, expression: Constant) {
        self.push(SemanticStackContext::ExpressionConst { expression });
    }

    pub fn expression_struct_value(&mut self, expression: Value, index: u32) {
        self.push(SemanticStackContext::ExpressionStructValue { expression, index });
    }

    pub fn expression_operation(
        &mut self,
        operation: ExpressionOperations,
        left_value: ExpressionResult,
        right_value: ExpressionResult,
    ) {
        self.push(SemanticStackContext::ExpressionOperation {
            operation,
            left_value,
            right_value,
        });
    }

    pub fn call(&mut self, call: Function, params: Vec<ExpressionResult>) {
        self.push(SemanticStackContext::Call { call, params });
    }

    pub fn let_binding(&mut self, let_decl: Value, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::LetBinding {
            let_decl,
            expr_result,
        });
    }

    pub fn binding(&mut self, val: Value, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::Binding { val, expr_result });
    }

    pub fn function_declaration(&mut self, fn_decl: FunctionStatement) {
        self.push(SemanticStackContext::FunctionDeclaration { fn_decl });
    }

    pub fn constant(&mut self, const_decl: Constant) {
        self.push(SemanticStackContext::Constant { const_decl });
    }

    pub fn types(&mut self, type_decl: StructTypes) {
        self.push(SemanticStackContext::Types { type_decl });
    }

    pub fn expression_function_return(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::ExpressionFunctionReturnWithLabel { expr_result });
    }

    pub fn expression_function_return_with_label(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::ExpressionFunctionReturnWithLabel { expr_result });
    }

    pub fn set_label(&mut self, label: LabelName) {
        self.push(SemanticStackContext::SetLabel { label });
    }

    pub fn jump_to(&mut self, label: LabelName) {
        self.push(SemanticStackContext::JumpTo { label });
    }

    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,
        });
    }

    pub fn condition_expression(
        &mut self,
        left_result: ExpressionResult,
        right_result: ExpressionResult,
        condition: Condition,
    ) {
        self.push(SemanticStackContext::ConditionExpression {
            left_result,
            right_result,
            condition,
        });
    }

    pub fn jump_function_return(&mut self, expr_result: ExpressionResult) {
        self.push(SemanticStackContext::JumpFunctionReturn { expr_result });
    }

    pub fn logic_condition(
        &mut self,
        left_condition_register: u64,
        right_condition_register: u64,
        logic_condition: LogicCondition,
    ) {
        self.push(SemanticStackContext::LogicCondition {
            left_condition_register,
            right_condition_register,
            logic_condition,
        });
    }

    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,
        });
    }
}

#[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 {
        left_condition_register: u64,
        right_condition_register: u64,
        logic_condition: LogicCondition,
    },
    IfConditionLogic {
        label_if_begin: LabelName,
        label_if_end: LabelName,
    },
}