traverse-solidity 0.1.4

Solidity language parser and AST for code analysis
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
//! Solidity Expression Interpreter
//! 
//! A Rust-based interpreter for Solidity expressions with focus on predicate evaluation.
//! Evaluates boolean expressions, comparisons, and logical operations commonly used in smart contracts.
//! 
//! # Features
//! 
//! - **Predicate evaluation**: expressions that return boolean values
//! - **Variable context**: support for variable bindings and lookups  
//! - **Type system**: handle Solidity's basic types (bool, uint, int, string, address, bytes)
//! - **Error handling**: comprehensive error reporting for runtime issues
//! 
//! # Supported Operations
//! 
//! - **Literals**: `true`, `false`, `42`, `"hello"`, `hex"deadbeef"`, `unicode"hello"`
//! - **Binary ops**: `+`, `-`, `*`, `/`, `%`, `**`, `==`, `!=`, `<`, `<=`, `>`, `>=`, `&&`, `||`, `&`, `|`, `^`, `<<`, `>>`, `>>>`
//! - **Unary ops**: `+`, `-`, `!`, `~`
//! - **Conditional**: `condition ? true_expr : false_expr`
//! - **Variables**: variable lookup with context management
//! - **Built-ins**: `keccak256()`, `sha256()`, `ripemd160()`, `ecrecover()` (mock implementations)
//! 
//! # Value Types
//! 
//! - `Bool(bool)`: `true`, `false`
//! - `UInt(u64)`: `0`, `42`, `1000`  
//! - `Int(i64)`: `-1`, `0`, `42`
//! - `String(String)`: `"hello"`, `"world"`
//! - `Address(String)`: `"0x1234..."`
//! - `Bytes(Vec<u8>)`: `[0x12, 0x34, ...]`
//! - `Null`: null/undefined
//! 
//! # Boolean Conversion
//! 
//! All values convert to boolean for predicate evaluation:
//! - `Bool(true)` → `true`, `Bool(false)` → `false`
//! - `UInt(0)` → `false`, `UInt(n)` → `true`
//! - `Int(0)` → `false`, `Int(n)` → `true`  
//! - `String("")` → `false`, `String(s)` → `true`
//! - `Address("0x0000...")` → `false`, other addresses → `true`
//! - `Bytes([])` → `false`, non-empty bytes → `true`
//! - `Null` → `false`
//! 
//! # Limitations
//! 
//! Intentional limitations for predicate evaluation:
//! - No assignment operations (predicate evaluation shouldn 't mutate state)
//! - No object creation (new expressions not supported)
//! - No complex types (arrays/mappings/structs limited)
//! - No member/index access (not yet implemented)
//! 
//! # Architecture
//! 
//! - **Parser integration**: uses existing Solidity parser to generate AST
//! - **Context management**: maintains variable bindings and function definitions
//! - **Type system**: runtime type checking and conversion
//! - **Error propagation**: comprehensive error handling throughout evaluation
//! - **Extensibility**: designed to be easily extended with new operations and types

use crate::ast::*;
use std::collections::HashMap;
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Bool(bool),
    UInt(u64),
    Int(i64),
    String(String),
    Address(String),
    Bytes(Vec<u8>),
    Null,
}

impl Value {
    pub fn to_bool(&self) -> Result<bool, InterpreterError> {
        match self {
            Value::Bool(b) => Ok(*b),
            Value::UInt(n) => Ok(*n != 0),
            Value::Int(n) => Ok(*n != 0),
            Value::String(s) => Ok(!s.is_empty()),
            Value::Address(addr) => Ok(addr != "0x0000000000000000000000000000000000000000"),
            Value::Bytes(b) => Ok(!b.is_empty()),
            Value::Null => Ok(false),
        }
    }

