bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! Code Generation for Bash-to-Rash Transpiler

use super::patterns::*;
use super::TranspileResult;
use crate::bash_parser::ast::*;

pub struct TranspileOptions {
    pub add_safety_checks: bool,
    pub preserve_comments: bool,
    pub indent_size: usize,
}

impl Default for TranspileOptions {
    fn default() -> Self {
        Self {
            add_safety_checks: true,
            preserve_comments: true,
            indent_size: 4,
        }
    }
}

pub struct BashToRashTranspiler {
    options: TranspileOptions,
    current_indent: usize,
}

impl BashToRashTranspiler {
    pub fn new(options: TranspileOptions) -> Self {
        Self {
            options,
            current_indent: 0,
        }
    }

    pub fn transpile(&mut self, ast: &BashAst) -> TranspileResult<String> {
        let mut output = String::new();

        // Add header comment
        output.push_str("// Transpiled from bash by rash\n\n");

        // Process all statements
        for stmt in &ast.statements {
            let rash_code = self.transpile_statement(stmt)?;
            output.push_str(&self.indent(&rash_code));
            output.push('\n');
        }

        Ok(output)
    }

    fn transpile_statement(&mut self, stmt: &BashStmt) -> TranspileResult<String> {
        match stmt {
            BashStmt::Assignment {
                name,
                value,
                exported,
                ..
            } => self.transpile_assignment(name, value, *exported),

            BashStmt::Command { name, args, .. } => self.transpile_command_stmt(name, args),

            BashStmt::Function { name, body, .. } => self.transpile_function_stmt(name, body),

            BashStmt::If {
                condition,
                then_block,
                elif_blocks,
                else_block,
                ..
            } => self.transpile_if_stmt(condition, then_block, elif_blocks, else_block),

            BashStmt::While {
                condition, body, ..
            } => self.transpile_while_stmt(condition, body),

            BashStmt::Until {
                condition, body, ..
            } => self.transpile_until_stmt(condition, body),

            BashStmt::For {
                variable,
                items,
                body,
                ..
            } => self.transpile_for_stmt(variable, items, body),

            BashStmt::ForCStyle { body, .. } => self.transpile_for_c_style_stmt(body),

            BashStmt::Return { code, .. } => self.transpile_return_stmt(code.as_ref()),

            BashStmt::Comment { text, .. } => self.transpile_comment(text),

            BashStmt::Case { word, arms, .. } => self.transpile_case_stmt(word, arms),

            BashStmt::Pipeline { commands, .. } => self.transpile_pipeline_stmt(commands),

            BashStmt::AndList { left, right, .. } => self.transpile_and_list(left, right),

            BashStmt::OrList { left, right, .. } => self.transpile_or_list(left, right),

            BashStmt::BraceGroup { body, .. } => self.transpile_brace_group(body),

            BashStmt::Coproc { name, body, .. } => {
                self.transpile_coproc_stmt(name.as_deref(), body)
            }

            BashStmt::Select {
                variable,
                items,
                body,
                ..
            } => self.transpile_select_stmt(variable, items, body),

            BashStmt::Negated { command, .. } => self.transpile_negated_stmt(command),
        }
    }

    fn transpile_assignment(
        &mut self,
        name: &str,
        value: &BashExpr,
        exported: bool,
    ) -> TranspileResult<String> {
        let value_rash = self.transpile_expression(value)?;
        let pattern = VariablePattern { exported };
        Ok(pattern.to_rash(name, &value_rash))
    }

    fn transpile_command_stmt(&mut self, name: &str, args: &[BashExpr]) -> TranspileResult<String> {
        let mut rash_args = Vec::new();
        for arg in args {
            rash_args.push(self.transpile_expression(arg)?);
        }
        Ok(CommandPattern::to_rash(name, &rash_args))
    }

    fn transpile_function_stmt(
        &mut self,
        name: &str,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        self.current_indent += 1;
        let mut body_stmts = Vec::new();
        for stmt in body {
            body_stmts.push(self.transpile_statement(stmt)?);
        }
        let body_rash = body_stmts.join("\n");
        self.current_indent -= 1;

        Ok(FunctionPattern::to_rash(name, &self.indent(&body_rash)))
    }

    fn transpile_if_stmt(
        &mut self,
        condition: &BashExpr,
        then_block: &[BashStmt],
        elif_blocks: &[(BashExpr, Vec<BashStmt>)],
        else_block: &Option<Vec<BashStmt>>,
    ) -> TranspileResult<String> {
        let cond_rash = self.transpile_test_expression(condition)?;

        self.current_indent += 1;
        let then_rash = self.transpile_block(then_block)?;

        let mut elif_rash = Vec::new();
        for (elif_cond, elif_body) in elif_blocks {
            let cond = self.transpile_test_expression(elif_cond)?;
            let body = self.transpile_block(elif_body)?;
            elif_rash.push((cond, body));
        }

        let else_rash = if let Some(else_body) = else_block {
            Some(self.transpile_block(else_body)?)
        } else {
            None
        };

        self.current_indent -= 1;

        Ok(IfPattern::to_rash(
            &cond_rash,
            &then_rash,
            &elif_rash,
            else_rash.as_deref(),
        ))
    }

