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

checkDuplicates return value #1

Open
fosler opened this issue Oct 6, 2021 · 9 comments
Open

checkDuplicates return value #1

fosler opened this issue Oct 6, 2021 · 9 comments

Comments

@fosler
Copy link
Contributor

fosler commented Oct 6, 2021

Currently, the Env classes defined in files envVal and envRef have a void checkDuplicates method that can be used to throw an exception if a list of VAR tokens has any duplicates. This is used by the parser in LetDecls, Formals, Fields, Methods, and Statics constructor :init hooks to catch duplicate identifiers.

I propose to modify this method to return the Set<String> instance constructed in the method body instead of returning nothing (void). This will not break existing code (where the return value is simply ignored), but it can be useful in the OBJ language to check if the sets of Static, Field, and Method identifiers contain any of the 'reserved' identifiers self, super, etc. In the current OBJ version, these identifier sets are checked only for duplicates.

@jashelio
Copy link

jashelio commented Oct 6, 2021 via email

@StoneyJackson
Copy link
Member

I don't see envVal and envRef in ourplcc/course or ourplcc/plcc. I found checkDuplicates in ourplcc/course/code/TYPE1/tenv. It matches your description, but it's as static method of Type. I'll assume this is the method we're discussing. Adding the new return type to its signature I get:

public static Set<String> checkDuplicates(List<String> varList)

Just looking at this, I would assume the returned set should be the duplicates that this method found. But then I think a better name would be getDuplicates. But based on my understanding of what it does -- throws an exception if a duplicate is found -- I think a void return type is more appropriate. It signals that this method will have side effects, and I consider an exception a side effect.

How about adding a void checkReservedNames() that will raise an exception if it finds an identifier with the same name as a reserved word?

@fosler
Copy link
Contributor Author

fosler commented Oct 7, 2021 via email

@jashelio
Copy link

jashelio commented Oct 7, 2021 via email

@StoneyJackson
Copy link
Member

StoneyJackson commented Oct 8, 2021

I will not say "no" either. Just offering suggestions/advice/opinions.

Maybe factor out the conversion of Tokens to Strings and Lists to Sets from checkDuplicates?

public static void runStaticChecks(_Start parseTree) {
    List<Token> tokenOccurrences = parseTree.getTokens();
    List<String> idOccurrences = Helpers.toStrings(tokenOccurrences);
    Set<String> ids = Helpers.toSet(idOccurrences);
    checkDuplicates(ids, idOccurrences);
    checkReserved(ids);
}

Edit: clean up format of code.

@StoneyJackson
Copy link
Member

Is this issue done?

@StoneyJackson
Copy link
Member

StoneyJackson commented Oct 6, 2023

Note to self:

  1. Move Code from ourPLCC to a new repository named Languages.
  2. Transfer this issue to this new repository.

DONE

@StoneyJackson StoneyJackson transferred this issue from ourPLCC/plcc Oct 13, 2023
@StoneyJackson
Copy link
Member

@fosler Return void.

@fosler
Copy link
Contributor Author

fosler commented Oct 14, 2023 via email

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

3 participants