Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local buffer = {} -- stores strings until new line or space 'n shit
- --local string = "12+32"
- local a_z = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
- local digits = "1234567890"
- local symbols = {"||"}
- local operators = {"+","-","/","*","=","#","<",">"} -- operators
- local whitespace = {" ","\n","\t","\r"} -- list of whitespaces
- local keywords = {
- "var",
- "log", -- print but its a keyword and is shorter than print so it has a higher ranking.
- "pairs",
- "const",
- "and",
- "else",
- "elseif",
- "or",
- "till", -- until is deprecated and unreliable
- "end",
- "function",
- "break",
- "continue",
- "not",
- "false",
- "true",
- "then",
- "while",
- "for",
- "goto", -- we added all lua keywords + some more
- "return",
- "do",
- "in",
- "local"
- }
- local built_in_functions = {
- "print" -- the same as log
- }
- --return
- function find(f, l) -- find element v of l satisfying f(v)
- for _, v in ipairs(f) do
- if l == v then
- return true
- end
- end
- return false
- end
- local module = {}
- module = function(data) -- lexer is a module
- local id = 1 -- position in the string; also the id of the token
- local pos = 1 -- position in the string
- local start = 1 -- the start
- local lastnewline = 1 -- the last newline
- local buffer = {} -- complicated stuff rc8s doesnt know
- local lines = {}
- local lex = {}
- local function PushToken(type, value)
- local d = {
- type = type,
- token = value,
- id = id
- }
- table.insert(lex, d)
- end
- local function BufferAdd(v)
- buffer[#buffer + 1] = v -- adds to buffer (long value thing)
- end
- local function clearb()
- buffer = {}
- end
- local function newline()
- lastnewline = pos
- lines[#lines + 1] = buffer
- buffer = {} -- clears buffer
- end
- while pos < #data + 1 and pos ~= #data + 1 do
- local char = data:sub(pos, pos)
- local behind = data:sub(start, pos)
- local behind2 = data:sub(lastnewline, pos)
- if digits:find(char,1,true) then
- clearb()
- --print(char)
- repeat
- BufferAdd(char)
- pos = pos + 1
- char = data:sub(pos,pos)
- until not digits:find(char,1,true) or pos > #data
- PushToken("int", table.concat(buffer))
- buffer = {} -- clears buffer
- elseif find(symbols,char) then
- PushToken("symbol",char)
- pos = pos + 1
- elseif char == "'" or char == '"' and not find(operators,char) then -- activating string
- clearb()
- char = ""
- --print(char)
- repeat
- BufferAdd(char)
- pos = pos + 1
- char = data:sub(pos,pos)
- until char == '"' or char == "'" or pos > #data
- PushToken("string", table.concat(buffer))
- buffer = {} -- clears buffer
- pos = pos + 1
- elseif find(operators,char) then
- clearb()
- repeat
- BufferAdd(char)
- pos = pos + 1
- char = data:sub(pos,pos)
- until not find(operators,char) or pos > #data
- PushToken("operator", table.concat(buffer))
- buffer = {} -- clears buffer
- else
- --print("t")'
- pos = pos + 1
- --local char = data:sub(pos, pos)
- BufferAdd(char)
- --print(table.concat(buffer))
- if find(keywords,table.concat(buffer)) then
- PushToken("keyword",table.concat(buffer))
- elseif find({'~~'},table.concat(buffer)) then -- activation of comment
- --print(char)
- clearb()
- char = ""
- pos = pos - 1
- repeat
- BufferAdd(char)
- pos = pos + 1
- char = data:sub(pos,pos)
- until char == "\n" or char == "" or pos > #data
- PushToken("comment",table.concat(buffer))
- elseif find(built_in_functions,table.concat(buffer)) then
- PushToken("Built_in_function",table.concat(buffer))
- end
- --pos = pos + 1
- end
- --if pos == #data or pos > #data then break end
- end
- return lex
- end
- return module
Add Comment
Please, Sign In to add comment