-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add "for each" loop and c-style for loop #76
base: main
Are you sure you want to change the base?
Conversation
Initially, my decision was to not add anything that generates hacked blocks, but it seems like the for block is popular demand due to its performance benefits. We will definitely use the |
I also have plans to add the C/JS/Java for(i = 0; i <10; i++) loop |
added "in" to for loop syntax. It might also be renamed "foreach" or "for each" to distinguish it from C-style loops should that be a future feature |
does this compile? |
yes (at least on my machine) |
you can do this with a macro, although it isn't as efficient # Loop through a list or string
# The warnings for the FOR macro probably apply here as well
%define FOREACH(var,lst,substack) \
local _std_loops_foreach_macro_loop_i = 1; \
repeat length lst { \
var = lst[_std_loops_foreach_macro_loop_i]; \
substack; \
_std_loops_foreach_macro_loop_i++; \
}
# Example usage:
# # list rand = python```
# import random
# for _ in range(10):
# print(random.randint(1, 10))
# ```;
#
# FOREACH(local rnd, rand,
# add rnd to rand;
# ) you can also do c-style for loops # For loop for goboscript.
# Has to be used inside a proc or func, as it uses a local variable.
%define FOR(substack_start,cond,substack_rep,substack_inr) \
substack_start; \
local _std_loops_for_macro_loop_cond = cond; \ # These ridiculous names are to avoid name conflicts. (Macro local vars would be so nice. there are a lot of other things that would be nice. For now we have to make do. Goboscript is already really good)
until not(_std_loops_for_macro_loop_cond) { \
substack_inr; \ # Strange bug gets rid of seconds parameter in say block (not really sure how that happens) - but wrapping in a repeat seems to do the trick. That's your job though. Hopefully this gets fixed. Idk what causes it though...
substack_rep; \ # You should note that when using this :\\
_std_loops_for_macro_loop_cond = cond; \ # Goboscript doesn't handle until + functions returing properly (highly doubt this is intended)
}
# Example usage:
# FOR(local i = 0, i <= 10, i++,
# say i;
# wait 0.4; # As said above, say for secs block doesn't work here unless it is in a repeat or something.
# ) |
could you implement #77? |
Added c-style loops, however there are a few issues with it
Syntax:
Output:
|
|
issue 2 fixed, now behavior of for loop should be more consistent with other programming languages by incrementing after the loop runs |
should be fully working as expected now |
The "for each" loop is a hidden block that basically acts as a repeat loop with a counter. It would set a variable to 1 and increment that variable each time the loop ran. This PR adds support for that kind of loop.
Additionally adds support for c-style for loop.
Syntax:
For Each
C-style For