-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.go
384 lines (350 loc) · 9.64 KB
/
lex.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package expronaut
import (
"fmt"
)
type TokenType string
const (
TokenTypeEOF TokenType = "EOF"
TokenTypeIllegal TokenType = "ILLEGAL"
TokenTypeParenLeft TokenType = "PAREN_LEFT"
TokenTypeParenRight TokenType = "PAREN_RIGHT"
TokenTypeInt TokenType = "INT"
TokenTypeFloat TokenType = "FLOAT"
TokenTypeString TokenType = "STRING"
TokenTypeVariable TokenType = "VARIABLE"
TokenTypeFunction TokenType = "FUNCTION"
TokenTypeArray TokenType = "ARRAY"
TokenTypeArrayStart TokenType = "ARRAY_START"
TokenTypeArrayEnd TokenType = "ARRAY_END"
TokenTypeComma TokenType = "COMMA"
TokenTypeAnd TokenType = "AND"
TokenTypeOr TokenType = "OR"
TokenTypePlus TokenType = "PLUS"
TokenTypeMinus TokenType = "MINUS"
TokenTypeMultiply TokenType = "MULTIPLY"
TokenTypeDivide TokenType = "DIVIDE"
TokenTypeDivideInteger TokenType = "DIVIDE_INTEGER"
TokenTypeModulo TokenType = "MODULO"
TokenTypeEqual TokenType = "EQUAL"
TokenTypeNotEqual TokenType = "NOT_EQUAL"
TokenTypeLessThan TokenType = "LESS_THAN"
TokenTypeGreaterThan TokenType = "GREATER_THAN"
TokenTypeLessThanOrEqual TokenType = "LESS_THAN_OR_EQUAL"
TokenTypeGreaterThanOrEqual TokenType = "GREATER_THAN_OR_EQUAL"
TokenTypeBool TokenType = "BOOL"
TokenTypeExponent TokenType = "EXPONENT"
TokenTypeLeftShift TokenType = "LEFT_SHIFT"
TokenTypeRightShift TokenType = "RIGHT_SHIFT"
)
func TokenGoTemplate(tok TokenType) string {
switch tok {
case TokenTypeAnd:
return "and"
case TokenTypeOr:
return "or"
case TokenTypeEqual:
return "eq"
case TokenTypeNotEqual:
return "ne"
case TokenTypeLessThan:
return "lt"
case TokenTypeGreaterThan:
return "gt"
case TokenTypeLessThanOrEqual:
return "le"
case TokenTypeGreaterThanOrEqual:
return "ge"
case TokenTypePlus:
return "add"
case TokenTypeMinus:
return "sub"
case TokenTypeMultiply:
return "mul"
case TokenTypeDivide:
return "div"
case TokenTypeModulo:
return "mod"
default:
return string(tok)
}
}
type Token struct {
Type TokenType // The type of token, indicating its role (e.g., operator, number, parenthesis)
Literal string // The actual text that the token represents (e.g., "123", "+", "(")
}
type Lexer struct {
input string
position int // Current position in input (points to current char)
readPosition int // Current reading position in input (after current char)
ch byte // Current char under examination
errors []error
}
func NewLexer(input string) *Lexer {
l := &Lexer{input: input, errors: []error{}}
l.readChar() // Initialize the first character
return l
}
func (l *Lexer) readChar() {
if l.readPosition >= len(l.input) {
l.ch = 0 // ASCII code for "NUL" character signifies end of input
} else {
l.ch = l.input[l.readPosition]
}
l.position = l.readPosition
l.readPosition += 1
}
func (l *Lexer) NextToken() Token {
var tok Token
l.skipWhitespace()
switch l.ch {
case '(':
tok = newToken(TokenTypeParenLeft, l.ch)
case ')':
tok = newToken(TokenTypeParenRight, l.ch)
case '+':
tok = newToken(TokenTypePlus, l.ch)
case '-':
tok = newToken(TokenTypeMinus, l.ch)
if isDigit(l.peekChar()) {
return l.readNumber(true)
} else {
tok = newToken(TokenTypeMinus, l.ch)
}
case '*':
if l.peekChar() == '*' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeExponent, Literal: literal}
} else {
tok = newToken(TokenTypeMultiply, l.ch)
}
case '^':
tok = newToken(TokenTypeExponent, l.ch)
case '/':
if l.peekChar() == '/' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeDivideInteger, Literal: literal}
} else {
tok = newToken(TokenTypeDivide, l.ch)
}
case '%':
tok = newToken(TokenTypeModulo, l.ch)
case ',':
tok = newToken(TokenTypeComma, l.ch)
case '[':
tok = newToken(TokenTypeArrayStart, l.ch)
case ']':
tok = newToken(TokenTypeArrayEnd, l.ch)
case '<':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeLessThanOrEqual, Literal: literal}
} else if l.peekChar() == '<' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeLeftShift, Literal: literal}
} else {
tok = newToken(TokenTypeLessThan, l.ch)
}
case '>':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeGreaterThanOrEqual, Literal: literal}
} else if l.peekChar() == '>' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeRightShift, Literal: literal}
} else {
tok = newToken(TokenTypeGreaterThan, l.ch)
}
case '=':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeEqual, Literal: literal}
} else {
tok = newToken(TokenTypeIllegal, l.ch)
}
case '!':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeNotEqual, Literal: literal}
} else {
tok = newToken(TokenTypeIllegal, l.ch)
}
case '"', '\'':
tok.Literal = l.readString()
tok.Type = TokenTypeString
case '`':
tok.Literal = l.readUntilBacktick()
case '&':
if l.peekChar() == '&' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeAnd, Literal: literal}
} else {
tok = newToken(TokenTypeIllegal, l.ch)
}
case '|':
if l.peekChar() == '|' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = Token{Type: TokenTypeOr, Literal: literal}
} else {
tok = newToken(TokenTypeIllegal, l.ch)
}
case 0:
tok.Type = TokenTypeEOF
tok.Literal = ""
default:
if isLetter(l.ch) {
identifier, iType := l.readIdentifier()
switch iType {
case identifierTypeFunction:
tok.Type = TokenTypeFunction
tok.Literal = identifier
// The '(' character will be processed in the next iteration of NextToken
return tok
case identifierTypeArray:
tok.Type = TokenTypeArray
tok.Literal = identifier
// The '[' character will be processed in the next iteration of NextToken
return tok
case identifierTypeVariable:
tok.Type = TokenTypeVariable
tok.Literal = identifier
return tok
case identifierTypeBool:
tok.Type = TokenTypeBool
tok.Literal = identifier
default:
fmt.Println("Unknown identifier type")
}
} else if isDigit(l.ch) {
return l.readNumber(false)
} else {
tok = newToken(TokenTypeIllegal, l.ch)
}
}
l.readChar()
return tok
}
// newToken is a helper function to create a new Token.
func newToken(tokenType TokenType, ch byte) Token {
return Token{Type: tokenType, Literal: string(ch)}
}
// peekChar looks at the next character without moving the current position.
func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
} else {
return l.input[l.readPosition]
}
}
func (l *Lexer) peek2Char() byte {
if l.readPosition+1 >= len(l.input) {
return 0
} else {
return l.input[l.readPosition+1]
}
}
func (l *Lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
}
}
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}
func isLetter(ch byte) bool {
return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '_'
}
func (l *Lexer) readNumber(allowNegative bool) Token {
position := l.position
hasDecimal := false
hasExponent := false
if allowNegative && l.ch == '-' {
// Consume the negative sign
l.readChar()
}
for isDigit(l.ch) || (!hasDecimal && l.ch == '.') || (!hasExponent && (l.ch == 'e' || l.ch == 'E')) || (hasExponent && (l.ch == '+' || l.ch == '-')) {
if l.ch == '.' {
hasDecimal = true
}
if l.ch == 'e' || l.ch == 'E' {
hasExponent = true
if l.peekChar() == '+' || l.peekChar() == '-' {
// Consume 'e' or 'E'
l.readChar()
}
}
l.readChar()
}
literal := l.input[position:l.position]
if hasDecimal || hasExponent {
return Token{Type: TokenTypeFloat, Literal: literal}
}
return Token{Type: TokenTypeInt, Literal: literal}
}
type identifierType string
const (
identifierTypeVariable identifierType = "variable"
identifierTypeFunction identifierType = "function"
identifierTypeArray identifierType = "array"
identifierTypeBool identifierType = "bool"
)
func (l *Lexer) readIdentifier() (string, identifierType) {
startPosition := l.position
returnType := identifierTypeVariable
for isLetter(l.peekChar()) || isDigit(l.peekChar()) || l.peekChar() == '_' || l.peekChar() == '.' {
l.readChar()
}
if l.peekChar() == '[' {
returnType = identifierTypeArray
} else if l.peekChar() == '(' {
returnType = identifierTypeFunction
}
l.readChar()
ident := l.input[startPosition:l.position]
if ident == "true" || ident == "false" {
return ident, identifierTypeBool
}
return ident, returnType
}
// Helper function to read string literals surrounded by double quotes
func (l *Lexer) readString() string {
position := l.position + 1 // Start after the initial double quote
for {
l.readChar()
if l.ch == '"' || l.ch == 0 {
break
}
}
return l.input[position:l.position]
}
func (l *Lexer) readUntilBacktick() string {
position := l.position + 1 // Start after the initial backtick
for {
if l.peekChar() == '`' {
l.readChar()
break
}
l.readChar()
}
l.readChar()
return l.input[position:l.position]
}