Advertisement
lauriszz123

Lua Compiler

Oct 10th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.14 KB | None | 0 0
  1. tArgs={ ... }
  2.  
  3. local pri=print
  4. local tstring=tostring
  5. local erro=error
  6. local _PRS=pairs
  7. local lfile=loadfile
  8. local tClear=term.clear
  9. local tSetCursorPos=term.setCursorPos
  10. local tSetTextColor=term.setTextColor
  11. local pcl=pcall
  12. local fsexists=fs.exists
  13. local fsopen=fs.open
  14. local osTime=os.time
  15. local prErr=printError
  16. local ver="15.10.12"
  17.  
  18. local classSystem = [[
  19. local Packages={}
  20. local Extends={
  21.     __TERM=term;
  22.     __SHELL=shell;
  23.     __OS=os;
  24.     __COLORS=colors;
  25.     __FS=fs;
  26. }
  27.  
  28. function Package(func)
  29.     local pkg={}
  30.     pkg.Classes={}
  31.     pkg.print=print
  32.     pkg.read=read
  33.     pkg.write=write
  34.     pkg.Class=function(func, ext)
  35.         local cls={ print=pkg.print; read=pkg.read; write=pkg.write; }
  36.         cls.using=function(name)
  37.             if Packages[name] then
  38.                 for k, v in pairs(Packages[name]["Classes"]) do
  39.                     if not pkg["Classes"][k] then
  40.                         pkg["Classes"][k]=v
  41.                     end
  42.                 end
  43.             else
  44.                 printError("Package "..name.." does not exist.")
  45.                 return nil;
  46.             end
  47.         end
  48.         cls.new=function(name, ...)
  49.             if pkg.Classes[name] then
  50.                 if pkg.Classes[name][name] then
  51.                     local newClass={}
  52.                     for k, v in pairs(pkg.Classes[name]) do
  53.                         newClass[k]=v
  54.                     end
  55.                     return newClass[name](...)
  56.                 else
  57.                     printError("Class "..name.." constructor does not exist.")
  58.                     return nil;
  59.                 end
  60.             else
  61.                 printError("Class "..name.." does not exist.")
  62.                 return nil;
  63.             end
  64.         end
  65.         if Extends[ext] then
  66.             for k, v in pairs(Extends[ext]) do
  67.                 cls[k]=v
  68.             end
  69.         elseif pkg.Classes[ext] then
  70.             cls[ext]={}
  71.             for k, v in pairs(pkg.Classes[ext]) do
  72.                 cls[ext][k]=v
  73.             end
  74.         end
  75.         setfenv(func, cls)
  76.         func(cls)
  77.         return cls
  78.     end
  79.     setfenv(func, pkg)
  80.     func()
  81.     local pkgMainCount=0
  82.     if pkg.Classes.Main then
  83.         if pkg.Classes.Main.Main then
  84.             pkg.Classes.Main.Main()
  85.         end
  86.     end
  87.     return pkg
  88. end
  89.  
  90. Packages["craftOS"]=Package(function()
  91.     Classes["term"]=Class(function(this)
  92.         function term()
  93.             return this
  94.         end
  95.     end, "__TERM")
  96.     Classes["os"]=Class(function(this)
  97.         function os()
  98.             return this
  99.         end
  100.     end, "__OS")
  101.     Classes["shell"]=Class(function(this)
  102.         function shell()
  103.             return this
  104.         end
  105.     end, "__SHELL")
  106.     Classes["colors"]=Class(function(this)
  107.         function colors()
  108.             return this
  109.         end
  110.     end, "__COLORS")
  111.     Classes["fs"]=Class(function(this)
  112.         function fs()
  113.             return this
  114.         end
  115.     end, "__FS")
  116. end)
  117.  
  118. --Main code
  119.  
  120. ]]
  121.  
  122. local function printTable(tbl)
  123.     local str=""
  124.     for k, v in _PRS(tbl) do
  125.         if k then
  126.             str=str..tstring(v)
  127.         end
  128.     end
  129.     pri(tstring(str))
  130. end
  131.  
  132. local symbols={}
  133. symbols["("]=true
  134. symbols["{"]=true
  135. symbols["}"]=true
  136. symbols[")"]=true
  137. symbols[","]=true
  138. symbols["."]=true
  139. symbols["+"]=true
  140. symbols["-"]=true
  141. symbols["/"]=true
  142. symbols["*"]=true
  143. symbols[";"]=true
  144. symbols["<"]=true
  145. symbols[">"]=true
  146. symbols["\n"]=true
  147. symbols["="]=true
  148. symbols[" "]=true
  149. symbols["\t"]=true
  150. symbols["\""]=true
  151. symbols["\'"]=true
  152. symbols["|"]=true
  153. symbols["&"]=true
  154. symbols["!"]=true
  155.  
  156. function tokenize(file)
  157.     local tokens={}
  158.     local keyword=""
  159.     local lt=""
  160.     local ignoreSpace=false
  161.     for i=1, #file do
  162.         local lt=file:sub(i,i)
  163.         keyword=keyword..lt
  164.         if symbols[lt]==true and not ignoreSpace then
  165.             if #keyword>1 then
  166.                 tokens[#tokens+1]=keyword:sub(1, #keyword-1)
  167.             end
  168.             tokens[#tokens+1]=lt
  169.             keyword=""
  170.         elseif symbols[lt]==false then
  171.             ignoreSpace = not ignoreSpace
  172.         end
  173.     end
  174.     return tokens
  175. end
  176.  
  177. local function parse(toks)
  178.     local i=1
  179.     local parsedTokens={}
  180.     local endClassCount=0
  181.     local addThen=false
  182.     local addDo=false
  183.     local startString=false
  184.     local startingClass=false
  185.     local foundExtends=false
  186.     local extendsTo=""
  187.     local funcStart=false
  188.     while i<=#toks do
  189.         if toks[i]=="package" then
  190.             parsedTokens[#parsedTokens+1]="Packages[\""..toks[i+2].."\"]"
  191.             parsedTokens[#parsedTokens+1]="=Package(function()"
  192.             if toks[i+4]=="{" then
  193.                 parsedTokens[#parsedTokens+1]=""
  194.             else
  195.                 printError("Cant create a package.")
  196.                 return nil;
  197.             end
  198.             i=i+4
  199.         elseif toks[i]=="class" then
  200.             parsedTokens[#parsedTokens+1]="Classes[\""..toks[i+2].."\"]"
  201.             parsedTokens[#parsedTokens+1]="=Class(function(this)"
  202.             if toks[i+4]=="extends" then
  203.                 foundExtends=true;
  204.                 extendsTo=toks[i+6]
  205.             end
  206.             if foundExtends then
  207.                 if toks[i+8]=="{" then
  208.                     endClassCount=endClassCount+1
  209.                     parsedTokens[#parsedTokens+1]=""
  210.                 else
  211.                     term.setTextColor(colors.red)
  212.                     print("Can't start a class!");
  213.                     return nil;
  214.                 end
  215.             else
  216.                 if toks[i+4]=="{" then
  217.                     endClassCount=endClassCount+1
  218.                     parsedTokens[#parsedTokens+1]=""
  219.                 else
  220.                     term.setTextColor(colors.red)
  221.                     print("Can't start a class!")
  222.                     return nil;
  223.                 end
  224.             end
  225.             if foundExtends then
  226.                 i=i+8
  227.             else
  228.                 i=i+4
  229.             end
  230.         elseif (toks[i]=="public" or toks[i]=="private") then
  231.             if toks[i+2]=="function" then
  232.                 local ii=i+2
  233.                 local step=0
  234.                 if toks[i]=="private" then
  235.                     parsedTokens[#parsedTokens+1]="local "
  236.                 end
  237.                 local isIf=false
  238.                 local ifCount=0
  239.                 while ii<=#toks do
  240.                     if toks[ii]=="if" then
  241.                         local pos=ii
  242.                         while true do
  243.                             if toks[pos]=="{" then
  244.                                 parsedTokens[#parsedTokens+1]="then"
  245.                                 endClassCount=endClassCount+1
  246.                                 break
  247.                             elseif toks[pos]=="!" then
  248.                                 if toks[pos+1]=="=" then
  249.                                     parsedTokens[#parsedTokens+1]="~="
  250.                                     pos=pos+1
  251.                                 else
  252.                                     parsedTokens[#parsedTokens+1]=" not "
  253.                                 end
  254.                             else
  255.                                 parsedTokens[#parsedTokens+1]=toks[pos]
  256.                             end
  257.                             pos=pos+1
  258.                         end
  259.                         pos=pos-ii
  260.                         ii=ii+pos+1
  261.                     elseif toks[ii]=="while" then
  262.                         local pos=ii
  263.                         while true do
  264.                             if toks[pos]=="{" then
  265.                                 endClassCount=endClassCount+1
  266.                                 parsedTokens[#parsedTokens+1]="do"
  267.                                 break
  268.                             elseif toks[pos]=="!" then
  269.                                 if toks[pos+1]=="=" then
  270.                                     parsedTokens[#parsedTokens+1]="~="
  271.                                     pos=pos+1
  272.                                 else
  273.                                     parsedTokens[#parsedTokens+1]=" not "
  274.                                 end
  275.                             else
  276.                                 parsedTokens[#parsedTokens+1]=toks[pos]
  277.                             end
  278.                             pos=pos+1
  279.                         end
  280.                         pos=pos-ii
  281.                         ii=ii+pos+1
  282.                     elseif toks[ii]=="for" then
  283.                         local pos=ii
  284.                         while true do
  285.                             if toks[pos]=="{" then
  286.                                 endClassCount=endClassCount+1
  287.                                 parsedTokens[#parsedTokens+1]="do"
  288.                                 break
  289.                             elseif (toks[pos]=="(" or toks[pos]==")") then
  290.                                 parsedTokens[#parsedTokens+1]=" "
  291.                             elseif toks[pos]=="!" then
  292.                                 if toks[pos+1]=="=" then
  293.                                     parsedTokens[#parsedTokens+1]="~="
  294.                                     pos=pos+1
  295.                                 else
  296.                                     parsedTokens[#parsedTokens+1]=" not "
  297.                                 end
  298.                             else
  299.                                 parsedTokens[#parsedTokens+1]=toks[pos]
  300.                             end
  301.                             pos=pos+1
  302.                         end
  303.                         pos=pos-ii
  304.                         ii=ii+pos+1
  305.                     end
  306.                     if toks[ii]=="{" then
  307.                         endClassCount=endClassCount+1
  308.                         parsedTokens[#parsedTokens+1]=""
  309.                     elseif toks[ii]=="}" then
  310.                         endClassCount=endClassCount-1
  311.                         if isIf==false and ifCount==0 then
  312.                             parsedTokens[#parsedTokens+1]="end"
  313.                             break
  314.                         else
  315.                             parsedTokens[#parsedTokens+1]="end"
  316.                             isIf=false
  317.                             ifCount=ifCount-1
  318.                         end
  319.                     else
  320.                         if (toks[ii]=="function" and step==0) then
  321.                             step=step+1
  322.                         end
  323.                         if (toks[ii]==" " and step==1) then
  324.                             step=step+1
  325.                         end
  326.                         if toks[ii]=="/" then
  327.                             if toks[ii+1]=="/" then
  328.                                 parsedTokens[#parsedTokens+1]="--"
  329.                             elseif toks[ii+1]=="*" then
  330.                                 while true do
  331.                                     if toks[ii]=="*" then
  332.                                         if toks[ii+1]=="/" then break; end
  333.                                     else
  334.                                         ii=ii+1
  335.                                     end
  336.                                 end
  337.                             end
  338.                         elseif toks[ii]=="\"" then
  339.                             parsedTokens[#parsedTokens+1]=toks[ii]
  340.                             startString = not startString
  341.                         elseif toks[ii]=="!" and not startString then
  342.                             if toks[ii+1]=="=" then
  343.                                 parsedTokens[#parsedTokens+1]="~="
  344.                                 ii=ii+1
  345.                             else
  346.                                 parsedTokens[#parsedTokens+1]=" not "
  347.                             end
  348.                         elseif toks[ii]=="|" and not startString then
  349.                             parsedTokens[#parsedTokens+1]=" or "
  350.                         elseif toks[ii]=="&" and not startString then
  351.                             parsedTokens[#parsedTokens+1]=" and "
  352.                         else
  353.                             if toks[ii]=="private" then
  354.                                 parsedTokens[#parsedTokens+1]="local "
  355.                                 ii=ii+1
  356.                                 while true do
  357.                                     if not (toks[ii]==" ") then
  358.                                         if toks[ii]==";" then
  359.                                             parsedTokens[#parsedTokens+1]=toks[ii]
  360.                                             funcStart=false
  361.                                             break
  362.                                         elseif toks[ii]=="\"" then
  363.                                             startString = not startString
  364.                                         elseif toks[ii]=="|" and not startString then
  365.                                             parsedTokens[#parsedTokens+1]=" or "
  366.                                             ii=ii+1
  367.                                         elseif toks[ii]=="&" and not startString then
  368.                                             parsedTokens[#parsedTokens+1]=" and "
  369.                                             ii=ii+1
  370.                                         elseif toks[ii]=="\n" then
  371.                                             term.setTextColor(colors.red)
  372.                                             print("No semicolon at the end of variable.")
  373.                                             return nil
  374.                                         end
  375.                                         parsedTokens[#parsedTokens+1]=toks[ii]
  376.                                     end
  377.                                     ii=ii+1
  378.                                 end
  379.                                 ii=ii+1
  380.                             elseif toks[ii]=="public" then
  381.                                 --parsedTokens[#parsedTokens+1]="_G."
  382.                                 ii=ii+1
  383.                                 while true do
  384.                                     if not (toks[ii]==" ") then
  385.                                         if toks[ii]==";" then
  386.                                             parsedTokens[#parsedTokens+1]=toks[ii]
  387.                                             funcStart=false
  388.                                             break
  389.                                         elseif toks[ii]=="\"" then
  390.                                             startString = not startString
  391.                                         elseif toks[ii]=="|" and not startString then
  392.                                             parsedTokens[#parsedTokens+1]=" or "
  393.                                             ii=ii+1
  394.                                         elseif toks[ii]=="&" and not startString then
  395.                                             parsedTokens[#parsedTokens+1]=" and "
  396.                                             ii=ii+1
  397.                                         elseif toks[ii]=="\n" then
  398.                                             term.setTextColor(colors.red)
  399.                                             print("No semicolon at the end of variable.")
  400.                                             return nil
  401.                                         end
  402.                                         parsedTokens[#parsedTokens+1]=toks[ii]
  403.                                     end
  404.                                     ii=ii+1
  405.                                 end
  406.                                 ii=ii+1
  407.                             else
  408.                                 parsedTokens[#parsedTokens+1]=toks[ii]
  409.                             end
  410.                         end
  411.                         if step==2 then
  412.                             if toks[i]=="public" then
  413.                                 parsedTokens[#parsedTokens+1]=""
  414.                                 step=0
  415.                             end
  416.                         end
  417.                     end
  418.                     ii=ii+1
  419.                 end
  420.                 ii=ii-i
  421.                 i=i+ii
  422.             else
  423.                 local ii=i+2
  424.                 funcStart=false
  425.                 local varType=""
  426.                 if toks[i]=="private" then
  427.                     parsedTokens[#parsedTokens+1]="local "
  428.                 elseif toks[i]=="public" then
  429.                     parsedTokens[#parsedTokens+1]=""
  430.                 end
  431.                 while true do
  432.                     if not (toks[ii]==" ") then
  433.                         if toks[ii]==";" then
  434.                             funcStart=false
  435.                             parsedTokens[#parsedTokens+1]=toks[ii]
  436.                             parsedTokens[#parsedTokens+1]="\n"
  437.                             funcStart=false
  438.                             break
  439.                         elseif toks[ii]=="|" then
  440.                             parsedTokens[#parsedTokens+1]=" or "
  441.                             ii=ii+1
  442.                         elseif toks[ii]=="new" then
  443.                             local iii=ii+2
  444.                             parsedTokens[#parsedTokens+1]="new("
  445.                             local startCapture=false
  446.                             local capturedTable={}
  447.                             local startStr=false
  448.                             local strCount=0
  449.                             while true do
  450.                                 if toks[iii]==";" then
  451.                                     if #capturedTable>0 then
  452.                                         for ps=1, #capturedTable do
  453.                                             parsedTokens[#parsedTokens+1]=capturedTable[ps]
  454.                                         end
  455.                                         parsedTokens[#parsedTokens+1]=")"
  456.                                     else
  457.                                         parsedTokens[#parsedTokens+1]="nil"
  458.                                         parsedTokens[#parsedTokens+1]=")"
  459.                                         parsedTokens[#parsedTokens+1]=toks[iii]
  460.                                     end
  461.                                     break
  462.                                 elseif toks[iii]=="\n" then
  463.                                     term.setTextColor(colors.red);
  464.                                     print("Can't create a new class object.")
  465.                                     return nil;
  466.                                 else
  467.                                     if (toks[iii]=="(" or toks[iii]==")") then
  468.                                         startCapture=not startCapture
  469.                                         parsedTokens[#parsedTokens+1]=""
  470.                                     else
  471.                                         if startCapture then
  472.                                             if toks[iii]=="\"" then
  473.                                                 capturedTable[#capturedTable+1]="\""
  474.                                             else
  475.                                                 capturedTable[#capturedTable+1]=toks[iii]
  476.                                             end
  477.                                         else
  478.                                             parsedTokens[#parsedTokens+1]="\""
  479.                                             parsedTokens[#parsedTokens+1]=toks[iii]
  480.                                             parsedTokens[#parsedTokens+1]="\""
  481.                                             parsedTokens[#parsedTokens+1]=","
  482.                                         end
  483.                                     end
  484.                                 end
  485.                                 iii=iii+1
  486.                             end
  487.                             iii=iii-ii
  488.                             ii=ii+iii-1
  489.                             break;
  490.                         elseif toks[ii]=="&" then
  491.                             parsedTokens[#parsedTokens+1]=" and "
  492.                             ii=ii+1
  493.                         elseif toks[ii]=="\n" then
  494.                             term.setTextColor(colors.red)
  495.                             print("No semicolon at the end of variable.")
  496.                             return nil
  497.                         end
  498.                         parsedTokens[#parsedTokens+1]=toks[ii]
  499.                     end
  500.                     ii=ii+1
  501.                 end
  502.                 ii=ii-i
  503.                 i=i+ii+1
  504.             end
  505.         else
  506.             if not (toks[i]=="private" or toks[i]=="public") then
  507.                 if toks[i]=="if" then
  508.                     local pos=i
  509.                     while true do
  510.                         if toks[pos]=="{" then
  511.                             endClassCount=endClassCount+1
  512.                             parsedTokens[#parsedTokens+1]="then"
  513.                             break
  514.                         elseif toks[pos]=="!" then
  515.                             if toks[pos+1]=="=" then
  516.                                 parsedTokens[#parsedTokens+1]="~="
  517.                                 pos=pos+1
  518.                             else
  519.                                 parsedTokens[#parsedTokens+1]=" not "
  520.                             end
  521.                         else
  522.                             parsedTokens[#parsedTokens+1]=toks[pos]
  523.                         end
  524.                         pos=pos+1
  525.                     end
  526.                     pos=pos-i
  527.                     i=i+pos
  528.                 elseif toks[i]=="while" then
  529.                     local pos=i
  530.                     while true do
  531.                         if toks[pos]=="{" then
  532.                             endClassCount=endClassCount+1
  533.                             parsedTokens[#parsedTokens+1]="do"
  534.                             break
  535.                         elseif toks[pos]=="!" then
  536.                             if toks[pos+1]=="=" then
  537.                                 parsedTokens[#parsedTokens+1]="~="
  538.                                 pos=pos+1
  539.                             else
  540.                                 parsedTokens[#parsedTokens+1]=" not "
  541.                             end
  542.                         else
  543.                             parsedTokens[#parsedTokens+1]=toks[pos]
  544.                         end
  545.                         pos=pos+1
  546.                     end
  547.                     pos=pos-i
  548.                     i=i+pos
  549.                 elseif toks[i]=="for" then
  550.                     local pos=i
  551.                     while true do
  552.                         if toks[pos]=="{" then
  553.                             endClassCount=endClassCount+1
  554.                             print(endClassCount)
  555.                             parsedTokens[#parsedTokens+1]="do"
  556.                             break
  557.                         elseif (toks[pos]=="(" or toks[pos]==")") then
  558.                             parsedTokens[#parsedTokens+1]=" "
  559.                         elseif toks[pos]=="!" then
  560.                             if toks[pos+1]=="=" then
  561.                                 parsedTokens[#parsedTokens+1]="~="
  562.                                 pos=pos+1
  563.                             else
  564.                                 parsedTokens[#parsedTokens+1]=" not "
  565.                             end
  566.                         else
  567.                             parsedTokens[#parsedTokens+1]=toks[pos]
  568.                         end
  569.                         pos=pos+1
  570.                     end
  571.                     pos=pos-i
  572.                     i=i+pos
  573.                 elseif toks[i]=="}" then
  574.                     endClassCount=endClassCount-1
  575.                     parsedTokens[#parsedTokens+1]="end"
  576.                     if endClassCount<1 then
  577.                         if foundExtends then
  578.                             parsedTokens[#parsedTokens+1]=",\""..extendsTo.."\""
  579.                             foundExtends=false
  580.                             endClassCount=0
  581.                         end
  582.                         parsedTokens[#parsedTokens+1]=")"
  583.                     end
  584.                 else
  585.                     if toks[i]=="/" then
  586.                         if toks[i+1]=="/" then
  587.                             parsedTokens[#parsedTokens+1]="--"
  588.                         elseif toks[i+1]=="*" then
  589.                             while true do
  590.                                 if toks[i]=="*" then
  591.                                     if toks[i+1]=="/" then break; end
  592.                                 end
  593.                                 i=i+1
  594.                             end
  595.                         end
  596.                     elseif toks[i]=="\"" then
  597.                         startString = not startString
  598.                         parsedTokens[#parsedTokens+1]=toks[i]
  599.                     elseif toks[i]=="!" and not startString then
  600.                         if toks[i+1]=="=" then
  601.                             parsedTokens[#parsedTokens+1]="~="
  602.                             i=i+1
  603.                         else
  604.                             parsedTokens[#parsedTokens+1]=" not "
  605.                         end
  606.                     elseif toks[i]=="|" and not startString then
  607.                         parsedTokens[#parsedTokens+1]=" or "
  608.                     elseif toks[i]=="&" and not startString then
  609.                         parsedTokens[#parsedTokens+1]=" and "
  610.                     elseif toks[i]=="/" then
  611.                         if toks[i+1]=="/" then
  612.                             parsedTokens[#parsedTokens+1]="--"
  613.                         end
  614.                     elseif toks[i]=="+" or toks[i]=="-" or toks[i]=="/" or toks[i]=="*" or toks[i]=="%" then
  615.                     elseif toks[i]=="using" then
  616.                         local pos=i+2
  617.                         parsedTokens[#parsedTokens+1]="using(\""
  618.                         while true do
  619.                             if toks[pos]==";" then
  620.                                 parsedTokens[#parsedTokens+1]="\");"
  621.                                 break
  622.                             elseif toks[pos]=="\n" then
  623.                                 printError("No semicolon found.")
  624.                                 return nil;
  625.                             else
  626.                                 parsedTokens[#parsedTokens+1]=toks[pos]
  627.                             end
  628.                             pos=pos+1
  629.                         end
  630.                         pos=pos-i
  631.                         i=i+pos
  632.                     elseif toks[i]=="=" then
  633.                         if toks[i-1]=="+" then
  634.                             local varNamePos=i-2
  635.                             if toks[i-2]==" " then
  636.                                 varNamePos=i-3
  637.                             end
  638.                             parsedTokens[#parsedTokens+1]="="
  639.                             parsedTokens[#parsedTokens+1]=toks[varNamePos]
  640.                             parsedTokens[#parsedTokens+1]="+"
  641.                         elseif toks[i-1]=="-" then
  642.                             local varNamePos=i-2
  643.                             if toks[i-2]==" " then
  644.                                 varNamePos=i-3
  645.                             end
  646.                             parsedTokens[#parsedTokens+1]="="
  647.                             parsedTokens[#parsedTokens+1]=toks[varNamePos]
  648.                             parsedTokens[#parsedTokens+1]="-"
  649.                         elseif toks[i-1]=="/" then
  650.                             local varNamePos=i-2
  651.                             if toks[i-2]==" " then
  652.                                 varNamePos=i-3
  653.                             end
  654.                             parsedTokens[#parsedTokens+1]="="
  655.                             parsedTokens[#parsedTokens+1]=toks[varNamePos]
  656.                             parsedTokens[#parsedTokens+1]="/"
  657.                         elseif toks[i-1]=="*" then
  658.                             local varNamePos=i-2
  659.                             if toks[i-2]==" " then
  660.                                 varNamePos=i-3
  661.                             end
  662.                             parsedTokens[#parsedTokens+1]="="
  663.                             parsedTokens[#parsedTokens+1]=toks[varNamePos]
  664.                             parsedTokens[#parsedTokens+1]="*"
  665.                         elseif toks[i-1]=="%" then
  666.                             local varNamePos=i-2
  667.                             if toks[i-2]==" " then
  668.                                 varNamePos=i-3
  669.                             end
  670.                             parsedTokens[#parsedTokens+1]="="
  671.                             parsedTokens[#parsedTokens+1]=toks[varNamePos]
  672.                             parsedTokens[#parsedTokens+1]="%"
  673.                         else
  674.                             parsedTokens[#parsedTokens+1]=toks[i]
  675.                         end
  676.                     else
  677.                         if toks[i]=="/" then
  678.                             if toks[i+1]=="/" then
  679.                                 parsedTokens[#parsedTokens+1]="--"
  680.                             elseif toks[i+1]=="*" then
  681.                                 while true do
  682.                                     if toks[i]=="*" then
  683.                                         if toks[i+1]=="/" then break; end
  684.                                     end
  685.                                     i=i+1
  686.                                 end
  687.                             end
  688.                         elseif toks[i]=="(" then
  689.                             local pos=i
  690.                             while true do
  691.                                 if toks[pos]==")" then
  692.                                     if toks[pos+1]==";" then
  693.                                         break;
  694.                                     elseif toks[pos+1]=="\n" then
  695.                                         term.setTextColor(colors.red)
  696.                                         print("No semicolon found at the end of function.");
  697.                                         return nil;
  698.                                     end
  699.                                 end
  700.                                 pos=pos+1
  701.                             end
  702.                         end
  703.                         parsedTokens[#parsedTokens+1]=toks[i]
  704.                     end
  705.                 end
  706.             end
  707.         end
  708.         i=i+1
  709.     end
  710.     return parsedTokens
  711. end
  712.  
  713. if #tArgs<2 then
  714.     pri("Usage:")
  715.     pri("compile -f <path> [ -p pack1 pack2 ... ]")
  716.     return;
  717. end
  718.  
  719. local filename=""
  720. local lookingForPacks=false
  721. local packs={}
  722. local addClasses=true;
  723.  
  724. for i=1, #tArgs do
  725.     if tArgs[i]=="-f" then
  726.         lookingForPacks=false
  727.         filename=tArgs[i+1]
  728.     elseif tArgs[i]=="-acls" then
  729.         lookingForPacks=false
  730.         addClasses=tArgs[i+1]
  731.     end
  732.     if lookingForPacks then
  733.         packs[#packs+1]=tArgs[i]
  734.     end
  735.     if tArgs[i]=="-p" then
  736.         lookingForPacks=true
  737.     end
  738. end
  739.  
  740. if not fsexists(filename) then
  741.     prErr("File: "..filename.." does not exist!")
  742.     return;
  743. end
  744.  
  745. local time = osTime()
  746.  
  747. if (addClasses==true or addClasses=="true") then
  748.     tClear()
  749.     tSetCursorPos(1,1)
  750.     pri("JaC compiler.")
  751.     pri("JaC version: "..ver)
  752.     pri("Compiling: "..filename)
  753.     if #packs>0 then
  754.         local str=""
  755.         for i=1, #packs do
  756.             str=str..packs[i].." ; "
  757.         end
  758.         pri("Compiling packs: "..str)
  759.     end
  760. end
  761.  
  762. local file=fsopen(filename, "r")
  763. local contents = file.readAll().."\n"
  764. file.close()
  765.  
  766. local toks=tokenize(contents)
  767.  
  768. --printTable(toks)
  769.  
  770. local parsedToks=parse(toks)
  771.  
  772. if not parsedToks then
  773.     tSetTextColor(colors.red)
  774.     pri("Compiling error.")
  775.     return;
  776. end
  777.  
  778. --printTable(parsedToks)
  779.  
  780. local strToks="";
  781.  
  782. if addClasses==true or addClasses=="true" then
  783.     strToks=classSystem
  784. end
  785.  
  786. --printTable(packs)
  787.  
  788. for i=1, #packs do
  789.     shell.run(shell.getRunningProgram(), "-acls", "false", "-f", packs[i])
  790.     local file = fs.open(packs[i]..".lua", "r")
  791.     local content = file.readAll()
  792.     file.close()
  793.     strToks=strToks..content.."\n\n"
  794.     fs.delete(packs[i]..".lua")
  795. end
  796.  
  797. for i=1, #parsedToks do
  798.     strToks=strToks..tstring(parsedToks[i])
  799. end
  800.  
  801. local file=fsopen(filename..".lua", "w")
  802. file.write(strToks)
  803. file.close()
  804.  
  805. local newTime = osTime()
  806. if addClasses==true or addClasses=="true" then
  807.     pri("Compiled: \""..filename.."\" as \""..filename..".lua\"")
  808.     pri("Started at: "..time)
  809.     pri("Ended at: "..newTime)
  810.     pri("Compiling time: "..newTime-time)
  811. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement