Skip to content

Commit c60f71f

Browse files
author
梶塚太智
authored
Merge pull request #10 from develop branch
Developed error type
2 parents fb6b0dd + bb82b08 commit c60f71f

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

src/main.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ enum Type {
9090
String(String),
9191
Bool(bool),
9292
List(Vec<Type>),
93+
Error(String),
9394
}
9495

9596
/// Implement methods
@@ -104,6 +105,7 @@ impl Type {
104105
let syntax: Vec<String> = list.iter().map(|token| token.display()).collect();
105106
format!("[{}]", syntax.join(" "))
106107
}
108+
Type::Error(err) => format!("error:{err}"),
107109
}
108110
}
109111

@@ -114,6 +116,7 @@ impl Type {
114116
Type::Number(i) => i.to_string(),
115117
Type::Bool(b) => b.to_string(),
116118
Type::List(l) => Type::List(l.to_owned()).display(),
119+
Type::Error(err) => format!("error:{err}"),
117120
}
118121
}
119122

@@ -130,6 +133,7 @@ impl Type {
130133
}
131134
}
132135
Type::List(l) => l.len() as f64,
136+
Type::Error(_) => 0f64,
133137
}
134138
}
135139

@@ -140,6 +144,7 @@ impl Type {
140144
Type::Number(i) => *i != 0.0,
141145
Type::Bool(b) => *b,
142146
Type::List(l) => !l.is_empty(),
147+
Type::Error(_) => false,
143148
}
144149
}
145150

@@ -154,6 +159,7 @@ impl Type {
154159
Type::Number(i) => vec![Type::Number(*i)],
155160
Type::Bool(b) => vec![Type::Bool(*b)],
156161
Type::List(l) => l.to_vec(),
162+
Type::Error(e) => vec![Type::Error(e.to_string())],
157163
}
158164
}
159165
}
@@ -456,7 +462,7 @@ impl Executor {
456462
Some(c) => self.stack.push(Type::String(c.to_string())),
457463
None => {
458464
self.log_print("Error! failed of number decoding\n".to_string());
459-
self.stack.push(Type::Number(code));
465+
self.stack.push(Type::Error("number-decoding".to_string()));
460466
}
461467
}
462468
}
@@ -468,7 +474,7 @@ impl Executor {
468474
self.stack.push(Type::Number((first_char as u32) as f64));
469475
} else {
470476
self.log_print("Error! failed of string encoding\n".to_string());
471-
self.stack.push(Type::String(string))
477+
self.stack.push(Type::Error("string-encoding".to_string()));
472478
}
473479
}
474480

@@ -523,7 +529,8 @@ impl Executor {
523529
let pattern: Regex = match Regex::new(pattern.as_str()) {
524530
Ok(i) => i,
525531
Err(e) => {
526-
self.log_print(format!("Error! {e}\n"));
532+
self.log_print(format!("Error! {}\n", e.to_string().replace("Error", "")));
533+
self.stack.push(Type::Error("regex".to_string()));
527534
return;
528535
}
529536
};
@@ -541,10 +548,17 @@ impl Executor {
541548

542549
// Write string in the file
543550
"write-file" => {
544-
let mut file =
545-
File::create(self.pop_stack().get_string()).expect("Failed to create file");
551+
let mut file = match File::create(self.pop_stack().get_string()) {
552+
Ok(file) => file,
553+
Err(e) => {
554+
self.log_print(format!("Error! {e}\n"));
555+
self.stack.push(Type::Error("create-file".to_string()));
556+
return;
557+
}
558+
};
546559
if let Err(e) = file.write_all(self.pop_stack().get_string().as_bytes()) {
547-
self.log_print(format!("Error! {}\n", e))
560+
self.log_print(format!("Error! {}\n", e));
561+
self.stack.push(Type::Error("write-file".to_string()));
548562
}
549563
}
550564

@@ -553,7 +567,10 @@ impl Executor {
553567
let name = self.pop_stack().get_string();
554568
match get_file_contents(name) {
555569
Ok(s) => self.stack.push(Type::String(s)),
556-
Err(e) => self.log_print(format!("Error! {}\n", e)),
570+
Err(e) => {
571+
self.log_print(format!("Error! {}\n", e));
572+
self.stack.push(Type::Error("read-file".to_string()));
573+
}
557574
};
558575
}
559576

@@ -670,7 +687,7 @@ impl Executor {
670687
self.stack.push(list[index].clone());
671688
} else {
672689
self.log_print("Error! Index specification is out of range\n".to_string());
673-
self.stack.push(Type::List(list));
690+
self.stack.push(Type::Error("index-out-range".to_string()));
674691
}
675692
}
676693

@@ -684,7 +701,7 @@ impl Executor {
684701
self.stack.push(Type::List(list));
685702
} else {
686703
self.log_print("Error! Index specification is out of range\n".to_string());
687-
self.stack.push(Type::List(list));
704+
self.stack.push(Type::Error("index-out-range".to_string()));
688705
}
689706
}
690707

