fossil-mcp 0.1.4

Multi-language static analysis toolkit with MCP server. Detects dead code, code clones, and AI scaffolding.
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
//! Expression-level constant evaluation.
//!
//! Evaluates constant expressions by pattern-matching on source text,
//! handling integer arithmetic, string operations, boolean logic,
//! and comparison operators.

use std::collections::HashMap;

use super::constant_prop::ConstValue;

/// Evaluate a constant expression from source text.
///
/// Attempts to evaluate the expression given the current constant environment.
/// Returns `Bottom` if the expression cannot be evaluated at compile time.
pub fn eval_const_expr(expr: &str, env: &HashMap<String, ConstValue>) -> ConstValue {
    let trimmed = expr.trim();

    if trimmed.is_empty() {
        return ConstValue::Bottom;
    }

    // Try parenthesized expression: strip outer parens and recurse.
    if let Some(inner) = strip_balanced_parens(trimmed) {
        return eval_const_expr(inner, env);
    }

    // Try integer literal
    if let Some(v) = parse_int_literal(trimmed) {
        return ConstValue::Constant(v);
    }

    // Try boolean literal
    if let Some(v) = parse_bool_literal(trimmed) {
        return ConstValue::BoolConst(v);
    }

    // Try string literal
    if let Some(v) = parse_string_literal(trimmed) {
        return ConstValue::StringConst(v);
    }

    // Try variable reference
    if is_identifier(trimmed) {
        return env.get(trimmed).cloned().unwrap_or(ConstValue::Top);
    }

    // Try unary operators: not, -, !
    if let Some(result) = eval_unary(trimmed, env) {
        return result;
    }

    // Try ternary/conditional (before binary to avoid ambiguity with "if" keyword)
    if let Some(result) = eval_ternary(trimmed, env) {
        return result;
    }

    // Try len() call
    if let Some(result) = eval_len(trimmed, env) {
        return result;
    }

    // Try binary operators
    if let Some(result) = eval_binary(trimmed, env) {
        return result;
    }

    ConstValue::Bottom
}

/// Strip balanced outer parentheses from an expression, if present.
fn strip_balanced_parens(s: &str) -> Option<&str> {
    if !s.starts_with('(') || !s.ends_with(')') {
        return None;
    }
    // Verify the parens are actually matched: the open paren at index 0
    // must match the close paren at the end.
    let mut depth = 0i32;
    for (i, ch) in s.char_indices() {
        match ch {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                // If depth hits zero before the last char, the outer parens
                // are not wrapping the whole expression.
                if depth == 0 && i < s.len() - 1 {
                    return None;
                }
            }
            _ => {}
        }
    }
    if depth == 0 {
        Some(&s[1..s.len() - 1])
    } else {
        None
    }
}

/// Parse an integer literal (decimal, hex, octal, binary).
/// Handles underscores in numbers (e.g. `1_000_000`) and optional leading `-`.
fn parse_int_literal(s: &str) -> Option<i64> {
    let (negative, digits) = if let Some(rest) = s.strip_prefix('-') {
        (true, rest.trim())
    } else {
        (false, s)
    };

    if digits.is_empty() {
        return None;
    }

    let cleaned: String = digits.chars().filter(|&c| c != '_').collect();
    if cleaned.is_empty() {
        return None;
    }

    let value = if let Some(hex) = cleaned
        .strip_prefix("0x")
        .or_else(|| cleaned.strip_prefix("0X"))
    {
        i64::from_str_radix(hex, 16).ok()?
    } else if let Some(oct) = cleaned
        .strip_prefix("0o")
        .or_else(|| cleaned.strip_prefix("0O"))
    {
        i64::from_str_radix(oct, 8).ok()?
    } else if let Some(bin) = cleaned
        .strip_prefix("0b")
        .or_else(|| cleaned.strip_prefix("0B"))
    {
        i64::from_str_radix(bin, 2).ok()?
    } else {
        cleaned.parse::<i64>().ok()?
    };

    Some(if negative { -value } else { value })
}

/// Parse a boolean literal.
fn parse_bool_literal(s: &str) -> Option<bool> {
    match s {
        "true" | "True" | "TRUE" => Some(true),
        "false" | "False" | "FALSE" => Some(false),
        _ => None,
    }
}

