forked from skx/go.vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.go
218 lines (189 loc) · 4.15 KB
/
lexer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Package lexer contains our lexer.
package lexer
import (
"github.com/skx/go.vm/token"
)
// Lexer is used as a lexer for our VM
type Lexer struct {
position int //current character position
readPosition int //next character position
ch rune //current character
characters []rune //rune slice of input string
}
// New a Lexer instance from string input.
func New(input string) *Lexer {
l := &Lexer{characters: []rune(input)}
l.readChar()
return l
}
// read one forward character
func (l *Lexer) readChar() {
if l.readPosition >= len(l.characters) {
l.ch = rune(0)
} else {
l.ch = l.characters[l.readPosition]
}
l.position = l.readPosition
l.readPosition++
}
// NextToken to read next token, skipping the white space.
func (l *Lexer) NextToken() token.Token {
var tok token.Token
l.skipWhitespace()
//
// skip single-line comments
// Unless they are immediately followed by a number, because
// our registers are "#N".
//
if l.ch == rune('#') {
if !isDigit(l.peekChar()) {
l.skipComment()
return (l.NextToken())
}
}
switch l.ch {
case rune(','):
tok = newToken(token.COMMA, l.ch)
case rune('"'):
tok.Type = token.STRING
tok.Literal = l.readString()
case rune(':'):
tok.Type = token.LABEL
tok.Literal = l.readLabel()
case rune(0):
tok.Literal = ""
tok.Type = token.EOF
default:
if isDigit(l.ch) {
return l.readDecimal()
}
tok.Literal = l.readIdentifier()
tok.Type = token.LookupIdentifier(tok.Literal)
return tok
}
l.readChar()
return tok
}
// return new token
func newToken(tokenType token.Type, ch rune) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
// read Identifier
func (l *Lexer) readIdentifier() string {
position := l.position
for isIdentifier(l.ch) {
l.readChar()
}
return string(l.characters[position:l.position])
}
// skip white space
func (l *Lexer) skipWhitespace() {
for isWhitespace(l.ch) {
l.readChar()
}
}
// skip comment (until the end of the line).
func (l *Lexer) skipComment() {
for l.ch != '\n' && l.ch != rune(0) {
l.readChar()
}
l.skipWhitespace()
}
// read number
func (l *Lexer) readNumber() string {
position := l.position
for isHexDigit(l.ch) {
l.readChar()
}
return string(l.characters[position:l.position])
}
// read until white space
func (l *Lexer) readUntilWhitespace() string {
position := l.position
for !isWhitespace(l.ch) && l.ch != rune(0) {
l.readChar()
}
return string(l.characters[position:l.position])
}
// read decimal - this needs love to handle decimal and hex.
func (l *Lexer) readDecimal() token.Token {
integer := l.readNumber()
if isEmpty(l.ch) || isWhitespace(l.ch) || l.ch == rune(',') {
return token.Token{Type: token.INT, Literal: integer}
}
illegalPart := l.readUntilWhitespace()
return token.Token{Type: token.ILLEGAL, Literal: integer + illegalPart}
}
// read string
func (l *Lexer) readString() string {
out := ""
for {
l.readChar()
if l.ch == '"' {
break
}
//
// Handle \n, \r, \t, \", etc.
//
if l.ch == '\\' {
l.readChar()
if l.ch == rune('n') {
l.ch = '\n'
}
if l.ch == rune('r') {
l.ch = '\r'
}
if l.ch == rune('t') {
l.ch = '\t'
}
if l.ch == rune('"') {
l.ch = '"'
}
if l.ch == rune('\\') {
l.ch = '\\'
}
}
out = out + string(l.ch)
}
return out
}
func (l *Lexer) readLabel() string {
return l.readUntilWhitespace()
}
// peek character
func (l *Lexer) peekChar() rune {
if l.readPosition >= len(l.characters) {
return rune(0)
}
return l.characters[l.readPosition]
}
func isIdentifier(ch rune) bool {
return ch != rune(',') && !isWhitespace(ch) && !isEmpty(ch)
}
// is white space
func isWhitespace(ch rune) bool {
return ch == rune(' ') || ch == rune('\t') || ch == rune('\n') || ch == rune('\r')
}
// is empty
func isEmpty(ch rune) bool {
return rune(0) == ch
}
// is Digit
func isDigit(ch rune) bool {
return rune('0') <= ch && ch <= rune('9')
}
func isHexDigit(ch rune) bool {
if isDigit(ch) {
return true
}
if rune('a') <= ch && ch <= rune('f') {
return true
}
if rune('A') <= ch && ch <= rune('F') {
return true
}
if (rune('x') == ch) || (rune('X') == ch) {
return true
}
return false
}