@@ -91,6 +91,7 @@ enum Type {
91
91
String ( String ) ,
92
92
Bool ( bool ) ,
93
93
List ( Vec < Type > ) ,
94
+ Object ( String , HashMap < String , Type > ) ,
94
95
Error ( String ) ,
95
96
}
96
97
@@ -103,10 +104,13 @@ impl Type {
103
104
Type :: String ( s) => format ! ( "({})" , s) ,
104
105
Type :: Bool ( b) => b. to_string ( ) ,
105
106
Type :: List ( list) => {
106
- let syntax : Vec < String > = list. iter ( ) . map ( |token| token. display ( ) ) . collect ( ) ;
107
- format ! ( "[{}]" , syntax . join( " " ) )
107
+ let result : Vec < String > = list. iter ( ) . map ( |token| token. display ( ) ) . collect ( ) ;
108
+ format ! ( "[{}]" , result . join( " " ) )
108
109
}
109
110
Type :: Error ( err) => format ! ( "error:{err}" ) ,
111
+ Type :: Object ( name, _) => {
112
+ format ! ( "Object<{name}>" )
113
+ }
110
114
}
111
115
}
112
116
@@ -118,6 +122,9 @@ impl Type {
118
122
Type :: Bool ( b) => b. to_string ( ) ,
119
123
Type :: List ( l) => Type :: List ( l. to_owned ( ) ) . display ( ) ,
120
124
Type :: Error ( err) => format ! ( "error:{err}" ) ,
125
+ Type :: Object ( name, _) => {
126
+ format ! ( "Object<{name}>" )
127
+ }
121
128
}
122
129
}
123
130
@@ -135,6 +142,7 @@ impl Type {
135
142
}
136
143
Type :: List ( l) => l. len ( ) as f64 ,
137
144
Type :: Error ( e) => e. parse ( ) . unwrap_or ( 0f64 ) ,
145
+ Type :: Object ( _, object) => object. len ( ) as f64 ,
138
146
}
139
147
}
140
148
@@ -146,6 +154,7 @@ impl Type {
146
154
Type :: Bool ( b) => * b,
147
155
Type :: List ( l) => !l. is_empty ( ) ,
148
156
Type :: Error ( e) => e. parse ( ) . unwrap_or ( false ) ,
157
+ Type :: Object ( _, object) => object. is_empty ( ) ,
149
158
}
150
159
}
151
160
@@ -161,6 +170,7 @@ impl Type {
161
170
Type :: Bool ( b) => vec ! [ Type :: Bool ( * b) ] ,
162
171
Type :: List ( l) => l. to_vec ( ) ,
163
172
Type :: Error ( e) => vec ! [ Type :: Error ( e. to_string( ) ) ] ,
173
+ Type :: Object ( _, object) => object. values ( ) . map ( |x| x. to_owned ( ) ) . collect :: < Vec < Type > > ( ) ,
164
174
}
165
175
}
166
176
}
@@ -918,13 +928,14 @@ impl Executor {
918
928
// Get data type of value
919
929
"type" => {
920
930
let result = match self . pop_stack ( ) {
921
- Type :: Number ( _) => "number" ,
922
- Type :: String ( _) => "string" ,
923
- Type :: Bool ( _) => "bool" ,
924
- Type :: List ( _) => "list" ,
925
- Type :: Error ( _) => "error" ,
926
- }
927
- . to_string ( ) ;
931
+ Type :: Number ( _) => "number" . to_string ( ) ,
932
+ Type :: String ( _) => "string" . to_string ( ) ,
933
+ Type :: Bool ( _) => "bool" . to_string ( ) ,
934
+ Type :: List ( _) => "list" . to_string ( ) ,
935
+ Type :: Error ( _) => "error" . to_string ( ) ,
936
+ Type :: Object ( name, _) => name. to_string ( ) ,
937
+ } ;
938
+
928
939
self . stack . push ( Type :: String ( result) ) ;
929
940
}
930
941
@@ -994,7 +1005,46 @@ impl Executor {
994
1005
// Sleep fixed time
995
1006
"sleep" => sleep ( Duration :: from_secs_f64 ( self . pop_stack ( ) . get_number ( ) ) ) ,
996
1007
997
- // Command of external cooperation processing
1008
+ // Commands of object oriented system
1009
+
1010
+ // Generate a instance of object
1011
+ "instance" => {
1012
+ let data = self . pop_stack ( ) . get_list ( ) ;
1013
+ let mut class = self . pop_stack ( ) . get_list ( ) ;
1014
+ let mut object: HashMap < String , Type > = HashMap :: new ( ) ;
1015
+ let name = class[ 0 ] . get_string ( ) ;
1016
+
1017
+ for ( name, element) in & mut class. to_owned ( ) [ 1 ..class. len ( ) ] . iter ( ) . zip ( data) {
1018
+ object. insert ( name. to_owned ( ) . get_string ( ) , element) ;
1019
+ }
1020
+
1021
+ self . stack . push ( Type :: Object ( name, object) )
1022
+ }
1023
+
1024
+ // Get property of object
1025
+ "property" => {
1026
+ let name = self . pop_stack ( ) . get_string ( ) ;
1027
+ match self . pop_stack ( ) {
1028
+ Type :: Object ( _, data) => self . stack . push (
1029
+ data. get ( name. as_str ( ) )
1030
+ . unwrap_or ( & Type :: Error ( "property" . to_string ( ) ) )
1031
+ . clone ( ) ,
1032
+ ) ,
1033
+ _ => self . stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
1034
+ }
1035
+ }
1036
+
1037
+ // Get all of properties
1038
+ "all" => match self . pop_stack ( ) {
1039
+ Type :: Object ( _, data) => self . stack . push ( Type :: List (
1040
+ data. keys ( )
1041
+ . map ( |x| Type :: String ( x. to_owned ( ) ) )
1042
+ . collect :: < Vec < Type > > ( ) ,
1043
+ ) ) ,
1044
+ _ => self . stack . push ( Type :: Error ( "not-object" . to_string ( ) ) ) ,
1045
+ } ,
1046
+
1047
+ // Commands of external cooperation processing
998
1048
999
1049
// Send the http request
1000
1050
"request" => {
0 commit comments