/// Parse a string literal (single or double quoted).
/// Handles basic escape sequences.
fn parse_string_literal(s: &str) -> Option<String> {
    let (quote, inner) = if s.len() >= 2 {
        if s.starts_with('"') && s.ends_with('"') {
            ('"', &s[1..s.len() - 1])
        } else if s.starts_with('\'') && s.ends_with('\'') {
            ('\'', &s[1..s.len() - 1])
        } else {
            return None;
        }
    } else {
        return None;
    };

    // Make sure there are no unescaped quotes inside
    let mut result = String::new();
    let mut chars = inner.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\\' {
            match chars.next() {
                Some('n') => result.push('\n'),
                Some('t') => result.push('\t'),
                Some('r') => result.push('\r'),
                Some('\\') => result.push('\\'),
                Some('\'') => result.push('\''),
                Some('"') => result.push('"'),
                Some('0') => result.push('\0'),
                Some(other) => {
                    result.push('\\');
                    result.push(other);
                }
                None => result.push('\\'),
            }
        } else if ch == quote {
            // Unescaped quote inside -- invalid
            return None;
        } else {
            result.push(ch);
        }
    }
    Some(result)
}

/// Check if string is a valid identifier.
fn is_identifier(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
        && s.chars().all(|c| c.is_alphanumeric() || c == '_')
}

/// Evaluate unary operators.
fn eval_unary(expr: &str, env: &HashMap<String, ConstValue>) -> Option<ConstValue> {
    // Boolean negation: "not expr" or "!expr"
    if let Some(rest) = expr.strip_prefix("not ") {
        let inner = eval_const_expr(rest.trim(), env);
        return Some(match inner.is_truthy() {
            Some(b) => ConstValue::BoolConst(!b),
            None => ConstValue::Bottom,
        });
    }
    if let Some(rest) = expr.strip_prefix('!') {
        let rest = rest.trim();
        if !rest.is_empty() {
            let inner = eval_const_expr(rest, env);
            return Some(match inner.is_truthy() {
                Some(b) => ConstValue::BoolConst(!b),
                None => ConstValue::Bottom,
            });
        }
    }

    // Unary minus: "-expr" (but not a literal, which was already handled)
    if let Some(rest) = expr.strip_prefix('-') {
        let rest = rest.trim();
        if !rest.is_empty() && !rest.chars().next().unwrap().is_ascii_digit() {
            let inner = eval_const_expr(rest, env);
            return Some(match inner {
                ConstValue::Constant(v) => ConstValue::Constant(-v),
                _ => ConstValue::Bottom,
            });
        }
    }

    None
}

/// Operator precedence levels (lower number = lower precedence = binds more loosely).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Precedence {
    Or = 1,
    And = 2,
    Comparison = 3,
    Addition = 4,
    Multiplication = 5,
    Power = 6,
}

/// Recognized binary operators with their textual representation and precedence.
static OPERATORS: &[(&str, Precedence)] = &[
    // Logical (lowest precedence)
    (" or ", Precedence::Or),
    ("||", Precedence::Or),
    (" and ", Precedence::And),
    ("&&", Precedence::And),
    // Comparison
    ("==", Precedence::Comparison),
    ("!=", Precedence::Comparison),
    ("<=", Precedence::Comparison),
    (">=", Precedence::Comparison),
    ("<", Precedence::Comparison),
    (">", Precedence::Comparison),
    // Additive
    ("+", Precedence::Addition),
    ("-", Precedence::Addition),
    // Multiplicative
    ("*", Precedence::Multiplication),
    ("/", Precedence::Multiplication),
    ("%", Precedence::Multiplication),
    // Power
    ("**", Precedence::Power),
];

