aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-18 22:28:04 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-18 22:28:04 -0400
commit7472837ee5a00bd9313e82f71f55b6f76ee7083b (patch)
tree57b9b9f5823f2b58fbcf6e25901bc307e0df27fa /compile.c
parent19c51194f0b2ad945bfb7b544032210274b431dd (diff)
Add array:first(predicate:func(x:&T)->Bool)->@%T?
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index 17d19f30..748f5541 100644
--- a/compile.c
+++ b/compile.c
@@ -2263,6 +2263,13 @@ CORD compile(env_t *env, ast_t *ast)
arg_t *arg_spec = new(arg_t, .name="item", .type=item_t);
return CORD_all("Array$find_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args),
", ", compile_type_info(env, self_value_t), ")");
+ } else if (streq(call->name, "first")) {
+ CORD self = compile_to_pointer_depth(env, call->self, 0, false);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *predicate_type = Type(
+ ClosureType, .fn=Type(FunctionType, .args=new(arg_t, .name="item", .type=item_ptr), .ret=Type(BoolType)));
+ arg_t *arg_spec = new(arg_t, .name="predicate", .type=predicate_type);
+ return CORD_all("Array$first(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ")");
} else if (streq(call->name, "from")) {
CORD self = compile_to_pointer_depth(env, call->self, 0, false);
arg_t *arg_spec = new(arg_t, .name="first", .type=INT_TYPE);