-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.go
208 lines (190 loc) · 4.63 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
package main
import (
"unicode"
)
type TokenType string
type Token struct {
name TokenType
value string
}
const (
TkBinaryOperator TokenType = "BinaryOperator"
TkNumber TokenType = "Number"
TkString TokenType = "String"
TkIdentifier TokenType = "Identifier"
TkDeclareVar TokenType = "DeclareVariable"
TkDeclareFunc TokenType = "DeclareFunction"
TkIf TokenType = "If"
TkElse TokenType = "Else"
TkWhile TokenType = "While"
TkReturn TokenType = "Return"
TkBreak TokenType = "Break"
TkComma TokenType = "Comma"
TKColon TokenType = "Colon"
TkDot TokenType = "Dot"
TkNot TokenType = "Not"
TkOpenRound TokenType = "OpenRoundBrace"
TkCloseRound TokenType = "CloseRoundBrace"
TkOpenCurly TokenType = "OpenCurlyBrace"
TkCloseCurly TokenType = "CloseCurlyBrace"
TKOpenSquare TokenType = "OpenSquareBrace"
TkCloseSquare TokenType = "CloseSquareBrace"
)
var Keywords = map[string]TokenType{
"let": TkDeclareVar,
"cho": TkDeclareVar,
"fn": TkDeclareFunc,
"hàm": TkDeclareFunc,
"if": TkIf,
"nếu": TkIf,
"else": TkElse,
"hay": TkElse,
"while": TkWhile,
"khi": TkWhile,
"return": TkReturn,
"trả": TkReturn,
"break": TkBreak,
"nghỉ": TkBreak,
}
func NewToken(name TokenType, value string) Token {
return Token{
name: name,
value: value,
}
}
func isDigit(ch rune) bool {
return '0' <= ch && ch <= '9'
}
func isAlpha(ch rune) bool {
return unicode.IsLetter(rune(ch))
//return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z')
}
func isIgnored(ch rune) bool {
return ch == ' ' || ch == '\n' || ch == '\t'
}
func isOneCharBinaryOperator(ch rune) bool {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=' || ch == '>' || ch == '<'
}
func isTwoCharBinaryOperator(first rune, second rune) bool {
if first == '=' && second == '=' {
return true
}
if first == '!' && second == '=' {
return true
}
if first == '>' && second == '=' {
return true
}
if first == '<' && second == '=' {
return true
}
if first == '&' && second == '&' {
return true
}
if first == '|' && second == '|' {
return true
}
return false
}
func Tokenize(source string) []Token {
var tokens []Token
runeArr := []rune(source)
for i := 0; i < len(runeArr); i++ {
ch := runeArr[i]
if isIgnored(ch) {
continue
}
if i+1 < len(runeArr) && isTwoCharBinaryOperator(ch, runeArr[i+1]) {
tokens = append(tokens, NewToken(TkBinaryOperator, string(ch)+string(runeArr[i+1])))
i++
continue
}
if ch == ';' {
// comments
for i+1 < len(runeArr) && runeArr[i+1] != '\n' {
i++
}
}
if ch == '[' {
tokens = append(tokens, NewToken(TKOpenSquare, string(ch)))
continue
}
if ch == ']' {
tokens = append(tokens, NewToken(TkCloseSquare, string(ch)))
continue
}
if ch == '(' {
tokens = append(tokens, NewToken(TkOpenRound, string(ch)))
continue
}
if ch == ')' {
tokens = append(tokens, NewToken(TkCloseRound, string(ch)))
continue
}
if ch == '{' {
tokens = append(tokens, NewToken(TkOpenCurly, string(ch)))
continue
}
if ch == '}' {
tokens = append(tokens, NewToken(TkCloseCurly, string(ch)))
continue
}
if ch == ',' {
tokens = append(tokens, NewToken(TkComma, string(ch)))
continue
}
if ch == ':' {
tokens = append(tokens, NewToken(TKColon, string(ch)))
continue
}
if ch == '.' {
tokens = append(tokens, NewToken(TkDot, string(ch)))
continue
}
if ch == '"' {
str := ""
for i+1 < len(runeArr) && (runeArr[i+1] != '"' || (runeArr[i] == '\\' && runeArr[i+1] == '"')) {
i++
// skip escape character
if runeArr[i] != '\\' {
str += string(runeArr[i])
}
}
i++ // skip closing quote
tokens = append(tokens, NewToken(TkString, str))
continue
}
if ch == '!' {
tokens = append(tokens, NewToken(TkNot, string(ch)))
continue
}
if isOneCharBinaryOperator(ch) {
tokens = append(tokens, NewToken(TkBinaryOperator, string(ch)))
continue
}
if isDigit(ch) {
numStr := string(ch)
for i+1 < len(runeArr) && isDigit(runeArr[i+1]) {
i++
numStr = numStr + string(runeArr[i])
}
tokens = append(tokens, NewToken(TkNumber, numStr))
continue
}
if isAlpha(ch) {
word := string(ch)
for i+1 < len(runeArr) && (isAlpha(runeArr[i+1]) || isDigit(runeArr[i+1])) {
i++
word = word + string(runeArr[i])
}
keywordType, found := Keywords[word]
if found {
tokens = append(tokens, NewToken(keywordType, word))
} else {
tokens = append(tokens, NewToken(TkIdentifier, word))
}
continue
}
}
return tokens
}