1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use async_trait::async_trait;
use endbasic_core::ast::{ArgSep, Expr, Value, VarType};
use endbasic_core::eval::{CallableMetadata, CallableMetadataBuilder};
use endbasic_core::exec::{new_usage_error, Command, Machine, Result};
use std::rc::Rc;
pub struct ClearCommand {
metadata: CallableMetadata,
}
impl ClearCommand {
pub fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("CLEAR", VarType::Void)
.with_syntax("")
.with_category("Interpreter manipulation")
.with_description("Clears all variables to restore initial state.")
.build(),
})
}
}
#[async_trait(?Send)]
impl Command for ClearCommand {
fn metadata(&self) -> &CallableMetadata {
&self.metadata
}
async fn exec(&self, args: &[(Option<Expr>, ArgSep)], machine: &mut Machine) -> Result<()> {
if !args.is_empty() {
return new_usage_error("CLEAR takes no arguments");
}
machine.clear();
Ok(())
}
}
pub struct ExitCommand {
metadata: CallableMetadata,
}
impl ExitCommand {
pub fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("EXIT", VarType::Void)
.with_syntax("[code%]")
.with_category("Interpreter manipulation")
.with_description(
"Exits the interpreter.
The optional code indicates the return value to return to the system.",
)
.build(),
})
}
}
#[async_trait(?Send)]
impl Command for ExitCommand {
fn metadata(&self) -> &CallableMetadata {
&self.metadata
}
async fn exec(&self, args: &[(Option<Expr>, ArgSep)], machine: &mut Machine) -> Result<()> {
let arg = match args {
[] => 0,
[(Some(expr), ArgSep::End)] => {
match expr.eval(machine.get_vars(), machine.get_functions())? {
Value::Integer(n) => {
if n < 0 {
return new_usage_error("Exit code must be a positive integer");
}
if n >= 128 {
return new_usage_error("Exit code cannot be larger than 127");
}
n as u8
}
_ => return new_usage_error("Exit code must be a positive integer"),
}
}
_ => return new_usage_error("EXIT takes zero or one argument"),
};
machine.exit(arg);
Ok(())
}
}
pub fn add_all(machine: &mut Machine) {
machine.add_command(ClearCommand::new());
machine.add_command(ExitCommand::new());
}
#[cfg(test)]
mod tests {
use crate::testutils::*;
use endbasic_core::exec::StopReason;
#[test]
fn test_clear_ok() {
Tester::default().run("a = 1: CLEAR").check();
}
#[test]
fn test_clear_errors() {
check_stmt_err("CLEAR takes no arguments", "CLEAR 123");
}
#[test]
fn test_exit_no_code() {
Tester::default()
.run("a = 3: EXIT: a = 4")
.expect_ok(StopReason::Exited(0))
.expect_var("a", 3)
.check();
}
fn do_exit_with_code_test(code: u8) {
Tester::default()
.run(format!("a = 3: EXIT {}: a = 4", code))
.expect_ok(StopReason::Exited(code))
.expect_var("a", 3)
.check();
}
#[test]
fn text_exit_with_code() {
do_exit_with_code_test(0);
do_exit_with_code_test(1);
do_exit_with_code_test(42);
do_exit_with_code_test(127);
}
#[test]
fn test_exit_errors() {
check_stmt_err("EXIT takes zero or one argument", "EXIT 1, 2");
check_stmt_err("Exit code must be a positive integer", "EXIT -3");
check_stmt_err("Exit code cannot be larger than 127", "EXIT 128");
}
}