Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: min(), max(), sum() and avg() #145

Merged
merged 5 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ This section contains some useful information for Tokay maintenance.
This repository holds all required source files to provide Tokay with examples.

```
. # Build scripts, Cargo.toml, etc.
. # Cargo.toml, README.md, LICENSE, etc.
├── build # Build scripts to generate parts of Tokay using Tokay itself
├── examples # Example programs
├── macros # Source of the tokay-macros crate required for building
├── src # Tokay's source code
│ ├── compiler # Compiler source
│ ├── value # Object system source
│ └── vm # Virtual machine source
└── tests # Some use-case examples required by the test suite
└── tests # Part of the test suite with plenty of Tokay test cases
```

## Releasing a version
Expand Down
10 changes: 9 additions & 1 deletion src/_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::builtin::Builtin;

/*GENERATE cargo run -- _builtins.tok -- `find . -name "*.rs"` */
pub static BUILTINS: [Builtin; 65] = [
pub static BUILTINS: [Builtin; 67] = [
Builtin {
name: "Float",
func: crate::value::token::tokay_token_float,
Expand Down Expand Up @@ -135,6 +135,14 @@ pub static BUILTINS: [Builtin; 65] = [
name: "iter_map",
func: crate::value::iter::mapiter::MapIter::tokay_method_iter_map,
},
Builtin {
name: "iter_max",
func: crate::value::iter::iter::Iter::tokay_method_iter_max,
},
Builtin {
name: "iter_min",
func: crate::value::iter::iter::Iter::tokay_method_iter_min,
},
Builtin {
name: "iter_next",
func: crate::value::iter::iter::Iter::tokay_method_iter_next,
Expand Down
517 changes: 517 additions & 0 deletions src/compiler/prelude.rs

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions src/prelude.tok
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,59 @@ Number : Float | Int

# Parse any token, which is just no whitespace in default terms.
Token : AsciiPunctuation | Word | Number

# Functions

# Calculates the maximum of a value.
#
# In case the provided value is not an iterator,
# it will be turned into an iterator with just
# its own value.
max : @value {
iter(value).max
}

# Calculates the minimum of a value.
#
# In case the provided value is not an iterator,
# it will be turned into an iterator with just
# its own value.
min : @value {
iter(value).min
}

# Calculates the sum of a value.
#
# In case the provided value is not an iterator,
# it will be turned into an iterator with just
# its own value.
sum : @value {
res = void

for i in value {
res += i
}

res
}

# Calculates the average of a value.
#
# In case the provided value is not an iterator,
# it will be turned into an iterator with just
# its own value.
avg : @value {
res = void
cnt = 0

for i in value {
res += i
cnt++
}

# TODO: Facilities for type checking divisable types must be improved!
if !cnt || (type(res) != "int" && type(res) != "float" && type(res) != "bool" && type(res) != "null")
return void

res / cnt
}
22 changes: 21 additions & 1 deletion src/value/iter/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Iter {

tokay_method!("iter : @value", {
// If parameter is already an iterator, just return it
if value.is("iter") || value.is_void() {
if value.is("iter") {
Ok(value)
}
// Check for an available iter() method on the provided value first
Expand Down Expand Up @@ -93,6 +93,26 @@ impl Iter {
))
});

tokay_method!("iter_max : @iter", {
let mut borrowed_iter = iter.borrow_mut();

if let Some(iter) = borrowed_iter.object_mut::<Iter>() {
Ok(iter.max().unwrap_or(RefValue::from(Value::Void)))
} else {
Ok(iter.clone())
}
});

tokay_method!("iter_min : @iter", {
let mut borrowed_iter = iter.borrow_mut();

if let Some(iter) = borrowed_iter.object_mut::<Iter>() {
Ok(iter.min().unwrap_or(RefValue::from(Value::Void)))
} else {
Ok(iter.clone())
}
});

tokay_method!("iter_rev : @iter", {
{
let mut iter = iter.borrow_mut();
Expand Down
2 changes: 1 addition & 1 deletion src/value/refvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl RefValue {
},

// Int inline fast-lane
(Value::Int(int), _) => match op {
(Value::Int(int), no_float) if !no_float.is("float") => match op {
"iadd" => {
*int += that.to_i64()?;
return Ok(self.clone());
Expand Down
76 changes: 76 additions & 0 deletions tests/aggregates.tok
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#testmode:repl
# Aggregate Functions

# aggregate with list of integers
l = list(range(10))

min(l)
max(l)
sum(l)
avg(l)

# aggregate with list of mixed float/int
l = 1, 23.5, 42, 3.1415

min(l)
max(l)
sum(l)
avg(l)

# aggregate with single value
min(42)
max(42)
sum(42)
avg(42)

# aggregate with void
print(type(min(void)))
print(type(max(void)))
print(type(sum(void)))
print(type(avg(void)))

# special cases

## aggregate str

s = "Hello"
min(s)
max(s)
sum(s)
print(type(avg(s))) # should be void

## aggrgate list of list, or mixed
sum(((1,2),3))
sum((1,(2,3)))
sum(((1,2),(3,4)))

#---

#0
#9
#45
#4.5

#1
#42
#69.6415
#17.410375

#42
#42
#42
#42

#void
#void
#void
#void

#"H"
#"o"
#"Hello"
#void

#(1, 2, 3)
#(1, 2, 3)
#(1, 2, 3, 4)
Loading