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
//! Unary operation code generation utilities
//!
//! This module provides helper functions for unary operation handling
//! in Python-to-Rust transpilation.

use crate::hir::{HirExpr, Literal, Type, UnaryOp};
use syn::parse_quote;

/// Convert Python unary operator to Rust expression
pub fn python_to_rust_unary(op: UnaryOp, operand: syn::Expr) -> syn::Expr {
    match op {
        UnaryOp::Not => parse_quote! { !#operand },
        UnaryOp::Neg => parse_quote! { -#operand },
        UnaryOp::Pos => operand, // No +x in Rust, just return operand
        UnaryOp::BitNot => parse_quote! { !#operand },
    }
}

/// Check if unary operator is logical NOT
pub fn is_logical_not(op: UnaryOp) -> bool {
    matches!(op, UnaryOp::Not)
}

/// Check if unary operator is arithmetic negation
pub fn is_negation(op: UnaryOp) -> bool {
    matches!(op, UnaryOp::Neg)
}

/// Check if unary operator is positive (no-op in Rust)
pub fn is_positive(op: UnaryOp) -> bool {
    matches!(op, UnaryOp::Pos)
}

/// Check if unary operator is bitwise NOT
pub fn is_bitwise_not(op: UnaryOp) -> bool {
    matches!(op, UnaryOp::BitNot)
}

/// Check if a type requires special NOT handling (collections use .is_empty())
pub fn type_needs_is_empty_for_not(ty: &Type) -> bool {
    matches!(
        ty,
        Type::List(_) | Type::Dict(_, _) | Type::Set(_) | Type::String
    )
}

/// Check if a type requires .is_none() for NOT (Optional types)
pub fn type_needs_is_none_for_not(ty: &Type) -> bool {
    matches!(ty, Type::Optional(_))
}

/// Check if expression is a double negation (--x)
pub fn is_double_negation(expr: &HirExpr) -> bool {
    matches!(
        expr,
        HirExpr::Unary {
            op: UnaryOp::Neg,
            operand
        } if matches!(operand.as_ref(), HirExpr::Unary { op: UnaryOp::Neg, .. })
    )
}

/// Check if expression is a double NOT (not not x)
pub fn is_double_not(expr: &HirExpr) -> bool {
    matches!(
        expr,
        HirExpr::Unary {
            op: UnaryOp::Not,
            operand
        } if matches!(operand.as_ref(), HirExpr::Unary { op: UnaryOp::Not, .. })
    )
}

/// Simplify double negation to the inner operand
pub fn simplify_double_negation(expr: &HirExpr) -> Option<&HirExpr> {
    if let HirExpr::Unary {
        op: UnaryOp::Neg,
        operand: outer,
    } = expr
    {
        if let HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: inner,
        } = outer.as_ref()
        {
            return Some(inner.as_ref());
        }
    }
    None
}

/// Simplify double NOT to the inner operand
pub fn simplify_double_not(expr: &HirExpr) -> Option<&HirExpr> {
    if let HirExpr::Unary {
        op: UnaryOp::Not,
        operand: outer,
    } = expr
    {
        if let HirExpr::Unary {
            op: UnaryOp::Not,
            operand: inner,
        } = outer.as_ref()
        {
            return Some(inner.as_ref());
        }
    }
    None
}

/// Check if expression is negation of a literal
pub fn is_negated_literal(expr: &HirExpr) -> bool {
    matches!(
        expr,
        HirExpr::Unary {
            op: UnaryOp::Neg,
            operand
        } if matches!(operand.as_ref(), HirExpr::Literal(Literal::Int(_) | Literal::Float(_)))
    )
}

/// Get the value of a negated int literal
pub fn get_negated_int_value(expr: &HirExpr) -> Option<i64> {
    if let HirExpr::Unary {
        op: UnaryOp::Neg,
        operand,
    } = expr
    {
        if let HirExpr::Literal(Literal::Int(n)) = operand.as_ref() {
            return Some(-n);
        }
    }
    None
}

/// Get the value of a negated float literal
pub fn get_negated_float_value(expr: &HirExpr) -> Option<f64> {
    if let HirExpr::Unary {
        op: UnaryOp::Neg,
        operand,
    } = expr
    {
        if let HirExpr::Literal(Literal::Float(f)) = operand.as_ref() {
            return Some(-f);
        }
    }
    None
}

/// Check if method name returns Option (needs .is_none() for NOT)
pub fn is_option_returning_method(method: &str) -> bool {
    matches!(method, "find" | "search" | "match" | "get" | "ok")
}

/// Check if function name returns Option (needs .is_none() for NOT)
pub fn is_option_returning_function(func: &str) -> bool {
    matches!(func, "match" | "search" | "find")
}

/// Generate .is_empty() expression for collection NOT
pub fn generate_is_empty(operand: syn::Expr) -> syn::Expr {
    parse_quote! { #operand.is_empty() }
}

/// Generate .is_none() expression for Option NOT
pub fn generate_is_none(operand: syn::Expr) -> syn::Expr {
    parse_quote! { #operand.is_none() }
}

/// Generate .is_some() expression for Option truthiness
pub fn generate_is_some(operand: syn::Expr) -> syn::Expr {
    parse_quote! { #operand.is_some() }
}

/// Generate !.is_empty() expression for collection truthiness
pub fn generate_not_is_empty(operand: syn::Expr) -> syn::Expr {
    parse_quote! { !#operand.is_empty() }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::ToTokens;

    // ============================================================================
    // python_to_rust_unary tests
    // ============================================================================

    #[test]
    fn test_unary_not() {
        let operand: syn::Expr = syn::parse_quote! { x };
        let result = python_to_rust_unary(UnaryOp::Not, operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("!"));
    }

    #[test]
    fn test_unary_neg() {
        let operand: syn::Expr = syn::parse_quote! { x };
        let result = python_to_rust_unary(UnaryOp::Neg, operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("-"));
    }

    #[test]
    fn test_unary_pos() {
        let operand: syn::Expr = syn::parse_quote! { x };
        let result = python_to_rust_unary(UnaryOp::Pos, operand);
        let s = result.to_token_stream().to_string();
        assert_eq!(s.trim(), "x");
    }

    #[test]
    fn test_unary_bitnot() {
        let operand: syn::Expr = syn::parse_quote! { x };
        let result = python_to_rust_unary(UnaryOp::BitNot, operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("!"));
    }

    // ============================================================================
    // is_* operator tests
    // ============================================================================

    #[test]
    fn test_is_logical_not() {
        assert!(is_logical_not(UnaryOp::Not));
        assert!(!is_logical_not(UnaryOp::Neg));
        assert!(!is_logical_not(UnaryOp::Pos));
        assert!(!is_logical_not(UnaryOp::BitNot));
    }

    #[test]
    fn test_is_negation() {
        assert!(is_negation(UnaryOp::Neg));
        assert!(!is_negation(UnaryOp::Not));
        assert!(!is_negation(UnaryOp::Pos));
        assert!(!is_negation(UnaryOp::BitNot));
    }

    #[test]
    fn test_is_positive() {
        assert!(is_positive(UnaryOp::Pos));
        assert!(!is_positive(UnaryOp::Neg));
        assert!(!is_positive(UnaryOp::Not));
        assert!(!is_positive(UnaryOp::BitNot));
    }

    #[test]
    fn test_is_bitwise_not() {
        assert!(is_bitwise_not(UnaryOp::BitNot));
        assert!(!is_bitwise_not(UnaryOp::Not));
        assert!(!is_bitwise_not(UnaryOp::Neg));
        assert!(!is_bitwise_not(UnaryOp::Pos));
    }

    // ============================================================================
    // type_needs_* tests
    // ============================================================================

    #[test]
    fn test_type_needs_is_empty_list() {
        assert!(type_needs_is_empty_for_not(&Type::List(Box::new(
            Type::Int
        ))));
    }

    #[test]
    fn test_type_needs_is_empty_dict() {
        assert!(type_needs_is_empty_for_not(&Type::Dict(
            Box::new(Type::String),
            Box::new(Type::Int)
        )));
    }

    #[test]
    fn test_type_needs_is_empty_set() {
        assert!(type_needs_is_empty_for_not(&Type::Set(Box::new(Type::Int))));
    }

    #[test]
    fn test_type_needs_is_empty_string() {
        assert!(type_needs_is_empty_for_not(&Type::String));
    }

    #[test]
    fn test_type_not_needs_is_empty() {
        assert!(!type_needs_is_empty_for_not(&Type::Int));
        assert!(!type_needs_is_empty_for_not(&Type::Float));
        assert!(!type_needs_is_empty_for_not(&Type::Bool));
    }

    #[test]
    fn test_type_needs_is_none() {
        assert!(type_needs_is_none_for_not(&Type::Optional(Box::new(
            Type::Int
        ))));
        assert!(type_needs_is_none_for_not(&Type::Optional(Box::new(
            Type::String
        ))));
    }

    #[test]
    fn test_type_not_needs_is_none() {
        assert!(!type_needs_is_none_for_not(&Type::Int));
        assert!(!type_needs_is_none_for_not(&Type::String));
        assert!(!type_needs_is_none_for_not(&Type::List(Box::new(
            Type::Int
        ))));
    }

    // ============================================================================
    // is_double_* tests
    // ============================================================================

    #[test]
    fn test_is_double_negation_true() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Unary {
                op: UnaryOp::Neg,
                operand: Box::new(HirExpr::Var("x".to_string())),
            }),
        };
        assert!(is_double_negation(&expr));
    }

    #[test]
    fn test_is_double_negation_false() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert!(!is_double_negation(&expr));
    }

    #[test]
    fn test_is_double_negation_wrong_op() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Unary {
                op: UnaryOp::Neg,
                operand: Box::new(HirExpr::Var("x".to_string())),
            }),
        };
        assert!(!is_double_negation(&expr));
    }

    #[test]
    fn test_is_double_not_true() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Unary {
                op: UnaryOp::Not,
                operand: Box::new(HirExpr::Var("x".to_string())),
            }),
        };
        assert!(is_double_not(&expr));
    }

    #[test]
    fn test_is_double_not_false() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert!(!is_double_not(&expr));
    }

    // ============================================================================
    // simplify_double_* tests
    // ============================================================================

    #[test]
    fn test_simplify_double_negation() {
        let inner = HirExpr::Var("x".to_string());
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Unary {
                op: UnaryOp::Neg,
                operand: Box::new(inner.clone()),
            }),
        };
        let result = simplify_double_negation(&expr);
        assert!(result.is_some());
        assert!(matches!(result.unwrap(), HirExpr::Var(v) if v == "x"));
    }

    #[test]
    fn test_simplify_double_negation_not_double() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert!(simplify_double_negation(&expr).is_none());
    }

    #[test]
    fn test_simplify_double_not() {
        let inner = HirExpr::Var("x".to_string());
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Unary {
                op: UnaryOp::Not,
                operand: Box::new(inner.clone()),
            }),
        };
        let result = simplify_double_not(&expr);
        assert!(result.is_some());
        assert!(matches!(result.unwrap(), HirExpr::Var(v) if v == "x"));
    }

    #[test]
    fn test_simplify_double_not_not_double() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert!(simplify_double_not(&expr).is_none());
    }

    // ============================================================================
    // is_negated_literal tests
    // ============================================================================

    #[test]
    fn test_is_negated_int_literal() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Int(42))),
        };
        assert!(is_negated_literal(&expr));
    }

    #[test]
    fn test_is_negated_float_literal() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Float(3.15))),
        };
        assert!(is_negated_literal(&expr));
    }

    #[test]
    fn test_is_negated_var_not_literal() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert!(!is_negated_literal(&expr));
    }

    #[test]
    fn test_is_negated_wrong_op() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Not,
            operand: Box::new(HirExpr::Literal(Literal::Int(42))),
        };
        assert!(!is_negated_literal(&expr));
    }

    // ============================================================================
    // get_negated_*_value tests
    // ============================================================================

    #[test]
    fn test_get_negated_int_value() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Int(42))),
        };
        assert_eq!(get_negated_int_value(&expr), Some(-42));
    }

    #[test]
    fn test_get_negated_int_value_zero() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Int(0))),
        };
        assert_eq!(get_negated_int_value(&expr), Some(0));
    }

    #[test]
    fn test_get_negated_int_value_not_int() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Var("x".to_string())),
        };
        assert_eq!(get_negated_int_value(&expr), None);
    }

    #[test]
    fn test_get_negated_float_value() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Float(3.15))),
        };
        assert_eq!(get_negated_float_value(&expr), Some(-3.15));
    }

    #[test]
    fn test_get_negated_float_value_not_float() {
        let expr = HirExpr::Unary {
            op: UnaryOp::Neg,
            operand: Box::new(HirExpr::Literal(Literal::Int(42))),
        };
        assert_eq!(get_negated_float_value(&expr), None);
    }

    // ============================================================================
    // is_option_returning_* tests
    // ============================================================================

    #[test]
    fn test_is_option_returning_method() {
        assert!(is_option_returning_method("find"));
        assert!(is_option_returning_method("search"));
        assert!(is_option_returning_method("match"));
        assert!(is_option_returning_method("get"));
        assert!(is_option_returning_method("ok"));
        assert!(!is_option_returning_method("append"));
        assert!(!is_option_returning_method("push"));
    }

    #[test]
    fn test_is_option_returning_function() {
        assert!(is_option_returning_function("match"));
        assert!(is_option_returning_function("search"));
        assert!(is_option_returning_function("find"));
        assert!(!is_option_returning_function("len"));
        assert!(!is_option_returning_function("print"));
    }

    // ============================================================================
    // generate_* expression tests
    // ============================================================================

    #[test]
    fn test_generate_is_empty() {
        let operand: syn::Expr = syn::parse_quote! { list };
        let result = generate_is_empty(operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("is_empty"));
    }

    #[test]
    fn test_generate_is_none() {
        let operand: syn::Expr = syn::parse_quote! { opt };
        let result = generate_is_none(operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("is_none"));
    }

    #[test]
    fn test_generate_is_some() {
        let operand: syn::Expr = syn::parse_quote! { opt };
        let result = generate_is_some(operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("is_some"));
    }

    #[test]
    fn test_generate_not_is_empty() {
        let operand: syn::Expr = syn::parse_quote! { list };
        let result = generate_not_is_empty(operand);
        let s = result.to_token_stream().to_string();
        assert!(s.contains("!"));
        assert!(s.contains("is_empty"));
    }
}