Open brandonLi8 opened 1 year ago
I started working on this and I think I realized that maybe redeclared variables shouldn't be allowed at all. The best way to do it for now could be to just do it the "java" model, where
let a = 1
if true {
let a = 2; // ERROR: redeclare in the same scope (
}
In java, if statements are sort of a psuedo scope where anything it creates doesnt live past the if statement, but it is the same scope in the sense that It cant create a local a
.
However, this will need to be revisited later on when functions are implemented (since they do have their own entire scope)
let a: int = 0
let b: int = 9
function f() {
let a: int = 1; // entirely local a
a + b // 10
}
In this case, the f() will by default inherit the global scope (so b=9, a=0), but allow it to have a local a. We will have to mangle the a within the f() to be different from the global a
Here is my progress for name_mangler:
Consider the following program:
One approach to have this behavior is to remove the "redeclare" check in type checker. In the then_block, the
a
is added toidentifier_types
in the ir. However, im thinking that redceclaring might have problems in the register allocation, as a variable in a if block would share the conflicts of whatever it previously had prematurely. Additionally, without name mangling, botha
s would be assigned to the same location and this the result would not even be correct.