@@ -778,17 +778,15 @@ pub fn execute_command(executor: &mut Executor, command: String) {
778
778
779
779
// Generate a instance of object
780
780
"instance" => {
781
- let data = executor . pop_stack ( ) . get_list ( ) ;
782
- let mut class = executor . pop_stack ( ) . get_list ( ) ;
781
+ let data = self . pop_stack ( ) . get_list ( ) ;
782
+ let class = self . pop_stack ( ) . get_list ( ) ;
783
783
let mut object: HashMap < String , Type > = HashMap :: new ( ) ;
784
784
785
785
let name = if !class. is_empty ( ) {
786
786
class[ 0 ] . get_string ( )
787
787
} else {
788
- executor. log_print ( "Error! the type name is not found." . to_string ( ) ) ;
789
- executor
790
- . stack
791
- . push ( Type :: Error ( "instance-name" . to_string ( ) ) ) ;
788
+ self . log_print ( "Error! the type name is not found." . to_string ( ) ) ;
789
+ self . stack . push ( Type :: Error ( "instance-name" . to_string ( ) ) ) ;
792
790
return ;
793
791
} ;
794
792
@@ -799,9 +797,8 @@ pub fn execute_command(executor: &mut Executor, command: String) {
799
797
let element = match data. get ( index) {
800
798
Some ( value) => value,
801
799
None => {
802
- executor. log_print ( "Error! initial data is shortage\n " . to_string ( ) ) ;
803
- executor
804
- . stack
800
+ self . log_print ( "Error! initial data is shortage\n " . to_string ( ) ) ;
801
+ self . stack
805
802
. push ( Type :: Error ( "instance-shortage" . to_string ( ) ) ) ;
806
803
return ;
807
804
}
@@ -815,78 +812,67 @@ pub fn execute_command(executor: &mut Executor, command: String) {
815
812
let item = item. get_list ( ) ;
816
813
object. insert ( item[ 0 ] . clone ( ) . get_string ( ) , item[ 1 ] . clone ( ) ) ;
817
814
} else {
818
- executor. log_print ( "Error! the class data structure is wrong." . to_string ( ) ) ;
819
- executor
820
- . stack
821
- . push ( Type :: Error ( "instance-default" . to_string ( ) ) ) ;
815
+ self . log_print ( "Error! the class data structure is wrong." . to_string ( ) ) ;
816
+ self . stack . push ( Type :: Error ( "instance-default" . to_string ( ) ) ) ;
822
817
}
823
818
}
824
819
825
- executor . stack . push ( Type :: Object ( name, object) )
820
+ self . stack . push ( Type :: Object ( name, object) )
826
821
}
827
822
828
823
// Get property of object
829
824
"property" => {
830
- let name = executor. pop_stack ( ) . get_string ( ) ;
831
- match executor. pop_stack ( ) {
832
- Type :: Object ( _, data) => executor. stack . push (
833
- data. get ( name. as_str ( ) )
834
- . unwrap_or ( & Type :: Error ( "property" . to_string ( ) ) )
835
- . clone ( ) ,
836
- ) ,
837
- _ => executor. stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
838
- }
825
+ let name = self . pop_stack ( ) . get_string ( ) ;
826
+ let ( _, object) = self . pop_stack ( ) . get_object ( ) ;
827
+ self . stack . push (
828
+ object
829
+ . get ( name. as_str ( ) )
830
+ . unwrap_or ( & Type :: Error ( "property" . to_string ( ) ) )
831
+ . clone ( ) ,
832
+ )
839
833
}
840
834
841
835
// Call the method of object
842
836
"method" => {
843
- let method = executor. pop_stack ( ) . get_string ( ) ;
844
- match executor. pop_stack ( ) {
845
- Type :: Object ( name, value) => {
846
- let data = Type :: Object ( name, value. clone ( ) ) ;
847
- executor
848
- . memory
849
- . entry ( "executor" . to_string ( ) )
850
- . and_modify ( |value| * value = data. clone ( ) )
851
- . or_insert ( data) ;
852
-
853
- let program: String = match value. get ( & method) {
854
- Some ( i) => i. to_owned ( ) . get_string ( ) . to_string ( ) ,
855
- None => "" . to_string ( ) ,
856
- } ;
837
+ let method = self . pop_stack ( ) . get_string ( ) ;
838
+ let ( name, value) = self . pop_stack ( ) . get_object ( ) ;
839
+ let data = Type :: Object ( name, value. clone ( ) ) ;
840
+ self . memory
841
+ . entry ( "self" . to_string ( ) )
842
+ . and_modify ( |value| * value = data. clone ( ) )
843
+ . or_insert ( data) ;
857
844
858
- executor. evaluate_program ( program)
859
- }
860
- _ => executor. stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
861
- }
845
+ let program: String = match value. get ( & method) {
846
+ Some ( i) => i. to_owned ( ) . get_string ( ) . to_string ( ) ,
847
+ None => "" . to_string ( ) ,
848
+ } ;
849
+
850
+ self . evaluate_program ( program) ;
862
851
}
863
852
864
853
// Modify the property of object
865
854
"modify" => {
866
- let data = executor. pop_stack ( ) ;
867
- let property = executor. pop_stack ( ) . get_string ( ) ;
868
- match executor. pop_stack ( ) {
869
- Type :: Object ( name, mut value) => {
870
- value
871
- . entry ( property)
872
- . and_modify ( |value| * value = data. clone ( ) )
873
- . or_insert ( data. clone ( ) ) ;
874
-
875
- executor. stack . push ( Type :: Object ( name, value) )
876
- }
877
- _ => executor. stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
878
- }
855
+ let data = self . pop_stack ( ) ;
856
+ let property = self . pop_stack ( ) . get_string ( ) ;
857
+ let ( name, mut value) = self . pop_stack ( ) . get_object ( ) ;
858
+ value
859
+ . entry ( property)
860
+ . and_modify ( |value| * value = data. clone ( ) )
861
+ . or_insert ( data. clone ( ) ) ;
862
+
863
+ self . stack . push ( Type :: Object ( name, value) )
879
864
}
880
865
881
866
// Get all of properties
882
- "all" => match executor. pop_stack ( ) {
883
- Type :: Object ( _, data) => executor. stack . push ( Type :: List (
884
- data. keys ( )
867
+ "all" => {
868
+ let ( _, value) = self . pop_stack ( ) . get_object ( ) ;
869
+ self . stack . push ( Type :: List (
870
+ value
871
+ . keys ( )
885
872
. map ( |x| Type :: String ( x. to_owned ( ) ) )
886
873
. collect :: < Vec < Type > > ( ) ,
887
- ) ) ,
888
- _ => executor. stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
889
- } ,
874
+ ) ) ;
875
+ }
890
876
891
877
// Commands of external cooperation processing
892
878
0 commit comments