@@ -512,7 +512,7 @@ impl<'a> VM<'a> {
512512 self . push ( Val :: int ( idx) ) ;
513513 Ok ( ( ) )
514514 }
515- ListCount => {
515+ ListCount => {
516516 if positional. len ( ) != 1 {
517517 return Err ( VmErr :: Type ( "count() takes exactly one argument" ) ) ;
518518 }
@@ -526,6 +526,62 @@ impl<'a> VM<'a> {
526526 self . push ( Val :: int ( n) ) ;
527527 Ok ( ( ) )
528528 }
529+ DictGet => {
530+ if positional. is_empty ( ) || positional. len ( ) > 2 {
531+ return Err ( VmErr :: Type ( "get() takes 1 or 2 arguments" ) ) ;
532+ }
533+ let key = positional[ 0 ] ;
534+ let default = if positional. len ( ) == 2 { positional[ 1 ] } else { Val :: none ( ) } ;
535+ let result = match self . heap . get ( recv) {
536+ HeapObj :: Dict ( rc) => rc. borrow ( ) . get ( & key) . copied ( ) . unwrap_or ( default) ,
537+ _ => return Err ( VmErr :: Type ( "get: receiver is not a dict" ) ) ,
538+ } ;
539+ self . push ( result) ;
540+ Ok ( ( ) )
541+ }
542+ DictUpdate => {
543+ if positional. len ( ) != 1 {
544+ return Err ( VmErr :: Type ( "update() takes exactly one argument" ) ) ;
545+ }
546+ let pairs = match self . heap . get ( positional[ 0 ] ) {
547+ HeapObj :: Dict ( rc) => rc. borrow ( ) . entries . clone ( ) ,
548+ _ => return Err ( VmErr :: Type ( "update() argument must be a dict" ) ) ,
549+ } ;
550+ match self . heap . get_mut ( recv) {
551+ HeapObj :: Dict ( rc) => {
552+ let mut b = rc. borrow_mut ( ) ;
553+ for ( k, v) in pairs { b. insert ( k, v) ; }
554+ }
555+ _ => return Err ( VmErr :: Type ( "update: receiver is not a dict" ) ) ,
556+ }
557+ self . mark_impure ( ) ;
558+ self . push ( Val :: none ( ) ) ;
559+ Ok ( ( ) )
560+ }
561+ DictPop => {
562+ if positional. is_empty ( ) || positional. len ( ) > 2 {
563+ return Err ( VmErr :: Type ( "pop() takes 1 or 2 arguments" ) ) ;
564+ }
565+ let key = positional[ 0 ] ;
566+ let default = if positional. len ( ) == 2 { Some ( positional[ 1 ] ) } else { None } ;
567+ let result = match self . heap . get_mut ( recv) {
568+ HeapObj :: Dict ( rc) => {
569+ let mut b = rc. borrow_mut ( ) ;
570+ if let Some ( val) = b. remove ( & key) {
571+ val
572+ } else {
573+ match default {
574+ Some ( d) => d,
575+ None => return Err ( VmErr :: Value ( "key not found" ) ) ,
576+ }
577+ }
578+ }
579+ _ => return Err ( VmErr :: Type ( "pop: receiver is not a dict" ) ) ,
580+ } ;
581+ self . mark_impure ( ) ;
582+ self . push ( result) ;
583+ Ok ( ( ) )
584+ }
529585 }
530586 }
531587
0 commit comments