Panic function

This commit is contained in:
Laurenz 2023-02-19 21:08:01 +01:00
parent 1e681b35c7
commit 6e65ebf236
3 changed files with 52 additions and 6 deletions

View File

@ -56,6 +56,29 @@ pub fn repr(args: &mut Args) -> SourceResult<Value> {
Ok(args.expect::<Value>("value")?.repr().into()) Ok(args.expect::<Value>("value")?.repr().into())
} }
/// # Panic
/// Fail with an error.
///
/// ## Example
/// The code below produces the error `panicked at: "this is wrong"`.
/// ```typ
/// #panic("this is wrong")
/// ```
///
/// ## Parameters
/// - payload: `Value` (positional)
/// The value (or message) to panic with.
///
/// ## Category
/// foundations
#[func]
pub fn panic(args: &mut Args) -> SourceResult<Value> {
match args.eat::<Value>()? {
Some(v) => bail!(args.span, "panicked with: {}", v.repr()),
None => bail!(args.span, "panicked"),
}
}
/// # Assert /// # Assert
/// Ensure that a condition is fulfilled. /// Ensure that a condition is fulfilled.
/// ///
@ -64,24 +87,26 @@ pub fn repr(args: &mut Args) -> SourceResult<Value> {
/// ///
/// ## Example /// ## Example
/// ```example /// ```example
/// #assert(1 < 2) /// #assert(1 < 2, message: "one is")
/// ``` /// ```
/// ///
/// ## Parameters /// ## Parameters
/// - condition: `bool` (positional, required) /// - condition: `bool` (positional, required)
/// The condition that must be true for the assertion to pass. /// The condition that must be true for the assertion to pass.
/// - message: `EcoString` (named)
/// The error message when the assertion fails.
/// ///
/// ## Category /// ## Category
/// foundations /// foundations
#[func] #[func]
pub fn assert(args: &mut Args) -> SourceResult<Value> { pub fn assert(args: &mut Args) -> SourceResult<Value> {
let Spanned { v, span } = args.expect::<Spanned<bool>>("condition")?; let check = args.expect::<bool>("condition")?;
let message = args.named::<EcoString>("message")?; let message = args.named::<EcoString>("message")?;
if !v { if !check {
if let Some(message) = message { if let Some(message) = message {
bail!(span, "assertion failed: {}", message); bail!(args.span, "assertion failed: {}", message);
} else { } else {
bail!(span, "assertion failed"); bail!(args.span, "assertion failed");
} }
} }
Ok(Value::None) Ok(Value::None)

View File

@ -96,6 +96,7 @@ fn global(math: Module, calc: Module) -> Module {
// Compute. // Compute.
global.def_func::<compute::TypeFunc>("type"); global.def_func::<compute::TypeFunc>("type");
global.def_func::<compute::ReprFunc>("repr"); global.def_func::<compute::ReprFunc>("repr");
global.def_func::<compute::PanicFunc>("panic");
global.def_func::<compute::AssertFunc>("assert"); global.def_func::<compute::AssertFunc>("assert");
global.def_func::<compute::EvalFunc>("eval"); global.def_func::<compute::EvalFunc>("eval");
global.def_func::<compute::IntFunc>("int"); global.def_func::<compute::IntFunc>("int");

View File

@ -10,11 +10,31 @@
#test(repr(ltr), "ltr") #test(repr(ltr), "ltr")
#test(repr((1, 2, false, )), "(1, 2, false)") #test(repr((1, 2, false, )), "(1, 2, false)")
---
// Test panic.
// Error: 7-9 panicked
#panic()
---
// Test panic.
// Error: 7-12 panicked with: 123
#panic(123)
---
// Test panic.
// Error: 7-24 panicked with: "this is wrong"
#panic("this is wrong")
--- ---
// Test failing assertions. // Test failing assertions.
// Error: 9-15 assertion failed // Error: 8-16 assertion failed
#assert(1 == 2) #assert(1 == 2)
---
// Test failing assertions.
// Error: 8-51 assertion failed: two is smaller than one
#assert(2 < 1, message: "two is smaller than one")
--- ---
// Test failing assertions. // Test failing assertions.
// Error: 9-15 expected boolean, found string // Error: 9-15 expected boolean, found string