    fn transpile_while_stmt(
        &mut self,
        condition: &BashExpr,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        let cond_rash = self.transpile_test_expression(condition)?;

        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;

        Ok(WhilePattern::to_rash(&cond_rash, &body_rash))
    }

    fn transpile_until_stmt(
        &mut self,
        condition: &BashExpr,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        // Until loop transpiles to while with negated condition
        let cond_rash = self.transpile_test_expression(condition)?;
        let negated_cond = format!("!({})", cond_rash);

        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;

        Ok(WhilePattern::to_rash(&negated_cond, &body_rash))
    }

    fn transpile_for_stmt(
        &mut self,
        variable: &str,
        items: &BashExpr,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        let items_rash = self.transpile_expression(items)?;

        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;

        Ok(ForPattern::to_rash(variable, &items_rash, &body_rash))
    }

    fn transpile_for_c_style_stmt(&mut self, body: &[BashStmt]) -> TranspileResult<String> {
        // Issue #68: C-style for loop (transpile to Rust while loop)
        // For now, transpile C-style loops as a comment + body
        // Full conversion would need parsing C arithmetic to Rust
        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;

        Ok(format!(
            "// C-style for loop (not yet fully transpiled)\n{}",
            body_rash
        ))
    }

    fn transpile_return_stmt(&mut self, code: Option<&BashExpr>) -> TranspileResult<String> {
        if let Some(expr) = code {
            let val = self.transpile_expression(expr)?;
            Ok(format!("return {};", val))
        } else {
            Ok("return;".to_string())
        }
    }

    fn transpile_comment(&self, text: &str) -> TranspileResult<String> {
        if self.options.preserve_comments {
            Ok(format!("//{}", text))
        } else {
            Ok(String::new())
        }
    }

    fn transpile_case_stmt(
        &mut self,
        word: &BashExpr,
        arms: &[CaseArm],
    ) -> TranspileResult<String> {
        let word_rash = self.transpile_expression(word)?;
        let mut result = format!("match {} {{\n", word_rash);

        self.current_indent += 1;

        for arm in arms {
            let pattern_str = arm.patterns.join(" | ");
            result.push_str(&self.indent(&format!("{} => {{\n", pattern_str)));

            self.current_indent += 1;
            for stmt in &arm.body {
                let stmt_rash = self.transpile_statement(stmt)?;
                result.push_str(&self.indent(&stmt_rash));
                result.push('\n');
            }
            self.current_indent -= 1;

            result.push_str(&self.indent("}\n"));
        }

        self.current_indent -= 1;
        result.push_str(&self.indent("}"));

        Ok(result)
    }

    fn transpile_pipeline_stmt(&mut self, commands: &[BashStmt]) -> TranspileResult<String> {
        // TODO: Full pipeline transpilation not implemented yet
        // For now, transpile each command separately
        let mut result = String::new();
        for cmd in commands {
            result.push_str(&self.transpile_statement(cmd)?);
            result.push_str(" | ");
        }
        // Remove trailing " | "
        if result.ends_with(" | ") {
            result.truncate(result.len() - 3);
        }
        Ok(result)
    }

    fn transpile_and_list(&mut self, left: &BashStmt, right: &BashStmt) -> TranspileResult<String> {
        // Transpile AND list: left && right
        let left_str = self.transpile_statement(left)?;
        let right_str = self.transpile_statement(right)?;
        Ok(format!("{} && {}", left_str, right_str))
    }

    fn transpile_or_list(&mut self, left: &BashStmt, right: &BashStmt) -> TranspileResult<String> {
        // Transpile OR list: left || right
        let left_str = self.transpile_statement(left)?;
        let right_str = self.transpile_statement(right)?;
        Ok(format!("{} || {}", left_str, right_str))
    }

    fn transpile_brace_group(&mut self, body: &[BashStmt]) -> TranspileResult<String> {
        // Transpile brace group as a block
        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;
        Ok(format!("{{\n{}\n}}", body_rash))
    }

    fn transpile_coproc_stmt(
        &mut self,
        name: Option<&str>,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        // Coproc is bash-specific, transpile as async block
        // Note: This is a placeholder - coproc has no direct Rust equivalent
        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;
        if let Some(n) = name {
            Ok(format!(
                "// coproc {} - async subprocess\n{{\n{}\n}}",
                n, body_rash
            ))
        } else {
            Ok(format!(
                "// coproc - async subprocess\n{{\n{}\n}}",
                body_rash
            ))
        }
    }

