depyler-core 3.24.0

Core transpilation engine for the Depyler Python-to-Rust transpiler
Documentation
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Random Module Code Generation - EXTREME TDD
//!
//! Handles Python `random` module method conversions to Rust rand crate.
//! Extracted from expr_gen.rs for testability and maintainability.
//!
//! Coverage target: 100% line coverage, 100% branch coverage

use crate::hir::HirExpr;
use crate::rust_gen::context::{CodeGenContext, ToRustExpr};
use anyhow::{bail, Result};
use syn::parse_quote;

/// Convert Python random module method calls to Rust rand crate
///
/// # Supported Methods
/// - Basic: random, randint, randrange, uniform
/// - Sequence: choice, shuffle, sample, choices
/// - Distribution: gauss/normalvariate, expovariate
///
/// # Complexity: 10 (match with 10+ branches)
pub fn convert_random_method(
    method: &str,
    args: &[HirExpr],
    ctx: &mut CodeGenContext,
) -> Result<Option<syn::Expr>> {
    // DEPYLER-1018: In NASA mode, return stub values instead of using rand crate
    // This ensures std-only compilation at the cost of runtime correctness
    if ctx.type_mapper.nasa_mode {
        return convert_random_method_nasa_stub(method, args, ctx);
    }

    let arg_exprs: Vec<syn::Expr> = args
        .iter()
        .map(|arg| arg.to_rust_expr(ctx))
        .collect::<Result<Vec<_>>>()?;

    ctx.needs_rand = true;

    let result = match method {
        "random" => convert_random(&arg_exprs)?,
        "randint" => convert_randint(&arg_exprs)?,
        "randrange" => convert_randrange(&arg_exprs)?,
        "uniform" => convert_uniform(&arg_exprs)?,
        "choice" => convert_choice(&arg_exprs)?,
        "shuffle" => convert_shuffle(&arg_exprs)?,
        "sample" => convert_sample(&arg_exprs)?,
        "choices" => convert_choices(&arg_exprs)?,
        "gauss" | "normalvariate" => {
            ctx.needs_rand_distr = true;
            convert_gauss(method, &arg_exprs)?
        }
        "expovariate" => {
            ctx.needs_rand_distr = true;
            convert_expovariate(&arg_exprs)?
        }
        "seed" => convert_seed(&arg_exprs)?,
        _ => bail!("random.{} not implemented yet", method),
    };

    Ok(Some(result))
}

/// random.random() → rand::random::<f64>()
fn convert_random(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if !arg_exprs.is_empty() {
        bail!("random.random() takes no arguments");
    }
    Ok(parse_quote! { rand::random::<f64>() })
}

/// random.randint(a, b) → inclusive range
fn convert_randint(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("random.randint() requires exactly 2 arguments");
    }
    let a = &arg_exprs[0];
    let b = &arg_exprs[1];
    Ok(parse_quote! {
        {
            use rand::Rng;
            rand::thread_rng().gen_range(#a..=#b)
        }
    })
}

