Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when defining variables in different scopes #7

Open
pmenon opened this issue Apr 4, 2019 · 2 comments
Open

Error when defining variables in different scopes #7

pmenon opened this issue Apr 4, 2019 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@pmenon
Copy link
Owner

pmenon commented Apr 4, 2019

TPL allows variables with the same name to be declared in different scopes. However, during bytecode generation, variables are resolved by performing a linear start-to-end lookup in the function's frame. This may not always be correct.

Example TPL:

fun main() -> int {
  var a = 2
  if (a < 10) {
    var b = 8
    if (a < b) {
      var b = 5
      if (a < b) {
        var b = 1
        if (a < b) {
          return 100
        }
      }
    }
  }
  return 2
}

One solution is to automatically uniq-ify all variable names internally, but we'll need to add extra metadata to the AST nodes. A simpler solution is to perform the linear search from inner-most scope to outer-most scope (as is done during semantic checks).

@pmenon pmenon added bug Something isn't working good first issue Good for newcomers labels Apr 4, 2019
@lmwnshn lmwnshn self-assigned this Apr 7, 2019
@lmwnshn
Copy link
Contributor

lmwnshn commented Apr 7, 2019

Reminds me strongly of 312. We were taught that alpha-conversion is the one true way to do this, but I'll go look at what real compilers are doing for a bit.

@lmwnshn
Copy link
Contributor

lmwnshn commented Apr 8, 2019

I think this rightfully would call for something like de Bruijn indexes or Haskell's rapier, e.g. as described on [1]. But I can start by doing the dumb thing. I'm trying to figure out if there's a reason other people aren't doing the dumb thing.

[1] https://www.schoolofhaskell.com/user/edwardk/bound

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants