-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokeniser.go
242 lines (208 loc) · 6.81 KB
/
tokeniser.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
package main
import (
"fmt"
"unicode"
opt "github.com/moltenwolfcub/moltenCompiler/optional"
)
type TokenType int
const (
exit TokenType = iota
intLiteral
semiColon
openRoundBracket
closeRoundBracket
identifier
_var
equals
plus
asterisk
minus
fslash
openCurlyBracket
closeCurlyBracket
_if
while
_else
_break
_continue
_func
comma
_return
)
func (t TokenType) GetBinPrec() opt.Optional[int] {
switch t {
case plus, minus:
return opt.ToOptional(0)
case asterisk, fslash:
return opt.ToOptional(1)
default:
return opt.Optional[int]{}
}
}
type Token struct {
tokenType TokenType
value opt.Optional[string]
lineInfo LineInfo
}
type Tokeniser struct {
program string
currentIndex int
currentLineInfo LineInfo
}
func NewTokeniser(program string, fileName string) Tokeniser {
return Tokeniser{
program: program,
currentLineInfo: NewLineInfo(fileName),
}
}
func (t *Tokeniser) Tokenise() ([]Token, error) {
tokens := []Token{}
buf := []rune{}
for t.peek().HasValue() {
if t.peek().MustGetValue() == '\n' {
t.consume()
t.currentLineInfo.NextLine()
} else if unicode.IsSpace(t.peek().MustGetValue()) {
t.consume()
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == ';' {
t.consume()
tokens = append(tokens, Token{tokenType: semiColon, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '(' {
t.consume()
tokens = append(tokens, Token{tokenType: openRoundBracket, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == ')' {
t.consume()
tokens = append(tokens, Token{tokenType: closeRoundBracket, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '{' {
t.consume()
tokens = append(tokens, Token{tokenType: openCurlyBracket, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '}' {
t.consume()
tokens = append(tokens, Token{tokenType: closeCurlyBracket, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '=' {
t.consume()
tokens = append(tokens, Token{tokenType: equals, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '+' {
t.consume()
tokens = append(tokens, Token{tokenType: plus, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '*' {
t.consume()
tokens = append(tokens, Token{tokenType: asterisk, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '-' {
t.consume()
tokens = append(tokens, Token{tokenType: minus, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == ',' {
t.consume()
tokens = append(tokens, Token{tokenType: comma, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
} else if t.peek().MustGetValue() == '/' {
t.consume()
if t.peek().HasValue() && t.peek().MustGetValue() == '/' {
t.consume()
t.currentLineInfo.IncColumn()
for t.peek().HasValue() && t.peek().MustGetValue() != '\n' {
t.consume()
t.currentLineInfo.IncColumn()
}
} else if t.peek().HasValue() && t.peek().MustGetValue() == '*' {
t.consume()
t.currentLineInfo.IncColumn()
for {
if !t.peek().HasValue() {
return nil, t.currentLineInfo.PositionedError("multiline comment wasn't closed. terminate it with `*/`")
}
c := t.consume()
t.currentLineInfo.IncColumn()
if c == '*' && t.peek().HasValue() && t.peek().MustGetValue() == '/' {
t.consume()
t.currentLineInfo.IncColumn()
break
} else if c == '\n' {
t.currentLineInfo.NextLine()
}
}
} else {
tokens = append(tokens, Token{tokenType: fslash, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncColumn()
}
} else if unicode.IsLetter(t.peek().MustGetValue()) || t.peek().MustGetValue() == '$' || t.peek().MustGetValue() == '_' {
buf = append(buf, t.consume())
for t.peek().HasValue() && (unicode.IsLetter(t.peek().MustGetValue()) || unicode.IsDigit(t.peek().MustGetValue()) || t.peek().MustGetValue() == '$' || t.peek().MustGetValue() == '_') {
buf = append(buf, t.consume())
}
if string(buf) == "exit" {
tokens = append(tokens, Token{tokenType: exit, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "var" {
tokens = append(tokens, Token{tokenType: _var, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "if" {
tokens = append(tokens, Token{tokenType: _if, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "while" {
tokens = append(tokens, Token{tokenType: while, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "else" {
tokens = append(tokens, Token{tokenType: _else, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "break" {
tokens = append(tokens, Token{tokenType: _break, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "continue" {
tokens = append(tokens, Token{tokenType: _continue, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "func" {
tokens = append(tokens, Token{tokenType: _func, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else if string(buf) == "return" {
tokens = append(tokens, Token{tokenType: _return, lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else {
tokens = append(tokens, Token{tokenType: identifier, value: opt.ToOptional(string(buf)), lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
}
} else if unicode.IsDigit(t.peek().MustGetValue()) {
buf = append(buf, t.consume())
for t.peek().HasValue() && unicode.IsDigit(t.peek().MustGetValue()) {
buf = append(buf, t.consume())
}
tokens = append(tokens, Token{tokenType: intLiteral, value: opt.ToOptional(string(buf)), lineInfo: t.currentLineInfo})
t.currentLineInfo.IncWord(buf)
buf = []rune{}
} else {
return nil, t.currentLineInfo.PositionedError(fmt.Sprintf("invalid token: %c", t.peek().MustGetValue()))
}
}
return tokens, nil
}
func (t Tokeniser) peek() opt.Optional[rune] {
if t.currentIndex >= len(t.program) {
return opt.NewOptional[rune]()
}
return opt.ToOptional(rune(t.program[t.currentIndex]))
}
func (t *Tokeniser) consume() rune {
r := rune(t.program[t.currentIndex])
t.currentIndex++
return r
}