Skip to content

Commit ca4a005

Browse files
author
梶塚太智
committed
Implement object oriented system
1 parent 26099bd commit ca4a005

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

src/main.rs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enum Type {
9191
String(String),
9292
Bool(bool),
9393
List(Vec<Type>),
94+
Object(String, HashMap<String, Type>),
9495
Error(String),
9596
}
9697

@@ -103,10 +104,13 @@ impl Type {
103104
Type::String(s) => format!("({})", s),
104105
Type::Bool(b) => b.to_string(),
105106
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(" "))
108109
}
109110
Type::Error(err) => format!("error:{err}"),
111+
Type::Object(name, _) => {
112+
format!("Object<{name}>")
113+
}
110114
}
111115
}
112116

@@ -118,6 +122,9 @@ impl Type {
118122
Type::Bool(b) => b.to_string(),
119123
Type::List(l) => Type::List(l.to_owned()).display(),
120124
Type::Error(err) => format!("error:{err}"),
125+
Type::Object(name, _) => {
126+
format!("Object<{name}>")
127+
}
121128
}
122129
}
123130

@@ -135,6 +142,7 @@ impl Type {
135142
}
136143
Type::List(l) => l.len() as f64,
137144
Type::Error(e) => e.parse().unwrap_or(0f64),
145+
Type::Object(_, object) => object.len() as f64,
138146
}
139147
}
140148

@@ -146,6 +154,7 @@ impl Type {
146154
Type::Bool(b) => *b,
147155
Type::List(l) => !l.is_empty(),
148156
Type::Error(e) => e.parse().unwrap_or(false),
157+
Type::Object(_, object) => object.is_empty(),
149158
}
150159
}
151160

@@ -161,6 +170,7 @@ impl Type {
161170
Type::Bool(b) => vec![Type::Bool(*b)],
162171
Type::List(l) => l.to_vec(),
163172
Type::Error(e) => vec![Type::Error(e.to_string())],
173+
Type::Object(_, object) => object.values().map(|x| x.to_owned()).collect::<Vec<Type>>(),
164174
}
165175
}
166176
}
@@ -918,13 +928,14 @@ impl Executor {
918928
// Get data type of value
919929
"type" => {
920930
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+
928939
self.stack.push(Type::String(result));
929940
}
930941

@@ -994,7 +1005,46 @@ impl Executor {
9941005
// Sleep fixed time
9951006
"sleep" => sleep(Duration::from_secs_f64(self.pop_stack().get_number())),
9961007

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
9981048

9991049
// Send the http request
10001050
"request" => {

0 commit comments

Comments
 (0)