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

Codegen readability improvements #65

Open
johnfn opened this issue Dec 20, 2021 · 0 comments
Open

Codegen readability improvements #65

johnfn opened this issue Dec 20, 2021 · 0 comments

Comments

@johnfn
Copy link
Owner

johnfn commented Dec 20, 2021

The discussion in #59 got me thinking a lot about improving the readability of our codegen.

null comparisons

One codegen that is extremely non-readable is this:

    let x = 1
    print(x != null)

This codegens to the following:

  var x: int = 1
  
  print(((typeof(x) != typeof(null)) or ((typeof(x) == typeof(null)) and (x != null))))

Ouch! We could just codegen

   print(x != null)

⚠️⚠️⚠️ NOTE ⚠️⚠️⚠️

The reason we cant do this all the time because print(self != 5) is a RUNTIME ERROR: you can't compare Node2D to 5. This is why I codegen such disastrous code. However, we can compare null to anything without doing a typeof check, so we could special case that for readability.

Similar type comparisons that aren't unions

Another simple comparison that codegens terribly is this:

    let x = 5
    print(x == 5)

This codegens to:

  var x: int = 5
  
  print(((typeof(x) == typeof(5)) and (x == 5)))

Again, ouch.

The reason we codegen like this is because == has the same issue and will throw a runtime error if we compare two values of the wrong type. I currently just always emit the typeof() checks because it produces correct code 100% of the time. However, if we check the types on both sides of the equals sign, and we see they:

  • Are the same
  • They aren't union types
  • Or they are union types, but the union is only with null/undefined
    Then we could emit the simpler case.

I think this would go a long way towards improving readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant