Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cppl = {}
- Cppl.convert = function(CppProgramFilename,Args,ToFile)
- program = ""
- io = require("io");
- string = require("string");
- LOG = ""
- case = false
- func = 0
- stringnum = 1
- if Args ~= nil then
- x = string.gfind(Args,"%-.*%s")
- while true do
- sh = x()
- if sh == "-noprint " then
- noprint = true
- end
- if sh == "-tofile " then
- tofile = true
- end
- if sh == "-printlog " then
- printlog = true
- end
- if sh == "-returnlog " then
- returnlog = true
- end
- if sh == nil then
- break
- end
- end
- end
- includeList = {}
- --C++ libs
- includeList["<fstream>"] = "--fstream in process"
- includeList["<iomanip>"] = "--iomanip in process"
- includeList["<ios>"] = "--ios in process"
- includeList["<iostream>"] = "io = require(\"io\")"
- includeList["<sstream>"] = "--sstream in process"
- includeList["<Cstring>"] = "--Cstring (C++ String) lib in process"
- --default lua libs
- includeList["<io>"] = "io = require(\"io\")"
- includeList["<string>"] = "string = require(\"string\")"
- includeList["<table>"] = "table = require(\"table\")"
- --Opencomputers libs
- includeList["<colors>"] = "colors = require(\"colors\")"
- includeList["<component>"] = "component = require(\"colors\")"
- includeList["<computer>"] = "computer = require(\"computer\")"
- includeList["<event>"] = "event = require(\"event\")"
- includeList["<filesystem>"] = "filesystem = require(\"filesystem\")"
- includeList["<gpu>"] = "gpu = require(\"component\").gpu"
- includeList["<internet>"] = "internet = require(\"internet\")"
- includeList["<keyboard>"] = "keyboard = require(\"component\").keyboard"
- includeList["<note>"] = "note = require(\"note\")"
- includeList["<process>"] = "process = require(\"process\")"
- includeList["<robot>"] = "robot = require(\"robot\")"
- includeList["<serialization>"] = "serialization = require(\"serialization\")"
- includeList["<shell>"] = "shell = require(\"shell\")"
- includeList["<term>"] = "term = require(\"term\")"
- includeList["<text>"] = "text = require(\"text\")"
- includeList["<unicode>"] = "unicode = require(\"unicode\")"
- Classes = {}
- Classes["int"] = "0"
- Classes["float"] = "0.0"
- Classes["double"] = "0.0"
- Classes["char"] = "\"\""
- Classes["bool"] = "false"
- Classes["array"]= "table"
- Class = ""
- for line in io.lines(CppProgramFilename) do
- edit = true
- if line == "using namespase std;" or line == "{" or line == "!=" or line == "null" then
- edit = false
- end
- if string.sub(line,1,5) == "%lua:" then
- line = string.sub(line,6,#line)
- edit = false
- end
- if case == true then
- edit = false
- end
- if string.sub(line,1,5) == "%lua*" then
- line = string.sub(line,6,#line)
- case = true
- edit = false
- end
- if string.sub(line,1,5) == "*lua%" then
- line = string.sub(line,6,#line)
- case = false
- edit = false
- end
- if edit then
- line = string.gsub(line,"using namespase std;","")
- line = string.gsub(line,"{","")
- line = string.gsub(line,"!=","~=")
- line = string.gsub(line,"null","nil")
- line = string.gsub(line,"endl","\"\\n\"")
- line = string.gsub(line,"<<","..")
- line = string.gsub(line,"std::","")
- line = string.gsub(line," & "," and ")
- line = string.gsub(line," | "," or ")
- line = string.gsub(line," ^ "," xor ")
- line = string.gsub(line,"new","")
- end
- if edit then
- x,y = string.find(line,"Class ")
- if x ~= nil then
- func = 0
- f = string.sub(line,y+1,#line)
- Class = string.gsub(f,"{","")
- Classes[Class] = "()"
- line = Class.." = {nil\n"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"while")
- if x ~= nil then
- if Class ~= "" then
- func = func+1
- end
- line = string.gsub(line,"%("," ")
- line = string.gsub(line,"%)"," ")
- line = line.."do"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"for")
- if x ~= nil then
- if Class ~= "" then
- func = func+1
- end
- line = string.gsub(line,"%("," ")
- line = string.gsub(line,"%)"," ")
- line = line.."do"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"}elseif")
- if x ~= nil then
- line = string.gsub(line,"}elseif","ELSEIF")
- line = string.gsub(line,"%("," ")
- line = string.gsub(line,"%)"," ")
- line = line.."then"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"if")
- if x ~= nil then
- if Class ~= "" then
- func = func+1
- end
- line = string.gsub(line,"%("," ")
- line = string.gsub(line,"%)"," ")
- line = line.."then"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"cout")
- if x ~= nil then
- line = string.gsub(line,";","")
- line = string.gsub(line,"cout","io.write(\"\"")
- line = line..")"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"cin")
- if x ~= nil then
- line = string.gsub(line,";","")
- x = string.find(line,">>")
- line = string.sub(line,x+2,#line).." = io.read();"
- edit = false
- end
- end
- if edit then
- x,y = string.find(line,"}")
- if x ~= nil then
- if Class ~= "" and line == "};" then
- line = "}\nsetmetatable("..Class..","..Class..")"
- Class = ""
- else
- line = string.gsub(line,"}else"," else ")
- line = string.gsub(line,"}elseif"," elseif ")
- x = string.gfind(line,"}")
- while true do
- if x() ~= nil then
- func = func-1
- else
- break
- end
- end
- line = string.gsub(line,"}"," end ")
- end
- edit = false
- end
- end
- if edit then
- for k,v in pairs(Classes) do
- x,y = string.find(line,k.."%s.*%(.*%)%s*%{*")
- if x ~= nil then
- if Class == "" then
- line = "function "..string.sub(line,x+#k,y)
- else
- func = func+1
- str = string.sub(line,x+#k,y)
- x,y = string.find(str,".*%(")
- xx,yy = string.find(line,"%(.*%)")
- str = string.sub(str,x,y)
- line = ",\n"..string.gsub(str,"%(","").." = function"..string.sub(line,xx,yy+2)
- end
- edit = false
- end
- end
- end
- if edit then
- x,y = string.find(line,"return")
- if x ~= nil then
- edit = false
- end
- end
- if edit then
- str = string.gsub(line,";","")
- x,y = string.find(line,"delete")
- if x ~= nil then
- line = string.gsub(str,"delete","")
- x = string.gfind(line,",")
- xx = true
- cou = 1
- while xx do
- if x() ~= nil then
- cou = cou+1
- else
- xx = false
- end
- end
- line = line.."= nil"
- for i=1,cou-1 do
- line = line..",nil"
- end
- edit = false
- end
- end
- if edit then
- for k,v in pairs(Classes) do
- xx,yy = string.find(line,k.."%*%s*.*;")
- x,y = string.find(line,k.."%s*.*;")
- if x == nil then
- k = "array"
- v = "table"
- x = xx;
- y = yy;
- end
- if x ~= nil then
- x,y = string.find(line,"%s.*;")
- if x ~= nil then
- line = string.sub(line,x,y)
- end
- if v == "()" then
- line = string.gsub(line,"[%;%*]","").." = "..k..v
- else
- x = string.gfind(line,",")
- xx = true
- cou = 1
- while xx do
- if x() ~= nil then
- cou = cou+1
- else
- xx = false
- end
- end
- line = string.gsub(line,"[%;%*]","")
- x,y = string.find(line,k.."%[.*%]")
- if x ~= nil then
- str = string.sub(line,x,y)
- str = string.gsub(line,"%D","")
- line = string.sub(line,1,x-2).."table;for for_i=1,"..str.." do "..string.gsub(string.sub(line,1,x-2),"%=","[for_i]=")..Classes[k].." end"
- else
- x = string.find(line,"%=")
- if x ~= nil then
- line = string.sub(line,1,x-1).." = "
- line = line..""..v
- for i=1,cou-1 do
- line = line..","..v
- end
- else
- line = line.." = "..Classes[k]
- end
- end
- end
- if Class ~= "" and func == 0 then
- line = ",\n"..line
- end
- edit = false
- end
- end
- end
- if edit then
- x,y = string.find(line,"%#.*% ")
- if x ~= nil then
- f = string.sub(line,x,y)
- if f == "#include " then
- x,y = string.find(line,"%<.*%>")
- lib = string.sub(line,x,y);
- x = string.find(line,"%.%h")
- if x == nil then
- if Class == "" then
- line = includeList[lib]
- else
- line = Class.."."..includeList[lib]
- end
- else
- line = string.gsub(lib,"[<>]","")
- file = io.open("lib/CpplLibs/"..line,"r")
- line = ""
- g = true
- while g do
- filledata = file:read()
- if filledata == nil then
- g = false
- else
- line = line..filledata.."\n"
- end
- end
- end
- edit = false
- end
- end
- end
- if line ~= nil then
- line = string.gsub(line,"}ELSEIF","elseif")
- line = string.gsub(line,"}elseif","elseif")
- line = string.gsub(line,"ELSEIF","elseif")
- line = string.gsub(line," "," ")
- program = program..line.."\n"
- end
- if edit == true then
- LOG = LOG.."No convert string: "..stringnum.." { "..line.." }\n"
- end
- stringnum = stringnum+1
- if noprint == nil then
- if edit == true and line ~= "" then
- print("Noedit:"..line)
- else
- print(line)
- end
- end
- end
- program = program.."main()"
- if printlog then
- print(LOG)
- end
- if tofile == nil then
- if retunlog then
- return program,LOG
- else
- return program
- end
- else
- file = io.open(ToFile,"w")
- file:write(program)
- file:close()
- if returnlog then
- return LOG
- end
- end
- end
- return Cppl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement