moroso / compiler

The compiler for the Moroso project.
4 stars 3 forks source link

Support else statements for for loops #27

Open mrwright opened 10 years ago

gwillen commented 10 years ago

Under what circumstances does the 'else' execute? I always found the concept of an 'else' on a loop to be unclear. Is it if we break, or if the condition becomes false (i.e. we don't break), or if the loop body doesn't execute at all?

mrwright commented 10 years ago

I was thinking that they would act as they do in Python; see here. The summary:

it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

This is useful when searching for something with a loop; if you find it you break; if you fail, you run the else statement.

gwillen commented 10 years ago

Okay, I do see why that's useful. Is there a more clever name for it than 'else'?

jwise commented 6 years ago

Yes. Clearly, it should be:

for (INIT-EXPRESSION; COND-EXPRESSION; LOOP-EXPRESSION)
  STATEMENT
rof
  STATEMENT
mrwright commented 4 years ago

On IRC there was a suggestion of separate keywords for the two cases--perhaps broken and completed. (And it seems fine to allow either, none, or both, in any order, for a particular for loop.)

I think this would also allow for loops to be used as expressions (though any for loop of non-unit type would need to specify both extra clauses).