For as builtin and new example that plays primes

This commit is contained in:
Robert Bendun 2022-08-29 21:38:15 +02:00
parent 1fa2ec6b76
commit 7b5e43293a
4 changed files with 31 additions and 16 deletions

View File

@ -1,3 +0,0 @@
say (1 + 3);
say (3 * 10);
say 42

View File

@ -1,11 +0,0 @@
var numbers = [1;2;3;4;5];
var for = [array iteration |
var iter = [i | if (i < (len array)) [
iteration (numbers.i);
iter (i+1)
]];
iter 0
];
for numbers say;

View File

@ -0,0 +1,16 @@
------------------------------------------------------------
Inspired by Marc Evanstein video "The Rythm of The Primes"
Link: https://www.youtube.com/watch?v=8x374slJGuo
------------------------------------------------------------
var Length = 20;
var cmajor = [c;d;e;f;g];
var scale = reverse cmajor;
var primes = nprimes (len scale);
var indicies = up (len scale);
oct 3;
len (1/16);
for (2 + up Length) [ i | play (chord scale.(indicies.(i % primes == 0))); ];

View File

@ -349,7 +349,7 @@ static Result<Value> builtin_primes(Interpreter&, std::vector<Value> args)
return Error { return Error {
.details = errors::Unsupported_Types_For { .details = errors::Unsupported_Types_For {
.type = errors::Unsupported_Types_For::Function, .type = errors::Unsupported_Types_For::Function,
.name = "primes", .name = "nprimes",
.possibilities = { .possibilities = {
"(number) -> array of number" "(number) -> array of number"
} }
@ -357,6 +357,18 @@ static Result<Value> builtin_primes(Interpreter&, std::vector<Value> args)
}; };
} }
static Result<Value> builtin_for(Interpreter &i, std::vector<Value> args)
{
if (is_indexable(args[0].type)) {
for (size_t n = 0; n < args[0].size(); ++n) {
Try(args[1](i, { Try(args[0].index(i, n)) }));
}
return Value{};
}
unimplemented();
}
void Interpreter::register_builtin_functions() void Interpreter::register_builtin_functions()
{ {
auto &global = *Env::global; auto &global = *Env::global;
@ -609,5 +621,6 @@ error:
global.force_define("up", builtin_range<Range_Direction::Up>); global.force_define("up", builtin_range<Range_Direction::Up>);
global.force_define("down", builtin_range<Range_Direction::Down>); global.force_define("down", builtin_range<Range_Direction::Down>);
global.force_define("primes", builtin_primes); global.force_define("nprimes", builtin_primes);
global.force_define("for", builtin_for);
} }