Merge: transform loops
authorJean Privat <jean@pryen.org>
Wed, 15 Oct 2014 20:15:47 +0000 (16:15 -0400)
committerJean Privat <jean@pryen.org>
Wed, 15 Oct 2014 20:15:47 +0000 (16:15 -0400)
commit380d22070188dd1a255dbc3e33a0f2881242aab2
tree3782fd9cdda3d3f163bdb48243578f734b81694d
parentc446ed48d490f8d8bb69e07d552e2b8117fa72e3
parent829dd5323db2771facc865fe753cb34b0a1819a4
Merge: transform loops

In order to activate more optimizations and simplify code generators and analysis, the `while` and `for` loops must be transformed into simpler forms (in fact in complex forms but with simpler elements)

However, to do such a transformation, the escaping mechanism (break/continue/goto) must be rewrote.
Previously, for each loop control structure (loop for, while), a single EscapeMark was generated and breaks and continues are associated to the same mark, even if they branch in distinct points (so each escapemark had 2 branching mechanisms)
Now, with the first commits, two escapemarks are generated for each loop control structure, and breaks and continues are associated to a different one. The advantage is that each mark only have a single branching mechanism and that once associated to their mark, there is no need to distinguish break and continue (both become simple gotos).

The transformations of loops can be then implemented.

The `while`

~~~
while cond do block
~~~

is transformed into

~~~
loop if cond then block else break
~~~

and `for`

~~~
for i in col do block
~~~

is transformed into

~~~
var it = col.iterator
loop
   if it.is_ok then
      var i = it.item
      do block
      # ^ `continue` in the block it attached to the `do`
      # this is why the transformation of escape-marks where needed in fact.
      # and break is associated to the main loop
      it.next
   else
      break
   end
end
it.finish
~~~

Note that these transformations are basically what is currently implemented in rta, niti and nitg. Except that, now with a single transformation point, those three workers does not need to do their own transformation and basically can just rely on the one done on the AST level.

Pull-Request: #818
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
src/astbuilder.nit
src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/semantize/typing.nit