Skip to content

Commit c13b5b6

Browse files
Update main.rs
Fix `range` command
1 parent 285d0f5 commit c13b5b6

File tree

1 file changed

+7
-120
lines changed

1 file changed

+7
-120
lines changed

src/main.rs

+7-120
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ enum Type {
281281
Bool(bool),
282282
List(Vec<Type>),
283283
Matrix(Vec<Fraction>, (usize, usize)),
284-
Object(String, HashMap<String, Type>),
285284
Error(String),
286285
}
287286

@@ -331,7 +330,6 @@ impl Type {
331330
format!("[{}]", result.join(" "))
332331
}
333332
Type::Error(err) => format!("error:{err}"),
334-
Type::Object(name, _) => format!("Object<{name}>"),
335333
Type::Matrix(mx, (_, length)) => Type::to_matrix(mx, *length),
336334
}
337335
}
@@ -344,7 +342,6 @@ impl Type {
344342
Type::Bool(b) => b.to_string(),
345343
Type::List(l) => Type::List(l.to_owned()).display(),
346344
Type::Error(err) => format!("error:{err}"),
347-
Type::Object(name, _) => format!("Object<{name}>"),
348345
Type::Matrix(mx, (_, length)) => Type::to_matrix(mx, *length),
349346
}
350347
}
@@ -363,7 +360,6 @@ impl Type {
363360
}
364361
Type::List(l) => Fraction::new(l.len() as f64),
365362
Type::Error(e) => Fraction::new(e.parse().unwrap_or(0f64)),
366-
Type::Object(_, object) => Fraction::new(object.len() as f64),
367363
_ => Fraction::new(0f64),
368364
}
369365
}
@@ -376,7 +372,6 @@ impl Type {
376372
Type::Bool(b) => *b,
377373
Type::List(l) => !l.is_empty(),
378374
Type::Error(e) => e.parse().unwrap_or(false),
379-
Type::Object(_, object) => object.is_empty(),
380375
_ => false,
381376
}
382377
}
@@ -393,7 +388,6 @@ impl Type {
393388
Type::Bool(b) => vec![Type::Bool(*b)],
394389
Type::List(l) => l.to_vec(),
395390
Type::Error(e) => vec![Type::Error(e.to_string())],
396-
Type::Object(_, object) => object.values().map(|x| x.to_owned()).collect::<Vec<Type>>(),
397391
Type::Matrix(l, _) => l
398392
.to_owned()
399393
.iter()
@@ -1155,14 +1149,16 @@ impl Executor {
11551149

11561150
// Generate a range
11571151
"range" => {
1158-
let step = self.pop_stack().get_number().to_f64();
1159-
let max = self.pop_stack().get_number().to_f64();
1160-
let min = self.pop_stack().get_number().to_f64();
1152+
let step = self.pop_stack().get_number();
1153+
let max = self.pop_stack().get_number();
1154+
let min = self.pop_stack().get_number();
11611155

11621156
let mut range: Vec<Type> = Vec::new();
1157+
let mut i = min;
11631158

1164-
for i in (min as usize..max as usize).step_by(step as usize) {
1165-
range.push(Type::Number(Fraction::new(i as f64)));
1159+
while i < max {
1160+
range.push(Type::Number(i));
1161+
i += step;
11661162
}
11671163

11681164
self.stack.push(Type::List(range));
@@ -1296,7 +1292,6 @@ impl Executor {
12961292
Type::List(_) => "list".to_string(),
12971293
Type::Error(_) => "error".to_string(),
12981294
Type::Matrix(_, _) => "matrix".to_string(),
1299-
Type::Object(name, _) => name.to_string(),
13001295
};
13011296

13021297
self.stack.push(Type::String(result));
@@ -1364,114 +1359,6 @@ impl Executor {
13641359
self.pop_stack().get_number().to_f64(),
13651360
)),
13661361

1367-
// Commands of object oriented system
1368-
1369-
// Generate a instance of object
1370-
"instance" => {
1371-
let data = self.pop_stack().get_list();
1372-
let mut class = self.pop_stack().get_list();
1373-
let mut object: HashMap<String, Type> = HashMap::new();
1374-
1375-
let name = if !class.is_empty() {
1376-
class[0].get_string()
1377-
} else {
1378-
self.log_print("Error! the type name is not found.".to_string());
1379-
self.stack.push(Type::Error("instance-name".to_string()));
1380-
return;
1381-
};
1382-
1383-
let mut index = 0;
1384-
for item in &mut class.to_owned()[1..class.len()].iter() {
1385-
let mut item = item.to_owned();
1386-
if item.get_list().len() == 1 {
1387-
let element = match data.get(index) {
1388-
Some(value) => value,
1389-
None => {
1390-
self.log_print("Error! initial data is shortage\n".to_string());
1391-
self.stack
1392-
.push(Type::Error("instance-shortage".to_string()));
1393-
return;
1394-
}
1395-
};
1396-
object.insert(
1397-
item.get_list()[0].to_owned().get_string(),
1398-
element.to_owned(),
1399-
);
1400-
index += 1;
1401-
} else if item.get_list().len() >= 2 {
1402-
let item = item.get_list();
1403-
object.insert(item[0].clone().get_string(), item[1].clone());
1404-
} else {
1405-
self.log_print("Error! the class data structure is wrong.".to_string());
1406-
self.stack.push(Type::Error("instance-default".to_string()));
1407-
}
1408-
}
1409-
1410-
self.stack.push(Type::Object(name, object))
1411-
}
1412-
1413-
// Get property of object
1414-
"property" => {
1415-
let name = self.pop_stack().get_string();
1416-
match self.pop_stack() {
1417-
Type::Object(_, data) => self.stack.push(
1418-
data.get(name.as_str())
1419-
.unwrap_or(&Type::Error("property".to_string()))
1420-
.clone(),
1421-
),
1422-
_ => self.stack.push(Type::Error("not-object".to_string())),
1423-
}
1424-
}
1425-
1426-
// Call the method of object
1427-
"method" => {
1428-
let method = self.pop_stack().get_string();
1429-
match self.pop_stack() {
1430-
Type::Object(name, value) => {
1431-
let data = Type::Object(name, value.clone());
1432-
self.memory
1433-
.entry("self".to_string())
1434-
.and_modify(|value| *value = data.clone())
1435-
.or_insert(data);
1436-
1437-
let program: String = match value.get(&method) {
1438-
Some(i) => i.to_owned().get_string().to_string(),
1439-
None => "".to_string(),
1440-
};
1441-
1442-
self.evaluate_program(program)
1443-
}
1444-
_ => self.stack.push(Type::Error("not-object".to_string())),
1445-
}
1446-
}
1447-
1448-
// Modify the property of object
1449-
"modify" => {
1450-
let data = self.pop_stack();
1451-
let property = self.pop_stack().get_string();
1452-
match self.pop_stack() {
1453-
Type::Object(name, mut value) => {
1454-
value
1455-
.entry(property)
1456-
.and_modify(|value| *value = data.clone())
1457-
.or_insert(data.clone());
1458-
1459-
self.stack.push(Type::Object(name, value))
1460-
}
1461-
_ => self.stack.push(Type::Error("not-object".to_string())),
1462-
}
1463-
}
1464-
1465-
// Get all of properties
1466-
"all" => match self.pop_stack() {
1467-
Type::Object(_, data) => self.stack.push(Type::List(
1468-
data.keys()
1469-
.map(|x| Type::String(x.to_owned()))
1470-
.collect::<Vec<Type>>(),
1471-
)),
1472-
_ => self.stack.push(Type::Error("not-object".to_string())),
1473-
},
1474-
14751362
// Commands of matrix
14761363
"scalar-mul" => {
14771364
let number = self.pop_stack().get_number().to_f64();

0 commit comments

Comments
 (0)