-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_lexer.go
233 lines (211 loc) · 4.63 KB
/
1_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
233
// Lexical analysis, lexing, or tokenization is the process of converting a sequence of
// characters into lexical tokens. A program that performs lexical analysis may be termed
// a lexer, tokenizer, or scanner, although scanner is also a term for the first stage of
// a lexer. A lexer will take an input character stream and convert it into the smallest
// meaningful characters called tokens. The stream of lexemes can be fed to a parser
// which will convert it into a parser tree.
package main
import (
"bufio"
"log"
"strings"
"unicode"
)
// ************
// ** Token **
// ************
type TokenType string
const (
ILLEGAL TokenType = "ILLEGAL"
EOF TokenType = "EOF"
// Delimiters
COMMA TokenType = ","
COLON TokenType = ":"
LPAREN TokenType = "("
RPAREN TokenType = ")"
LBRACKET TokenType = "["
RBRACKET TokenType = "]"
LCURLY TokenType = "{"
RCURLY TokenType = "}"
// Identifiers and literals
IDENT TokenType = "IDENT"
INT TokenType = "INT"
FLOAT TokenType = "FLOAT"
STRING TokenType = "STRING"
// Keywords
TRUE TokenType = "TRUE"
FALSE TokenType = "FALSE"
VAR TokenType = "VAR"
IF TokenType = "IF"
ELSE TokenType = "ELSE"
WHILE TokenType = "WHILE"
FOR TokenType = "FOR"
IN TokenType = "IN"
FN TokenType = "FN"
RETURN TokenType = "RETURN"
LEN TokenType = "LEN"
PRINT TokenType = "PRINT"
PRINTLN TokenType = "PRINTLN"
// Operators
ASSIGN TokenType = "="
PLUS TokenType = "+"
MINUS TokenType = "-"
ASTERISK TokenType = "*"
SLASH TokenType = "/"
NOT TokenType = "!"
LT TokenType = "<"
GT TokenType = ">"
LEQ TokenType = "<="
GEQ TokenType = ">="
EQ TokenType = "=="
NEQ TokenType = "!="
OR TokenType = "OR"
AND TokenType = "AND"
)
type Token struct {
Type TokenType
Value string
}
func NewToken(t TokenType, v string) Token {
return Token{Type: t, Value: v}
}
// ***********
// ** Lexer **
// ***********
type Lexer struct {
reader *bufio.Reader
}
func NewLexer(in string) *Lexer {
return &Lexer{reader: bufio.NewReader(strings.NewReader(in))}
}
func (l *Lexer) Lex() chan Token {
tokens := make(chan Token)
go func() {
defer close(tokens)
for {
l.lexWhitespace()
r := l.readRune()
switch {
case r == 0:
tokens <- NewToken(EOF, "")
return
case r == '"':
tokens <- l.lexString(r)
case unicode.IsDigit(r):
tokens <- l.lexNumber(r)
case unicode.IsLetter(r):
tokens <- l.lexIdentifier(r)
default:
token := l.lexSymbol(r)
if token.Type == ILLEGAL {
log.Fatalf("error: invalid token found: \"%s\"", token.Value)
}
tokens <- token
}
}
}()
return tokens
}
func (l *Lexer) lexIdentifier(r rune) Token {
keywords := map[string]TokenType{
"true": TRUE,
"false": FALSE,
"var": VAR,
"if": IF,
"else": ELSE,
"while": WHILE,
"for": FOR,
"in": IN,
"fn": FN,
"return": RETURN,
"len": LEN,
"print": PRINT,
"println": PRINTLN,
"or": OR,
"and": AND,
}
v := string(r)
for {
r = l.readRune()
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
l.unreadRune()
break
}
v += string(r)
}
if t, ok := keywords[v]; ok {
return NewToken(t, v)
}
return NewToken(IDENT, v)
}
func (l *Lexer) lexNumber(r rune) Token {
t := INT
v := string(r)
for {
r = l.readRune()
if !unicode.IsDigit(r) && r != '.' {
l.unreadRune()
break
}
v += string(r)
if r == '.' {
t = FLOAT
}
}
return NewToken(t, v)
}
func (l *Lexer) lexString(_ rune) Token {
str, _ := l.reader.ReadString('"')
return NewToken(STRING, strings.TrimRight(str, `"`))
}
func (l *Lexer) lexSymbol(r rune) Token {
symbols := map[string]TokenType{
",": COMMA,
":": COLON,
"(": LPAREN,
")": RPAREN,
"[": LBRACKET,
"]": RBRACKET,
"{": LCURLY,
"}": RCURLY,
"=": ASSIGN,
"+": PLUS,
"-": MINUS,
"*": ASTERISK,
"/": SLASH,
"!": NOT,
"<": LT,
">": GT,
"<=": LEQ,
">=": GEQ,
"==": EQ,
"!=": NEQ,
}
singleCharSymbol := string(r)
doubleCharSymbol := singleCharSymbol + string(l.readRune())
if t, ok := symbols[doubleCharSymbol]; ok {
return NewToken(t, doubleCharSymbol)
}
l.unreadRune()
if t, ok := symbols[singleCharSymbol]; ok {
return NewToken(t, singleCharSymbol)
}
return NewToken(ILLEGAL, singleCharSymbol)
}
func (l *Lexer) lexWhitespace() {
r := l.readRune()
for r == ' ' || r == '\t' || r == '\n' || r == '\r' || r == '#' {
if r == '#' {
l.reader.ReadLine()
}
r = l.readRune()
}
l.unreadRune()
}
func (l *Lexer) readRune() rune {
r, _, _ := l.reader.ReadRune()
return r
}
func (l *Lexer) unreadRune() {
l.reader.UnreadRune()
}