mighty

The mighty programming language, compiler and tools (WIP)
Log | Files | Refs

commit e0dd19e78c6fae540f403c99216fd4a89ab6f20b
parent 1db4b03a50474862ea143ad3bbe586f3feb03dd9
Author: citbl <citbl@citbl.org>
Date:   Sat, 23 May 2026 23:51:11 +1000

refac

Diffstat:
Mmtcl/lexer.lua | 149++++++++++++++++++++++++-------------------------------------------------------
1 file changed, 44 insertions(+), 105 deletions(-)

diff --git a/mtcl/lexer.lua b/mtcl/lexer.lua @@ -1,4 +1,9 @@ -local lexer = { tokens = {} } +local lexer = { + tokens = {}, + file = "", + line = 1, + pos = 1, +} local a = string.byte("a") local z = string.byte("z") @@ -93,10 +98,19 @@ local function print_token(t) ) end +function lexer:add(kind, lexeme) + token = { + kind = kind, + lexeme = lexeme, + file = self.file, + line = self.line, + col = self.col, + } + table.insert(self.tokens, token) +end + function lexer:lex(file, src) local i = 1 - local line, col = 1, 1 - local tokens = {} local function next() if i + 1 <= #src then @@ -110,142 +124,67 @@ function lexer:lex(file, src) if is_space(c) then -- skip... if c == "\n" then - col = 1 - line = line + 1 + self.col = 1 + self.line = self.line + 1 else - col = col + 1 + self.col = self.col + 1 end elseif is_alpha(c:byte()) then word, i = read_ident(i, src) - col = i - tokens[#tokens + 1] = { - kind = TK.IDENT, - lexeme = word, - file = file, - line = line, - col = col, - } + self.col = i + self:add(TK.IDENT, word) elseif is_digit(c:byte()) then number, i, is_float = read_number(i, src) - col = i - local kind = TK.IDK + self.col = i if is_float then - kind = TK.LIT_FLOAT + self:add(TK.LIT_FLOAT, number) else - kind = TK.LIT_INT + self:add(TK.LIT_INT, number) end - tokens[#tokens + 1] = { - kind = kind, - lexeme = number, - file = file, - line = line, - col = col, - } elseif c == ":" then if next() == ":" then - tokens[#tokens + 1] = { - kind = TK.COLONCOLON, - lexeme = "::", - file = file, - line = line, - col = col, - } + self:add(TK.COLONCOLON, "::") i = i + 1 - col = col + 1 + self.col = self.col + 1 else - tokens[#tokens + 1] = { - kind = TK.COLON, - lexeme = ":", - file = file, - line = line, - col = col, - } + self:add(TK.COLON, ":") end elseif c == "=" then if next() == "=" then - tokens[#tokens + 1] = { - kind = TK.EQEQ, - lexeme = "==", - - file = file, - line = line, - col = col, - } + self:add(TK.EQEQ, "==") i = i + 1 - col = col + 1 + self.col = self.col + 1 else - tokens[#tokens + 1] = { - kind = TK.EQ, - lexeme = "=", - - file = file, - line = line, - col = col, - } + self:add(TK.EQ, "=") end elseif c == "(" then - tokens[#tokens + 1] = { - kind = TK.L_PAREN, - lexeme = "(", - - file = file, - line = line, - col = col, - } + self:add(TK.L_PAREN, "(") i = i + 1 - col = col + 1 + self.col = self.col + 1 elseif c == ")" then - tokens[#tokens + 1] = { - kind = TK.R_PAREN, - lexeme = ")", - - file = file, - line = line, - col = col, - } + self:add(TK.R_PAREN, ")") i = i + 1 - col = col + 1 + self.col = self.col + 1 elseif c == "'" then - tokens[#tokens + 1] = { - kind = TK.SGL_QUOTE, - lexeme = "'", - - file = file, - line = line, - col = col, - } + self:add(TK.SGL_QUOTE, "'") i = i + 1 - col = col + 1 + self.col = self.col + 1 elseif c == '"' then - tokens[#tokens + 1] = { - kind = TK.DBL_QUOTE, - lexeme = '"', - - file = file, - line = line, - col = col, - } + self:add(TK.DBL_QUOTE, '"') i = i + 1 - col = col + 1 + self.col = self.col + 1 else - tokens[#tokens + 1] = { - kind = TK.IDK, - lexeme = "", - file = file, - line = line, - col = col, - } + self:add(TK.IDK, "") i = i + 1 - col = col + 1 + self.col = self.col + 1 end i = i + 1 - col = col + 1 + self.col = self.col + 1 end - for i = 1, #tokens do - print_token(tokens[i]) + for i = 1, #self.tokens do + print_token(self.tokens[i]) end - self.tokens = tokens end return lexer