Skip to content

Add a code generator for enum boilerplate#401

Merged
mcy merged 9 commits intomainfrom
mcy/keywords
Jan 7, 2025
Merged

Add a code generator for enum boilerplate#401
mcy merged 9 commits intomainfrom
mcy/keywords

Conversation

@mcy
Copy link
Member

@mcy mcy commented Dec 18, 2024

Go does not have enums, but they are quite useful, so we emulate them with int-kinded types and use switches and whatnot to convert them to and from strings.

There are enough of these in the new compiler stack at this point that maintaining all of these stringification functions is getting tedious and error-prone, so this PR contains a very simple, 200-line //go:generate helper for generating those functions.

I surveyed other existing packages of this kind, but unfortunately they all want to put varying strong constraints on how we organize and name our "enums", so I felt that we should just write the simplest possible thing to fit our use-case.

@mcy mcy requested a review from jhump December 18, 2024 23:11
Comment on lines 43 to 44
// - //enum:stringfunc Name provides a function to use for stringifying
// based on the name of a constant (see below).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality is used in only one place. I am so-so on whether it pulls its weight. Feel free to veto this.

return false
}

func ParseDirectives(fs *token.FileSet, comments []*ast.CommentGroup) ([]Directive, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seems like serious overkill. How about just define the enums (name, doc, constants) in a yaml file and then parsing the YAML and generating Go code (including for the type definition and constants) is trivial. I don't think it needs to be very configurable -- you could make it always emit some sort of "Lookup" factory function or maybe just make it a simple bool flag in the yaml.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol using a config file hadn't occurred to me.

@mcy mcy requested a review from jhump December 20, 2024 20:41
Copy link
Member

@jhump jhump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Just a couple of small things.

@mcy mcy enabled auto-merge (squash) January 7, 2025 19:15
@mcy mcy disabled auto-merge January 7, 2025 19:18
@mcy mcy enabled auto-merge (squash) January 7, 2025 19:18
@mcy mcy merged commit 1618dd6 into main Jan 7, 2025
7 checks passed
@mcy mcy deleted the mcy/keywords branch January 7, 2025 19:23
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 this pull request may close these issues.

2 participants