Skip to content

How to implement GoTo #1144

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

Closed
certik opened this issue Sep 23, 2022 · 3 comments · Fixed by #1838
Closed

How to implement GoTo #1144

certik opened this issue Sep 23, 2022 · 3 comments · Fixed by #1838
Assignees

Comments

@certik
Copy link
Contributor

certik commented Sep 23, 2022

The AST to ASR has to do something like the following.

  • Maintain a hashtable/mapping of labels (strings) -> (unique IDs, bool declared); initialize it as empty when entering a new function
  • Every time you encounger a "goto label", lookup the label in the mapping, two cases can happen:
    • if it is already there, use the ID from the mapping and create GoTo(ID)
    • if it is not there, add it to the mapping, and note that it has not been declared yet, and assign a unique ID to it; then use that unique ID in GoTo(ID)
  • Every time you encounter "label label", lookup the label in the mapping, two cases can happen:
    • if it is there, change declared to yes, use the ID in GoToTarget(ID)
    • if it is not there, create a new label in the mapping, assign unique ID and declared as yes, then use that ID in GoToTarget(ID)
  • at the end of the function, go over this mapping and check that each label has been declared. If there is one or more labels that has not been declared, raise a compiler error. This corresponds to the case of using a "goto label" without having the corresponding "label label" statement.
@czgdp1807

This comment was marked as resolved.

@czgdp1807
Copy link
Collaborator

czgdp1807 commented Sep 23, 2022

Maintain a hashtable/mapping of labels (strings) -> (unique IDs, bool declared); initialize it as empty when entering a new function

Will we allow something like,

@with_goto
def f():
   # body with goto and label
   @with_goto
   def g():
       # body with goto and label

If we are going to allow the above case, then will we generate hash maps for f and g separately? Will goto in f be allowed to jump to label defined inside g?

@certik
Copy link
Contributor Author

certik commented Sep 23, 2022

If we are going to allow the above case, then will we generate hash maps for f and g separately?

I think so. We can handle this case later.

Will goto in f be allowed to jump to label defined inside g?

I don't think so.

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

Successfully merging a pull request may close this issue.

3 participants