Created
March 16, 2021 19:36
-
-
Save korzio/7375034e74832af294ce90fb6a09c8fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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