/// Find the main (lowest-precedence, rightmost for left-associative) binary
/// operator in an expression, respecting parentheses and string literals.
fn find_split_operator(expr: &str) -> Option<(usize, usize, &'static str)> {
    let bytes = expr.as_bytes();
    let len = bytes.len();

    // We want the lowest precedence operator. Among equal precedence, we want
    // the rightmost occurrence (for left-associativity).
    // For power (**), we want the leftmost (right-associativity), but we keep
    // it simple and just pick rightmost -- good enough for constant eval.
    let mut best: Option<(usize, usize, &str, Precedence)> = None;

    let mut i = 0;
    let mut paren_depth: i32 = 0;
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    while i < len {
        let ch = bytes[i] as char;

        // Track string literals
        if ch == '\'' && !in_double_quote {
            if i == 0 || bytes[i - 1] != b'\\' {
                in_single_quote = !in_single_quote;
            }
            i += 1;
            continue;
        }
        if ch == '"' && !in_single_quote {
            if i == 0 || bytes[i - 1] != b'\\' {
                in_double_quote = !in_double_quote;
            }
            i += 1;
            continue;
        }
        if in_single_quote || in_double_quote {
            i += 1;
            continue;
        }

        // Track parentheses
        if ch == '(' {
            paren_depth += 1;
            i += 1;
            continue;
        }
        if ch == ')' {
            paren_depth -= 1;
            i += 1;
            continue;
        }

        // Only look for operators at depth 0
        if paren_depth == 0 {
            // At each position, find the best (longest) matching operator.
            // We try all operators but prefer multi-char operators over
            // single-char ones at the same position to avoid e.g. matching
            // `*` at a position that is part of `**`.
            let mut pos_match: Option<(usize, &str, Precedence)> = None;

            for &(op_str, prec) in OPERATORS {
                let op_len = op_str.len();
                if i + op_len > len {
                    continue;
                }
                if &expr[i..i + op_len] != op_str {
                    continue;
                }
                // Avoid matching + or - at the start (unary)
                if (op_str == "+" || op_str == "-") && i == 0 {
                    continue;
                }

                // Prefer the longest operator at this position
                let dominated = match &pos_match {
                    Some((existing_len, _, _)) => op_len <= *existing_len,
                    None => false,
                };
                if !dominated {
                    pos_match = Some((op_len, op_str, prec));
                }
            }

            if let Some((op_len, op_str, prec)) = pos_match {
                let should_replace = match &best {
                    None => true,
                    Some((_, _, _, best_prec)) => prec <= *best_prec,
                };
                if should_replace {
                    best = Some((i, op_len, op_str, prec));
                }
                // Skip past the multi-char operator so we don't re-match
                // a suffix of it (e.g. the second `*` in `**`).
                if op_len > 1 {
                    i += op_len;
                    continue;
                }
            }
        }

        i += 1;
    }

    best.map(|(pos, op_len, op_str, _)| (pos, op_len, op_str))
}

