Skip to content

Commit 4a76dca

Browse files
author
梶塚太智
authored
Merge pull request #13 from rensatopc/main
Add Command "clear" & "cls"
2 parents a251e3d + fb47ebf commit 4a76dca

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ Cargo.lock
1111
**/*.rs.bk
1212

1313
# MSVC Windows builds of rustc generate these, which store debugging information
14-
*.pdb
15-
16-
hidden/
14+
*.pdb

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ regex = "0.1"
1212
reqwest = { version = "0.11.0", features = ["blocking"] }
1313
rodio = "0.17.3"
1414
sys-info = "0.7.0"
15-
clap = "3.0"
15+
clap = "3.0"
16+
clearscreen = "2.0.1"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ The powerful script language designed with a stack oriented approach for efficie
77

88
### If you don't want to install...
99

10+
**Launching Stack needs cargo!**
11+
1012
```bash
1113
git clone https://github.com/Stack-Programing-Community/Stack-Programing-Language.git
1214
cd Stack-Programing-Language

src/main.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ impl Executor {
335335
self.stack.push(Type::Bool(token.parse().unwrap_or(true)));
336336
} else if chars[0] == '(' && chars[chars.len() - 1] == ')' {
337337
// Push string value on the stack
338+
self.stack
339+
.push(Type::String(token[1..token.len() - 1].to_string()));
338340
let string = {
339341
let mut buffer = String::new(); // Temporary storage
340342
let mut brackets = 0; // String's nest structure
@@ -380,6 +382,12 @@ impl Executor {
380382
'r' => buffer.push_str("\\r"),
381383
_ => buffer.push(c),
382384
}
385+
buffer.push(match c {
386+
'n' => '\n',
387+
't' => '\t',
388+
'r' => '\r',
389+
_ => c,
390+
})
383391
} else {
384392
buffer.push(c);
385393
}
@@ -396,6 +404,8 @@ impl Executor {
396404
buffer
397405
};
398406
self.stack.push(Type::String(string));
407+
self.stack
408+
.push(Type::String(token[1..token.len() - 1].to_string()));
399409
} else if chars[0] == '[' && chars[chars.len() - 1] == ']' {
400410
// Push list value on the stack
401411
let old_len = self.stack.len(); // length of old stack
@@ -700,15 +710,10 @@ impl Executor {
700710
// Standard output
701711
"print" => {
702712
let a = self.pop_stack().get_string();
703-
704-
let a = a.replace("\\n", "\n");
705-
let a = a.replace("\\t", "\t");
706-
let a = a.replace("\\r", "\r");
707-
708713
if let Mode::Debug = self.mode {
709714
println!("[Output]: {a}");
710715
} else {
711-
print!("{a}");
716+
println!("{a}");
712717
}
713718
}
714719

@@ -868,7 +873,6 @@ impl Executor {
868873
return;
869874
}
870875
}
871-
872876
self.log_print(String::from("Error! item not found in the list\n"));
873877
self.stack.push(Type::Error(String::from("item-not-found")));
874878
}
@@ -1378,11 +1382,28 @@ impl Executor {
13781382
})
13791383
}
13801384

1385+
"cls" => {
1386+
self.clearscreen();
1387+
}
1388+
1389+
"clear" => {
1390+
self.clearscreen();
1391+
}
1392+
13811393
// If it is not recognized as a command, use it as a string.
13821394
_ => self.stack.push(Type::String(command)),
13831395
}
13841396
}
13851397

1398+
fn clearscreen(&mut self) {
1399+
let result = clearscreen::clear();
1400+
if result.is_err() {
1401+
println!("Error! Failed to clear screen");
1402+
self.stack
1403+
.push(Type::Error(String::from("failed-to-clear-screen")));
1404+
}
1405+
}
1406+
13861407
/// Pop stack's top value
13871408
fn pop_stack(&mut self) -> Type {
13881409
if let Some(value) = self.stack.pop() {

0 commit comments

Comments
 (0)