From 5729ab1d4d72b181793e27f22dd85a6b393cebe4 Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Sat, 9 Nov 2024 11:26:39 +0100 Subject: [PATCH] Added tests and fixed some issues --- src/compiler/prelude.rs | 3 +-- src/prelude.tok | 2 +- src/value/iter/iter.rs | 4 ++-- tests/aggregates.tok | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/aggregates.tok diff --git a/src/compiler/prelude.rs b/src/compiler/prelude.rs index 7630724..dff40aa 100644 --- a/src/compiler/prelude.rs +++ b/src/compiler/prelude.rs @@ -1475,8 +1475,7 @@ impl Compiler { "emit" => "op_accept", "children" => (value!([ - "emit" => "value_integer", - "value" => 0 + "emit" => "value_void" ])) ])) ])) diff --git a/src/prelude.tok b/src/prelude.tok index 4634c5a..13fbe88 100644 --- a/src/prelude.tok +++ b/src/prelude.tok @@ -148,6 +148,6 @@ avg : @value { cnt++ } - if !cnt return 0 + if !cnt return void res / cnt } diff --git a/src/value/iter/iter.rs b/src/value/iter/iter.rs index 51b2302..98d8c8e 100644 --- a/src/value/iter/iter.rs +++ b/src/value/iter/iter.rs @@ -97,7 +97,7 @@ impl Iter { let mut borrowed_iter = iter.borrow_mut(); if let Some(iter) = borrowed_iter.object_mut::() { - Ok(iter.max().unwrap()) + Ok(iter.max().unwrap_or(RefValue::from(Value::Void))) } else { Ok(iter.clone()) } @@ -107,7 +107,7 @@ impl Iter { let mut borrowed_iter = iter.borrow_mut(); if let Some(iter) = borrowed_iter.object_mut::() { - Ok(iter.min().unwrap()) + Ok(iter.min().unwrap_or(RefValue::from(Value::Void))) } else { Ok(iter.clone()) } diff --git a/tests/aggregates.tok b/tests/aggregates.tok new file mode 100644 index 0000000..4751cd4 --- /dev/null +++ b/tests/aggregates.tok @@ -0,0 +1,36 @@ +#testmode:repl +# Aggregate Functions + +l = list(range(10)) + +min(l) +max(l) +sum(l) +avg(l) + +min(42) +max(42) +sum(42) +avg(42) + +print(type(min(void))) +print(type(max(void))) +print(type(sum(void))) +print(type(avg(void))) + + +#--- +#0 +#9 +#45 +#4.5 + +#42 +#42 +#42 +#42 + +#void +#void +#void +#void