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
//! Equation and statement formatting for the format visitor.
//!
//! Provides formatting methods for equations, statements, and for-indices.
use crate::ir::ast::{Equation, Expression, ForIndex, Statement};
use super::FormatVisitor;
impl FormatVisitor {
pub fn format_equation(&self, eq: &Equation, level: usize) -> String {
let indent = self.indent_str.repeat(level);
match eq {
Equation::Empty => String::new(),
Equation::Simple { lhs, rhs } => {
let lhs_str = self.format_expression(lhs);
// Check if RHS is a multi-line array
if let Expression::Array { elements, .. } = rhs
&& self.should_format_array_multiline(elements, level)
{
return format!(
"{}{} = {};\n",
indent,
lhs_str,
self.format_array_multiline(elements, level)
);
}
let rhs_str = self.format_expression(rhs);
format!("{}{} = {};\n", indent, lhs_str, rhs_str)
}
Equation::Connect { lhs, rhs } => {
format!(
"{}connect({}, {});\n",
indent,
self.format_comp_ref(lhs),
self.format_comp_ref(rhs)
)
}
Equation::For { indices, equations } => {
let idx_str = self.format_for_indices(indices);
let mut result = format!("{}for {} loop\n", indent, idx_str);
for sub_eq in equations {
result.push_str(&self.format_equation(sub_eq, level + 1));
}
result.push_str(&format!("{}end for;\n", indent));
result
}
Equation::When(blocks) => {
let mut result = String::new();
for (i, block) in blocks.iter().enumerate() {
if i == 0 {
result.push_str(&format!(
"{}when {} then\n",
indent,
self.format_expression(&block.cond)
));
} else {
result.push_str(&format!(
"{}elsewhen {} then\n",
indent,
self.format_expression(&block.cond)
));
}
for sub_eq in &block.eqs {
result.push_str(&self.format_equation(sub_eq, level + 1));
}
}
result.push_str(&format!("{}end when;\n", indent));
result
}
Equation::If {
cond_blocks,
else_block,
} => {
let mut result = String::new();
for (i, block) in cond_blocks.iter().enumerate() {
if i == 0 {
result.push_str(&format!(
"{}if {} then\n",
indent,
self.format_expression(&block.cond)
));
} else {
result.push_str(&format!(
"{}elseif {} then\n",
indent,
self.format_expression(&block.cond)
));
}
for sub_eq in &block.eqs {
result.push_str(&self.format_equation(sub_eq, level + 1));
}
}
if let Some(else_eqs) = else_block {
result.push_str(&format!("{}else\n", indent));
for sub_eq in else_eqs {
result.push_str(&self.format_equation(sub_eq, level + 1));
}
}
result.push_str(&format!("{}end if;\n", indent));
result
}
Equation::FunctionCall { comp, args } => {
let args_str: Vec<String> =
args.iter().map(|a| self.format_expression(a)).collect();
format!(
"{}{}({});\n",
indent,
self.format_comp_ref(comp),
args_str.join(", ")
)
}
}
}
pub fn format_statement(&self, stmt: &Statement, level: usize) -> String {
let indent = self.indent_str.repeat(level);
match stmt {
Statement::Empty => String::new(),
Statement::Assignment { comp, value } => {
format!(
"{}{} := {};\n",
indent,
self.format_comp_ref(comp),
self.format_expression(value)
)
}
Statement::FunctionCall {
comp,
args,
outputs,
} => {
let args_str: Vec<String> =
args.iter().map(|a| self.format_expression(a)).collect();
if outputs.is_empty() {
format!(
"{}{}({});\n",
indent,
self.format_comp_ref(comp),
args_str.join(", ")
)
} else {
let outputs_str: Vec<String> =
outputs.iter().map(|o| self.format_expression(o)).collect();
format!(
"{}({}) := {}({});\n",
indent,
outputs_str.join(", "),
self.format_comp_ref(comp),
args_str.join(", ")
)
}
}
Statement::For { indices, equations } => {
let idx_str = self.format_for_indices(indices);
let mut result = format!("{}for {} loop\n", indent, idx_str);
for sub_stmt in equations {
result.push_str(&self.format_statement(sub_stmt, level + 1));
}
result.push_str(&format!("{}end for;\n", indent));
result
}
Statement::While(block) => {
let mut result = format!(
"{}while {} loop\n",
indent,
self.format_expression(&block.cond)
);
for sub_stmt in &block.stmts {
result.push_str(&self.format_statement(sub_stmt, level + 1));
}
result.push_str(&format!("{}end while;\n", indent));
result
}
Statement::If {
cond_blocks,
else_block,
} => {
let mut result = String::new();
for (i, block) in cond_blocks.iter().enumerate() {
if i == 0 {
result.push_str(&format!(
"{}if {} then\n",
indent,
self.format_expression(&block.cond)
));
} else {
result.push_str(&format!(
"{}elseif {} then\n",
indent,
self.format_expression(&block.cond)
));
}
for sub_stmt in &block.stmts {
result.push_str(&self.format_statement(sub_stmt, level + 1));
}
}
if let Some(else_stmts) = else_block {
result.push_str(&format!("{}else\n", indent));
for sub_stmt in else_stmts {
result.push_str(&self.format_statement(sub_stmt, level + 1));
}
}
result.push_str(&format!("{}end if;\n", indent));
result
}
Statement::When(blocks) => {
let mut result = String::new();
for (i, block) in blocks.iter().enumerate() {
if i == 0 {
result.push_str(&format!(
"{}when {} then\n",
indent,
self.format_expression(&block.cond)
));
} else {
result.push_str(&format!(
"{}elsewhen {} then\n",
indent,
self.format_expression(&block.cond)
));
}
for sub_stmt in &block.stmts {
result.push_str(&self.format_statement(sub_stmt, level + 1));
}
}
result.push_str(&format!("{}end when;\n", indent));
result
}
Statement::Return { .. } => format!("{}return;\n", indent),
Statement::Break { .. } => format!("{}break;\n", indent),
}
}
pub fn format_for_indices(&self, indices: &[ForIndex]) -> String {
let parts: Vec<String> = indices
.iter()
.map(|idx| {
if matches!(idx.range, Expression::Empty) {
idx.ident.text.clone()
} else {
format!(
"{} in {}",
idx.ident.text,
self.format_expression(&idx.range)
)
}
})
.collect();
parts.join(", ")
}
}