@@ -8,6 +8,7 @@ extern crate serde_json;
88
99//extern crate eval; use eval::eval::*;
1010use std:: collections:: HashMap ;
11+ use std:: cell:: RefCell ;
1112use std:: env;
1213use std:: fs:: File ;
1314use std:: io:: prelude:: * ;
@@ -24,7 +25,8 @@ pub enum NativeType{
2425 Float ( f64 ) ,
2526 Str ( String ) ,
2627 Unicode ( String ) ,
27- List ( Vec < NativeType > ) ,
28+ #[ serde( skip_serializing, skip_deserializing) ]
29+ List ( RefCell < Vec < NativeType > > ) ,
2830 Tuple ( Vec < NativeType > ) ,
2931 Iter ( Vec < NativeType > ) , // TODO: use Iterator instead
3032 Code ( PyCodeObject ) ,
@@ -271,7 +273,7 @@ impl VirtualMachine {
271273 vec. push ( ( * curr_frame. stack . pop ( ) . unwrap ( ) ) . clone ( ) ) ;
272274 }
273275 vec. reverse ( ) ;
274- curr_frame. stack . push ( Rc :: new ( NativeType :: List ( vec) ) ) ;
276+ curr_frame. stack . push ( Rc :: new ( NativeType :: List ( RefCell :: new ( vec) ) ) ) ;
275277 None
276278 } ,
277279
@@ -305,7 +307,7 @@ impl VirtualMachine {
305307 let iter = match * tos {
306308 //TODO: is this clone right?
307309 // Return a Iterator instead vvv
308- NativeType :: List ( ref vec) => NativeType :: Iter ( vec. clone ( ) ) ,
310+ NativeType :: List ( ref vec) => NativeType :: Iter ( vec. borrow ( ) . clone ( ) ) ,
309311 _ => panic ! ( "TypeError: object is not iterable" )
310312 } ;
311313 curr_frame. stack . push ( Rc :: new ( iter) ) ;
@@ -448,11 +450,9 @@ impl VirtualMachine {
448450 let tos1 = curr_frame. stack . pop ( ) . unwrap ( ) ;
449451 let tos2 = curr_frame. stack . pop ( ) . unwrap ( ) ;
450452 match ( tos1. deref ( ) , tos. deref ( ) ) {
451- /* TODO: support list assignment
452- (&NativeType::List(mut refl), &NativeType::Int(index)) => {
453- refl[index as usize] = (*tos2).clone();
453+ ( & NativeType :: List ( ref refl) , & NativeType :: Int ( index) ) => {
454+ refl. borrow_mut ( ) [ index as usize ] = ( * tos2) . clone ( ) ;
454455 } ,
455- */
456456 ( & NativeType :: Str ( _) , & NativeType :: Int ( _) ) => {
457457 // TODO: raise TypeError: 'str' object does not support item assignment
458458 panic ! ( "TypeError: 'str' object does not support item assignment" )
@@ -486,7 +486,7 @@ impl VirtualMachine {
486486 ( & NativeType :: List ( ref l1) , & NativeType :: List ( ref l2) ) => {
487487 let mut new_l = l2. clone ( ) ;
488488 // TODO: remove unnessary copy
489- new_l. append ( & mut l1. clone ( ) ) ;
489+ new_l. borrow_mut ( ) . append ( & mut l1. borrow ( ) . clone ( ) ) ;
490490 curr_frame. stack . push ( Rc :: new ( NativeType :: List ( new_l) ) ) ;
491491
492492 }
@@ -583,25 +583,25 @@ impl VirtualMachine {
583583 debug ! ( "tos: {:?}, tos1: {:?}" , tos, tos1) ;
584584 match ( tos1. deref ( ) , tos. deref ( ) ) {
585585 ( & NativeType :: List ( ref l) , & NativeType :: Int ( ref index) ) => {
586- let pos_index = ( index + l. len ( ) as i32 ) % l. len ( ) as i32 ;
587- curr_frame. stack . push ( Rc :: new ( l[ pos_index as usize ] . clone ( ) ) )
586+ let pos_index = ( index + l. borrow ( ) . len ( ) as i32 ) % l. borrow ( ) . len ( ) as i32 ;
587+ curr_frame. stack . push ( Rc :: new ( l. borrow ( ) [ pos_index as usize ] . clone ( ) ) )
588588 } ,
589589 ( & NativeType :: List ( ref l) , & NativeType :: Slice ( ref opt_start, ref opt_stop, ref opt_step) ) => {
590590 let start = match opt_start {
591- & Some ( start) => ( ( start + l. len ( ) as i32 ) % l. len ( ) as i32 ) as usize ,
591+ & Some ( start) => ( ( start + l. borrow ( ) . len ( ) as i32 ) % l. borrow ( ) . len ( ) as i32 ) as usize ,
592592 & None => 0 ,
593593 } ;
594594 let stop = match opt_stop {
595- & Some ( stop) => ( ( stop + l. len ( ) as i32 ) % l. len ( ) as i32 ) as usize ,
596- & None => l. len ( ) as usize ,
595+ & Some ( stop) => ( ( stop + l. borrow ( ) . len ( ) as i32 ) % l. borrow ( ) . len ( ) as i32 ) as usize ,
596+ & None => l. borrow ( ) . len ( ) as usize ,
597597 } ;
598598 let step = match opt_step {
599599 //Some(step) => step as usize,
600600 & None => 1 as usize ,
601601 _ => unimplemented ! ( ) ,
602602 } ;
603603 // TODO: we could potentially avoid this copy and use slice
604- curr_frame. stack . push ( Rc :: new ( NativeType :: List ( l [ start..stop] . to_vec ( ) ) ) ) ;
604+ curr_frame. stack . push ( Rc :: new ( NativeType :: List ( RefCell :: new ( l . borrow ( ) [ start..stop] . to_vec ( ) ) ) ) ) ;
605605 } ,
606606 ( & NativeType :: Tuple ( ref t) , & NativeType :: Int ( ref index) ) => curr_frame. stack . push ( Rc :: new ( t[ * index as usize ] . clone ( ) ) ) ,
607607 ( & NativeType :: Str ( ref s) , & NativeType :: Int ( ref index) ) => {
0 commit comments