-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
96 lines (85 loc) · 1.81 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gmars
import "strings"
type tokenType uint8
const (
tokError tokenType = iota // returned when an error is encountered
tokText // used for labels, symbols, and opcodes
tokNumber // (optionally) signed integer
tokSymbol // address modes and symbols for aritmetic, comparison, ang logic
tokComma
tokColon
tokParenL
tokParenR
tokComment // includes semi-colon, no newline char
tokNewline
tokInvalid // pass invalid Runes through individually
tokEOF
)
type token struct {
typ tokenType
val string
}
func (t token) String() string {
switch t.typ {
case tokEOF:
return "EOF"
case tokNewline:
return "newline"
default:
return t.val
}
}
func (t token) IsOp() bool {
if t.typ != tokText {
return false
}
if strings.Contains(t.val, ".") {
return true
}
_, err := getOpCode(t.val)
if err == nil {
return true
}
return t.IsPseudoOp()
}
func (t token) IsAddressMode() bool {
if t.typ != tokSymbol {
return false
}
if t.val == "$" || t.val == "#" || t.val == "@" || t.val == "*" || t.val == "{" || t.val == "<" || t.val == "}" || t.val == ">" {
return true
}
return false
}
func (t token) NoOperandsOk() bool {
lower := strings.ToLower(t.val)
return lower == "end" || lower == "rof"
}
func (t token) IsPseudoOp() bool {
switch strings.ToLower(t.val) {
case "end":
return true
case "equ":
return true
case "org":
return true
case "for":
return true
case "rof":
return true
default:
return false
}
}
func (t token) IsExpressionTerm() bool {
if t.typ == tokSymbol || t.typ == tokNumber || t.typ == tokText || t.typ == tokParenL || t.typ == tokParenR {
return true
}
if t.typ == tokSymbol {
if t.val == "}" || t.val == "{" || t.val == "#" || t.val == "$" || t.val == "@" {
return false
}
return true
}
return false
}