Advertisement
incinirate

Compascript

Aug 4th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. local tArgs = {...}
  2. local file = tArgs[1]
  3. local code = ""
  4. if file and fs.exists(file) then
  5.   local ex = fs.open(tArgs[1],"r")
  6.   code = ex.readAll()
  7. else
  8.   printError("gimme a valid file fool")
  9.   return
  10. end
  11.  
  12. --local tEnv = {running = false}
  13.  
  14. local lineInt = 0
  15. function throw(er)
  16.   printError("Error at line "..lineInt..": "..er)
  17.   error()
  18. end
  19.  
  20. local ipairs = ipairs
  21.  
  22. local vars = {}
  23.  
  24. function preprocess(sStr)
  25.   local val = sStr:match("%%%<%s?%*%&%s?(%S+)%s?%/%&")
  26.   if val then
  27.     if vars[val] then
  28.       return vars[val]
  29.     else
  30.       throw("Attempt to use non-initialized variable ("..val..")")
  31.     end
  32.   else
  33.     return sStr
  34.   end
  35. end
  36.  
  37. local funchandles = {
  38.   {
  39.     key = "v%>",
  40.    
  41.     print = {
  42.       key = "%>%_",
  43.       func = function(arg)
  44.         print(arg)
  45.       end,
  46.     }
  47.   },
  48. }
  49.  
  50. local lexicon = {
  51.   {
  52.     key = "%%%>%s?.*%s?%d%s?%*%&.*%/%&%s?%%%?",
  53.     def = function(str)
  54.       --   %>*%0*&Oh Hi There!!!/&%?
  55.       local varType = str:match("%%%>%s?([^%s]*)%s?%d")
  56.       local varName = str:match("%%%>%s?.*%s?%d+")
  57.       local varNP = varName:find("%d")
  58.       varName = varName:sub(varNP)
  59.       local varContents = str:match("%*%&(.*)%/%&%s?%%%?")
  60.      
  61.       if varType == "!%" then
  62.         vars[varName] = tonumber(varContents)
  63.       elseif varType == "*%" then
  64.         vars[varName] = varContents
  65.         --print("Store "..varContents.." into "..varName)
  66.       else
  67.         throw("Invalid variable type! Got '"..varType.."'")
  68.       end
  69.     end,
  70.   },
  71.   {
  72.     key = "%^%>.*%^%?",
  73.     def = function(str)
  74.       local func = str:match("%^%>%s?(.*)%s?%^%?")
  75.       for k,v in ipairs(funchandles) do
  76.         local s,e = func:find("^%s*("..v.key..")")
  77.         if s and e then
  78.           local agp = func:sub(e+1)
  79.           for kk,vv in pairs(v) do
  80.             if type(vv)=="table" then
  81.               if agp:match("^%s*("..vv.key..")%s?%*%&") then
  82.                 local args = agp:match("^%s*"..vv.key.."%s?%*%&(.+)%/%&")
  83.                 args = args:match("%s*(.*)%s*")
  84.                 local tArg = {}
  85.                 local ss,ee = args:find("%%%.")
  86.                 if ss and ee then
  87.                   local le = 1
  88.                   while ss and ee do
  89.                     local n = args:sub(le,ss-1)
  90.                     n = n:match("%s*(.*)%s*")
  91.                     le = ee+1
  92.                     table.insert(tArg,preprocess(n))
  93.                     ss,ee = args:find("%%%.",ee)
  94.                   end
  95.                   table.insert(tArg,preprocess(args:sub(le)))
  96.                   vv.func(unpack(tArg))
  97.                 else
  98.                   vv.func(preprocess(args))
  99.                 end
  100.                 break
  101.               end
  102.             end
  103.           end
  104.           break
  105.         end
  106.       end
  107.     end,
  108.   },
  109. }
  110.  
  111. local lines = {}
  112.  
  113. for line in io.lines(tArgs[1]) do
  114.   lines[#lines+1] = line
  115. end
  116.  
  117. local opened = false
  118. for k,v in ipairs(lines) do
  119.   lineInt = k
  120.   if not opened then
  121.     if v:match("%&%^%>%*%&") then
  122.       opened = true
  123.     end
  124.   else
  125.     --print("proccess line "..k)
  126.     for kk,vv in ipairs(lexicon) do
  127.       local mtc = v:match(vv.key)
  128.       if mtc then
  129.         vv.def(mtc)
  130.         break
  131.       end
  132.     end
  133.     if v:match("^%s*%/%&") then
  134.       break
  135.     end
  136.   end
  137. end
  138.  
  139. if not opened then
  140.   printError("Program was never opened!")
  141.   return
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement