-- src/catscript/lexer.cat -- the catscript lexer, written in catscript. -- -- This is the self-hosted front-end: lexer, parser, checker and compiler -- are all catscript programs. The C runtime only provides the object -- model, the VM and the bytecode builder (bc_run/bc_dump). -- -- The lexer produces an array of token tables: -- { kind = , text = , line = } -- where is "NAME", "INT", "FLOAT", "STRING", "KW", "EOF", or the -- literal symbol text ("+", "==", "..", ...). Keywords carry kind "KW" -- and their text. -- -- All state lives in globals prefixed LEX_ so nested helpers need no -- upvalues (catscript closures do not capture outer locals). LEX_SRC = "" LEX_POS = 0 LEX_N = 0 LEX_LINE = 1 LEX_KEYWORDS = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while" } function lexer_is_digit(c) return c >= 48 and c <= 57 end function lexer_is_alpha(c) return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95 end function lexer_is_alnum(c) return lexer_is_alpha(c) or lexer_is_digit(c) end function lexer_is_keyword(s) local i = 1 while i <= len(LEX_KEYWORDS) do if LEX_KEYWORDS[i] == s then return true end i = i + 1 end return false end function lexer_init(src) LEX_SRC = src LEX_POS = 0 LEX_N = len(src) LEX_LINE = 1 end function lexer_peek(off) local p = LEX_POS + off if p < 0 or p >= LEX_N then return -1 end return ord(LEX_SRC, p) end function lexer_advance() local c = lexer_peek(0) if c < 0 then return -1 end LEX_POS = LEX_POS + 1 if c == 10 then LEX_LINE = LEX_LINE + 1 end return c end function lexer_skip_ws() local go = true while go do local c = lexer_peek(0) if c == 32 or c == 9 or c == 13 or c == 10 then lexer_advance() elseif c == 45 and lexer_peek(1) == 45 then lexer_advance() lexer_advance() while lexer_peek(0) >= 0 and lexer_peek(0) ~= 10 do lexer_advance() end else go = false end end end function lexer_read_name() local start = LEX_POS while lexer_is_alnum(lexer_peek(0)) do lexer_advance() end local text = substr(LEX_SRC, start, LEX_POS - start) local kind = "NAME" if lexer_is_keyword(text) then kind = text end return { kind = kind, text = text, line = LEX_LINE } end function lexer_read_number() local start = LEX_POS local isf = false while lexer_is_digit(lexer_peek(0)) do lexer_advance() end if lexer_peek(0) == 46 and lexer_is_digit(lexer_peek(1)) then isf = true lexer_advance() while lexer_is_digit(lexer_peek(0)) do lexer_advance() end end local text = substr(LEX_SRC, start, LEX_POS - start) if isf then return { kind = "FLOAT", text = text, line = LEX_LINE } end return { kind = "INT", text = text, line = LEX_LINE } end function lexer_read_string(quote) local buf = "" lexer_advance() while lexer_peek(0) >= 0 and lexer_peek(0) ~= quote do local c = lexer_advance() if c == 92 then local e = lexer_advance() if e == 110 then buf = buf .. chr(10) elseif e == 116 then buf = buf .. chr(9) elseif e == 92 then buf = buf .. "\\" else buf = buf .. chr(e) end else buf = buf .. chr(c) end end lexer_advance() return { kind = "STRING", text = buf, line = LEX_LINE } end function lexer_read_symbol() local c = lexer_advance() local k = chr(c) if c == 61 then if lexer_peek(0) == 61 then lexer_advance() k = "==" end elseif c == 126 then if lexer_peek(0) == 61 then lexer_advance() k = "~=" end elseif c == 60 then if lexer_peek(0) == 61 then lexer_advance() k = "<=" end elseif c == 62 then if lexer_peek(0) == 61 then lexer_advance() k = ">=" end elseif c == 46 then if lexer_peek(0) == 46 then lexer_advance() k = ".." end elseif c == 58 then if lexer_peek(0) == 58 then lexer_advance() k = "::" end elseif c == 45 then if lexer_peek(0) == 62 then lexer_advance() k = "->" end end return { kind = k, text = k, line = LEX_LINE } end function lexer_next() lexer_skip_ws() local c = lexer_peek(0) if c < 0 then return { kind = "EOF", text = "", line = LEX_LINE } end if lexer_is_alpha(c) then return lexer_read_name() end if lexer_is_digit(c) then return lexer_read_number() end if c == 34 or c == 39 then return lexer_read_string(c) end return lexer_read_symbol() end function lexer_tokenize(src) lexer_init(src) local toks = {} local n = 0 local stop = false while not stop do local t = lexer_next() n = n + 1 toks[n] = t if t.kind == "EOF" then stop = true end end return toks end