@@ -697,7 +714,7 @@ impl Executor {
697714
self.stack.push(Type::List(list));
698715
} else {
699716
self.log_print("Error! Index specification is out of range\n".to_string());
700-
self.stack.push(Type::List(list));
717+
self.stack.push(Type::Error("index-out-range".to_string()));
701718
}
702719
}
703720

@@ -883,6 +900,7 @@ impl Executor {
883900
Type::String(_) => "string",
884901
Type::Bool(_) => "bool",
885902
Type::List(_) => "list",
903+
Type::Error(_) => "error",
886904
}
887905
.to_string();
888906
self.stack.push(Type::String(result));
@@ -960,14 +978,16 @@ impl Executor {
960978
// Open the file or url
961979
"open" => {
962980
if let Err(e) = opener::open(self.pop_stack().get_string()) {
963-
self.log_print(format!("Error! {e}\n"))
981+
self.log_print(format!("Error! {e}\n"));
982+
self.stack.push(Type::Error("open".to_string()));
964983
}
965984
}
966985

967986
// Change current directory
968987
"cd" => {
969988
if let Err(err) = std::env::set_current_dir(self.pop_stack().get_string()) {
970989
self.log_print(format!("Error! {}\n", err));
990+
self.stack.push(Type::Error("cd".to_string()));
971991
}
972992
}
973993

@@ -984,7 +1004,8 @@ impl Executor {
9841004
"mkdir" => {
9851005
let name = self.pop_stack().get_string();
9861006
if let Err(e) = fs::create_dir(name) {
987-
self.log_print(format!("Error! {e}\n"))
1007+
self.log_print(format!("Error! {e}\n"));
1008+
self.stack.push(Type::Error("mkdir".to_string()));
9881009
}
9891010
}
9901011

@@ -993,10 +1014,12 @@ impl Executor {
9931014
let name = self.pop_stack().get_string();
9941015
if Path::new(name.as_str()).is_dir() {
9951016
if let Err(e) = fs::remove_dir(name) {
996-
self.log_print(format!("Error! {e}\n"))
1017+
self.log_print(format!("Error! {e}\n"));
1018+
self.stack.push(Type::Error("rm".to_string()));
9971019
}
9981020
} else if let Err(e) = fs::remove_file(name) {
999-
self.log_print(format!("Error! {e}\n"))
1021+
self.log_print(format!("Error! {e}\n"));
1022+
self.stack.push(Type::Error("rm".to_string()));
10001023
}
10011024
}
10021025

@@ -1005,7 +1028,8 @@ impl Executor {
10051028
let to = self.pop_stack().get_string();
10061029
let from = self.pop_stack().get_string();
10071030
if let Err(e) = fs::rename(from, to) {
1008-
self.log_print(format!("Error! {e}\n"))
1031+
self.log_print(format!("Error! {e}\n"));
1032+
self.stack.push(Type::Error("rename".to_string()));
10091033
}
10101034
}
10111035

src/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ fn control_while() {
9393

9494
assert_eq!(
9595
{
96-
executor.evaluate_program("5 (i) var (i 1 add (i) var) (i 10 less) while i".to_string());
96+
executor
97+
.evaluate_program("5 (i) var (i 1 add (i) var) (i 10 less) while i".to_string());
9798
executor.pop_stack().get_number()
9899
},
99100
10f64

0 commit comments

Comments
 (0)