Skip to content

Commit 7a00ade

Browse files
author
梶塚太智
committed
Update main.rs
1 parent 15ed514 commit 7a00ade

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

src/main.rs

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use opener;
21
use rand::seq::SliceRandom;
32
use regex::Regex;
43
use std::collections::HashMap;
@@ -13,45 +12,41 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
1312
fn main() {
1413
// コマンドライン引数を読み込む
1514
let args = env::args().collect::<Vec<_>>();
16-
if args.len() > 2 {
17-
// ファイルを開く
18-
if let Ok(code) = get_file_contents(args[2].clone()) {
19-
// 実行モードを判定する
20-
if args[1].contains("d") {
15+
// ファイルを開く
16+
if let Ok(code) = get_file_contents(args[1].clone()) {
17+
// 実行モードを判定する
18+
if args.len() > 2 {
19+
if args[2].contains("-d") {
2120
let mut executor = Executor::new(Mode::Debug);
2221
executor.evaluate_program(code); //デバッグ実行
2322
} else {
2423
let mut executor = Executor::new(Mode::Script);
2524
executor.evaluate_program(code); // スクリプト実行
2625
}
27-
} else {
28-
println!("エラー! ファイルが見つかりません")
29-
}
30-
} else if args.len() > 1 {
31-
// ファイルを開く
32-
if let Ok(code) = get_file_contents(args[1].clone()) {
26+
} else if args.len() > 1 {
27+
// ファイルを開く
3328
let mut executor = Executor::new(Mode::Script); //デフォルト値はスクリプト実行
3429
executor.evaluate_program(code);
3530
} else {
36-
println!("エラー! ファイルが見つかりません")
37-
}
38-
} else {
39-
// タイトルを表示する
40-
println!("Stack プログラミング言語");
41-
let mut executor = Executor::new(Mode::Debug);
42-
// REPL実行
43-
loop {
44-
let mut code = String::new();
31+
// タイトルを表示する
32+
println!("Stack プログラミング言語");
33+
let mut executor = Executor::new(Mode::Debug);
34+
// REPL実行
4535
loop {
46-
let inputed = input("> ");
47-
code += &format!("{inputed}\n");
48-
if inputed.is_empty() {
49-
break;
36+
let mut code = String::new();
37+
loop {
38+
let inputed = input("> ");
39+
code += &format!("{inputed}\n");
40+
if inputed.is_empty() {
41+
break;
42+
}
5043
}
51-
}
5244

53-
executor.evaluate_program(code)
45+
executor.evaluate_program(code)
46+
}
5447
}
48+
} else {
49+
println!("エラー! ファイルが見つかりません")
5550
}
5651
}
5752

@@ -65,7 +60,7 @@ fn get_file_contents(name: String) -> Result<String, Error> {
6560

6661
/// 標準入力を受け取る
6762
fn input(prompt: &str) -> String {
68-
print!("{}", prompt.to_string());
63+
print!("{}", prompt);
6964
io::stdout().flush().unwrap();
7065
let mut result = String::new();
7166
io::stdin().read_line(&mut result).ok();
@@ -132,10 +127,10 @@ impl Type {
132127
/// 論理値を取得
133128
fn get_bool(&mut self) -> bool {
134129
match self {
135-
Type::String(s) => s.len() != 0,
130+
Type::String(s) => !s.is_empty(),
136131
Type::Number(i) => *i != 0.0,
137132
Type::Bool(b) => *b,
138-
Type::List(l) => l.len() != 0,
133+
Type::List(l) => !l.is_empty(),
139134
}
140135
}
141136

@@ -205,11 +200,7 @@ impl Executor {
205200

206201
/// 構文解析
207202
fn analyze_syntax(&mut self, code: String) -> Vec<String> {
208-
let code = code
209-
.replace("\n", " ")
210-
.replace("\t", " ")
211-
.replace("\r", " ")
212-
.replace(" ", " ");
203+
let code = code.replace(['\n', '\t', '\r', ' '], " ");
213204

214205
let mut syntax = Vec::new();
215206
let mut buffer = String::new();
@@ -319,8 +310,8 @@ impl Executor {
319310
}
320311

321312
// コメントを処理
322-
if token.contains("#") {
323-
self.log_print(format!("※ コメント「{}」\n", token.replace("#", "")));
313+
if token.contains('#') {
314+
self.log_print(format!("※ コメント「{}」\n", token.replace('#', "")));
324315
continue;
325316
}
326317

@@ -518,14 +509,13 @@ impl Executor {
518509
// 正規表現で検索
519510
"regex" => {
520511
let pt = self.pop_stack().get_string();
521-
let patern: Regex;
522-
match Regex::new(pt.as_str()) {
523-
Ok(i) => patern = i,
512+
let patern: Regex = match Regex::new(pt.as_str()) {
513+
Ok(i) => i,
524514
Err(_) => {
525515
self.log_print("エラー! 正規表現が不正です\n".to_string());
526516
return;
527517
}
528-
}
518+
};
529519

530520
let text = self.pop_stack().get_string();
531521

@@ -649,7 +639,7 @@ impl Executor {
649639
let index = self.pop_stack().get_number() as usize;
650640
let mut list = self.pop_stack().get_list();
651641
if list.len() > index {
652-
list.remove(index as usize);
642+
list.remove(index);
653643
self.stack.push(Type::List(list));
654644
} else {
655645
self.log_print("エラー! インデックス指定が範囲外です\n".to_string());
@@ -922,10 +912,8 @@ impl Executor {
922912
if let Err(e) = fs::remove_dir(name) {
923913
self.log_print(format!("エラー! {e}\n"))
924914
}
925-
} else {
926-
if let Err(e) = fs::remove_file(name) {
927-
self.log_print(format!("エラー! {e}\n"))
928-
}
915+
} else if let Err(e) = fs::remove_file(name) {
916+
self.log_print(format!("エラー! {e}\n"))
929917
}
930918
}
931919

@@ -937,7 +925,7 @@ impl Executor {
937925
entry
938926
.ok()
939927
.and_then(|e| e.file_name().into_string().ok())
940-
.map(|x| Type::String(x))
928+
.map(Type::String)
941929
})
942930
.collect();
943931
self.stack.push(Type::List(value));

0 commit comments

Comments
 (0)