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
// Control flow purification for Bash scripts
//
// Handles: If, While, Until, For, ForCStyle, Case, Select
use super::{PurificationResult, Purifier};
use crate::bash_parser::ast::*;
impl Purifier {
/// Purify control flow statements: If, While, Until, For, ForCStyle, Case, Select
pub(super) fn purify_control_flow(&mut self, stmt: &BashStmt) -> PurificationResult<BashStmt> {
match stmt {
BashStmt::If {
condition,
then_block,
elif_blocks,
else_block,
span,
} => {
let purified_condition = self.purify_expression(condition)?;
let purified_then = self.purify_body(then_block)?;
let mut purified_elif = Vec::new();
for (cond, body) in elif_blocks {
let p_cond = self.purify_expression(cond)?;
let p_body = self.purify_body(body)?;
purified_elif.push((p_cond, p_body));
}
let purified_else = if let Some(else_body) = else_block {
Some(self.purify_body(else_body)?)
} else {
None
};
Ok(BashStmt::If {
condition: purified_condition,
then_block: purified_then,
elif_blocks: purified_elif,
else_block: purified_else,
span: *span,
})
}
BashStmt::While {
condition,
body,
span,
} => {
let purified_condition = self.purify_expression(condition)?;
let purified_body = self.purify_body(body)?;
Ok(BashStmt::While {
condition: purified_condition,
body: purified_body,
span: *span,
})
}
BashStmt::Until {
condition,
body,
span,
} => {
let purified_condition = self.purify_expression(condition)?;
let purified_body = self.purify_body(body)?;
Ok(BashStmt::Until {
condition: purified_condition,
body: purified_body,
span: *span,
})
}
BashStmt::For {
variable,
items,
body,
span,
} => {
let purified_items = self.purify_expression(items)?;
let purified_body = self.purify_body(body)?;
Ok(BashStmt::For {
variable: variable.clone(),
items: purified_items,
body: purified_body,
span: *span,
})
}
// Issue #68: Purify C-style for loop (already handled by codegen)
BashStmt::ForCStyle {
init,
condition,
increment,
body,
span,
} => {
// Purify the body statements
let purified_body = self.purify_body(body)?;
// Return the purified C-style for loop as-is
// The codegen will convert it to POSIX while loop
Ok(BashStmt::ForCStyle {
init: init.clone(),
condition: condition.clone(),
increment: increment.clone(),
body: purified_body,
span: *span,
})
}
BashStmt::Case { word, arms, span } => {
let purified_word = self.purify_expression(word)?;
let mut purified_arms = Vec::new();
for arm in arms {
let purified_body = self.purify_body(&arm.body)?;
purified_arms.push(crate::bash_parser::ast::CaseArm {
patterns: arm.patterns.clone(),
body: purified_body,
});
}
Ok(BashStmt::Case {
word: purified_word,
arms: purified_arms,
span: *span,
})
}
BashStmt::Select {
variable,
items,
body,
span,
} => {
// F017: Purify select statement
let purified_items = self.purify_expression(items)?;
let purified_body = self.purify_body(body)?;
Ok(BashStmt::Select {
variable: variable.clone(),
items: purified_items,
body: purified_body,
span: *span,
})
}
_ => Ok(stmt.clone()),
}
}
}