Skip to content

Commit 3ad675b

Browse files
committed
Add builtin quit function
1 parent e73b5ca commit 3ad675b

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/environment.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ impl<'src> Environment<'src> {
127127
Ok(Value::Null)
128128
});
129129

130+
env.add_builtin_function("quit", |arguments, span| {
131+
if arguments.is_empty() {
132+
process::exit(0);
133+
}
134+
135+
if arguments.len() != 1 {
136+
return Err(Error::new(
137+
span,
138+
format!(
139+
"Function `quit` expects 0 or 1 arguments, got {}",
140+
arguments.len()
141+
),
142+
));
143+
}
144+
145+
process::exit(arguments[0].number(span)? as i32);
146+
});
147+
130148
env.add_variable("e", Value::Number(std::f64::consts::E));
131149
env.add_variable("pi", Value::Number(std::f64::consts::PI));
132150

tests/integration.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,18 @@ fn len() -> Result {
624624
.expected_stdout(Exact("13\n"))
625625
.run()
626626
}
627+
628+
#[test]
629+
fn quit() -> Result {
630+
Test::new()?.program("quit()").expected_status(0).run()?;
631+
632+
Test::new()?.program("quit(1)").expected_status(1).run()?;
633+
634+
Test::new()?
635+
.program("quit(1, 2)")
636+
.expected_status(1)
637+
.expected_stderr(Contains(
638+
"Function `quit` expects 0 or 1 arguments, got 2",
639+
))
640+
.run()
641+
}

0 commit comments

Comments
 (0)