Fix bug with upvalues pointing to the outer scope of the enclosing function

The bug happens when a closure refer to a variable in the outer scope
of the enclosing function. If the variable is declared in an inner block
an assertion was raised.

Now, when the upvalue is first encountered we determine if is a local of
the enclosing function of if it is in an outer scope. In the first case
we just store the register index used for the variable. In the second case
the variable is declared as an upvalue in the enclosing function and its
upvalue index, in the context of the enclosing function, is stored.

This behavior is required by Lua upvalues as described in
"The implementation of Lua 5.0" by R. Ierusalimschy, L. H. de Figueiredo,
W. Celes. The second case is explicitly taken into account and this kind
of closures are called "flat closures".
Fix bug with local variables declaration

The declaration of the variables in the lhs of the assignment should be
done only after the code for rhs is generated.