MontysCoconut / moco

The Monty to LLVM compiler
http://www.informatik.uni-bremen.de/monty/
GNU General Public License v3.0
10 stars 5 forks source link

Tuple unpacking #33

Closed cpfr closed 9 years ago

cpfr commented 9 years ago

This merge-request implements an unpacking syntax for tuples.

In particular a new UnpackAssignment is introduced, which provides some syntactic sugar for assigning tuple values. The syntax is inspired by Python. The unpack assignment is similar to a regular assignment with the only difference that on the left-hand-side multiple expressions or declarations are possible. Consider the following example:

String a := "Hello"
Int x, Int y, a := (5,42, "World")

After the unpack assignment in the second line, the variables have the following values:

println(x)  // 5
println(y)  // 42
println(a)  // World

Also switching the values of variables is possible:

Int x := 1
Int y := 2
x,y := (y,x)

However, there is one point, which makes this commit a little bit more complicated than initially intended. The problem is that a temporary variable has to be introduced in order to avoid repeated evaluation of the right-hand-side expression during unpacking. The type of this variable depends on the rhs expression type, which is resolved after the declaration is created. For this reason, the compiler now has the ability to infer the type of a variable declaration from an expression (of course only if the type of that expression does not depend on the declaration itself, which is not the case for unpack assignments).