/// Evaluate binary operators.
fn eval_binary(expr: &str, env: &HashMap<String, ConstValue>) -> Option<ConstValue> {
    let (pos, op_len, op_str) = find_split_operator(expr)?;

    let lhs_str = expr[..pos].trim();
    let rhs_str = expr[pos + op_len..].trim();

    if lhs_str.is_empty() || rhs_str.is_empty() {
        return None;
    }

    let lhs = eval_const_expr(lhs_str, env);
    let rhs = eval_const_expr(rhs_str, env);

    // If either side is Bottom or Top, we usually cannot evaluate.
    // Exception: short-circuit logic.
    match op_str.trim() {
        "or" | "||" => {
            // Short circuit: true or _ = true
            if lhs.is_truthy() == Some(true) {
                return Some(ConstValue::BoolConst(true));
            }
            if lhs.is_truthy() == Some(false) {
                return rhs.is_truthy().map(ConstValue::BoolConst);
            }
            return None;
        }
        "and" | "&&" => {
            // Short circuit: false and _ = false
            if lhs.is_truthy() == Some(false) {
                return Some(ConstValue::BoolConst(false));
            }
            if lhs.is_truthy() == Some(true) {
                return rhs.is_truthy().map(ConstValue::BoolConst);
            }
            return None;
        }
        _ => {}
    }

    // For arithmetic and comparison, both sides need to be resolved.
    match (&lhs, &rhs) {
        // Integer arithmetic
        (ConstValue::Constant(a), ConstValue::Constant(b)) => {
            match op_str {
                "+" => Some(ConstValue::Constant(a.wrapping_add(*b))),
                "-" => Some(ConstValue::Constant(a.wrapping_sub(*b))),
                "*" => Some(ConstValue::Constant(a.wrapping_mul(*b))),
                "/" => {
                    if *b == 0 {
                        Some(ConstValue::Bottom)
                    } else {
                        Some(ConstValue::Constant(a.wrapping_div(*b)))
                    }
                }
                "%" => {
                    if *b == 0 {
                        Some(ConstValue::Bottom)
                    } else {
                        Some(ConstValue::Constant(a.wrapping_rem(*b)))
                    }
                }
                "**" => {
                    if *b < 0 {
                        Some(ConstValue::Bottom) // negative exponents yield fractions
                    } else {
                        Some(ConstValue::Constant(a.wrapping_pow(*b as u32)))
                    }
                }
                "==" => Some(ConstValue::BoolConst(a == b)),
                "!=" => Some(ConstValue::BoolConst(a != b)),
                "<" => Some(ConstValue::BoolConst(a < b)),
                ">" => Some(ConstValue::BoolConst(a > b)),
                "<=" => Some(ConstValue::BoolConst(a <= b)),
                ">=" => Some(ConstValue::BoolConst(a >= b)),
                _ => None,
            }
        }
        // String concatenation with +
        (ConstValue::StringConst(a), ConstValue::StringConst(b)) if op_str == "+" => {
            Some(ConstValue::StringConst(format!("{}{}", a, b)))
        }
        // String equality
        (ConstValue::StringConst(a), ConstValue::StringConst(b)) => match op_str {
            "==" => Some(ConstValue::BoolConst(a == b)),
            "!=" => Some(ConstValue::BoolConst(a != b)),
            _ => None,
        },
        // Boolean comparison
        (ConstValue::BoolConst(a), ConstValue::BoolConst(b)) => match op_str {
            "==" => Some(ConstValue::BoolConst(a == b)),
            "!=" => Some(ConstValue::BoolConst(a != b)),
            _ => None,
        },
        _ => None,
    }
}

/// Evaluate ternary expressions.
///
/// Supports:
/// - Python style: `value_true if condition else value_false`
/// - C style: `condition ? value_true : value_false`
fn eval_ternary(expr: &str, env: &HashMap<String, ConstValue>) -> Option<ConstValue> {
    // Python style: look for " if " ... " else "
    if let Some(if_pos) = find_keyword_at_depth0(expr, " if ") {
        let value_true_str = expr[..if_pos].trim();
        let rest = &expr[if_pos + 4..]; // skip " if "
        if let Some(else_pos) = find_keyword_at_depth0(rest, " else ") {
            let condition_str = rest[..else_pos].trim();
            let value_false_str = rest[else_pos + 6..].trim(); // skip " else "

            let cond = eval_const_expr(condition_str, env);
            return match cond.is_truthy() {
                Some(true) => Some(eval_const_expr(value_true_str, env)),
                Some(false) => Some(eval_const_expr(value_false_str, env)),
                None => None,
            };
        }
    }

    // C style: condition ? value_true : value_false
    if let Some(q_pos) = find_keyword_at_depth0(expr, "?") {
        let condition_str = expr[..q_pos].trim();
        let rest = &expr[q_pos + 1..];
        if let Some(colon_pos) = find_keyword_at_depth0(rest, ":") {
            let value_true_str = rest[..colon_pos].trim();
            let value_false_str = rest[colon_pos + 1..].trim();

            let cond = eval_const_expr(condition_str, env);
            return match cond.is_truthy() {
                Some(true) => Some(eval_const_expr(value_true_str, env)),
                Some(false) => Some(eval_const_expr(value_false_str, env)),
                None => None,
            };
        }
    }

    None
}

/// Find a keyword/substring in the expression at parenthesis depth 0,
/// not inside string literals. Returns the byte offset of the match.
fn find_keyword_at_depth0(expr: &str, keyword: &str) -> Option<usize> {
    let bytes = expr.as_bytes();
    let kw_bytes = keyword.as_bytes();
    let kw_len = kw_bytes.len();
    let len = bytes.len();

    if len < kw_len {
        return None;
    }

    let mut i = 0;
    let mut paren_depth: i32 = 0;
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    while i + kw_len <= len {
        let ch = bytes[i] as char;

        if ch == '\'' && !in_double_quote && (i == 0 || bytes[i - 1] != b'\\') {
            in_single_quote = !in_single_quote;
            i += 1;
            continue;
        }
        if ch == '"' && !in_single_quote && (i == 0 || bytes[i - 1] != b'\\') {
            in_double_quote = !in_double_quote;
            i += 1;
            continue;
        }
        if in_single_quote || in_double_quote {
            i += 1;
            continue;
        }
        if ch == '(' {
            paren_depth += 1;
            i += 1;
            continue;
        }
        if ch == ')' {
            paren_depth -= 1;
            i += 1;
            continue;
        }

        if paren_depth == 0 && &bytes[i..i + kw_len] == kw_bytes {
            return Some(i);
        }

        i += 1;
    }

    None
}

