-- lex - Lexer local log = require("log") local lex = { } lex.line = 1 lex.column = 1 function tkn_new(id, literal) return { type = id, column = lex.column, line = lex.line, literal = literal } end local function fmtnum(src) local ishex = false local buf = { } local i = 0 while true do local c = src:read(1) if tonumber(c) then table.insert(buf, c) elseif c == 'h' then if i ~= 1 then log.error("bad hexadecimal format at line " .. lex.line) end table.insert(buf, c) ishex = true elseif string.match(c, "[a-fA-F]") then if buf[2] == "h" then table.insert(buf, c) else log.error("bad hexadecimal format at line " .. lex.line) end elseif lex.tkns[c] then src:seek("cur", -1) break elseif c == nil then break else while true do if lex.tkns[c] then c = c - 1 break elseif c == nil then break end i = i + 1 table.insert(buf, c) break end return table.concat(buf) end i = i + 1 lex.column = lex.column + 1 end local raw = table.concat(buf) if ishex == true then local clean = string.sub(raw, 3) num = tonumber(clean, 16) raw = tostring(num) end return raw end lex.read = function(src) while true do local c = src:read(1) if c == nil then return nil end lex.column = lex.column + 1 if tonumber(c) then src:seek("cur", -1) local num = fmtnum(src) if tonumber(num) then return tkn_new(lex.tkns.integer, num) else return tkn_new(lex.id, num) end elseif string.match(c, "[%-%+%*%=]") then return tkn_new(lex.tkns[c], c) elseif c == "(" then return tkn_new(lex.tkns["("], "(") elseif c == ")" then return tkn_new(lex.tkns[")"], ")") elseif c == ' ' or c == '\t' or c == '\n' then if c == '\n' then lex.line = lex.line + 1 end elseif c == "'" then c = src:read(1) if src:read(1) ~= "'" then log.error("can only contain single character when using \"'\" at line " .. lex.line) end return tkn_new(lex.tkns.integer, tostring(string.byte(c))) elseif c == '\"' then local buf = { } while true do c = src:read(1) if c == '\"' then break elseif c == nil then log.error("missing closing quote for string at line " .. lex.line) end if c == "\\" then n = src:read(1) if n == "n" then c = '\n' table.insert(buf, c) end else table.insert(buf, c) end end return tkn_new(lex.tkns.string, table.concat(buf)) elseif lex.tkns[c] then return tkn_new(lex.tkns[c], c) else local buf = { c } while true do c = src:read(1) if c == ' ' or c == ')' or c == '(' or c == '\n' or c == '\t' or lex.tkns[c] then src:seek("cur", -1) break elseif c == nil then break end table.insert(buf, c) lex.column = lex.column + 1 end local name = table.concat(buf) if lex.tkns[name] then return tkn_new(lex.tkns[name], name) else return tkn_new(lex.tkns.id, name) end end end end lex.peek = function(src, depth) local line = lex.line local col = lex.column local ssave = src:seek("cur") if depth == nil then depth = 1 end local tk = nil for i = 1, depth do tk = lex.read(src) end src:seek("set", ssave) lex.line = line lex.column = col return tk end ------------------------------------------------------------------------------------------ -- DEFINING TOKEN TYPES ------------------------------------------------------------------------------------------ local TKNS = { "(", ")", -- NESTING "[", "]", "-", "+", "/", "*", -- ARITHMETICS "asm", -- KEYWORDS "id", "string", "integer", "func", "var", "loop", "set", "cond", "else", "break", "return", -- KEYWORDS " ", "\t", "\n", ";", } lex.tkns = { } for i = 1, #TKNS do lex.tkns[TKNS[i]] = i end lex.isreg8 = function(tkn) if tkn.literal == "al" or tkn.literal == "ah" or tkn.literal == "bl" or tkn.literal == "bh" or tkn.literal == "cl" or tkn.literal == "ch" or tkn.literal == "dl" or tkn.literal == "dh" then return true end return false end lex.isreg32 = function(tkn) if tkn.literal == "eax" or tkn.literal == "ebx" or tkn.literal == "ecx" or tkn.literal == "edx" or tkn.literal == "edi" or tkn.literal == "esp" or tkn.literal == "esi" or tkn.literal == "ebp" then return true end return false end lex.isreg64 = function(tkn) local literal = type(tkn) == "table" and tkn.literal or tkn if literal == "rax" or literal == "rbx" or literal == "rcx" or literal == "rdx" or literal == "rdi" or literal == "rsp" or literal == "rsi" or literal == "rbp" then return true end return false end return lex