    fn transpile_select_stmt(
        &mut self,
        variable: &str,
        items: &BashExpr,
        body: &[BashStmt],
    ) -> TranspileResult<String> {
        // F017: Select is bash-specific, transpile as loop with menu
        // Note: No direct Rust equivalent, generate a comment placeholder
        self.current_indent += 1;
        let body_rash = self.transpile_block(body)?;
        self.current_indent -= 1;
        let items_rash = self.transpile_expression(items)?;
        Ok(format!(
            "// select {} in {} - interactive menu loop\nfor {} in {} {{\n{}\n}}",
            variable, items_rash, variable, items_rash, body_rash
        ))
    }

    fn transpile_negated_stmt(&mut self, command: &BashStmt) -> TranspileResult<String> {
        // Issue #133: Negated command - transpile inner and negate
        let inner = self.transpile_statement(command)?;
        Ok(format!("// negated: ! {}", inner))
    }

    fn transpile_block(&mut self, stmts: &[BashStmt]) -> TranspileResult<String> {
        let mut result = Vec::new();
        for stmt in stmts {
            result.push(self.transpile_statement(stmt)?);
        }
        Ok(self.indent(&result.join("\n")))
    }

    fn transpile_expression(&mut self, expr: &BashExpr) -> TranspileResult<String> {
        match expr {
            BashExpr::Literal(s) => {
                // Quote strings for Rust
                if s.parse::<i64>().is_ok() || s.parse::<bool>().is_ok() {
                    Ok(s.clone())
                } else {
                    Ok(format!("\"{}\"", s.escape_default()))
                }
            }

            BashExpr::Variable(name) => Ok(name.clone()),

            BashExpr::Array(items) => {
                let mut rash_items = Vec::new();
                for item in items {
                    rash_items.push(self.transpile_expression(item)?);
                }
                Ok(format!("vec![{}]", rash_items.join(", ")))
            }

            BashExpr::Concat(parts) => {
                let mut rash_parts = Vec::new();
                for part in parts {
                    rash_parts.push(self.transpile_expression(part)?);
                }
                Ok(format!("format!(\"{{}}\", {})", rash_parts.join(" + ")))
            }

            BashExpr::CommandSubst(cmd) => {
                // Command substitution becomes a function call
                let cmd_rash = self.transpile_statement(cmd)?;
                Ok(format!("{{ {} }}", cmd_rash))
            }

            BashExpr::Arithmetic(arith) => self.transpile_arithmetic(arith),

            BashExpr::Test(test) => self.transpile_test(test),

            BashExpr::Glob(pattern) => {
                // Convert glob to Rust code that returns matching paths
                Ok(format!("glob(\"{}\")", pattern.escape_default()))
            }

            BashExpr::DefaultValue { variable, default } => {
                // ${VAR:-default} → var.unwrap_or("default")
                let default_rash = self.transpile_expression(default)?;
                Ok(format!("{}.unwrap_or({})", variable, default_rash))
            }

            BashExpr::AssignDefault { variable, default } => {
                // ${VAR:=default} → var.get_or_insert("default")
                // or: if var.is_none() { var = Some("default"); } var.unwrap()
                let default_rash = self.transpile_expression(default)?;
                Ok(format!("{}.get_or_insert({})", variable, default_rash))
            }

            BashExpr::ErrorIfUnset { variable, message } => {
                // ${VAR:?message} → var.expect("message")
                let message_rash = self.transpile_expression(message)?;
                Ok(format!("{}.expect({})", variable, message_rash))
            }

            BashExpr::AlternativeValue {
                variable,
                alternative,
            } => {
                // ${VAR:+alt} → var.as_ref().map(|_| alt).unwrap_or("")
                // or: if var.is_some() { alt } else { "" }
                let alt_rash = self.transpile_expression(alternative)?;
                Ok(format!(
                    "{}.as_ref().map(|_| {}).unwrap_or(\"\")",
                    variable, alt_rash
                ))
            }

            BashExpr::StringLength { variable } => {
                // ${#VAR} → var.len()
                Ok(format!("{}.len()", variable))
            }

            BashExpr::RemoveSuffix { variable, pattern } => {
                // ${VAR%pattern} → var.strip_suffix(pattern).unwrap_or(&var)
                let pattern_rash = self.transpile_expression(pattern)?;
                Ok(format!(
                    "{}.strip_suffix({}).unwrap_or(&{})",
                    variable, pattern_rash, variable
                ))
            }


}
}
}

                    include!("codegen_part2_incl2.rs");