-
Notifications
You must be signed in to change notification settings - Fork 4
/
lexer.go
164 lines (140 loc) · 2.94 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
package lexer
import (
"magpie/token"
"unicode"
)
// Lexer
type Lexer struct {
filename string
input []rune
ch rune //current character
position int //character offset
readPosition int //reading offset
line int
col int
}
func NewLexer(input string) *Lexer {
l := &Lexer{input: []rune(input)}
l.ch = ' '
l.position = 0
l.readPosition = 0
l.line = 1
l.col = 0
l.readNext()
//0xFEFF: BOM(byte order mark), only permitted as very first character
if l.ch == 0xFEFF {
l.readNext() //ignore BOM at file beginning
}
return l
}
func (l *Lexer) readNext() {
if l.readPosition >= len(l.input) {
l.ch = 0
} else {
l.ch = l.input[l.readPosition]
if l.ch == '\n' {
l.col = 0
l.line++
} else {
l.col += 1
}
}
l.position = l.readPosition
l.readPosition++
}
func (l *Lexer) peek() rune {
if l.readPosition >= len(l.input) {
return 0
}
return l.input[l.readPosition]
}
func (l *Lexer) NextToken() token.Token {
var tok token.Token
l.skipWhitespace()
pos := l.getPos()
switch l.ch {
case '+':
tok = newToken(token.TOKEN_PLUS, l.ch)
case '-':
tok = newToken(token.TOKEN_MINUS, l.ch)
case '*':
if l.peek() == '*' {
tok = token.Token{Type: token.TOKEN_POWER, Literal: string(l.ch) + string(l.peek())}
l.readNext()
} else {
tok = newToken(token.TOKEN_MULTIPLY, l.ch)
}
case '/':
tok = newToken(token.TOKEN_DIVIDE, l.ch)
case '%':
tok = newToken(token.TOKEN_MOD, l.ch)
case '(':
tok = newToken(token.TOKEN_LPAREN, l.ch)
case ')':
tok = newToken(token.TOKEN_RPAREN, l.ch)
case '=':
tok = newToken(token.TOKEN_ASSIGN, l.ch)
case ';':
tok = newToken(token.TOKEN_SEMICOLON, l.ch)
case 0:
tok.Literal = "<EOF>"
tok.Type = token.TOKEN_EOF
default:
if isDigit(l.ch) {
tok.Literal = l.readNumber()
tok.Type = token.TOKEN_NUMBER
tok.Pos = pos
return tok
} else if isLetter(l.ch) {
tok.Literal = l.readIdentifier()
tok.Pos = pos
tok.Type = token.LookupIdent(tok.Literal)
return tok
} else {
tok = newToken(token.TOKEN_ILLEGAL, l.ch)
}
}
tok.Pos = pos
l.readNext()
return tok
}
func (l *Lexer) readNumber() string {
var ret []rune
ch := l.ch
ret = append(ret, ch)
l.readNext()
for isDigit(l.ch) || l.ch == '.' {
ret = append(ret, l.ch)
l.readNext()
}
return string(ret)
}
func (l *Lexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) || isDigit(l.ch) {
l.readNext()
}
return string(l.input[position:l.position])
}
func (l *Lexer) skipWhitespace() {
for unicode.IsSpace(l.ch) {
l.readNext()
}
}
func (l *Lexer) getPos() token.Position {
return token.Position{
Filename: l.filename,
Offset: l.position,
Line: l.line,
Col: l.col,
}
}
func newToken(tokenType token.TokenType, ch rune) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
func isDigit(ch rune) bool {
return '0' <= ch && ch <= '9'
}
func isLetter(ch rune) bool {
return unicode.IsLetter(ch) || ch == '_'
}