ImplLooping
From dis-Emi-A
Looping
Looping is a unification of both traditional loops and of if statements. Refer to Booleans for how this combines the value types of Boolean/Set.
A loop looks like this:
{{{ [ var in condition ] {
body;
}; }}}
AutoTyping Code
The condition must evaluate to a Set type. This will serve as the short-hand for the pseudo-code evaluation:
{{{ iter = iter_begin( condition ); while( true ) {
if( iter_end( iter ) ) break;
var = iter_value( iter ); body( var ); iter = iter_next( iter );
} }}}
The functions in this pseudo-code will actually be called*. Therefore when we say Set type all we really need is this set of functions:
- iter_begin
- returns a starting iterator for the set
- iter_end
- determines if the iterator is in the end position (past final element)
- iter_value
- returns the value indexed by the iterator
- iter_next
- returns a new iterator refering to the next item
The return value of all items is open, except for iter_end, which must return a Boolean type. Which means that in the strictest sense we need the loop to evaluate it again (making an endless loop). So we'll have to assume the compiler has a special understand of a true Boolean type and only needs the above expansion in cases where it isn't Boolean.
- That is for the purpose of auto-typing, the emitter may choose to not call these exact functions, or in this order, should it have a better method (especially for the Boolean type itself). So in respect to the previous paragraph, these functions still need to be defined for Boolean because until auto-typing is finished the Compiler won't know for sure what the type is.
Boolean Iteration
The boolean form of the functions would look like this then:
iter_begin :> :Boolean: b -> b iter_end :> :Boolean: b -> !b iter_value :> :Boolean: b -> b iter_next :> :Boolean: b -> false
Easy enough. :)
