-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
47 lines (39 loc) · 899 Bytes
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"strconv"
)
type MachineWord int16
type TokenType string
const (
InvalidToken TokenType = ""
Keyword TokenType = "keyword"
SymbolTokenType TokenType = "symbol"
IntegerConstant TokenType = "integerConstant"
StringConstant TokenType = "stringConstant"
Identifier TokenType = "identifier"
)
type Token struct {
tokenType TokenType
terminal string
}
func IsTokenType(t Token, tt TokenType) bool {
return t.tokenType == tt
}
func IsTerminal(t Token, terminals ...string) bool {
for _, terminal := range terminals {
if t.terminal == terminal {
return true
}
}
return false
}
func (t *Token) asInt() MachineWord {
word, err := strconv.Atoi(t.terminal)
// < 0 as - is an operator
if err != nil || word > 32767 || word < 0 {
fmt.Printf("Cannot parse %q as 16 bit int!", t)
return MachineWord(0)
}
return MachineWord(word)
}