    pub fn type_name(&self) -> &'static str {
        match self {
            Value::Bool(_) => "bool",
            Value::UInt(_) => "uint",
            Value::Int(_) => "int",
            Value::String(_) => "string",
            Value::Address(_) => "address",
            Value::Bytes(_) => "bytes",
            Value::Null => "null",
        }
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Bool(b) => write!(f, "{}", b),
            Value::UInt(n) => write!(f, "{}", n),
            Value::Int(n) => write!(f, "{}", n),
            Value::String(s) => write!(f, "\"{}\"", s),
            Value::Address(addr) => write!(f, "{}", addr),
            Value::Bytes(b) => write!(f, "0x{}", hex::encode(b)),
            Value::Null => write!(f, "null"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum InterpreterError {
    UndefinedVariable(String),
    TypeMismatch { expected: String, found: String },
    UnsupportedOperation(String),
    DivisionByZero,
    InvalidLiteral(String),
    FunctionNotFound(String),
    InvalidArguments(String),
    RuntimeError(String),
}

impl fmt::Display for InterpreterError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            InterpreterError::UndefinedVariable(name) => write!(f, "Undefined variable: {}", name),
            InterpreterError::TypeMismatch { expected, found } => {
                write!(f, "Type mismatch: expected {}, found {}", expected, found)
            }
            InterpreterError::UnsupportedOperation(op) => write!(f, "Unsupported operation: {}", op),
            InterpreterError::DivisionByZero => write!(f, "Division by zero"),
            InterpreterError::InvalidLiteral(lit) => write!(f, "Invalid literal: {}", lit),
            InterpreterError::FunctionNotFound(name) => write!(f, "Function not found: {}", name),
            InterpreterError::InvalidArguments(msg) => write!(f, "Invalid arguments: {}", msg),
            InterpreterError::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
        }
    }
}

impl std::error::Error for InterpreterError {}

#[derive(Debug, Clone)]
pub struct InterpreterContext {
    variables: HashMap<String, Value>,
    functions: HashMap<String, BuiltinFunction>,
}

impl Default for InterpreterContext {
    fn default() -> Self {
        let mut context = Self {
            variables: HashMap::new(),
            functions: HashMap::new(),
        };
        context.register_builtin_functions();
        context
    }
}

impl InterpreterContext {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set_variable(&mut self, name: String, value: Value) {
        self.variables.insert(name, value);
    }

    pub fn get_variable(&self, name: &str) -> Option<&Value> {
        self.variables.get(name)
    }

    fn register_builtin_functions(&mut self) {
        self.functions.insert("keccak256".to_string(), BuiltinFunction::Keccak256);
        self.functions.insert("sha256".to_string(), BuiltinFunction::Sha256);
        self.functions.insert("ripemd160".to_string(), BuiltinFunction::Ripemd160);
        self.functions.insert("ecrecover".to_string(), BuiltinFunction::Ecrecover);
    }

