Skip to content

Instantly share code, notes, and snippets.

@korzio
Created March 16, 2021 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korzio/7375034e74832af294ce90fb6a09c8fc to your computer and use it in GitHub Desktop.
Save korzio/7375034e74832af294ce90fb6a09c8fc to your computer and use it in GitHub Desktop.
// iota enumerator gives an effective way in go language
// to use constants in enum-like constructs
const (
none = iota
plus
mul
)
// enum-like map of operator token keys and iota values defined above
var precendences = map[string]int{
token.PLUS: plus,
token.ASTERISK: mul,
}
// calculate binding power
func bp(tok *token.Token) int {
if precendence, ok := precendences[string(tok.Type)]; ok {
return precendence
}
return none
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment