gamesreboot

Lexer for my programming language

Jun 3rd, 2022 (edited)
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. local buffer = {} -- stores strings until new line or space 'n shit
  2. --local string = "12+32"
  3. local a_z = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
  4. local digits = "1234567890"
  5. local symbols = {"||"}
  6. local operators = {"+","-","/","*","=","#","<",">"} -- operators
  7. local whitespace = {" ","\n","\t","\r"} -- list of whitespaces
  8. local keywords = {
  9.     "var",
  10.     "log", -- print but its a keyword and is shorter than print so it has a higher ranking.
  11.     "pairs",
  12.     "const",
  13.     "and",
  14.     "else",
  15.     "elseif",
  16.     "or",
  17.     "till", -- until is deprecated and unreliable
  18.     "end",
  19.     "function",
  20.     "break",
  21.     "continue",
  22.     "not",
  23.     "false",
  24.     "true",
  25.     "then",
  26.     "while",
  27.     "for",
  28.     "goto", -- we added all lua keywords + some more
  29.     "return",
  30.     "do",
  31.     "in",
  32.     "local"
  33. }
  34. local built_in_functions = {
  35.     "print" -- the same as log
  36. }
  37. --return
  38. function find(f, l) -- find element v of l satisfying f(v)
  39.   for _, v in ipairs(f) do
  40.     if l == v then
  41.         return true
  42.     end
  43.   end
  44.   return false
  45. end
  46.  
  47.   local module = {}
  48. module = function(data) -- lexer is a module
  49.     local id = 1 -- position in the string; also the id of the token
  50.     local pos = 1 -- position in the string
  51.     local start = 1 -- the start
  52.     local lastnewline = 1 -- the last newline
  53.     local buffer = {} -- complicated stuff rc8s doesnt know
  54.     local lines = {}
  55.     local lex = {}
  56.     local function PushToken(type, value)
  57.         local d = {
  58.             type = type,
  59.             token = value,
  60.             id = id
  61.         }
  62.         table.insert(lex, d)
  63.     end
  64.     local function BufferAdd(v)
  65.         buffer[#buffer + 1] = v -- adds to buffer (long value thing)
  66.     end
  67.   local function clearb()
  68.       buffer = {}
  69.   end
  70.     local function newline()
  71.         lastnewline = pos
  72.         lines[#lines + 1] = buffer
  73.         buffer = {} -- clears buffer
  74.     end
  75.     while pos < #data + 1 and pos ~= #data + 1 do
  76.         local char = data:sub(pos, pos)
  77.         local behind = data:sub(start, pos)
  78.         local behind2 = data:sub(lastnewline, pos)
  79.         if digits:find(char,1,true) then
  80.         clearb()
  81.             --print(char)
  82.             repeat
  83.                 BufferAdd(char)
  84.                 pos = pos + 1
  85.                 char = data:sub(pos,pos)
  86.             until not digits:find(char,1,true) or pos > #data
  87.             PushToken("int", table.concat(buffer))
  88.             buffer = {} -- clears buffer
  89.     elseif find(symbols,char) then
  90.       PushToken("symbol",char)
  91.         pos = pos + 1
  92.         elseif char == "'" or char == '"' and not find(operators,char) then -- activating string
  93.       clearb()
  94.             char = ""
  95.         --print(char)
  96.             repeat
  97.                 BufferAdd(char)
  98.                 pos = pos + 1
  99.                 char = data:sub(pos,pos)
  100.             until char == '"' or char == "'" or pos > #data
  101.  
  102.             PushToken("string", table.concat(buffer))
  103.  
  104.             buffer = {} -- clears buffer
  105.         pos = pos + 1
  106.         elseif find(operators,char) then
  107.       clearb()
  108.             repeat
  109.                 BufferAdd(char)
  110.                 pos = pos + 1
  111.                 char = data:sub(pos,pos)
  112.             until not find(operators,char) or pos > #data
  113.             PushToken("operator", table.concat(buffer))
  114.             buffer = {} -- clears buffer
  115.     else
  116.       --print("t")'
  117.         pos = pos + 1
  118.         --local char = data:sub(pos, pos)
  119.       BufferAdd(char)
  120.         --print(table.concat(buffer))
  121.       if find(keywords,table.concat(buffer)) then
  122.           PushToken("keyword",table.concat(buffer))
  123.       elseif find({'~~'},table.concat(buffer)) then -- activation of comment
  124.         --print(char)
  125.         clearb()
  126.           char = ""
  127.           pos = pos - 1
  128.         repeat
  129.                   BufferAdd(char)
  130.                   pos = pos + 1
  131.           char = data:sub(pos,pos)
  132.               until char == "\n" or char == "" or pos > #data
  133.         PushToken("comment",table.concat(buffer))
  134.     elseif find(built_in_functions,table.concat(buffer)) then
  135.         PushToken("Built_in_function",table.concat(buffer))
  136.  
  137.       end
  138.       --pos = pos + 1
  139.      
  140.         end
  141.     --if pos == #data or pos > #data then break end
  142.        
  143.     end
  144.   return lex
  145.  
  146. end
  147. return module
  148.  
  149.  
Add Comment
Please, Sign In to add comment