Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs={ ... }
- local pri=print
- local tstring=tostring
- local erro=error
- local _PRS=pairs
- local lfile=loadfile
- local tClear=term.clear
- local tSetCursorPos=term.setCursorPos
- local tSetTextColor=term.setTextColor
- local pcl=pcall
- local fsexists=fs.exists
- local fsopen=fs.open
- local osTime=os.time
- local prErr=printError
- local ver="15.10.12"
- local classSystem = [[
- local Packages={}
- local Extends={
- __TERM=term;
- __SHELL=shell;
- __OS=os;
- __COLORS=colors;
- __FS=fs;
- }
- function Package(func)
- local pkg={}
- pkg.Classes={}
- pkg.print=print
- pkg.read=read
- pkg.write=write
- pkg.Class=function(func, ext)
- local cls={ print=pkg.print; read=pkg.read; write=pkg.write; }
- cls.using=function(name)
- if Packages[name] then
- for k, v in pairs(Packages[name]["Classes"]) do
- if not pkg["Classes"][k] then
- pkg["Classes"][k]=v
- end
- end
- else
- printError("Package "..name.." does not exist.")
- return nil;
- end
- end
- cls.new=function(name, ...)
- if pkg.Classes[name] then
- if pkg.Classes[name][name] then
- local newClass={}
- for k, v in pairs(pkg.Classes[name]) do
- newClass[k]=v
- end
- return newClass[name](...)
- else
- printError("Class "..name.." constructor does not exist.")
- return nil;
- end
- else
- printError("Class "..name.." does not exist.")
- return nil;
- end
- end
- if Extends[ext] then
- for k, v in pairs(Extends[ext]) do
- cls[k]=v
- end
- elseif pkg.Classes[ext] then
- cls[ext]={}
- for k, v in pairs(pkg.Classes[ext]) do
- cls[ext][k]=v
- end
- end
- setfenv(func, cls)
- func(cls)
- return cls
- end
- setfenv(func, pkg)
- func()
- local pkgMainCount=0
- if pkg.Classes.Main then
- if pkg.Classes.Main.Main then
- pkg.Classes.Main.Main()
- end
- end
- return pkg
- end
- Packages["craftOS"]=Package(function()
- Classes["term"]=Class(function(this)
- function term()
- return this
- end
- end, "__TERM")
- Classes["os"]=Class(function(this)
- function os()
- return this
- end
- end, "__OS")
- Classes["shell"]=Class(function(this)
- function shell()
- return this
- end
- end, "__SHELL")
- Classes["colors"]=Class(function(this)
- function colors()
- return this
- end
- end, "__COLORS")
- Classes["fs"]=Class(function(this)
- function fs()
- return this
- end
- end, "__FS")
- end)
- --Main code
- ]]
- local function printTable(tbl)
- local str=""
- for k, v in _PRS(tbl) do
- if k then
- str=str..tstring(v)
- end
- end
- pri(tstring(str))
- end
- local symbols={}
- symbols["("]=true
- symbols["{"]=true
- symbols["}"]=true
- symbols[")"]=true
- symbols[","]=true
- symbols["."]=true
- symbols["+"]=true
- symbols["-"]=true
- symbols["/"]=true
- symbols["*"]=true
- symbols[";"]=true
- symbols["<"]=true
- symbols[">"]=true
- symbols["\n"]=true
- symbols["="]=true
- symbols[" "]=true
- symbols["\t"]=true
- symbols["\""]=true
- symbols["\'"]=true
- symbols["|"]=true
- symbols["&"]=true
- symbols["!"]=true
- function tokenize(file)
- local tokens={}
- local keyword=""
- local lt=""
- local ignoreSpace=false
- for i=1, #file do
- local lt=file:sub(i,i)
- keyword=keyword..lt
- if symbols[lt]==true and not ignoreSpace then
- if #keyword>1 then
- tokens[#tokens+1]=keyword:sub(1, #keyword-1)
- end
- tokens[#tokens+1]=lt
- keyword=""
- elseif symbols[lt]==false then
- ignoreSpace = not ignoreSpace
- end
- end
- return tokens
- end
- local function parse(toks)
- local i=1
- local parsedTokens={}
- local endClassCount=0
- local addThen=false
- local addDo=false
- local startString=false
- local startingClass=false
- local foundExtends=false
- local extendsTo=""
- local funcStart=false
- while i<=#toks do
- if toks[i]=="package" then
- parsedTokens[#parsedTokens+1]="Packages[\""..toks[i+2].."\"]"
- parsedTokens[#parsedTokens+1]="=Package(function()"
- if toks[i+4]=="{" then
- parsedTokens[#parsedTokens+1]=""
- else
- printError("Cant create a package.")
- return nil;
- end
- i=i+4
- elseif toks[i]=="class" then
- parsedTokens[#parsedTokens+1]="Classes[\""..toks[i+2].."\"]"
- parsedTokens[#parsedTokens+1]="=Class(function(this)"
- if toks[i+4]=="extends" then
- foundExtends=true;
- extendsTo=toks[i+6]
- end
- if foundExtends then
- if toks[i+8]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]=""
- else
- term.setTextColor(colors.red)
- print("Can't start a class!");
- return nil;
- end
- else
- if toks[i+4]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]=""
- else
- term.setTextColor(colors.red)
- print("Can't start a class!")
- return nil;
- end
- end
- if foundExtends then
- i=i+8
- else
- i=i+4
- end
- elseif (toks[i]=="public" or toks[i]=="private") then
- if toks[i+2]=="function" then
- local ii=i+2
- local step=0
- if toks[i]=="private" then
- parsedTokens[#parsedTokens+1]="local "
- end
- local isIf=false
- local ifCount=0
- while ii<=#toks do
- if toks[ii]=="if" then
- local pos=ii
- while true do
- if toks[pos]=="{" then
- parsedTokens[#parsedTokens+1]="then"
- endClassCount=endClassCount+1
- break
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-ii
- ii=ii+pos+1
- elseif toks[ii]=="while" then
- local pos=ii
- while true do
- if toks[pos]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]="do"
- break
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-ii
- ii=ii+pos+1
- elseif toks[ii]=="for" then
- local pos=ii
- while true do
- if toks[pos]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]="do"
- break
- elseif (toks[pos]=="(" or toks[pos]==")") then
- parsedTokens[#parsedTokens+1]=" "
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-ii
- ii=ii+pos+1
- end
- if toks[ii]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]=""
- elseif toks[ii]=="}" then
- endClassCount=endClassCount-1
- if isIf==false and ifCount==0 then
- parsedTokens[#parsedTokens+1]="end"
- break
- else
- parsedTokens[#parsedTokens+1]="end"
- isIf=false
- ifCount=ifCount-1
- end
- else
- if (toks[ii]=="function" and step==0) then
- step=step+1
- end
- if (toks[ii]==" " and step==1) then
- step=step+1
- end
- if toks[ii]=="/" then
- if toks[ii+1]=="/" then
- parsedTokens[#parsedTokens+1]="--"
- elseif toks[ii+1]=="*" then
- while true do
- if toks[ii]=="*" then
- if toks[ii+1]=="/" then break; end
- else
- ii=ii+1
- end
- end
- end
- elseif toks[ii]=="\"" then
- parsedTokens[#parsedTokens+1]=toks[ii]
- startString = not startString
- elseif toks[ii]=="!" and not startString then
- if toks[ii+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- ii=ii+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- elseif toks[ii]=="|" and not startString then
- parsedTokens[#parsedTokens+1]=" or "
- elseif toks[ii]=="&" and not startString then
- parsedTokens[#parsedTokens+1]=" and "
- else
- if toks[ii]=="private" then
- parsedTokens[#parsedTokens+1]="local "
- ii=ii+1
- while true do
- if not (toks[ii]==" ") then
- if toks[ii]==";" then
- parsedTokens[#parsedTokens+1]=toks[ii]
- funcStart=false
- break
- elseif toks[ii]=="\"" then
- startString = not startString
- elseif toks[ii]=="|" and not startString then
- parsedTokens[#parsedTokens+1]=" or "
- ii=ii+1
- elseif toks[ii]=="&" and not startString then
- parsedTokens[#parsedTokens+1]=" and "
- ii=ii+1
- elseif toks[ii]=="\n" then
- term.setTextColor(colors.red)
- print("No semicolon at the end of variable.")
- return nil
- end
- parsedTokens[#parsedTokens+1]=toks[ii]
- end
- ii=ii+1
- end
- ii=ii+1
- elseif toks[ii]=="public" then
- --parsedTokens[#parsedTokens+1]="_G."
- ii=ii+1
- while true do
- if not (toks[ii]==" ") then
- if toks[ii]==";" then
- parsedTokens[#parsedTokens+1]=toks[ii]
- funcStart=false
- break
- elseif toks[ii]=="\"" then
- startString = not startString
- elseif toks[ii]=="|" and not startString then
- parsedTokens[#parsedTokens+1]=" or "
- ii=ii+1
- elseif toks[ii]=="&" and not startString then
- parsedTokens[#parsedTokens+1]=" and "
- ii=ii+1
- elseif toks[ii]=="\n" then
- term.setTextColor(colors.red)
- print("No semicolon at the end of variable.")
- return nil
- end
- parsedTokens[#parsedTokens+1]=toks[ii]
- end
- ii=ii+1
- end
- ii=ii+1
- else
- parsedTokens[#parsedTokens+1]=toks[ii]
- end
- end
- if step==2 then
- if toks[i]=="public" then
- parsedTokens[#parsedTokens+1]=""
- step=0
- end
- end
- end
- ii=ii+1
- end
- ii=ii-i
- i=i+ii
- else
- local ii=i+2
- funcStart=false
- local varType=""
- if toks[i]=="private" then
- parsedTokens[#parsedTokens+1]="local "
- elseif toks[i]=="public" then
- parsedTokens[#parsedTokens+1]=""
- end
- while true do
- if not (toks[ii]==" ") then
- if toks[ii]==";" then
- funcStart=false
- parsedTokens[#parsedTokens+1]=toks[ii]
- parsedTokens[#parsedTokens+1]="\n"
- funcStart=false
- break
- elseif toks[ii]=="|" then
- parsedTokens[#parsedTokens+1]=" or "
- ii=ii+1
- elseif toks[ii]=="new" then
- local iii=ii+2
- parsedTokens[#parsedTokens+1]="new("
- local startCapture=false
- local capturedTable={}
- local startStr=false
- local strCount=0
- while true do
- if toks[iii]==";" then
- if #capturedTable>0 then
- for ps=1, #capturedTable do
- parsedTokens[#parsedTokens+1]=capturedTable[ps]
- end
- parsedTokens[#parsedTokens+1]=")"
- else
- parsedTokens[#parsedTokens+1]="nil"
- parsedTokens[#parsedTokens+1]=")"
- parsedTokens[#parsedTokens+1]=toks[iii]
- end
- break
- elseif toks[iii]=="\n" then
- term.setTextColor(colors.red);
- print("Can't create a new class object.")
- return nil;
- else
- if (toks[iii]=="(" or toks[iii]==")") then
- startCapture=not startCapture
- parsedTokens[#parsedTokens+1]=""
- else
- if startCapture then
- if toks[iii]=="\"" then
- capturedTable[#capturedTable+1]="\""
- else
- capturedTable[#capturedTable+1]=toks[iii]
- end
- else
- parsedTokens[#parsedTokens+1]="\""
- parsedTokens[#parsedTokens+1]=toks[iii]
- parsedTokens[#parsedTokens+1]="\""
- parsedTokens[#parsedTokens+1]=","
- end
- end
- end
- iii=iii+1
- end
- iii=iii-ii
- ii=ii+iii-1
- break;
- elseif toks[ii]=="&" then
- parsedTokens[#parsedTokens+1]=" and "
- ii=ii+1
- elseif toks[ii]=="\n" then
- term.setTextColor(colors.red)
- print("No semicolon at the end of variable.")
- return nil
- end
- parsedTokens[#parsedTokens+1]=toks[ii]
- end
- ii=ii+1
- end
- ii=ii-i
- i=i+ii+1
- end
- else
- if not (toks[i]=="private" or toks[i]=="public") then
- if toks[i]=="if" then
- local pos=i
- while true do
- if toks[pos]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]="then"
- break
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-i
- i=i+pos
- elseif toks[i]=="while" then
- local pos=i
- while true do
- if toks[pos]=="{" then
- endClassCount=endClassCount+1
- parsedTokens[#parsedTokens+1]="do"
- break
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-i
- i=i+pos
- elseif toks[i]=="for" then
- local pos=i
- while true do
- if toks[pos]=="{" then
- endClassCount=endClassCount+1
- print(endClassCount)
- parsedTokens[#parsedTokens+1]="do"
- break
- elseif (toks[pos]=="(" or toks[pos]==")") then
- parsedTokens[#parsedTokens+1]=" "
- elseif toks[pos]=="!" then
- if toks[pos+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- pos=pos+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-i
- i=i+pos
- elseif toks[i]=="}" then
- endClassCount=endClassCount-1
- parsedTokens[#parsedTokens+1]="end"
- if endClassCount<1 then
- if foundExtends then
- parsedTokens[#parsedTokens+1]=",\""..extendsTo.."\""
- foundExtends=false
- endClassCount=0
- end
- parsedTokens[#parsedTokens+1]=")"
- end
- else
- if toks[i]=="/" then
- if toks[i+1]=="/" then
- parsedTokens[#parsedTokens+1]="--"
- elseif toks[i+1]=="*" then
- while true do
- if toks[i]=="*" then
- if toks[i+1]=="/" then break; end
- end
- i=i+1
- end
- end
- elseif toks[i]=="\"" then
- startString = not startString
- parsedTokens[#parsedTokens+1]=toks[i]
- elseif toks[i]=="!" and not startString then
- if toks[i+1]=="=" then
- parsedTokens[#parsedTokens+1]="~="
- i=i+1
- else
- parsedTokens[#parsedTokens+1]=" not "
- end
- elseif toks[i]=="|" and not startString then
- parsedTokens[#parsedTokens+1]=" or "
- elseif toks[i]=="&" and not startString then
- parsedTokens[#parsedTokens+1]=" and "
- elseif toks[i]=="/" then
- if toks[i+1]=="/" then
- parsedTokens[#parsedTokens+1]="--"
- end
- elseif toks[i]=="+" or toks[i]=="-" or toks[i]=="/" or toks[i]=="*" or toks[i]=="%" then
- elseif toks[i]=="using" then
- local pos=i+2
- parsedTokens[#parsedTokens+1]="using(\""
- while true do
- if toks[pos]==";" then
- parsedTokens[#parsedTokens+1]="\");"
- break
- elseif toks[pos]=="\n" then
- printError("No semicolon found.")
- return nil;
- else
- parsedTokens[#parsedTokens+1]=toks[pos]
- end
- pos=pos+1
- end
- pos=pos-i
- i=i+pos
- elseif toks[i]=="=" then
- if toks[i-1]=="+" then
- local varNamePos=i-2
- if toks[i-2]==" " then
- varNamePos=i-3
- end
- parsedTokens[#parsedTokens+1]="="
- parsedTokens[#parsedTokens+1]=toks[varNamePos]
- parsedTokens[#parsedTokens+1]="+"
- elseif toks[i-1]=="-" then
- local varNamePos=i-2
- if toks[i-2]==" " then
- varNamePos=i-3
- end
- parsedTokens[#parsedTokens+1]="="
- parsedTokens[#parsedTokens+1]=toks[varNamePos]
- parsedTokens[#parsedTokens+1]="-"
- elseif toks[i-1]=="/" then
- local varNamePos=i-2
- if toks[i-2]==" " then
- varNamePos=i-3
- end
- parsedTokens[#parsedTokens+1]="="
- parsedTokens[#parsedTokens+1]=toks[varNamePos]
- parsedTokens[#parsedTokens+1]="/"
- elseif toks[i-1]=="*" then
- local varNamePos=i-2
- if toks[i-2]==" " then
- varNamePos=i-3
- end
- parsedTokens[#parsedTokens+1]="="
- parsedTokens[#parsedTokens+1]=toks[varNamePos]
- parsedTokens[#parsedTokens+1]="*"
- elseif toks[i-1]=="%" then
- local varNamePos=i-2
- if toks[i-2]==" " then
- varNamePos=i-3
- end
- parsedTokens[#parsedTokens+1]="="
- parsedTokens[#parsedTokens+1]=toks[varNamePos]
- parsedTokens[#parsedTokens+1]="%"
- else
- parsedTokens[#parsedTokens+1]=toks[i]
- end
- else
- if toks[i]=="/" then
- if toks[i+1]=="/" then
- parsedTokens[#parsedTokens+1]="--"
- elseif toks[i+1]=="*" then
- while true do
- if toks[i]=="*" then
- if toks[i+1]=="/" then break; end
- end
- i=i+1
- end
- end
- elseif toks[i]=="(" then
- local pos=i
- while true do
- if toks[pos]==")" then
- if toks[pos+1]==";" then
- break;
- elseif toks[pos+1]=="\n" then
- term.setTextColor(colors.red)
- print("No semicolon found at the end of function.");
- return nil;
- end
- end
- pos=pos+1
- end
- end
- parsedTokens[#parsedTokens+1]=toks[i]
- end
- end
- end
- end
- i=i+1
- end
- return parsedTokens
- end
- if #tArgs<2 then
- pri("Usage:")
- pri("compile -f <path> [ -p pack1 pack2 ... ]")
- return;
- end
- local filename=""
- local lookingForPacks=false
- local packs={}
- local addClasses=true;
- for i=1, #tArgs do
- if tArgs[i]=="-f" then
- lookingForPacks=false
- filename=tArgs[i+1]
- elseif tArgs[i]=="-acls" then
- lookingForPacks=false
- addClasses=tArgs[i+1]
- end
- if lookingForPacks then
- packs[#packs+1]=tArgs[i]
- end
- if tArgs[i]=="-p" then
- lookingForPacks=true
- end
- end
- if not fsexists(filename) then
- prErr("File: "..filename.." does not exist!")
- return;
- end
- local time = osTime()
- if (addClasses==true or addClasses=="true") then
- tClear()
- tSetCursorPos(1,1)
- pri("JaC compiler.")
- pri("JaC version: "..ver)
- pri("Compiling: "..filename)
- if #packs>0 then
- local str=""
- for i=1, #packs do
- str=str..packs[i].." ; "
- end
- pri("Compiling packs: "..str)
- end
- end
- local file=fsopen(filename, "r")
- local contents = file.readAll().."\n"
- file.close()
- local toks=tokenize(contents)
- --printTable(toks)
- local parsedToks=parse(toks)
- if not parsedToks then
- tSetTextColor(colors.red)
- pri("Compiling error.")
- return;
- end
- --printTable(parsedToks)
- local strToks="";
- if addClasses==true or addClasses=="true" then
- strToks=classSystem
- end
- --printTable(packs)
- for i=1, #packs do
- shell.run(shell.getRunningProgram(), "-acls", "false", "-f", packs[i])
- local file = fs.open(packs[i]..".lua", "r")
- local content = file.readAll()
- file.close()
- strToks=strToks..content.."\n\n"
- fs.delete(packs[i]..".lua")
- end
- for i=1, #parsedToks do
- strToks=strToks..tstring(parsedToks[i])
- end
- local file=fsopen(filename..".lua", "w")
- file.write(strToks)
- file.close()
- local newTime = osTime()
- if addClasses==true or addClasses=="true" then
- pri("Compiled: \""..filename.."\" as \""..filename..".lua\"")
- pri("Started at: "..time)
- pri("Ended at: "..newTime)
- pri("Compiling time: "..newTime-time)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement