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
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

use super::parser::parse;
use crate::ast::restricted::{BinaryOp, Expr, Literal, Pattern, Stmt, Type, UnaryOp};

// ============================================================================
// convert_method_call_expr: method calls on receivers
// ============================================================================

#[test]
fn test_method_call_basic_methods() {
    // push, len, trim, to_string all produce MethodCall nodes
    let ast = parse(r#"fn main() { let mut v = vec![1]; v.push(2); }"#).unwrap();
    assert!(
        matches!(&ast.functions[0].body[1], Stmt::Expr(Expr::MethodCall { method, .. }) if method == "push")
    );
    let ast2 = parse(r#"fn main() { let v = vec![1]; let n = v.len(); }"#).unwrap();
    assert!(
        matches!(&ast2.functions[0].body[1], Stmt::Let { value: Expr::MethodCall { method, .. }, .. } if method == "len")
    );
    let ast3 = parse(r#"fn main() { let s = "hi"; let t = s.trim(); }"#).unwrap();
    assert!(
        matches!(&ast3.functions[0].body[1], Stmt::Let { value: Expr::MethodCall { method, .. }, .. } if method == "trim")
    );
}

#[test]
fn test_method_call_with_args_and_chained() {
    let ast = parse(r#"fn main() { let s = "hi"; let b = s.contains("h"); }"#).unwrap();
    match &ast.functions[0].body[1] {
        Stmt::Let {
            value: Expr::MethodCall { method, args, .. },
            ..
        } => {
            assert_eq!(method, "contains");
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected MethodCall contains"),
    }
    // Chained method call: receiver is itself a MethodCall
    let ast2 = parse(r#"fn main() { let s = "hi"; let t = s.trim().to_string(); }"#).unwrap();
    match &ast2.functions[0].body[1] {
        Stmt::Let {
            value: Expr::MethodCall {
                method, receiver, ..
            },
            ..
        } => {
            assert_eq!(method, "to_string");
            assert!(matches!(**receiver, Expr::MethodCall { .. }));
        }
        _ => panic!("Expected chained MethodCall"),
    }
}

#[test]
fn test_method_call_env_args_collect_to_positional() {
    let ast =
        parse(r#"fn main() { let args: Vec<String> = std::env::args().collect(); }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[0],
        Stmt::Let {
            value: Expr::PositionalArgs,
            ..
        }
    ));
}

// ============================================================================
// convert_macro_stmt and convert_macro_expr
// ============================================================================

#[test]
fn test_macro_stmt_variants() {
    // println! single arg
    let ast = parse(r#"fn main() { println!("hello"); }"#).unwrap();
    match &ast.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_println");
            assert_eq!(args.len(), 1);
        }
        _ => panic!("Expected rash_println single arg"),
    }
    // eprintln! multi-arg produces __format_concat
    let ast2 = parse(r#"fn main() { eprintln!("error: {}", msg); }"#).unwrap();
    match &ast2.functions[0].body[0] {
        Stmt::Expr(Expr::FunctionCall { name, args }) => {
            assert_eq!(name, "rash_eprintln");
            assert!(
                matches!(&args[0], Expr::FunctionCall { name, .. } if name == "__format_concat")
            );
        }
        _ => panic!("Expected rash_eprintln with format_concat"),
    }
    // print! no-newline
    let ast3 = parse(r#"fn main() { print!("x"); }"#).unwrap();
    assert!(
        matches!(&ast3.functions[0].body[0], Stmt::Expr(Expr::FunctionCall { name, .. }) if name == "rash_print")
    );
    // unsupported macro returns error
    assert!(parse(r#"fn main() { panic!("boom"); }"#).is_err());
}

#[test]
fn test_macro_expr_format() {
    // format! with single arg produces a plain Str literal
    let ast = parse(r#"fn main() { let s = format!("hello"); }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[0],
        Stmt::Let {
            value: Expr::Literal(Literal::Str(_)),
            ..
        }
    ));
    // format! with multiple args produces __format_concat
    let ast2 = parse(r#"fn main() { let s = format!("hi {}", name); }"#).unwrap();
    assert!(
        matches!(&ast2.functions[0].body[0], Stmt::Let { value: Expr::FunctionCall { name, .. }, .. } if name == "__format_concat")
    );
    // unsupported macro in expression returns error
    assert!(parse(r#"fn main() { let x = assert!(true); }"#).is_err());
}

// ============================================================================
// convert_call_expr: function calls
// ============================================================================

#[test]
fn test_function_call_arities() {
    let ast = parse(r#"fn main() { let r = foo(); }"#).unwrap();
    assert!(
        matches!(&ast.functions[0].body[0], Stmt::Let { value: Expr::FunctionCall { name, args }, .. } if name == "foo" && args.is_empty())
    );
    let ast2 = parse(r#"fn main() { let r = add(1, 2, 3); }"#).unwrap();
    match &ast2.functions[0].body[0] {
        Stmt::Let {
            value: Expr::FunctionCall { name, args },
            ..
        } => {
            assert_eq!(name, "add");
            assert_eq!(args.len(), 3);
        }
        _ => panic!("Expected add(1,2,3)"),
    }
    // nested path like std::env::var
    let ast3 = parse(r#"fn main() { let r = std::env::var("HOME"); }"#).unwrap();
    assert!(
        matches!(&ast3.functions[0].body[0], Stmt::Let { value: Expr::FunctionCall { name, .. }, .. } if name == "std::env::var")
    );
    // closure call not supported
    assert!(parse(r#"fn main() { let f = (|x| x)(5); }"#).is_err());
}

// ============================================================================
// convert_impl_block
// ============================================================================

#[test]
fn test_impl_block_multiple_methods() {
    let src = r#"
        struct Counter { count: u32 }
        impl Counter {
            fn increment(&mut self) { self.count = self.count + 1; }
            fn reset(&mut self) { self.count = 0; }
        }
        fn main() { let x = 0; }
    "#;
    let ast = parse(src).unwrap();
    let names: Vec<&str> = ast.functions.iter().map(|f| f.name.as_str()).collect();
    assert!(names.contains(&"increment"));
    assert!(names.contains(&"reset"));
}

#[test]
fn test_impl_block_empty_skipped() {
    let src = r#"struct Foo {} impl Foo {} fn main() { let x = 0; }"#;
    let ast = parse(src).unwrap();
    assert_eq!(ast.entry_point, "main");
}

// ============================================================================
// convert_closure
// ============================================================================

#[test]
fn test_closure_body_extracted() {
    let ast = parse(r#"fn main() { let f = |x| x + 1; }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[0],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Add,
                ..
            },
            ..
        }
    ));
}

#[test]
fn test_closure_with_block_body() {
    let ast = parse(r#"fn main() { let f = |x| { let y = x; y }; }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[0],
        Stmt::Let {
            value: Expr::Block(_),
            ..
        }
    ));
}

// ============================================================================
// convert_unary_expr
// ============================================================================

#[test]
fn test_unary_operators() {
    // !b → Unary Not
    let ast = parse(r#"fn main() { let b = true; let n = !b; }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[1],
        Stmt::Let {
            value: Expr::Unary {
                op: UnaryOp::Not,
                ..
            },
            ..
        }
    ));
    // -var → Unary Neg on variable
    let ast2 = parse(r#"fn main() { let x = 5; let n = -x; }"#).unwrap();
    assert!(matches!(
        &ast2.functions[0].body[1],
        Stmt::Let {
            value: Expr::Unary {
                op: UnaryOp::Neg,
                ..
            },
            ..
        }
    ));
    // -5 literal → I32(-5) directly (try_negate_int_literal path)
    let ast3 = parse(r#"fn main() { let n = -5; }"#).unwrap();
    assert!(matches!(
        &ast3.functions[0].body[0],
        Stmt::Let {
            value: Expr::Literal(Literal::I32(-5)),
            ..
        }
    ));
    // *r → deref strips to variable (Deref path)
    let ast4 = parse(r#"fn main() { let x = 5; let r = &x; let d = *r; }"#).unwrap();
    assert!(
        matches!(&ast4.functions[0].body[2], Stmt::Let { value: Expr::Variable(n), .. } if n == "r")
    );
}

#[test]
fn test_unary_neg_i32_min_edge_case() {
    let ast = parse(r#"fn main() { let n = -2147483648; }"#).unwrap();
    assert!(
        matches!(&ast.functions[0].body[0], Stmt::Let { value: Expr::Literal(Literal::I32(v)), .. } if *v == i32::MIN)
    );
}

// ============================================================================
// convert_binary_op: bitwise + shift + rem operators
// ============================================================================

#[test]
fn test_binary_ops_bitwise_and_shift() {
    let ast = parse(r#"fn main() { let a = 3; let b = 5; let c = a & b; }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[2],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitAnd,
                ..
            },
            ..
        }
    ));
    let ast2 = parse(r#"fn main() { let a = 3; let b = 5; let c = a | b; }"#).unwrap();
    assert!(matches!(
        &ast2.functions[0].body[2],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitOr,
                ..
            },
            ..
        }
    ));
    let ast3 = parse(r#"fn main() { let a = 3; let b = 5; let c = a ^ b; }"#).unwrap();
    assert!(matches!(
        &ast3.functions[0].body[2],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitXor,
                ..
            },
            ..
        }
    ));
    let ast4 = parse(r#"fn main() { let a = 1; let b = a << 2; }"#).unwrap();
    assert!(matches!(
        &ast4.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Shl,
                ..
            },
            ..
        }
    ));
    let ast5 = parse(r#"fn main() { let a = 8; let b = a >> 2; }"#).unwrap();
    assert!(matches!(
        &ast5.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Shr,
                ..
            },
            ..
        }
    ));
    let ast6 = parse(r#"fn main() { let a = 10; let b = a % 3; }"#).unwrap();
    assert!(matches!(
        &ast6.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Rem,
                ..
            },
            ..
        }
    ));
}

// ============================================================================
// compound assignment: &=, |=, ^=, <<=, >>=, /=, %=
// ============================================================================

#[test]
fn test_compound_bitwise_assigns() {
    let ast = parse(r#"fn main() { let mut x = 0xFF; x &= 0x0F; }"#).unwrap();
    assert!(matches!(
        &ast.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitAnd,
                ..
            },
            ..
        }
    ));
    let ast2 = parse(r#"fn main() { let mut x = 0; x |= 1; }"#).unwrap();
    assert!(matches!(
        &ast2.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitOr,
                ..
            },
            ..
        }
    ));
    let ast3 = parse(r#"fn main() { let mut x = 5; x ^= 3; }"#).unwrap();
    assert!(matches!(
        &ast3.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::BitXor,
                ..
            },
            ..
        }
    ));
    let ast4 = parse(r#"fn main() { let mut x = 1; x <<= 2; }"#).unwrap();
    assert!(matches!(
        &ast4.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Shl,
                ..
            },
            ..
        }
    ));
    let ast5 = parse(r#"fn main() { let mut x = 8; x >>= 1; }"#).unwrap();
    assert!(matches!(
        &ast5.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Shr,
                ..
            },
            ..
        }
    ));
    let ast6 = parse(r#"fn main() { let mut x = 10; x /= 2; }"#).unwrap();
    assert!(matches!(
        &ast6.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Div,
                ..
            },
            ..
        }
    ));
    let ast7 = parse(r#"fn main() { let mut x = 10; x %= 3; }"#).unwrap();
    assert!(matches!(
        &ast7.functions[0].body[1],
        Stmt::Let {
            value: Expr::Binary {
                op: BinaryOp::Rem,
                ..
            },
            ..
        }
    ));
}

// ============================================================================
// convert_literal: suffix variants
// ============================================================================

#[test]

include!("parser_coverage_tests2_tests_literal_u16.rs");