-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.go
232 lines (200 loc) · 4.75 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import "unicode"
type TokenType int
type Token struct {
text string
kind int
}
var tokens = map[string]int{
"EOF": -1,
"NEWLINE": 0,
"NUMBER": 1,
"IDENT": 2,
"STRING": 3,
// Keyw||ds.
"YE": 101,
"JAO": 102,
"DEKHAO": 103,
"BTAO": 104,
"NAM": 105,
"AGR": 106,
"PHR": 107,
"AGRBND": 108,
"JAB": 109,
"KARO": 110,
"JABBND": 111,
// Operat||s.
"EQ": 201,
"PLUS": 202,
"MINUS": 203,
"ASTERISK": 204,
"SLASH": 205,
"EQEQ": 206,
"NOTEQ": 207,
"LT": 208,
"LTEQ": 209,
"GT": 210,
"GTEQ": 211,
}
func (token Token) pprint(s string) {
for key, value := range tokens {
if token.kind == value {
println(s, ">", key, ":", value, "value : ", token.text)
return
}
}
}
func isDigit(c string) bool {
return unicode.IsDigit([]rune(c)[0])
}
func isAlpha(c string) bool {
return unicode.IsLetter([]rune(c)[0])
}
func isAlNum(c string) bool {
return isAlpha(c) || isDigit(c)
}
// Return the lookahead character.
func (s Source) peek() string {
if s.curPos+1 >= len(s.source) {
return "\000" // EOF
}
return string(s.source[s.curPos+1])
}
// Skip whitespace except newlines, which we will use to indicate the end of a statement.
func (s *Source) skipWhitespace() {
for s.curChar == " " || s.curChar == "\t" || s.curChar == "\r" || s.curChar == "" {
s.nextChar()
}
}
// Skip comments in the code.
func (s *Source) skipComment() {
if s.curChar == "#" {
for s.curChar != "\n" {
s.nextChar()
}
}
}
func isKeyWord(tokenText string) int {
for key, value := range tokens {
if key == tokenText && value >= 100 && value < 200 {
return value
}
}
return 69
}
// Return the next token.
func (s *Source) getToken() Token {
var token Token
// Check the first character of this token to see if we can decide what it is.
// If it is a multiple character operat|| (e.g., !=), number, identifier, || keyw||d then we will process the rest.
// s.skipWhitespace()
//check cur is newline
s.skipWhitespace()
s.skipComment()
if s.curChar == "+" {
token = Token{s.curChar, tokens["PLUS"]}
} else if s.curChar == "-" {
token = Token{s.curChar, tokens["MINUS"]}
} else if s.curChar == "*" {
token = Token{s.curChar, tokens["ASTERISK"]}
} else if s.curChar == "/" {
token = Token{s.curChar, tokens["SLASH"]}
} else if s.curChar == string('\n') {
token = Token{s.curChar, tokens["NEWLINE"]}
} else if s.curChar == "\000" {
token = Token{s.curChar, tokens["EOF"]}
} else if s.curChar == "=" {
if s.peek() == "=" {
s.nextChar()
token = Token{"==", tokens["EQEQ"]}
} else {
token = Token{s.curChar, tokens["EQ"]}
}
} else if s.curChar == "!" {
if s.peek() == "=" {
s.nextChar()
token = Token{"!=", tokens["NOTEQ"]}
} else {
panic("Expected != got ! " + s.peek())
}
} else if s.curChar == "<" {
if s.peek() == "=" {
s.nextChar()
token = Token{"<=", tokens["LTEQ"]}
} else {
token = Token{s.curChar, tokens["LT"]}
}
} else if s.curChar == ">" {
if s.peek() == "=" {
s.nextChar()
token = Token{">=", tokens["GTEQ"]}
} else {
token = Token{s.curChar, tokens["GT"]}
}
} else if s.curChar == "\"" {
// get the string between the quotes
s.nextChar()
start := s.curPos
for s.curChar != "\"" {
//dont allow special characters in the string
//because of c"s printf
if s.curChar == "\r" || s.curChar == "\n" || s.curChar == "\t" || s.curChar == "\\" || s.curChar == "%" {
panic("Lexing Err: Illegal character in string")
}
s.nextChar()
}
token = Token{s.source[start:s.curPos], tokens["STRING"]}
} else if isDigit(s.curChar) {
//check digits
start := s.curPos
for isDigit(s.peek()) {
s.nextChar()
}
if s.peek() == "." {
s.nextChar()
if !isDigit(s.peek()) {
panic("Lexing Err: Expected digit after decimal")
}
for isDigit(s.peek()) {
s.nextChar()
}
}
token = Token{s.source[start : s.curPos+1], tokens["NUMBER"]}
} else if isAlpha(s.curChar) {
start := s.curPos
for isAlNum(s.peek()) {
s.nextChar()
}
tokenText := s.source[start : s.curPos+1]
println(tokenText, "token text")
keyword := isKeyWord(tokenText)
if keyword == 69 {
token = Token{tokenText, tokens["IDENT"]}
} else {
token = Token{tokenText, keyword}
}
} else {
panic("Lexing Err|| unknown token")
}
s.nextChar()
return token
}
// Process the next character.
func (s *Source) nextChar() {
s.curPos++
if s.curPos >= len(s.source) {
s.curChar = "\000" // EOF
} else {
s.curChar = string(s.source[s.curPos])
}
}
func do_lexing(source Source) { // code_string = `+- */`
// Initialize the source.
// Loop through and print all tokens.
token := source.getToken()
// return source
for token.kind != tokens["EOF"] {
token.pprint("token")
token = source.getToken()
}
}