/// random.randrange(stop) or (start, stop) or (start, stop, step)
fn convert_randrange(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.is_empty() || arg_exprs.len() > 3 {
        bail!("random.randrange() requires 1-3 arguments");
    }

    if arg_exprs.len() == 1 {
        let stop = &arg_exprs[0];
        Ok(parse_quote! {
            {
                use rand::Rng;
                rand::thread_rng().gen_range(0..#stop)
            }
        })
    } else if arg_exprs.len() == 2 {
        let start = &arg_exprs[0];
        let stop = &arg_exprs[1];
        Ok(parse_quote! {
            {
                use rand::Rng;
                rand::thread_rng().gen_range(#start..#stop)
            }
        })
    } else {
        let start = &arg_exprs[0];
        let stop = &arg_exprs[1];
        let step = &arg_exprs[2];
        Ok(parse_quote! {
            {
                use rand::Rng;
                let start = #start;
                let stop = #stop;
                let step: i32 = #step;
                let num_steps = ((stop - start) / step).max(0);
                let offset = rand::thread_rng().gen_range(0..num_steps);
                start + offset * step
            }
        })
    }
}

/// random.uniform(a, b) → float in [a, b]
fn convert_uniform(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("random.uniform() requires exactly 2 arguments");
    }
    let a = &arg_exprs[0];
    let b = &arg_exprs[1];
    Ok(parse_quote! {
        {
            use rand::Rng;
            rand::thread_rng().gen_range((#a as f64)..=(#b as f64))
        }
    })
}

/// random.choice(seq) → random element
fn convert_choice(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("random.choice() requires exactly 1 argument");
    }
    let seq = &arg_exprs[0];
    Ok(parse_quote! {
        {
            use rand::seq::SliceRandom;
            *#seq.choose(&mut rand::thread_rng()).unwrap()
        }
    })
}

/// random.shuffle(seq) → shuffle in place
fn convert_shuffle(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("random.shuffle() requires exactly 1 argument");
    }
    let seq = &arg_exprs[0];
    Ok(parse_quote! {
        {
            use rand::seq::SliceRandom;
            #seq.shuffle(&mut rand::thread_rng())
        }
    })
}

/// random.sample(seq, k) → k unique elements
fn convert_sample(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("random.sample() requires exactly 2 arguments");
    }
    let seq = &arg_exprs[0];
    let k = &arg_exprs[1];
    Ok(parse_quote! {
        {
            use rand::seq::SliceRandom;
            #seq.choose_multiple(&mut rand::thread_rng(), #k as usize)
                .cloned()
                .collect::<Vec<_>>()
        }
    })
}

