vpisarev / ficus

The programming language Ficus
Apache License 2.0
72 stars 9 forks source link

ExpAssign: special-case immutable l-values #15

Closed andrey-golubev closed 2 years ago

andrey-golubev commented 2 years ago

is_lvalue accepts a mutability flag and (among other things) decides whether to return true or false based on that. This in turn makes certain errors fairly cryptic. Consider:

fun min(X: 't[]): 't {
    val min_val = X[0]
    for x <- X {
        if x < min_val { min_val = x } // error: the left side of an assignment is not an l-value
    }
    min_val
}

min_val looks fairly l-value to me (in C++ terms at least), but it is val which makes it immutable and so we cannot re-assign to it.

andrey-golubev commented 2 years ago

The patch is far from ideal, there should probably be a better way to fix this. And I see at least one other case where !is_lvalue(true, ...) code is used - in ExpBinary check. Should this also be updated somehow?

andrey-golubev commented 2 years ago

Ping @vpisarev. Btw, do you accept contributions at this stage at all?

vpisarev commented 2 years ago

@andrey-golubev, thank you! And sorry for a big delay. For now the pull requests are very rare, so I'm not get used yet to check this tab of the project on a regular basis :) I simply did not notice it. The patch is definitely useful. As for the example, changing val min_val = X[0] to var min_val = X[0] should make it compile and work as expected, or you can use more functional style with fold:

fun min(X: 't[]): 't =
    fold min_val = X[0] for x <- X {min(min_val, x)}