-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.lua
175 lines (156 loc) · 4.19 KB
/
lexer.lua
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
require("tokens")
require("utils")
require("oo")
local function isIdentifier(char)
if string.match(char, "%a") or char == "_" then
return true
end
return false
end
local function isDigit(char)
if tonumber(char) ~= nil then
return true
end
return false
end
ZERO = ""
Lexer = Class()
function Lexer:__init()
self.position = 1
self.readPosition = 1
self.ch = ZERO
self:readChar()
end
function Lexer:readChar()
self.ch = self:peakChar()
self.position = self.readPosition
self.readPosition = (self.readPosition + 1)
end
function Lexer:peakChar()
if self.readPosition > #self.input then
return ZERO
else
return string.sub(self.input, self.readPosition, self.readPosition)
end
end
function Lexer:nextToken()
local function endsWithEqual(oneChar, twoChar, duplicateChar)
if self:peakChar() ~= "=" then
return self:token(oneChar)
end
local currentChar = self.ch
self:readChar()
return Token { tokenType = twoChar, literal = Ternary(duplicateChar, string.format("%s%s", currentChar, currentChar), string.format("%s%s", currentChar, self.ch)) }
end
self:skipWhitespace()
local switch = {
["="] = function()
return endsWithEqual(ASSIGN, EQ, true)
end,
[";"] = function()
return self:token(SEMICOLON)
end,
[":"] = function()
return self:token(COLON)
end,
[","] = function()
return self:token(COMMA)
end,
["("] = function()
return self:token(LPAREN)
end,
[")"] = function()
return self:token(RPAREN)
end,
["{"] = function()
return self:token(LBRACE)
end,
["}"] = function()
return self:token(RBRACE)
end,
["["] = function()
return self:token(LBRACKET)
end,
["]"] = function()
return self:token(RBRACKET)
end,
["+"] = function()
return self:token(PLUS)
end,
["-"] = function()
return self:token(MINUS)
end,
["*"] = function()
return self:token(ASTERISK)
end,
["/"] = function()
return self:token(SLASH)
end,
["<"] = function()
return self:token(LT)
end,
[">"] = function()
return self:token(GT)
end,
["!"] = function()
return endsWithEqual(BANG, NOT_EQ, false)
end,
['"'] = function()
return Token { tokenType = STRING, literal = self:readString() }
end,
[ZERO] = function()
return Token { tokenType = EOF, literal = "" }
end
}
local f = switch[self.ch]
if f ~= nil then
local r = f()
self:readChar()
return r
end
if isIdentifier(self.ch) then
local identifier = self:readIdentifier()
return Token { tokenType = LookupIdent(identifier), literal = identifier }
end
if isDigit(self.ch) then
return Token { tokenType = INT, literal = self:readNumber() }
end
return Token { tokenType = ILLEGAL, self.ch }
end
function Lexer:skipWhitespace()
local function isWhiteSpace(char)
if char == " " or char == "\n" or char == "\t" or char == "\r" then
return true
end
return false
end
while isWhiteSpace(self.ch) do
self:readChar()
end
end
function Lexer:readIdentifier()
return self:readValue(isIdentifier)
end
function Lexer:readNumber()
return self:readValue(isDigit)
end
function Lexer:readValue(predicate)
local currentPosition = self.position
while predicate(self.ch) do
self:readChar()
end
return string.sub(self.input, currentPosition, (self.position - 1))
end
function Lexer:token(tokenType)
return Token { tokenType = tokenType, literal = self.ch }
end
function Lexer:readString()
local start = self.position + 1
while true do
self:readChar()
if self.ch == '"' or self.ch == ZERO then
break
end
end
return string.sub(self.input, start, (self.position - 1))
end