Advertisement
Guest User

interpreter

a guest
Nov 25th, 2015
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. log = {}
  2. code = [[
  3. a(argument,argument)
  4. ]]
  5.  
  6. avoidInDef = {"?","=",";"}
  7.  
  8. function specialchar(char)
  9.   for i=1,#avoidInDef do
  10.     if(char == avoidInDef[i]) then
  11.       return false
  12.     end
  13.   end
  14.   return true
  15. end
  16.  
  17. function stringcheck(str)
  18.   if(str:sub(1,1) == '"' or str:sub(1,1) == "'" and str:sub(#str,#str) == '"' or str:sub(#str,#str) == "'") then
  19.     return true
  20.   end
  21.   return false
  22. end
  23.  
  24. function lookupVar(variable,variables)
  25.   for i=1,#variables do
  26.     if(variables[i] == variable) then
  27.       return true
  28.     end
  29.   end
  30.   return false
  31. end
  32.  
  33.  
  34. function parse(str)
  35.   out = {}
  36.   variables = {}
  37.   functions = {}
  38.   cur = "nostate"
  39.   secondary = ""
  40.   third = ""
  41.   _unknown = {}
  42.   for i=1,#str do
  43.     char = str:sub(i,i)
  44.     if(cur == "nostate" and char ~= " ") then
  45.       cur = "in-char"
  46.       _unknown[#_unknown + 1] = char
  47.     elseif(cur == "in-char" and specialchar(char) and char ~= " ") then
  48.       _unknown[#_unknown + 1] = char
  49.     end
  50.     if(char == "=" and cur == "in-char" and char ~= " ") then
  51.       built = ""
  52.       for i=1,#_unknown do
  53.         built = built.._unknown[i]
  54.       end
  55.       out[#out + 1] = {name = built,type = "variable",definition = ""}
  56.       secondary = "defineVariable"
  57.       log[#log + 1] = "Created variable "..built..", awaiting definition."
  58.       cur = "no-state"
  59.       third = built
  60.       built = ""
  61.       _unknown = {}
  62.     elseif(char == "(" and cur == "in-char" and char ~= " ") then
  63.       built = ""
  64.       for i=1,#_unknown do
  65.         built = built.._unknown[i]
  66.       end
  67.       print("Opening function "..built)
  68.       cur = i
  69.       secondary = "CloseFunction"
  70.       third = built
  71.     elseif(char == ")" and secondary == "CloseFunction") then
  72.       print("Preparing to close function "..third)
  73.       passedArgs = {}
  74.       if(cur == i) then else
  75.         print(str:sub(cur,i - 1))
  76.       end
  77.       print(stringToParse)
  78.     elseif(specialchar(char) and secondary == "defineVariable" and char ~= " ") then
  79.       _unknown[#_unknown + 1] = char
  80.     elseif(not specialchar(char) and char == ";" and secondary == "defineVariable") then
  81.       built = ""
  82.       for i=1,#_unknown do
  83.         built = built.._unknown[i]
  84.       end
  85.       for i=1,#out do
  86.         if(out[i].type == "variable") then
  87.           if(out[i].name == third) then
  88.             out[i].definition = built
  89.           end
  90.         end
  91.       end
  92.       log[#log + 1] = "Defined variable "..third.." as "..built
  93.       cur = "nostate"
  94.       secondary = ""
  95.       third = ""
  96.       _unknown = {}
  97.     end
  98.   end
  99.   return out
  100. end
  101.  
  102. function run(parsed)
  103.   for i=1,#parsed do
  104.     if(parsed[i].name == "onscreen") then
  105.       print(parsed[i].definition)
  106.     end
  107.   end
  108. end
  109.  
  110. parse(code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement