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
//! Value and expression emitters. Extracted from posix.rs.
use super::escape::{escape_command_name, escape_shell_string, escape_variable_name};
use super::posix::{
    arithmetic_op_str, classify_test_expression, needs_arithmetic_parens, try_fold_logical,
};
use crate::ir::shell_ir::Command;
use crate::ir::ShellValue;
use crate::models::Result;
impl super::posix::PosixEmitter {
    pub fn emit_shell_value(&self, value: &ShellValue) -> Result<String> {
        let choice = match value {
            ShellValue::String(_) => "literal_string",
            ShellValue::Variable(_) => "variable",
            ShellValue::Bool(_) => "bool",
            ShellValue::CommandSubst(_) => "cmd_subst",
            ShellValue::Concat(_) => "concat",
            ShellValue::Comparison { .. } => "comparison",
            ShellValue::Arithmetic { .. } => "arithmetic",
            ShellValue::LogicalAnd { .. }
            | ShellValue::LogicalOr { .. }
            | ShellValue::LogicalNot { .. } => "logical",
            ShellValue::Arg { .. } | ShellValue::ArgWithDefault { .. } | ShellValue::ArgCount => {
                "arg_access"
            }
            ShellValue::EnvVar { .. } => "env_var",
            ShellValue::ExitCode => "exit_code",
            ShellValue::DynamicArrayAccess { .. } => "dynamic_array",
            ShellValue::Glob(_) => "glob_pattern",
        };
        self.record_decision("value_emit", choice, "Value");

        match value {
            ShellValue::String(s) => Ok(escape_shell_string(s)),
            ShellValue::Bool(b) => Ok(self.emit_bool_value(*b)),
            ShellValue::Variable(name) => Ok(format!("\"${}\"", escape_variable_name(name))),
            // Sprint 27a: Environment variable expansion
            ShellValue::EnvVar { name, default } => match default {
                None => Ok(format!("\"${{{}}}\"", name)),
                Some(def) => Ok(format!("\"${{{}:-{}}}\"", name, def)),
            },
            ShellValue::Concat(parts) => self.emit_concatenation(parts),
            ShellValue::CommandSubst(cmd) => {
                let cmd_str = self.emit_command(cmd)?;
                Ok(format!("\"$({cmd_str})\""))
            }
            ShellValue::Comparison { op, left, right } => self.emit_comparison(op, left, right),
            ShellValue::Arithmetic { op, left, right } => self.emit_arithmetic(op, left, right),
            ShellValue::LogicalAnd { left, right } => {
                // Constant-fold all-literal boolean expressions at compile time
                if let (Some(l), Some(r)) = (try_fold_logical(left), try_fold_logical(right)) {
                    return Ok(self.emit_bool_value(l && r));
                }
                let left_str = self.emit_logical_operand(left)?;
                let right_str = self.emit_logical_operand(right)?;
                Ok(format!("$(({left_str} && {right_str}))"))
            }
            ShellValue::LogicalOr { left, right } => {
                if let (Some(l), Some(r)) = (try_fold_logical(left), try_fold_logical(right)) {
                    return Ok(self.emit_bool_value(l || r));
                }
                let left_str = self.emit_logical_operand(left)?;
                let right_str = self.emit_logical_operand(right)?;
                Ok(format!("$(({left_str} || {right_str}))"))
            }
            ShellValue::LogicalNot { operand } => {
                if let Some(b) = try_fold_logical(operand) {
                    return Ok(self.emit_bool_value(!b));
                }
                let operand_str = self.emit_logical_operand(operand)?;
                Ok(format!("$((!{operand_str}))"))
            }
            // Sprint 27b: Command-line argument access
            ShellValue::Arg { position } => match position {
                Some(n) => Ok(format!("\"${}\"", n)), // "$1", "$2", etc.
                None => Ok("\"$@\"".to_string()),     // All args
            },
            // P0-POSITIONAL-PARAMETERS: Argument with default value
            ShellValue::ArgWithDefault { position, default } => Ok(format!(
                "\"${{{}:-{}}}\"",
                position,
                escape_shell_string(default)
            )),
            ShellValue::ArgCount => Ok("\"$#\"".to_string()), // Argument count
            // Sprint 27c: Exit code access - GREEN PHASE
            ShellValue::ExitCode => Ok("\"$?\"".to_string()), // Exit code of last command
            // Dynamic array access: arr[i] → eval-based POSIX lookup
            ShellValue::DynamicArrayAccess { array, index } => {
                let idx_expr = self.emit_dynamic_index_expr(index)?;
                Ok(format!(
                    "\"$(eval \"printf '%s' \\\"\\${}_{}\\\"\")\"",
                    escape_variable_name(array),
                    idx_expr
                ))
            }
            // GH-148: Glob patterns emitted UNQUOTED for shell expansion
            ShellValue::Glob(pattern) => Ok(pattern.clone()),
        }
    }

    pub(crate) fn emit_comparison(
        &self,
        op: &crate::ir::shell_ir::ComparisonOp,
        left: &ShellValue,
        right: &ShellValue,
    ) -> Result<String> {
        use crate::ir::shell_ir::ComparisonOp;

        let left_val = self.emit_shell_value(left)?;
        let right_val = self.emit_shell_value(right)?;

        let op_str = match op {
            ComparisonOp::NumEq => "-eq",
            ComparisonOp::NumNe => "-ne",
            ComparisonOp::Gt => "-gt",
            ComparisonOp::Ge => "-ge",
            ComparisonOp::Lt => "-lt",
            ComparisonOp::Le => "-le",
            ComparisonOp::StrEq => "=",
            ComparisonOp::StrNe => "!=",
        };

        // Generate POSIX test command: [ "$left" -op "$right" ]
        Ok(format!("[ {left_val} {op_str} {right_val} ]"))
    }

    pub(crate) fn emit_arithmetic(
        &self,
        op: &crate::ir::shell_ir::ArithmeticOp,
        left: &ShellValue,
        right: &ShellValue,
    ) -> Result<String> {
        // For arithmetic, emit raw values (no quotes needed inside $((...)))
        // Pass parent op so children can decide about parentheses
        let left_str = self.emit_arithmetic_operand(left, Some(op), false)?;
        let right_str = self.emit_arithmetic_operand(right, Some(op), true)?;

        let op_str = arithmetic_op_str(op);

        // Generate POSIX arithmetic expansion: $((expr))
        Ok(format!("$(({left_str} {op_str} {right_str}))"))
    }

    pub(crate) fn emit_arithmetic_operand(
        &self,
        value: &ShellValue,
        parent_op: Option<&crate::ir::shell_ir::ArithmeticOp>,
        is_right: bool,
    ) -> Result<String> {
        match value {
            ShellValue::String(s) => Ok(s.clone()),
            ShellValue::Variable(name) => Ok(escape_variable_name(name)),
            ShellValue::Arithmetic { op, left, right } => {
                self.emit_nested_arithmetic(op, left, right, parent_op, is_right)
            }
            ShellValue::CommandSubst(cmd) => self.emit_arithmetic_cmd_subst(cmd),
            ShellValue::DynamicArrayAccess { array, index } => {
                self.emit_arithmetic_dynamic_access(array, index)
            }
            _ => Err(crate::models::Error::Emission(format!(
                "Unsupported value in arithmetic expression: {:?}",
                value
            ))),
        }
    }

    /// Emit a nested arithmetic expression with precedence-aware parenthesization.
    pub(crate) fn emit_nested_arithmetic(
        &self,
        op: &crate::ir::shell_ir::ArithmeticOp,
        left: &ShellValue,
        right: &ShellValue,
        parent_op: Option<&crate::ir::shell_ir::ArithmeticOp>,
        is_right: bool,
    ) -> Result<String> {
        let left_str = self.emit_arithmetic_operand(left, Some(op), false)?;
        let right_str = self.emit_arithmetic_operand(right, Some(op), true)?;
        let op_str = arithmetic_op_str(op);
        let expr = format!("{left_str} {op_str} {right_str}");

        if needs_arithmetic_parens(op, parent_op, is_right) {
            Ok(format!("({expr})"))
        } else {
            Ok(expr)
        }
    }

    /// Emit a command substitution in arithmetic context: $(func arg1 arg2).
    pub(crate) fn emit_arithmetic_cmd_subst(&self, cmd: &Command) -> Result<String> {
        let mut parts = vec![cmd.program.clone()];
        for arg in &cmd.args {
            parts.push(self.emit_shell_value(arg)?);
        }
        Ok(format!("$({})", parts.join(" ")))
    }

    /// Emit dynamic array access in arithmetic context.
    pub(crate) fn emit_arithmetic_dynamic_access(
        &self,
        array: &str,
        index: &ShellValue,
    ) -> Result<String> {
        let idx_expr = self.emit_dynamic_index_expr(index)?;
        Ok(format!(
            "$(eval \"printf '%s' \\\"\\${}_{}\\\"\")",
            escape_variable_name(array),
            idx_expr
        ))
    }

    /// Emit a value for use inside a logical/boolean arithmetic context.
    /// Returns bare variable names and values without quotes (for use inside $(( ))).
    pub(crate) fn emit_logical_operand(&self, value: &ShellValue) -> Result<String> {
        match value {
            ShellValue::String(s) => Ok(s.clone()),
            ShellValue::Variable(name) => Ok(escape_variable_name(name)),
            ShellValue::Bool(b) => Ok(if *b { "1".to_string() } else { "0".to_string() }),
            ShellValue::LogicalAnd { left, right } => {
                let l = self.emit_logical_operand(left)?;
                let r = self.emit_logical_operand(right)?;
                Ok(format!("{l} && {r}"))
            }
            ShellValue::LogicalOr { left, right } => {
                let l = self.emit_logical_operand(left)?;
                let r = self.emit_logical_operand(right)?;
                Ok(format!("({l} || {r})"))
            }
            ShellValue::LogicalNot { operand } => {
                let o = self.emit_logical_operand(operand)?;
                Ok(format!("!{o}"))
            }
            ShellValue::Arithmetic { op, left, right } => {
                let l = self.emit_arithmetic_operand(left, Some(op), false)?;
                let r = self.emit_arithmetic_operand(right, Some(op), true)?;
                let op_str = arithmetic_op_str(op);
                Ok(format!("({l} {op_str} {r})"))
            }
            ShellValue::Comparison { op, left, right } => self.emit_comparison(op, left, right),
            _ => self.emit_shell_value(value),
        }
    }

    pub(crate) fn emit_bool_value(&self, value: bool) -> String {
        if value { "true" } else { "false" }.to_string()
    }

    /// Emit the index expression for dynamic array access.
    /// Returns shell expression like `${i}` for variable or `$((i + 1))` for arithmetic.
    pub(crate) fn emit_dynamic_index_expr(&self, index: &ShellValue) -> Result<String> {
        match index {
            ShellValue::Variable(v) => Ok(format!("${{{}}}", escape_variable_name(v))),
            ShellValue::Arithmetic { op, left, right } => {
                let left_str = self.emit_arithmetic_operand(left, Some(op), false)?;
                let right_str = self.emit_arithmetic_operand(right, Some(op), true)?;
                let op_str = arithmetic_op_str(op);
                Ok(format!("$(({} {} {}))", left_str, op_str, right_str))
            }
            _ => Ok("0".to_string()),
        }
    }

    pub(crate) fn emit_concatenation(&self, parts: &[ShellValue]) -> Result<String> {
        let mut result = String::new();
        result.push('"');

        for part in parts {
            self.append_concat_part(&mut result, part)?;
        }

        result.push('"');
        Ok(result)
    }

    pub(crate) fn append_concat_part(&self, result: &mut String, part: &ShellValue) -> Result<()> {
        match part {
            ShellValue::String(s) => result.push_str(s),
            ShellValue::Bool(b) => result.push_str(&self.emit_bool_value(*b)),
            ShellValue::Variable(name) => {
                result.push_str(&format!("${{{}}}", escape_variable_name(name)));
            }
            // Sprint 27a: Environment variable expansion in concatenation
            ShellValue::EnvVar { name, default } => match default {
                None => result.push_str(&format!("${{{}}}", name)),
                Some(def) => result.push_str(&format!("${{{}:-{}}}", name, def)),
            },
            ShellValue::CommandSubst(cmd) => {
                let cmd_str = self.emit_command(cmd)?;
                result.push_str(&format!("$({cmd_str})"));
            }
            ShellValue::Concat(_) => {
                // Nested concatenation - flatten it
                let nested = self.emit_shell_value(part)?;
                self.append_flattened_content(result, &nested);
            }
            ShellValue::Comparison { .. } => {
                // Comparisons don't make sense in concatenation context
                // This should be caught at validation, but handle gracefully
                return Err(crate::models::Error::IrGeneration(
                    "Comparison expression cannot be used in string concatenation".to_string(),
                ));
            }
            ShellValue::Arithmetic { op, left, right } => {
                // Arithmetic in concat context - emit the $((...)) form
                let arith_str = self.emit_arithmetic(op, left, right)?;
                result.push_str(&arith_str);
            }
            ShellValue::LogicalAnd { .. }
            | ShellValue::LogicalOr { .. }
            | ShellValue::LogicalNot { .. } => {
                // Logical operators don't make sense in concatenation context
                return Err(crate::models::Error::IrGeneration(
                    "Logical expression cannot be used in string concatenation".to_string(),
                ));
            }
            // Sprint 27b: Command-line argument access in concatenation
            ShellValue::Arg { position } => match position {
                Some(n) => result.push_str(&format!("${}", n)),
                None => result.push_str("$@"),
            },
            // P0-POSITIONAL-PARAMETERS: Argument with default value in concatenation
            ShellValue::ArgWithDefault { position, default } => {
                result.push_str(&format!("${{{}:-{}}}", position, default));
            }
            ShellValue::ArgCount => {
                result.push_str("$#");
            }
            // Sprint 27c: Exit code in concatenation - GREEN PHASE
            ShellValue::ExitCode => {
                result.push_str("$?");
            }
            // Dynamic array access in concatenation
            ShellValue::DynamicArrayAccess { array, index } => {
                let idx_expr = self.emit_dynamic_index_expr(index)?;
                result.push_str(&format!(
                    "$(eval \"printf '%s' \\\"\\${}_{}\\\"\")",
                    escape_variable_name(array),
                    idx_expr
                ));
            }
            ShellValue::Glob(pattern) => {
                result.push_str(pattern);
            }
        }
        Ok(())
    }

    pub(crate) fn append_flattened_content(&self, result: &mut String, nested: &str) {
        // Remove quotes from nested value and add content
        if nested.starts_with('"') && nested.ends_with('"') {
            result.push_str(&nested[1..nested.len() - 1]);
        } else {
            result.push_str(nested);
        }
    }

    pub(crate) fn emit_command(&self, cmd: &Command) -> Result<String> {
        let mut result = escape_command_name(&cmd.program);

        for arg in &cmd.args {
            result.push(' ');
            result.push_str(&self.emit_shell_value(arg)?);
        }

        Ok(result)
    }

    pub fn emit_test_expression(&self, test: &ShellValue) -> Result<String> {
        self.record_decision("test_syntax", classify_test_expression(test), "Test");

        match test {
            ShellValue::Bool(true) => Ok("true".to_string()),
            ShellValue::Bool(false) => Ok("false".to_string()),
            ShellValue::Variable(name) => {
                Ok(format!("test -n \"${}\"", escape_variable_name(name)))
            }
            ShellValue::String(s) => Ok(self.emit_test_string_literal(s)),
            ShellValue::Comparison { .. } => self.emit_shell_value(test),
            ShellValue::LogicalNot { operand } => self.emit_test_not(operand),
            ShellValue::LogicalAnd { left, right } => {
                self.emit_test_binary_logical(left, right, true)
            }
            ShellValue::LogicalOr { left, right } => {
                self.emit_test_binary_logical(left, right, false)
            }
            ShellValue::CommandSubst(cmd) => self.emit_test_command_subst(test, cmd),
            other => {
                let value = self.emit_shell_value(other)?;
                Ok(format!("test -n {value}"))
            }
        }
    }

    /// Emit a string literal in test context: "true"/"0" map to true, all else to false.
    pub(crate) fn emit_test_string_literal(&self, s: &str) -> String {
        if s == "true" || s == "0" {
            "true".to_string()
        } else {
            "false".to_string()
        }
    }

    /// Emit a LogicalNot in test context using command negation (! cmd).
    pub(crate) fn emit_test_not(&self, operand: &ShellValue) -> Result<String> {
        if let Some(b) = try_fold_logical(operand) {
            return Ok(self.emit_bool_value(!b));
        }
        // For variable operands, use ! "$var" (treat as boolean command)
        if let ShellValue::Variable(name) = operand {
            return Ok(format!("! \"${}\"", escape_variable_name(name)));
        }
        let inner = self.emit_test_expression(operand)?;
        Ok(format!("! {inner}"))
    }

    /// Emit a LogicalAnd or LogicalOr in test context.
    /// When `is_and` is true, emits `&&`; otherwise emits `||`.
    pub(crate) fn emit_test_binary_logical(
        &self,
        left: &ShellValue,
        right: &ShellValue,
        is_and: bool,
    ) -> Result<String> {
        if let (Some(l), Some(r)) = (try_fold_logical(left), try_fold_logical(right)) {
            let folded = if is_and { l && r } else { l || r };
            return Ok(self.emit_bool_value(folded));
        }
        let l = self.emit_test_expression(left)?;
        let r = self.emit_test_expression(right)?;
        let op = if is_and { "&&" } else { "||" };
        Ok(format!("{l} {op} {r}"))
    }

    /// Emit a CommandSubst in test context, distinguishing predicates from value-producing functions.
    pub(crate) fn emit_test_command_subst(
        &self,
        test: &ShellValue,
        cmd: &Command,
    ) -> Result<String> {
        if self.is_predicate_function(&cmd.program) {
            self.emit_command(cmd)
        } else {
            let value = self.emit_shell_value(test)?;
            Ok(format!("test -n {value}"))
        }
    }

    pub(crate) fn is_predicate_function(&self, name: &str) -> bool {
        // Predicate functions return bool via exit code (0 = true, 1 = false)
        matches!(
            name,
            "rash_string_contains"
                | "rash_string_starts_with"
                | "rash_string_ends_with"
                | "rash_fs_exists"
                | "rash_fs_is_file"
                | "rash_fs_is_dir"
                | "test"
                | "["
        )
    }
}