diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-06-13 04:31:31 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-06-13 04:31:31 +0300 |
commit | 05626592208f56d88523887e2b80c0514f8ac210 (patch) | |
tree | b0db34890e7366008754e975a8f28e878c5b28bf /token.go |
initial
Diffstat (limited to 'token.go')
-rw-r--r-- | token.go | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/token.go b/token.go new file mode 100644 index 0000000..aa8047e --- /dev/null +++ b/token.go @@ -0,0 +1,80 @@ +package lexpr + +type Token struct { + typ lexType + value string + ivalue int + priority int + leftAssoc bool +} + +func (t Token) Number() (int, bool) { + return t.ivalue, t.typ == number +} + +func (t Token) String() (string, bool) { + return t.value, t.typ == str +} + +func (t Token) Word() (string, bool) { + return t.value, t.typ == word +} + +func TokenFromAny(variable any) (Token, bool) { + if s, ok := variable.(string); ok { + return Token{ + typ: str, + value: s, + }, true + } + if n, ok := variable.(int); ok { + return Token{ + typ: number, + ivalue: n, + }, true + } + if n, ok := variable.(float64); ok { + return Token{ + typ: number, + ivalue: int(n), + }, true + } + if n, ok := variable.(float32); ok { + return Token{ + typ: number, + ivalue: int(n), + }, true + } + if b, ok := variable.(bool); ok { + n := 0 + if b { + n = 1 + } + return Token{ + typ: number, + ivalue: n, + }, true + } + return Token{}, false +} + +func TokenFromWord(wordName string) Token { + return Token{ + typ: word, + value: wordName, + } +} + +func TokenFromString(s string) Token { + return Token{ + typ: str, + value: s, + } +} + +func TokenFromInt(n int) Token { + return Token{ + typ: number, + ivalue: n, + } +} |