/// random.choices(seq, k=1) → k elements with replacement
fn convert_choices(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.is_empty() {
        bail!("random.choices() requires at least 1 argument");
    }
    let seq = &arg_exprs[0];
    let k = if arg_exprs.len() > 1 {
        arg_exprs[1].clone()
    } else {
        parse_quote! { 1 }
    };
    Ok(parse_quote! {
        {
            use rand::seq::SliceRandom;
            let mut rng = rand::thread_rng();
            (0..#k)
                .map(|_| #seq.choose(&mut rng).cloned().unwrap())
                .collect::<Vec<_>>()
        }
    })
}

/// random.gauss(mu, sigma) → normal distribution
fn convert_gauss(method: &str, arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 2 {
        bail!("random.{}() requires exactly 2 arguments", method);
    }
    let mu = &arg_exprs[0];
    let sigma = &arg_exprs[1];
    Ok(parse_quote! {
        {
            use rand::distributions::Distribution;
            let normal = rand_distr::Normal::new(#mu as f64, #sigma as f64).unwrap();
            normal.sample(&mut rand::thread_rng())
        }
    })
}

/// random.expovariate(lambd) → exponential distribution
fn convert_expovariate(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() != 1 {
        bail!("random.expovariate() requires exactly 1 argument");
    }
    let lambd = &arg_exprs[0];
    Ok(parse_quote! {
        {
            use rand::distributions::Distribution;
            let exp = rand_distr::Exp::new(#lambd as f64).unwrap();
            exp.sample(&mut rand::thread_rng())
        }
    })
}

/// random.seed([n]) → set RNG seed (simplified: no-op)
fn convert_seed(arg_exprs: &[syn::Expr]) -> Result<syn::Expr> {
    if arg_exprs.len() > 1 {
        bail!("random.seed() requires 0 or 1 argument");
    }
    if arg_exprs.is_empty() {
        // seed() with no args - use system entropy (no-op, thread_rng is already seeded)
        Ok(parse_quote! { () })
    } else {
        let seed_val = &arg_exprs[0];
        // Note: thread_rng() cannot be seeded. For now, we generate a no-op.
        Ok(parse_quote! {
            {
                // Note: Seeding not fully implemented - use StdRng instead of thread_rng
                let _seed = #seed_val;
                ()
            }
        })
    }
}

/// DEPYLER-1018: NASA mode stub for random methods
///
/// Returns deterministic values to enable std-only compilation.
/// WARNING: These are NOT random - for compilation testing only!
fn convert_random_method_nasa_stub(
    method: &str,
    args: &[HirExpr],
    ctx: &mut CodeGenContext,
) -> Result<Option<syn::Expr>> {
    let arg_exprs: Vec<syn::Expr> = args
        .iter()
        .map(|arg| arg.to_rust_expr(ctx))
        .collect::<Result<Vec<_>>>()?;

    let result = match method {
        // random() returns 0.5 (middle of [0, 1))
        "random" => parse_quote! { 0.5_f64 },
        // randint(a, b) returns a (first value in range)
        "randint" => {
            if !arg_exprs.is_empty() {
                let a = &arg_exprs[0];
                parse_quote! { #a }
            } else {
                parse_quote! { 0 }
            }
        }
        // randrange returns start value
        "randrange" => {
            if arg_exprs.len() >= 2 {
                let start = &arg_exprs[0];
                parse_quote! { #start }
            } else {
                // Both single arg (0..stop) and no args return 0
                parse_quote! { 0 }
            }
        }
        // uniform(a, b) returns a
        "uniform" => {
            if !arg_exprs.is_empty() {
                let a = &arg_exprs[0];
                parse_quote! { #a as f64 }
            } else {
                parse_quote! { 0.0_f64 }
            }
        }
        // choice returns first element
        "choice" => {
            if !arg_exprs.is_empty() {
                let seq = &arg_exprs[0];
                parse_quote! { #seq[0].clone() }
            } else {
                bail!("random.choice() requires a sequence argument")
            }
        }
        // shuffle is no-op (already shuffled... not)
        "shuffle" => parse_quote! { () },
        // sample returns first k elements
        "sample" => {
            if arg_exprs.len() >= 2 {
                let seq = &arg_exprs[0];
                let k = &arg_exprs[1];
                parse_quote! { #seq[..#k as usize].to_vec() }
            } else {
                bail!("random.sample() requires 2 arguments")
            }
        }
        // choices returns first element repeated
        "choices" => {
            if !arg_exprs.is_empty() {
                let seq = &arg_exprs[0];
                let k = if arg_exprs.len() >= 2 {
                    &arg_exprs[1]
                } else {
                    &parse_quote! { 1 }
                };
                parse_quote! { vec![#seq[0].clone(); #k as usize] }
            } else {
                bail!("random.choices() requires a sequence argument")
            }
        }
        // gauss returns mu (mean)
        "gauss" | "normalvariate" => {
            if !arg_exprs.is_empty() {
                let mu = &arg_exprs[0];
                parse_quote! { #mu as f64 }
            } else {
                parse_quote! { 0.0_f64 }
            }
        }
        // expovariate returns 1/lambda (mean of exponential)
        "expovariate" => {
            if !arg_exprs.is_empty() {
                let lambd = &arg_exprs[0];
                parse_quote! { 1.0_f64 / (#lambd as f64) }
            } else {
                parse_quote! { 1.0_f64 }
            }
        }
        // seed is no-op
        "seed" => parse_quote! { () },
        _ => bail!("random.{} not implemented in NASA mode", method),
    };

    Ok(Some(result))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hir::Literal;

    /// Create a CodeGenContext with NASA mode disabled for testing rand crate integration
    fn ctx_with_rand_enabled() -> CodeGenContext<'static> {
        let mut ctx = CodeGenContext::default();
        // DEPYLER-1018: Disable NASA mode to test actual rand crate integration
        ctx.type_mapper = Box::leak(Box::new(ctx.type_mapper.clone().with_nasa_mode(false)));
        ctx
    }

    #[test]
    fn test_convert_random_random() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("random", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_rand);
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("rand") && code.contains("random"));
    }

    #[test]
    fn test_convert_random_random_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Int(1))];
        let result = convert_random_method("random", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_randint() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Int(1)),
            HirExpr::Literal(Literal::Int(10)),
        ];
        let result = convert_random_method("randint", &args, &mut ctx);
        assert!(result.is_ok());
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("gen_range"));
    }

    #[test]
    fn test_convert_random_randint_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Int(1))];
        let result = convert_random_method("randint", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_randrange_single() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Int(10))];
        let result = convert_random_method("randrange", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_randrange_two() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Int(5)),
            HirExpr::Literal(Literal::Int(10)),
        ];
        let result = convert_random_method("randrange", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_randrange_three() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Int(0)),
            HirExpr::Literal(Literal::Int(10)),
            HirExpr::Literal(Literal::Int(2)),
        ];
        let result = convert_random_method("randrange", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_randrange_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("randrange", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_uniform() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Float(0.0)),
            HirExpr::Literal(Literal::Float(1.0)),
        ];
        let result = convert_random_method("uniform", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_uniform_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Float(0.0))];
        let result = convert_random_method("uniform", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_choice() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Var("items".to_string())];
        let result = convert_random_method("choice", &args, &mut ctx);
        assert!(result.is_ok());
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("choose"));
    }

    #[test]
    fn test_convert_random_choice_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("choice", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_shuffle() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Var("items".to_string())];
        let result = convert_random_method("shuffle", &args, &mut ctx);
        assert!(result.is_ok());
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("shuffle"));
    }

    #[test]
    fn test_convert_random_shuffle_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("shuffle", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_sample() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Var("items".to_string()),
            HirExpr::Literal(Literal::Int(3)),
        ];
        let result = convert_random_method("sample", &args, &mut ctx);
        assert!(result.is_ok());
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("choose_multiple"));
    }

    #[test]
    fn test_convert_random_sample_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Var("items".to_string())];
        let result = convert_random_method("sample", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_choices() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Var("items".to_string())];
        let result = convert_random_method("choices", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_choices_with_k() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Var("items".to_string()),
            HirExpr::Literal(Literal::Int(5)),
        ];
        let result = convert_random_method("choices", &args, &mut ctx);
        assert!(result.is_ok());
    }

    #[test]
    fn test_convert_random_choices_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("choices", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_gauss() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Float(0.0)),
            HirExpr::Literal(Literal::Float(1.0)),
        ];
        let result = convert_random_method("gauss", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_rand_distr);
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("Normal"));
    }

    #[test]
    fn test_convert_random_normalvariate() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![
            HirExpr::Literal(Literal::Float(0.0)),
            HirExpr::Literal(Literal::Float(1.0)),
        ];
        let result = convert_random_method("normalvariate", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_rand_distr);
    }

    #[test]
    fn test_convert_random_gauss_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Float(0.0))];
        let result = convert_random_method("gauss", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_expovariate() {
        let mut ctx = ctx_with_rand_enabled();
        let args = vec![HirExpr::Literal(Literal::Float(1.0))];
        let result = convert_random_method("expovariate", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(ctx.needs_rand_distr);
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("Exp"));
    }

    #[test]
    fn test_convert_random_expovariate_wrong_args() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("expovariate", &args, &mut ctx);
        assert!(result.is_err());
    }

    #[test]
    fn test_convert_random_unknown() {
        let mut ctx = ctx_with_rand_enabled();
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("unknown", &args, &mut ctx);
        assert!(result.is_err());
        let err = result.err().unwrap();
        assert!(err.to_string().contains("not implemented"));
    }

    // NASA mode stub tests
    #[test]
    fn test_nasa_mode_random_returns_stub() {
        let mut ctx = CodeGenContext::default(); // Default is NASA mode
        let args: Vec<HirExpr> = vec![];
        let result = convert_random_method("random", &args, &mut ctx);
        assert!(result.is_ok());
        assert!(!ctx.needs_rand); // No rand crate needed in NASA mode
        let expr = result.unwrap().unwrap();
        let code = quote::quote!(#expr).to_string();
        assert!(code.contains("0.5")); // Stub value
    }
}