/// Evaluate `len()` calls.
fn eval_len(expr: &str, env: &HashMap<String, ConstValue>) -> Option<ConstValue> {
    let trimmed = expr.trim();
    if !trimmed.starts_with("len(") || !trimmed.ends_with(')') {
        return None;
    }
    let inner = &trimmed[4..trimmed.len() - 1].trim();
    let val = eval_const_expr(inner, env);
    match val {
        ConstValue::StringConst(s) => Some(ConstValue::Constant(s.len() as i64)),
        _ => None,
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    fn empty_env() -> HashMap<String, ConstValue> {
        HashMap::new()
    }

    fn env_with(pairs: &[(&str, ConstValue)]) -> HashMap<String, ConstValue> {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect()
    }

    // ---- Integer literal parsing ------------------------------------------

    #[test]
    fn test_parse_int_decimal() {
        assert_eq!(parse_int_literal("42"), Some(42));
        assert_eq!(parse_int_literal("0"), Some(0));
        assert_eq!(parse_int_literal("-7"), Some(-7));
    }

    #[test]
    fn test_parse_int_hex() {
        assert_eq!(parse_int_literal("0xFF"), Some(255));
        assert_eq!(parse_int_literal("0x10"), Some(16));
    }

    #[test]
    fn test_parse_int_octal() {
        assert_eq!(parse_int_literal("0o77"), Some(63));
    }

    #[test]
    fn test_parse_int_binary() {
        assert_eq!(parse_int_literal("0b1010"), Some(10));
    }

    #[test]
    fn test_parse_int_underscores() {
        assert_eq!(parse_int_literal("1_000_000"), Some(1_000_000));
    }

    // ---- Boolean literal parsing ------------------------------------------

    #[test]
    fn test_parse_bool() {
        assert_eq!(parse_bool_literal("true"), Some(true));
        assert_eq!(parse_bool_literal("True"), Some(true));
        assert_eq!(parse_bool_literal("FALSE"), Some(false));
        assert_eq!(parse_bool_literal("maybe"), None);
    }

    // ---- String literal parsing -------------------------------------------

    #[test]
    fn test_parse_string_double() {
        assert_eq!(parse_string_literal("\"hello\""), Some("hello".into()));
    }

    #[test]
    fn test_parse_string_single() {
        assert_eq!(parse_string_literal("'world'"), Some("world".into()));
    }

    #[test]
    fn test_parse_string_escapes() {
        assert_eq!(
            parse_string_literal("\"line\\nbreak\""),
            Some("line\nbreak".into())
        );
        assert_eq!(
            parse_string_literal("\"tab\\there\""),
            Some("tab\there".into())
        );
    }

    #[test]
    fn test_parse_string_empty() {
        assert_eq!(parse_string_literal("\"\""), Some(String::new()));
    }

    // ---- Identifier check -------------------------------------------------

    #[test]
    fn test_is_identifier() {
        assert!(is_identifier("foo"));
        assert!(is_identifier("_bar"));
        assert!(is_identifier("x2"));
        assert!(!is_identifier("2x"));
        assert!(!is_identifier(""));
        assert!(!is_identifier("a+b"));
    }

    // ---- Integer arithmetic -----------------------------------------------

    #[test]
    fn test_add() {
        assert_eq!(
            eval_const_expr("2+3", &empty_env()),
            ConstValue::Constant(5)
        );
    }

    #[test]
    fn test_subtract() {
        assert_eq!(
            eval_const_expr("10 - 4", &empty_env()),
            ConstValue::Constant(6)
        );
    }

    #[test]
    fn test_multiply() {
        assert_eq!(
            eval_const_expr("3 * 7", &empty_env()),
            ConstValue::Constant(21)
        );
    }

    #[test]
    fn test_divide() {
        assert_eq!(
            eval_const_expr("15 / 3", &empty_env()),
            ConstValue::Constant(5)
        );
    }

    #[test]
    fn test_modulo() {
        assert_eq!(
            eval_const_expr("17 % 5", &empty_env()),
            ConstValue::Constant(2)
        );
    }

    #[test]
    fn test_power() {
        assert_eq!(
            eval_const_expr("2 ** 10", &empty_env()),
            ConstValue::Constant(1024)
        );
    }

    #[test]
    fn test_divide_by_zero() {
        assert_eq!(eval_const_expr("5 / 0", &empty_env()), ConstValue::Bottom);
    }

    // ---- String operations ------------------------------------------------

    #[test]
    fn test_string_concat() {
        assert_eq!(
            eval_const_expr("\"hello\" + \" \" + \"world\"", &empty_env()),
            ConstValue::StringConst("hello world".into())
        );
    }

    #[test]
    fn test_string_equality() {
        assert_eq!(
            eval_const_expr("\"abc\" == \"abc\"", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("\"abc\" != \"def\"", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    // ---- Boolean logic ----------------------------------------------------

    #[test]
    fn test_and() {
        assert_eq!(
            eval_const_expr("true and false", &empty_env()),
            ConstValue::BoolConst(false)
        );
        assert_eq!(
            eval_const_expr("true and true", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_or() {
        assert_eq!(
            eval_const_expr("true or false", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("false or false", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    #[test]
    fn test_not() {
        assert_eq!(
            eval_const_expr("not true", &empty_env()),
            ConstValue::BoolConst(false)
        );
        assert_eq!(
            eval_const_expr("not false", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_bang_not() {
        assert_eq!(
            eval_const_expr("!true", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    // ---- Comparisons ------------------------------------------------------

    #[test]
    fn test_greater_than() {
        assert_eq!(
            eval_const_expr("5 > 3", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("3 > 5", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    #[test]
    fn test_equals() {
        assert_eq!(
            eval_const_expr("5 == 5", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("5 == 6", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    #[test]
    fn test_less_equal() {
        assert_eq!(
            eval_const_expr("3 <= 4", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("4 <= 4", &empty_env()),
            ConstValue::BoolConst(true)
        );
        assert_eq!(
            eval_const_expr("5 <= 4", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    #[test]
    fn test_greater_equal() {
        assert_eq!(
            eval_const_expr("3 >= 4", &empty_env()),
            ConstValue::BoolConst(false)
        );
        assert_eq!(
            eval_const_expr("4 >= 4", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_not_equal() {
        assert_eq!(
            eval_const_expr("5 != 3", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    // ---- Ternary ----------------------------------------------------------

    #[test]
    fn test_python_ternary_true() {
        assert_eq!(
            eval_const_expr("5 if True else 10", &empty_env()),
            ConstValue::Constant(5)
        );
    }

    #[test]
    fn test_python_ternary_false() {
        assert_eq!(
            eval_const_expr("5 if False else 10", &empty_env()),
            ConstValue::Constant(10)
        );
    }

    #[test]
    fn test_c_ternary_true() {
        assert_eq!(
            eval_const_expr("true ? 42 : 0", &empty_env()),
            ConstValue::Constant(42)
        );
    }

    #[test]
    fn test_c_ternary_false() {
        assert_eq!(
            eval_const_expr("false ? 42 : 0", &empty_env()),
            ConstValue::Constant(0)
        );
    }

    // ---- len() ------------------------------------------------------------

    #[test]
    fn test_len_string_literal() {
        assert_eq!(
            eval_const_expr("len(\"hello\")", &empty_env()),
            ConstValue::Constant(5)
        );
    }

    #[test]
    fn test_len_empty_string() {
        assert_eq!(
            eval_const_expr("len(\"\")", &empty_env()),
            ConstValue::Constant(0)
        );
    }

    #[test]
    fn test_len_variable() {
        let env = env_with(&[("s", ConstValue::StringConst("abcdef".into()))]);
        assert_eq!(eval_const_expr("len(s)", &env), ConstValue::Constant(6));
    }

    // ---- Variable substitution --------------------------------------------

    #[test]
    fn test_variable_lookup() {
        let env = env_with(&[("x", ConstValue::Constant(5))]);
        assert_eq!(eval_const_expr("x + 3", &env), ConstValue::Constant(8));
    }

    #[test]
    fn test_variable_unknown() {
        assert_eq!(eval_const_expr("x", &empty_env()), ConstValue::Top);
    }

    #[test]
    fn test_variable_bool() {
        let env = env_with(&[("flag", ConstValue::BoolConst(true))]);
        assert_eq!(
            eval_const_expr("not flag", &env),
            ConstValue::BoolConst(false)
        );
    }

    // ---- Nested / precedence ----------------------------------------------

    #[test]
    fn test_nested_parens() {
        assert_eq!(
            eval_const_expr("(2 + 3) * 4", &empty_env()),
            ConstValue::Constant(20)
        );
    }

    #[test]
    fn test_precedence_mul_before_add() {
        // 2 + 3 * 4 = 2 + 12 = 14
        assert_eq!(
            eval_const_expr("2 + 3 * 4", &empty_env()),
            ConstValue::Constant(14)
        );
    }

    #[test]
    fn test_complex_expression() {
        let env = env_with(&[
            ("a", ConstValue::Constant(10)),
            ("b", ConstValue::Constant(3)),
        ]);
        // a + b * 2 = 10 + 6 = 16
        assert_eq!(eval_const_expr("a + b * 2", &env), ConstValue::Constant(16));
    }

    #[test]
    fn test_comparison_with_logic() {
        // (5 > 3) and (2 < 4) -> true and true -> true
        assert_eq!(
            eval_const_expr("5 > 3 and 2 < 4", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_short_circuit_or() {
        // true or anything -> true
        assert_eq!(
            eval_const_expr("true or unknown_var", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_short_circuit_and() {
        // false and anything -> false
        assert_eq!(
            eval_const_expr("false and unknown_var", &empty_env()),
            ConstValue::BoolConst(false)
        );
    }

    #[test]
    fn test_bottom_for_unknown_expr() {
        assert_eq!(
            eval_const_expr("foo(bar, baz)", &empty_env()),
            ConstValue::Bottom
        );
    }

    #[test]
    fn test_boolean_and_c_style() {
        assert_eq!(
            eval_const_expr("true && false", &empty_env()),
            ConstValue::BoolConst(false)
        );
        assert_eq!(
            eval_const_expr("false || true", &empty_env()),
            ConstValue::BoolConst(true)
        );
    }

    #[test]
    fn test_negative_literal() {
        assert_eq!(
            eval_const_expr("-42", &empty_env()),
            ConstValue::Constant(-42)
        );
    }

    #[test]
    fn test_unary_negate_var() {
        let env = env_with(&[("x", ConstValue::Constant(7))]);
        assert_eq!(eval_const_expr("-x", &env), ConstValue::Constant(-7));
    }

    #[test]
    fn test_hex_literal_expr() {
        assert_eq!(
            eval_const_expr("0xFF", &empty_env()),
            ConstValue::Constant(255)
        );
    }

    #[test]
    fn test_empty_expr() {
        assert_eq!(eval_const_expr("", &empty_env()), ConstValue::Bottom);
        assert_eq!(eval_const_expr("  ", &empty_env()), ConstValue::Bottom);
    }

    #[test]
    fn test_truthy_integer() {
        // 0 is falsy, nonzero is truthy
        assert_eq!(
            eval_const_expr("0 or 5", &empty_env()),
            ConstValue::BoolConst(true) // 0 is false, then check 5 which is truthy
        );
    }

    #[test]
    fn test_string_concat_with_var() {
        let env = env_with(&[("greeting", ConstValue::StringConst("hi".into()))]);
        assert_eq!(
            eval_const_expr("greeting + \"!\"", &env),
            ConstValue::StringConst("hi!".into())
        );
    }
}