@@ -8,6 +8,7 @@ extern crate serde_json;
8
8
9
9
//extern crate eval; use eval::eval::*;
10
10
use std:: collections:: HashMap ;
11
+ use std:: cell:: RefCell ;
11
12
use std:: env;
12
13
use std:: fs:: File ;
13
14
use std:: io:: prelude:: * ;
@@ -24,7 +25,8 @@ pub enum NativeType{
24
25
Float ( f64 ) ,
25
26
Str ( String ) ,
26
27
Unicode ( String ) ,
27
- List ( Vec < NativeType > ) ,
28
+ #[ serde( skip_serializing, skip_deserializing) ]
29
+ List ( RefCell < Vec < NativeType > > ) ,
28
30
Tuple ( Vec < NativeType > ) ,
29
31
Iter ( Vec < NativeType > ) , // TODO: use Iterator instead
30
32
Code ( PyCodeObject ) ,
@@ -271,7 +273,7 @@ impl VirtualMachine {
271
273
vec. push ( ( * curr_frame. stack . pop ( ) . unwrap ( ) ) . clone ( ) ) ;
272
274
}
273
275
vec. reverse ( ) ;
274
- curr_frame. stack . push ( Rc :: new ( NativeType :: List ( vec) ) ) ;
276
+ curr_frame. stack . push ( Rc :: new ( NativeType :: List ( RefCell :: new ( vec) ) ) ) ;
275
277
None
276
278
} ,
277
279
@@ -305,7 +307,7 @@ impl VirtualMachine {
305
307
let iter = match * tos {
306
308
//TODO: is this clone right?
307
309
// Return a Iterator instead vvv
308
- NativeType :: List ( ref vec) => NativeType :: Iter ( vec. clone ( ) ) ,
310
+ NativeType :: List ( ref vec) => NativeType :: Iter ( vec. borrow ( ) . clone ( ) ) ,
309
311
_ => panic ! ( "TypeError: object is not iterable" )
310
312
} ;
311
313
curr_frame. stack . push ( Rc :: new ( iter) ) ;
@@ -448,11 +450,9 @@ impl VirtualMachine {
448
450
let tos1 = curr_frame. stack . pop ( ) . unwrap ( ) ;
449
451
let tos2 = curr_frame. stack . pop ( ) . unwrap ( ) ;
450
452
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 ( ) ;
454
455
} ,
455
- */
456
456
( & NativeType :: Str ( _) , & NativeType :: Int ( _) ) => {
457
457
// TODO: raise TypeError: 'str' object does not support item assignment
458
458
panic ! ( "TypeError: 'str' object does not support item assignment" )
@@ -486,7 +486,7 @@ impl VirtualMachine {
486
486
( & NativeType :: List ( ref l1) , & NativeType :: List ( ref l2) ) => {
487
487
let mut new_l = l2. clone ( ) ;
488
488
// TODO: remove unnessary copy
489
- new_l. append ( & mut l1. clone ( ) ) ;
489
+ new_l. borrow_mut ( ) . append ( & mut l1. borrow ( ) . clone ( ) ) ;
490
490
curr_frame. stack . push ( Rc :: new ( NativeType :: List ( new_l) ) ) ;
491
491
492
492
}
@@ -583,25 +583,25 @@ impl VirtualMachine {
583
583
debug ! ( "tos: {:?}, tos1: {:?}" , tos, tos1) ;
584
584
match ( tos1. deref ( ) , tos. deref ( ) ) {
585
585
( & 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 ( ) ) )
588
588
} ,
589
589
( & NativeType :: List ( ref l) , & NativeType :: Slice ( ref opt_start, ref opt_stop, ref opt_step) ) => {
590
590
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 ,
592
592
& None => 0 ,
593
593
} ;
594
594
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 ,
597
597
} ;
598
598
let step = match opt_step {
599
599
//Some(step) => step as usize,
600
600
& None => 1 as usize ,
601
601
_ => unimplemented ! ( ) ,
602
602
} ;
603
603
// 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 ( ) ) ) ) ) ;
605
605
} ,
606
606
( & NativeType :: Tuple ( ref t) , & NativeType :: Int ( ref index) ) => curr_frame. stack . push ( Rc :: new ( t[ * index as usize ] . clone ( ) ) ) ,
607
607
( & NativeType :: Str ( ref s) , & NativeType :: Int ( ref index) ) => {
0 commit comments