sunjay / brain

A high level programming language that compiles into the brainfuck esoteric programming language
MIT License
167 stars 12 forks source link

Self-assignment does not work #71

Open sunjay opened 7 years ago

sunjay commented 7 years ago

Consider the following code:

let x: bool = true;
stdout.println(x);
x = !x;
stdout.println(x);

You would expect it to output:

1
0

However, in reality it outputs:

1
1

This is a bug in the assignment implementation. The current implementation unconditionally zeros the target (lhs) of the assignment. This fails when doing self-assignment.

To fix this is non-trivial because the assignment does not know about its target at all. It treats the rhs expression as a black box and there is no way to inspect the result.

The more fundamental issue with this code is that it assumes that it is decidable whether the target should be zeroed before expression::into_operations is called. The assumption that expression::into_operation makes about the target being zero is just not sound. We should probably add a parameter into that function to indicate whether the target was just initialized or may need to be zeroed. Then expression::into_operations can decide what the proper course of action is.


Another related problem: This works for some reason:

let a: bool = a;

Notice that a can be used before it is properly initialized.