    pub fn get_function(&self, name: &str) -> Option<&BuiltinFunction> {
        self.functions.get(name)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum BuiltinFunction {
    Keccak256,
    Sha256,
    Ripemd160,
    Ecrecover,
}

#[derive(Debug)]
pub struct SolidityInterpreter {
    context: InterpreterContext,
}

impl Default for SolidityInterpreter {
    fn default() -> Self {
        Self::new()
    }
}

impl SolidityInterpreter {
    pub fn new() -> Self {
        Self {
            context: InterpreterContext::new(),
        }
    }

    pub fn with_context(context: InterpreterContext) -> Self {
        Self { context }
    }

    pub fn context_mut(&mut self) -> &mut InterpreterContext {
        &mut self.context
    }

    pub fn context(&self) -> &InterpreterContext {
        &self.context
    }

    pub fn evaluate(&self, expr: &Expression) -> Result<Value, InterpreterError> {
        match expr {
            Expression::Literal(lit) => self.evaluate_literal(lit),
            Expression::Identifier(name) => self.evaluate_identifier(name),
            Expression::Binary(bin_expr) => self.evaluate_binary_expression(bin_expr),
            Expression::Unary(unary_expr) => self.evaluate_unary_expression(unary_expr),
            Expression::FunctionCall(call_expr) => self.evaluate_function_call(call_expr),
            Expression::MemberAccess(member_expr) => self.evaluate_member_access(member_expr),
            Expression::IndexAccess(index_expr) => self.evaluate_index_access(index_expr),
            Expression::Conditional(cond_expr) => self.evaluate_conditional(cond_expr),
            Expression::Tuple(tuple_expr) => self.evaluate_tuple(tuple_expr),
            Expression::Array(array_expr) => self.evaluate_array(array_expr),
            Expression::TypeConversion(conv_expr) => self.evaluate_type_conversion(conv_expr),
            Expression::Assignment(_) => Err(InterpreterError::UnsupportedOperation(
                "Assignment expressions are not supported in predicate evaluation".to_string(),
            )),
            Expression::New(_) => Err(InterpreterError::UnsupportedOperation(
                "New expressions are not supported in predicate evaluation".to_string(),
            )),
        }
    }

    pub fn evaluate_predicate(&self, expr: &Expression) -> Result<bool, InterpreterError> {
        let value = self.evaluate(expr)?;
        value.to_bool()
    }

    fn evaluate_literal(&self, literal: &Literal) -> Result<Value, InterpreterError> {
        match literal {
            Literal::Boolean(b) => Ok(Value::Bool(*b)),
            Literal::Number(num_lit) => self.evaluate_number_literal(num_lit),
            Literal::String(str_lit) => Ok(Value::String(str_lit.value.clone())),
            Literal::HexString(hex_lit) => self.evaluate_hex_string_literal(hex_lit),
            Literal::UnicodeString(unicode_lit) => Ok(Value::String(unicode_lit.value.clone())),
        }
    }

    fn evaluate_number_literal(&self, num_lit: &NumberLiteral) -> Result<Value, InterpreterError> {
        let trimmed_value = num_lit.value.trim();
        if let Ok(uint_val) = trimmed_value.parse::<u64>() {
            Ok(Value::UInt(uint_val))
        } else if let Ok(int_val) = trimmed_value.parse::<i64>() {
            Ok(Value::Int(int_val))
        } else {
            Err(InterpreterError::InvalidLiteral(format!(
                "Cannot parse number: {}",
                num_lit.value
            )))
        }
    }

    fn evaluate_hex_string_literal(&self, hex_lit: &HexStringLiteral) -> Result<Value, InterpreterError> {
        let hex_str = hex_lit.value.strip_prefix("0x").unwrap_or(&hex_lit.value);
        
        // Try to decode as bytes
        match hex::decode(hex_str) {
            Ok(bytes) => Ok(Value::Bytes(bytes)),
            Err(_) => Err(InterpreterError::InvalidLiteral(format!(
                "Invalid hex string: {}",
                hex_lit.value
            ))),
        }
    }

    fn evaluate_identifier(&self, name: &str) -> Result<Value, InterpreterError> {
        self.context
            .get_variable(name)
            .cloned()
            .ok_or_else(|| InterpreterError::UndefinedVariable(name.to_string()))
    }

    fn evaluate_binary_expression(&self, bin_expr: &BinaryExpression) -> Result<Value, InterpreterError> {
        let left = self.evaluate(&bin_expr.left)?;
        let right = self.evaluate(&bin_expr.right)?;

        match &bin_expr.operator {
            BinaryOperator::Add => self.evaluate_add(&left, &right),
            BinaryOperator::Sub => self.evaluate_sub(&left, &right),
            BinaryOperator::Mul => self.evaluate_mul(&left, &right),
            BinaryOperator::Div => self.evaluate_div(&left, &right),
            BinaryOperator::Mod => self.evaluate_mod(&left, &right),
            BinaryOperator::Exp => self.evaluate_exp(&left, &right),
            BinaryOperator::Equal => Ok(Value::Bool(self.values_equal(&left, &right))),
            BinaryOperator::NotEqual => Ok(Value::Bool(!self.values_equal(&left, &right))),
            BinaryOperator::LessThan => self.evaluate_less_than(&left, &right),
            BinaryOperator::LessThanOrEqual => self.evaluate_less_than_or_equal(&left, &right),
            BinaryOperator::GreaterThan => self.evaluate_greater_than(&left, &right),
            BinaryOperator::GreaterThanOrEqual => self.evaluate_greater_than_or_equal(&left, &right),
            BinaryOperator::And => self.evaluate_logical_and(&left, &right),
            BinaryOperator::Or => self.evaluate_logical_or(&left, &right),
            BinaryOperator::BitAnd => self.evaluate_bit_and(&left, &right),
            BinaryOperator::BitOr => self.evaluate_bit_or(&left, &right),
            BinaryOperator::BitXor => self.evaluate_bit_xor(&left, &right),
            BinaryOperator::ShiftLeft => self.evaluate_shift_left(&left, &right),
            BinaryOperator::ShiftRight => self.evaluate_shift_right(&left, &right),
            BinaryOperator::ShiftRightArithmetic => self.evaluate_shift_right_arithmetic(&left, &right),
        }
    }

    fn evaluate_unary_expression(&self, unary_expr: &UnaryExpression) -> Result<Value, InterpreterError> {
        let operand = self.evaluate(&unary_expr.operand)?;

        match &unary_expr.operator {
            UnaryOperator::Plus => Ok(operand), // Unary plus is a no-op
            UnaryOperator::Minus => self.evaluate_unary_minus(&operand),
            UnaryOperator::Not => self.evaluate_logical_not(&operand),
            UnaryOperator::BitNot => self.evaluate_bit_not(&operand),
            UnaryOperator::Increment | UnaryOperator::Decrement => {
                Err(InterpreterError::UnsupportedOperation(
                    "Increment/decrement operators are not supported in predicate evaluation".to_string(),
                ))
            }
            UnaryOperator::Delete => Err(InterpreterError::UnsupportedOperation(
                "Delete operator is not supported in predicate evaluation".to_string(),
            )),
        }
    }

    fn evaluate_function_call(&self, call_expr: &FunctionCallExpression) -> Result<Value, InterpreterError> {
        // For now, we only support built-in functions identified by simple identifiers
        if let Expression::Identifier(func_name) = &*call_expr.function {
            if let Some(builtin_func) = self.context.get_function(func_name) {
                let args: Result<Vec<Value>, InterpreterError> = call_expr
                    .arguments
                    .iter()
                    .map(|arg| self.evaluate(arg))
                    .collect();
                let args = args?;
                return self.evaluate_builtin_function(builtin_func, &args);
            }
        }

        Err(InterpreterError::FunctionNotFound(
            "Complex function calls are not yet supported".to_string(),
        ))
    }

    fn evaluate_member_access(&self, member_expr: &MemberAccessExpression) -> Result<Value, InterpreterError> {
        let _object = self.evaluate(&member_expr.object)?;
        // For now, we don't support member access in predicate evaluation
        Err(InterpreterError::UnsupportedOperation(
            "Member access is not yet supported in predicate evaluation".to_string(),
        ))
    }

    fn evaluate_index_access(&self, index_expr: &IndexAccessExpression) -> Result<Value, InterpreterError> {
        let _object = self.evaluate(&index_expr.object)?;
        // For now, we don't support index access in predicate evaluation
        Err(InterpreterError::UnsupportedOperation(
            "Index access is not yet supported in predicate evaluation".to_string(),
        ))
    }

    fn evaluate_conditional(&self, cond_expr: &ConditionalExpression) -> Result<Value, InterpreterError> {
        let condition = self.evaluate(&cond_expr.condition)?;
        let condition_bool = condition.to_bool()?;

        if condition_bool {
            self.evaluate(&cond_expr.true_expr)
        } else {
            self.evaluate(&cond_expr.false_expr)
        }
    }

    fn evaluate_tuple(&self, tuple_expr: &TupleExpression) -> Result<Value, InterpreterError> {
        // For predicate evaluation, we don't support tuples
        Err(InterpreterError::UnsupportedOperation(
            format!("Tuple expressions are not supported in predicate evaluation (found {} elements)", 
                   tuple_expr.elements.len())
        ))
    }

    fn evaluate_array(&self, array_expr: &ArrayExpression) -> Result<Value, InterpreterError> {
        // For predicate evaluation, we don't support arrays
        Err(InterpreterError::UnsupportedOperation(
            format!("Array expressions are not supported in predicate evaluation (found {} elements)", 
                   array_expr.elements.len())
        ))
    }

    fn evaluate_type_conversion(&self, conv_expr: &TypeConversionExpression) -> Result<Value, InterpreterError> {
        let value = self.evaluate(&conv_expr.expression)?;
        // For now, we don't support type conversions in predicate evaluation
        Err(InterpreterError::UnsupportedOperation(
            format!("Type conversion is not yet supported in predicate evaluation (converting {} to type)", 
                   value.type_name())
        ))
    }

    fn evaluate_add(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::UInt(a + b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a + b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Int(*a as i64 + b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Int(a + *b as i64)),
            (Value::String(a), Value::String(b)) => Ok(Value::String(format!("{}{}", a, b))),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric or string".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_sub(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => {
                if a >= b {
                    Ok(Value::UInt(a - b))
                } else {
                    Ok(Value::Int(*a as i64 - *b as i64))
                }
            }
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Int(*a as i64 - b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Int(a - *b as i64)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_mul(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::UInt(a * b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a * b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Int(*a as i64 * b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Int(a * *b as i64)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_div(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::UInt(a / b))
                }
            }
            (Value::Int(a), Value::Int(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::Int(a / b))
                }
            }
            (Value::UInt(a), Value::Int(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::Int(*a as i64 / b))
                }
            }
            (Value::Int(a), Value::UInt(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::Int(a / *b as i64))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_mod(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::UInt(a % b))
                }
            }
            (Value::Int(a), Value::Int(b)) => {
                if *b == 0 {
                    Err(InterpreterError::DivisionByZero)
                } else {
                    Ok(Value::Int(a % b))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_exp(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(base), Value::UInt(exp)) => {
                if *exp > 32 {
                    Err(InterpreterError::RuntimeError("Exponent too large".to_string()))
                } else {
                    Ok(Value::UInt(base.pow(*exp as u32)))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "uint".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn values_equal(&self, left: &Value, right: &Value) -> bool {
        match (left, right) {
            (Value::Bool(a), Value::Bool(b)) => a == b,
            (Value::UInt(a), Value::UInt(b)) => a == b,
            (Value::Int(a), Value::Int(b)) => a == b,
            (Value::UInt(a), Value::Int(b)) => *a as i64 == *b,
            (Value::Int(a), Value::UInt(b)) => *a == *b as i64,
            (Value::String(a), Value::String(b)) => a == b,
            (Value::Address(a), Value::Address(b)) => a == b,
            (Value::Bytes(a), Value::Bytes(b)) => a == b,
            (Value::Null, Value::Null) => true,
            _ => false,
        }
    }

    fn evaluate_less_than(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::Bool(a < b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(a < b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Bool((*a as i64) < *b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Bool(*a < (*b as i64))),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_less_than_or_equal(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::Bool(a <= b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(a <= b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Bool((*a as i64) <= *b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Bool(*a <= (*b as i64))),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_greater_than(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::Bool(a > b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(a > b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Bool((*a as i64) > *b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Bool(*a > (*b as i64))),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_greater_than_or_equal(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::Bool(a >= b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(a >= b)),
            (Value::UInt(a), Value::Int(b)) => Ok(Value::Bool((*a as i64) >= *b)),
            (Value::Int(a), Value::UInt(b)) => Ok(Value::Bool(*a >= (*b as i64))),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_logical_and(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        let left_bool = left.to_bool()?;
        let right_bool = right.to_bool()?;
        Ok(Value::Bool(left_bool && right_bool))
    }

    fn evaluate_logical_or(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        let left_bool = left.to_bool()?;
        let right_bool = right.to_bool()?;
        Ok(Value::Bool(left_bool || right_bool))
    }

    fn evaluate_bit_and(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::UInt(a & b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a & b)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "integer".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_bit_or(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::UInt(a | b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a | b)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "integer".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_bit_xor(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => Ok(Value::UInt(a ^ b)),
            (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a ^ b)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "integer".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_shift_left(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => {
                if *b > 64 {
                    Err(InterpreterError::RuntimeError("Shift amount too large".to_string()))
                } else {
                    Ok(Value::UInt(a << b))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "uint".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_shift_right(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::UInt(a), Value::UInt(b)) => {
                if *b > 64 {
                    Err(InterpreterError::RuntimeError("Shift amount too large".to_string()))
                } else {
                    Ok(Value::UInt(a >> b))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "uint".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_shift_right_arithmetic(&self, left: &Value, right: &Value) -> Result<Value, InterpreterError> {
        match (left, right) {
            (Value::Int(a), Value::UInt(b)) => {
                if *b > 64 {
                    Err(InterpreterError::RuntimeError("Shift amount too large".to_string()))
                } else {
                    Ok(Value::Int(a >> b))
                }
            }
            _ => Err(InterpreterError::TypeMismatch {
                expected: "int and uint".to_string(),
                found: format!("{} and {}", left.type_name(), right.type_name()),
            }),
        }
    }

    fn evaluate_unary_minus(&self, operand: &Value) -> Result<Value, InterpreterError> {
        match operand {
            Value::UInt(n) => Ok(Value::Int(-(*n as i64))),
            Value::Int(n) => Ok(Value::Int(-n)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "numeric".to_string(),
                found: operand.type_name().to_string(),
            }),
        }
    }

    fn evaluate_logical_not(&self, operand: &Value) -> Result<Value, InterpreterError> {
        let bool_val = operand.to_bool()?;
        Ok(Value::Bool(!bool_val))
    }

    fn evaluate_bit_not(&self, operand: &Value) -> Result<Value, InterpreterError> {
        match operand {
            Value::UInt(n) => Ok(Value::UInt(!n)),
            Value::Int(n) => Ok(Value::Int(!n)),
            _ => Err(InterpreterError::TypeMismatch {
                expected: "integer".to_string(),
                found: operand.type_name().to_string(),
            }),
        }
    }

    fn evaluate_builtin_function(
        &self,
        func: &BuiltinFunction,
        args: &[Value],
    ) -> Result<Value, InterpreterError> {
        match func {
            BuiltinFunction::Keccak256 => {
                if args.len() != 1 {
                    return Err(InterpreterError::InvalidArguments(
                        "keccak256 expects exactly 1 argument".to_string(),
                    ));
                }
                Ok(Value::Bytes(vec![0u8; 32])) // Mock 32-byte hash
            }
            BuiltinFunction::Sha256 => {
                if args.len() != 1 {
                    return Err(InterpreterError::InvalidArguments(
                        "sha256 expects exactly 1 argument".to_string(),
                    ));
                }
                Ok(Value::Bytes(vec![1u8; 32])) // Mock 32-byte hash
            }
            BuiltinFunction::Ripemd160 => {
                if args.len() != 1 {
                    return Err(InterpreterError::InvalidArguments(
                        "ripemd160 expects exactly 1 argument".to_string(),
                    ));
                }
                Ok(Value::Bytes(vec![2u8; 20])) // Mock 20-byte hash
            }
            BuiltinFunction::Ecrecover => {
                if args.len() != 4 {
                    return Err(InterpreterError::InvalidArguments(
                        "ecrecover expects exactly 4 arguments".to_string(),
                    ));
                }
                // For predicate evaluation, we'll return a mock address
                Ok(Value::Address("0x0000000000000000000000000000000000000000".to_string()))
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse_expression; // Added for parsing expressions in tests

    #[test]
    fn test_evaluate_boolean_literal() {
        let interpreter = SolidityInterpreter::new();
        
        let true_expr = Expression::Literal(Literal::Boolean(true));
        let false_expr = Expression::Literal(Literal::Boolean(false));
        
        assert_eq!(interpreter.evaluate(&true_expr).unwrap(), Value::Bool(true));
        assert_eq!(interpreter.evaluate(&false_expr).unwrap(), Value::Bool(false));
    }

    #[test]
    fn test_evaluate_number_literal() {
        let interpreter = SolidityInterpreter::new();
        
        let num_expr = Expression::Literal(Literal::Number(NumberLiteral {
            value: "42".to_string(),
            sub_denomination: None,
        }));
        
        assert_eq!(interpreter.evaluate(&num_expr).unwrap(), Value::UInt(42));
    }

    #[test]
    fn test_evaluate_binary_comparison() {
        let interpreter = SolidityInterpreter::new();
        
        let left = Box::new(Expression::Literal(Literal::Number(NumberLiteral {
            value: "10".to_string(),
            sub_denomination: None,
        })));
        
        let right = Box::new(Expression::Literal(Literal::Number(NumberLiteral {
            value: "5".to_string(),
            sub_denomination: None,
        })));
        
        let gt_expr = Expression::Binary(BinaryExpression {
            left: left.clone(),
            operator: BinaryOperator::GreaterThan,
            right: right.clone(),
        });
        
        let lt_expr = Expression::Binary(BinaryExpression {
            left: left.clone(),
            operator: BinaryOperator::LessThan,
            right: right.clone(),
        });
        
        assert!(interpreter.evaluate_predicate(&gt_expr).unwrap());
        assert!(!interpreter.evaluate_predicate(&lt_expr).unwrap());
    }

    #[test]
    fn test_evaluate_logical_and() {
        let interpreter = SolidityInterpreter::new();
        
        let true_expr = Box::new(Expression::Literal(Literal::Boolean(true)));
        let false_expr = Box::new(Expression::Literal(Literal::Boolean(false)));
        
        let and_expr = Expression::Binary(BinaryExpression {
            left: true_expr.clone(),
            operator: BinaryOperator::And,
            right: false_expr.clone(),
        });
        
        assert!(!interpreter.evaluate_predicate(&and_expr).unwrap());
    }

    #[test]
    fn test_evaluate_logical_not() {
        let interpreter = SolidityInterpreter::new();
        
        let true_expr = Box::new(Expression::Literal(Literal::Boolean(true)));
        
        let not_expr = Expression::Unary(UnaryExpression {
            operator: UnaryOperator::Not,
            operand: true_expr,
            is_prefix: true,
        });
        
        assert!(!interpreter.evaluate_predicate(&not_expr).unwrap());
    }

    #[test]
    fn test_evaluate_conditional() {
        let interpreter = SolidityInterpreter::new();
        
        let condition = Box::new(Expression::Literal(Literal::Boolean(true)));
        let true_branch = Box::new(Expression::Literal(Literal::Number(NumberLiteral {
            value: "1".to_string(),
            sub_denomination: None,
        })));
        let false_branch = Box::new(Expression::Literal(Literal::Number(NumberLiteral {
            value: "0".to_string(),
            sub_denomination: None,
        })));
        
        let cond_expr = Expression::Conditional(ConditionalExpression {
            condition,
            true_expr: true_branch,
            false_expr: false_branch,
        });
        
        assert_eq!(interpreter.evaluate(&cond_expr).unwrap(), Value::UInt(1));
    }

    #[test]
    fn test_variable_lookup() {
        let mut interpreter = SolidityInterpreter::new();
        interpreter.context_mut().set_variable("x".to_string(), Value::Bool(true));
        
        let var_expr = Expression::Identifier("x".to_string());
        
        assert!(interpreter.evaluate_predicate(&var_expr).unwrap());
    }

    #[test]
    fn test_undefined_variable() {
        let interpreter = SolidityInterpreter::new();
        
        let var_expr = Expression::Identifier("undefined".to_string());
        
        match interpreter.evaluate(&var_expr) {
            Err(InterpreterError::UndefinedVariable(name)) => {
                assert_eq!(name, "undefined");
            }
            _ => panic!("Expected UndefinedVariable error"),
        }
    }

    #[test]
    fn test_value_to_bool_conversion() {
        assert!(Value::Bool(true).to_bool().unwrap());
        assert!(!Value::Bool(false).to_bool().unwrap());
        assert!(Value::UInt(1).to_bool().unwrap());
        assert!(!Value::UInt(0).to_bool().unwrap());
        assert!(Value::Int(-1).to_bool().unwrap());
        assert!(!Value::Int(0).to_bool().unwrap());
        assert!(Value::String("hello".to_string()).to_bool().unwrap());
        assert!(!Value::String("".to_string()).to_bool().unwrap());
        assert!(!Value::Null.to_bool().unwrap());
    }

    #[test]
    fn test_evaluate_new_value_gt_zero_predicate() {
        let mut interpreter = SolidityInterpreter::new();

        // Construct the expression: _newValue > 0
        let new_value_ident = Expression::Identifier("_newValue".to_string());
        let zero_literal = Expression::Literal(Literal::Number(NumberLiteral {
            value: "0".to_string(),
            sub_denomination: None,
        }));
        let expr = Expression::Binary(BinaryExpression {
            left: Box::new(new_value_ident),
            operator: BinaryOperator::GreaterThan,
            right: Box::new(zero_literal),
        });

        // Case 1: _newValue = 42 (positive)
        // Expected: _newValue > 0  =>  42 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(42));
        match interpreter.evaluate_predicate(&expr) {
            Ok(result) => assert!(result, "Expected '42 > 0' to be true"),
            Err(e) => panic!("Evaluation failed for _newValue = 42: {}", e),
        }

        // Case 2: _newValue = 0
        // Expected: _newValue > 0  =>  0 > 0  =>  false
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(0));
        match interpreter.evaluate_predicate(&expr) {
            Ok(result) => assert!(!result, "Expected '0 > 0' to be false"),
            Err(e) => panic!("Evaluation failed for _newValue = 0: {}", e),
        }
        
        // Case 3: _newValue = 1 (positive, edge case near 0)
        // Expected: _newValue > 0  =>  1 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(1));
        match interpreter.evaluate_predicate(&expr) {
            Ok(result) => assert!(result, "Expected '1 > 0' to be true"),
            Err(e) => panic!("Evaluation failed for _newValue = 1: {}", e),
        }
    }

    #[test]
    fn test_evaluate_parsed_expression_new_value_gt_zero() {
        let mut interpreter = SolidityInterpreter::new();

        // Parse the expression string into an AST
        let expression_str = "_newValue > 0";
        let parsed_expr = match parse_expression(expression_str) {
            Ok(expr) => expr,
            Err(e) => panic!("Failed to parse expression '{}': {}", expression_str, e),
        };

        // Case 1: _newValue = 42 (positive)
        // Expected: _newValue > 0  =>  42 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(42));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(result, "Expected '42 > 0' (parsed) to be true"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 42: {}", e),
        }

        // Case 2: _newValue = 0
        // Expected: _newValue > 0  =>  0 > 0  =>  false
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(0));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(!result, "Expected '0 > 0' (parsed) to be false"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 0: {}", e),
        }
        
        // Case 3: _newValue = 1 (positive, edge case near 0)
        // Expected: _newValue > 0  =>  1 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::UInt(1));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(result, "Expected '1 > 0' (parsed) to be true"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 1: {}", e),
        }
    }

    #[test]
    fn test_evaluate_parsed_expression_new_value_gt_zero_signed() {
        let mut interpreter = SolidityInterpreter::new();

        // Parse the expression string into an AST
        let expression_str = "_newValue > 0";
        let parsed_expr = match parse_expression(expression_str) {
            Ok(expr) => expr,
            Err(e) => panic!("Failed to parse expression '{}': {}", expression_str, e),
        };

        // Case 1: _newValue = 42 (positive)
        // Expected: _newValue > 0  =>  42 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::Int(42));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(result, "Expected '42 > 0' (parsed) to be true"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 42: {}", e),
        }

        // Case 2: _newValue = 0
        // Expected: _newValue > 0  =>  0 > 0  =>  false
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::Int(0));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(!result, "Expected '0 > 0' (parsed) to be false"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 0: {}", e),
        }
        
        // Case 3: _newValue = 1 (positive, edge case near 0)
        // Expected: _newValue > 0  =>  1 > 0  =>  true
        interpreter.context_mut().set_variable("_newValue".to_string(), Value::Int(1));
        match interpreter.evaluate_predicate(&parsed_expr) {
            Ok(result) => assert!(result, "Expected '1 > 0' (parsed) to be true"),
            Err(e) => panic!("Parsed evaluation failed for _newValue = 1: {}", e),
        }
    }


}