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

With statement #16

Open
jprochazk opened this issue Feb 25, 2023 · 0 comments
Open

With statement #16

jprochazk opened this issue Feb 25, 2023 · 0 comments

Comments

@jprochazk
Copy link
Owner

with_stmt := 'with' (var ':=')? expr ':' block
import file.open

with f := open("file.txt").append():
  for i in 0..100:
    f.write(str(i))

Semantics

  1. A new scope is opened
  2. An anonymous binding temp is created
  3. The expr is evaluated, and stored in temp
  4. The var binding is created
  5. temp is copied to var
  6. The enter metamethod is called on the value stored in temp
  7. The block is executed
  8. The leave metamethod is called on teh value stored in temp
  9. The scope is closed

A possible desugaring would look like:

temp := <expr>
var := temp
temp.enter()
<block>
temp.leave()
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