Skip to content

Commit 17affae

Browse files
committed
added: ability to remove dynamic array elements in for loop
1 parent 4ddf557 commit 17affae

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

compiler/src/utils.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ void expand_macro(Context *context, AstCall** pcall, AstFunction* template) {
906906
// This makes a lot of assumptions about how these nodes are being processed,
907907
// and I don't want to start using this with other nodes without considering
908908
// what the ramifications of that is.
909-
assert((*node)->kind == Ast_Kind_Static_If || (*node)->kind == Ast_Kind_File_Contents);
909+
// assert((*node)->kind == Ast_Kind_Static_If || (*node)->kind == Ast_Kind_File_Contents);
910910

911911
Scope *scope = argument_scope;
912912

@@ -919,6 +919,8 @@ void expand_macro(Context *context, AstCall** pcall, AstFunction* template) {
919919
scope = scope_create(context, scope, static_if->token->pos);
920920
scope_include(context, scope, template->poly_scope, static_if->token->pos);
921921
}
922+
} else {
923+
scope = template->poly_scope;
922924
}
923925

924926
add_entities_for_node(&context->entities, NULL, *node, scope, macro->entity->package);

core/container/array.onyx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,53 @@ Array.filter :: macro (arr: &[..] $T, body: Code) {
305305
}
306306

307307

308+
#overload #order 100
309+
__for_expansion :: macro (_s: &[..] $T, $flags: __For_Expansion_Flags, $body: Code) where
310+
!(flags & .BY_POINTER)
311+
{
312+
__s := _s
313+
__data := __s.data
314+
__count := __s.count
315+
316+
__i := 0
317+
while __i < __count {
318+
defer __i += 1
319+
320+
remove_code :: [] {
321+
Array.delete(__s, __i)
322+
__i -= 1
323+
__count -= 1
324+
}
325+
326+
v := __data[__i]
327+
#unquote body(v, __i, remove_code) #skip_scope(1)
328+
}
329+
}
330+
331+
#overload #order 100
332+
__for_expansion :: macro (_s: &[..] $T, $flags: __For_Expansion_Flags, $body: Code) where
333+
(flags & .BY_POINTER)
334+
{
335+
__s := _s
336+
__data := __s.data
337+
__count := __s.count
338+
339+
__i := 0
340+
while __i < __count {
341+
defer __i += 1
342+
343+
remove_code :: [] {
344+
Array.delete(__s, __i)
345+
__i -= 1
346+
__count -= 1
347+
}
348+
349+
v := &__data[__i]
350+
#unquote body(v, __i, remove_code) #skip_scope(1)
351+
}
352+
}
353+
354+
308355
/// Useful structure when talking about dynamic arrays where you don't know of what
309356
/// type they store. For example, when passing a dynamic array as an 'any' argument.
310357
Untyped_Array :: struct {

0 commit comments

Comments
 (0)