@@ -509,14 +509,10 @@ impl VirtualMachine {
509509 let curr_frame = self . curr_frame ( ) ;
510510 let tos = curr_frame. stack . pop ( ) . unwrap ( ) ;
511511 let tos1 = curr_frame. stack . pop ( ) . unwrap ( ) ;
512- if let ( NativeType :: List ( v) , NativeType :: Int ( idx) ) = ( tos1, tos) {
513- if idx as usize >= v. len ( ) {
514- // TODO: change this to a exception
515- panic ! ( "IndexError: list index out of range" ) ;
516- }
517- curr_frame. stack . push ( v[ idx as usize ] . clone ( ) ) ;
518- } else {
519- panic ! ( "TypeError in BINARY_SUBSTRACT" ) ;
512+ match ( & tos1, & tos) {
513+ ( & NativeType :: List ( ref l) , & NativeType :: Int ( ref index) ) => curr_frame. stack . push ( l[ * index as usize ] . clone ( ) ) ,
514+ ( & NativeType :: Tuple ( ref t) , & NativeType :: Int ( ref index) ) => curr_frame. stack . push ( t[ * index as usize ] . clone ( ) ) ,
515+ _ => panic ! ( "TypeError: indexing type {:?} with index {:?} is not supported" , tos1, tos)
520516 } ;
521517 None
522518 } ,
@@ -750,13 +746,17 @@ fn main() {
750746 let mut s = String :: new ( ) ;
751747 f. read_to_string ( & mut s) . unwrap ( ) ;
752748 // println!("Read string");
753- let code: PyCodeObject = serde_json:: from_str ( & s) . unwrap ( ) ;
749+ let code: PyCodeObject = match serde_json:: from_str ( & s) {
750+ Ok ( c) => c,
751+ Err ( _) => panic ! ( "Fail to parse the bytecode" )
752+ } ;
754753
755754 let mut vm = VirtualMachine :: new ( ) ;
756755 vm. run_code ( code) ;
757756 // println!("Done");
758757}
759758
759+ /*
760760#[test]
761761fn test_parse_native_type() {
762762
@@ -860,3 +860,10 @@ let input = "{\"co_consts\":[{\"Int\":1},\"NoneType\",{\"Int\":2}],\"co_names\":
860860 let deserialized: PyCodeObject = serde_json::from_str(&input).unwrap();
861861 assert_eq!(expected, deserialized)
862862}
863+ */
864+
865+ #[ test]
866+ fn test_tuple_serialization ( ) {
867+ let tuple = NativeType :: Tuple ( vec ! [ NativeType :: Int ( 1 ) , NativeType :: Int ( 2 ) ] ) ;
868+ println ! ( "{}" , serde_json:: to_string( & tuple) . unwrap( ) ) ;
869+ }
0 commit comments