aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-04-02 23:28:59 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-04-02 23:28:59 -0400
commitfae2b2caa0ec311821a7a7ef1f98500cfd25bb9e (patch)
tree9dcc467733b19705b51e7fe7b07a98800f1efe6d /compile.c
parent95100469b6c7f301bb14bcda5dbc16b93c9ce0dc (diff)
Add array:sample()
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index ff6a868d..08e2fe48 100644
--- a/compile.c
+++ b/compile.c
@@ -1481,6 +1481,12 @@ CORD compile(env_t *env, ast_t *ast)
CORD self = compile_to_pointer_depth(env, call->self, 0, false);
(void)compile_arguments(env, ast, NULL, call->args);
return CORD_all("Array$random(", self, ")");
+ } else if (streq(call->name, "sample")) {
+ CORD self = compile_to_pointer_depth(env, call->self, 0, false);
+ arg_t *arg_spec = new(arg_t, .name="count", .type=Type(IntType, .bits=64),
+ .next=new(arg_t, .name="weights", .type=self_value_t, .default_val=FakeAST(Array)));
+ return CORD_all("Array$sample(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
+ compile_type_info(env, self_value_t), ")");
} else if (streq(call->name, "shuffle")) {
CORD self = compile_to_pointer_depth(env, call->self, 1, false);
(void)compile_arguments(env, ast, NULL, call->args);