← 输入框|github.com|刷新
Skip to content

How to implement GoTo #1144

Closed
Closed

Description

@certik

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.

Activity

  1. czgdp1807 commented on Sep 23, 2022

    @czgdp1807
  2. czgdp1807 commented on Sep 23, 2022

    @czgdp1807
    Collaborator

    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?

  3. certik commented on Sep 23, 2022

    @certik
    ContributorAuthor

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions