-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Experiment: For-loops as macro expansions #182
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The main problem occuring at the moment is that the body of a for-loop expansion has already been through symbol resolution, so it's symbols are already completely known. When the macro is expanded however, the cloned symbols are reinserted and cause duplicate symbol definitions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This experimental pull-request make a large change to how
for
loops work in the language.Previously,
for
loops had many special cases baked into the compiler so that they could support a variety of different iterable types, includingi32
,i64
,range
,range64
,[N] T
,[] T
,[..] T
, andIterator(T)
. While this worked, and provided a way to have custom, user-defined iterators, it was limited to only have one iterable value at a time, and bloated the compiler by about 1000 lines to handle all the special code generation cases.In this pull-request,
for
loops are changed to simply be a function call to__for_expansion
, a special builtin overloaded function. This function has overloads for all the previously supported type, as well other types likeMap(K, V)
andSet(T)
. Afor
loop is converted to the function call in the following way.As you can see, the iterator of the loop is placed as the first argument unmodified. The body of the for-loop is converted into a code block that takes binding parameters with the specified names and given as the last argument. The second argument is a set of flags -- bit 1 is set if the for loop was by pointer (
for &
), and bit 2 is set if the for loop was specified with#no_close
.The overloads for
__for_expansion
all take on about the same shape. A while loop with an#unquote
directive inside of it, with a special specifier called#skip_scope(N)
(also introduced in this pull-request) that tell the unquoted block to start looking for symbolsN
scopes up, effectively hiding the symbols defined in the macro. Here is the definition of__for_expansion
for[] T
by value. Notice the compile-time check that the first bit offlags
is not set, ensuring this is not by pointer. This overload is also written in a way that provides 2 values to the code block, allowing for using an iterating index at the same time.