Skip to content

Commit 7316678

Browse files
Refactoring object
1 parent 52d6e6a commit 7316678

File tree

3 files changed

+54
-61
lines changed

3 files changed

+54
-61
lines changed

src/functions.rs

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -778,17 +778,15 @@ pub fn execute_command(executor: &mut Executor, command: String) {
778778

779779
// Generate a instance of object
780780
"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();
783783
let mut object: HashMap<String, Type> = HashMap::new();
784784

785785
let name = if !class.is_empty() {
786786
class[0].get_string()
787787
} 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()));
792790
return;
793791
};
794792

@@ -799,9 +797,8 @@ pub fn execute_command(executor: &mut Executor, command: String) {
799797
let element = match data.get(index) {
800798
Some(value) => value,
801799
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
805802
.push(Type::Error("instance-shortage".to_string()));
806803
return;
807804
}
@@ -815,78 +812,67 @@ pub fn execute_command(executor: &mut Executor, command: String) {
815812
let item = item.get_list();
816813
object.insert(item[0].clone().get_string(), item[1].clone());
817814
} 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()));
822817
}
823818
}
824819

825-
executor.stack.push(Type::Object(name, object))
820+
self.stack.push(Type::Object(name, object))
826821
}
827822

828823
// Get property of object
829824
"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+
)
839833
}
840834

841835
// Call the method of object
842836
"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);
857844

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);
862851
}
863852

864853
// Modify the property of object
865854
"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))
879864
}
880865

881866
// 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()
885872
.map(|x| Type::String(x.to_owned()))
886873
.collect::<Vec<Type>>(),
887-
)),
888-
_ => executor.stack.push(Type::Error("not-object".to_string())),
889-
},
874+
));
875+
}
890876

891877
// Commands of external cooperation processing
892878

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ impl Type {
178178
Type::Object(_, object) => object.values().map(|x| x.to_owned()).collect::<Vec<Type>>(),
179179
}
180180
}
181+
182+
fn get_object(&self) -> (String, HashMap<String, Type>) {
183+
match self {
184+
Type::Object(name, value) => (name.to_owned(), value.to_owned()),
185+
_ => ("".to_string(), HashMap::new()),
186+
}
187+
}
181188
}
182189

183190
/// Manage program execution

src/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ fn equal_false() {
125125
},
126126
false
127127
);
128-
}
128+
}

0 commit comments

Comments
 (0)