Advertisement
SergOmarov

C++ to lua

Jul 12th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. Cppl = {}
  2. Cppl.convert = function(CppProgramFilename,Args,ToFile)
  3. program = ""
  4. io = require("io");
  5. string = require("string");
  6. LOG = ""
  7. case = false
  8. func = 0
  9. stringnum = 1
  10. if Args ~= nil then
  11. x = string.gfind(Args,"%-.*%s")
  12. while true do
  13. sh = x()
  14. if sh == "-noprint " then
  15. noprint = true
  16. end
  17. if sh == "-tofile " then
  18. tofile = true
  19. end
  20. if sh == "-printlog " then
  21. printlog = true
  22. end
  23. if sh == "-returnlog " then
  24. returnlog = true
  25. end
  26. if sh == nil then
  27. break
  28. end
  29. end
  30. end
  31.  
  32. includeList = {}
  33. --C++ libs
  34. includeList["<fstream>"] = "--fstream in process"
  35. includeList["<iomanip>"] = "--iomanip in process"
  36. includeList["<ios>"] = "--ios in process"
  37. includeList["<iostream>"] = "io = require(\"io\")"
  38. includeList["<sstream>"] = "--sstream in process"
  39. includeList["<Cstring>"] = "--Cstring (C++ String) lib in process"
  40. --default lua libs
  41. includeList["<io>"] = "io = require(\"io\")"
  42. includeList["<string>"] = "string = require(\"string\")"
  43. includeList["<table>"] = "table = require(\"table\")"
  44. --Opencomputers libs
  45. includeList["<colors>"] = "colors = require(\"colors\")"
  46. includeList["<component>"] = "component = require(\"colors\")"
  47. includeList["<computer>"] = "computer = require(\"computer\")"
  48. includeList["<event>"] = "event = require(\"event\")"
  49. includeList["<filesystem>"] = "filesystem = require(\"filesystem\")"
  50. includeList["<gpu>"] = "gpu = require(\"component\").gpu"
  51. includeList["<internet>"] = "internet = require(\"internet\")"
  52. includeList["<keyboard>"] = "keyboard = require(\"component\").keyboard"
  53. includeList["<note>"] = "note = require(\"note\")"
  54. includeList["<process>"] = "process = require(\"process\")"
  55. includeList["<robot>"] = "robot = require(\"robot\")"
  56. includeList["<serialization>"] = "serialization = require(\"serialization\")"
  57. includeList["<shell>"] = "shell = require(\"shell\")"
  58. includeList["<term>"] = "term = require(\"term\")"
  59. includeList["<text>"] = "text = require(\"text\")"
  60. includeList["<unicode>"] = "unicode = require(\"unicode\")"
  61.  
  62. Classes = {}
  63. Classes["int"] = "0"
  64. Classes["float"] = "0.0"
  65. Classes["double"] = "0.0"
  66. Classes["char"] = "\"\""
  67. Classes["bool"] = "false"
  68. Classes["array"]= "table"
  69. Class = ""
  70. for line in io.lines(CppProgramFilename) do
  71. edit = true
  72. if line == "using namespase std;" or line == "{" or line == "!=" or line == "null" then
  73. edit = false
  74. end
  75.  
  76. if string.sub(line,1,5) == "%lua:" then
  77. line = string.sub(line,6,#line)
  78. edit = false
  79. end
  80.  
  81. if case == true then
  82. edit = false
  83. end
  84.  
  85. if string.sub(line,1,5) == "%lua*" then
  86. line = string.sub(line,6,#line)
  87. case = true
  88. edit = false
  89. end
  90.  
  91. if string.sub(line,1,5) == "*lua%" then
  92. line = string.sub(line,6,#line)
  93. case = false
  94. edit = false
  95. end
  96.  
  97. if edit then
  98. line = string.gsub(line,"using namespase std;","")
  99. line = string.gsub(line,"{","")
  100. line = string.gsub(line,"!=","~=")
  101. line = string.gsub(line,"null","nil")
  102. line = string.gsub(line,"endl","\"\\n\"")
  103. line = string.gsub(line,"<<","..")
  104. line = string.gsub(line,"std::","")
  105. line = string.gsub(line," & "," and ")
  106. line = string.gsub(line," | "," or ")
  107. line = string.gsub(line," ^ "," xor ")
  108. line = string.gsub(line,"new","")
  109. end
  110.  
  111. if edit then
  112. x,y = string.find(line,"Class ")
  113. if x ~= nil then
  114. func = 0
  115. f = string.sub(line,y+1,#line)
  116. Class = string.gsub(f,"{","")
  117. Classes[Class] = "()"
  118. line = Class.." = {nil\n"
  119. edit = false
  120. end
  121. end
  122.  
  123. if edit then
  124. x,y = string.find(line,"while")
  125. if x ~= nil then
  126. if Class ~= "" then
  127. func = func+1
  128. end
  129. line = string.gsub(line,"%("," ")
  130. line = string.gsub(line,"%)"," ")
  131. line = line.."do"
  132. edit = false
  133. end
  134. end
  135.  
  136. if edit then
  137. x,y = string.find(line,"for")
  138. if x ~= nil then
  139. if Class ~= "" then
  140. func = func+1
  141. end
  142. line = string.gsub(line,"%("," ")
  143. line = string.gsub(line,"%)"," ")
  144. line = line.."do"
  145. edit = false
  146. end
  147. end
  148.  
  149. if edit then
  150. x,y = string.find(line,"}elseif")
  151. if x ~= nil then
  152. line = string.gsub(line,"}elseif","ELSEIF")
  153. line = string.gsub(line,"%("," ")
  154. line = string.gsub(line,"%)"," ")
  155. line = line.."then"
  156. edit = false
  157. end
  158. end
  159.  
  160. if edit then
  161. x,y = string.find(line,"if")
  162. if x ~= nil then
  163. if Class ~= "" then
  164. func = func+1
  165. end
  166. line = string.gsub(line,"%("," ")
  167. line = string.gsub(line,"%)"," ")
  168. line = line.."then"
  169. edit = false
  170. end
  171. end
  172.  
  173. if edit then
  174. x,y = string.find(line,"cout")
  175. if x ~= nil then
  176. line = string.gsub(line,";","")
  177. line = string.gsub(line,"cout","io.write(\"\"")
  178. line = line..")"
  179. edit = false
  180. end
  181. end
  182.  
  183. if edit then
  184. x,y = string.find(line,"cin")
  185. if x ~= nil then
  186. line = string.gsub(line,";","")
  187. x = string.find(line,">>")
  188. line = string.sub(line,x+2,#line).." = io.read();"
  189. edit = false
  190. end
  191. end
  192.  
  193. if edit then
  194. x,y = string.find(line,"}")
  195. if x ~= nil then
  196.  
  197. if Class ~= "" and line == "};" then
  198. line = "}\nsetmetatable("..Class..","..Class..")"
  199. Class = ""
  200. else
  201. line = string.gsub(line,"}else"," else ")
  202. line = string.gsub(line,"}elseif"," elseif ")
  203.  
  204. x = string.gfind(line,"}")
  205. while true do
  206. if x() ~= nil then
  207. func = func-1
  208. else
  209. break
  210. end
  211. end
  212.  
  213.  
  214. line = string.gsub(line,"}"," end ")
  215. end
  216. edit = false
  217. end
  218. end
  219.  
  220. if edit then
  221. for k,v in pairs(Classes) do
  222. x,y = string.find(line,k.."%s.*%(.*%)%s*%{*")
  223. if x ~= nil then
  224. if Class == "" then
  225. line = "function "..string.sub(line,x+#k,y)
  226. else
  227. func = func+1
  228. str = string.sub(line,x+#k,y)
  229. x,y = string.find(str,".*%(")
  230. xx,yy = string.find(line,"%(.*%)")
  231. str = string.sub(str,x,y)
  232. line = ",\n"..string.gsub(str,"%(","").." = function"..string.sub(line,xx,yy+2)
  233. end
  234. edit = false
  235. end
  236. end
  237. end
  238.  
  239. if edit then
  240. x,y = string.find(line,"return")
  241. if x ~= nil then
  242. edit = false
  243. end
  244. end
  245.  
  246. if edit then
  247. str = string.gsub(line,";","")
  248. x,y = string.find(line,"delete")
  249. if x ~= nil then
  250. line = string.gsub(str,"delete","")
  251. x = string.gfind(line,",")
  252. xx = true
  253. cou = 1
  254. while xx do
  255. if x() ~= nil then
  256. cou = cou+1
  257. else
  258. xx = false
  259. end
  260. end
  261. line = line.."= nil"
  262. for i=1,cou-1 do
  263. line = line..",nil"
  264. end
  265. edit = false
  266. end
  267. end
  268.  
  269. if edit then
  270. for k,v in pairs(Classes) do
  271. xx,yy = string.find(line,k.."%*%s*.*;")
  272. x,y = string.find(line,k.."%s*.*;")
  273. if x == nil then
  274. k = "array"
  275. v = "table"
  276. x = xx;
  277. y = yy;
  278. end
  279. if x ~= nil then
  280. x,y = string.find(line,"%s.*;")
  281. if x ~= nil then
  282. line = string.sub(line,x,y)
  283. end
  284. if v == "()" then
  285. line = string.gsub(line,"[%;%*]","").." = "..k..v
  286. else
  287. x = string.gfind(line,",")
  288. xx = true
  289. cou = 1
  290. while xx do
  291. if x() ~= nil then
  292. cou = cou+1
  293. else
  294. xx = false
  295. end
  296. end
  297. line = string.gsub(line,"[%;%*]","")
  298. x,y = string.find(line,k.."%[.*%]")
  299. if x ~= nil then
  300. str = string.sub(line,x,y)
  301. str = string.gsub(line,"%D","")
  302. 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"
  303. else
  304. x = string.find(line,"%=")
  305. if x ~= nil then
  306. line = string.sub(line,1,x-1).." = "
  307. line = line..""..v
  308. for i=1,cou-1 do
  309. line = line..","..v
  310. end
  311. else
  312. line = line.." = "..Classes[k]
  313. end
  314. end
  315. end
  316. if Class ~= "" and func == 0 then
  317. line = ",\n"..line
  318. end
  319. edit = false
  320. end
  321. end
  322. end
  323.  
  324. if edit then
  325. x,y = string.find(line,"%#.*% ")
  326. if x ~= nil then
  327. f = string.sub(line,x,y)
  328. if f == "#include " then
  329. x,y = string.find(line,"%<.*%>")
  330. lib = string.sub(line,x,y);
  331. x = string.find(line,"%.%h")
  332. if x == nil then
  333. if Class == "" then
  334. line = includeList[lib]
  335. else
  336. line = Class.."."..includeList[lib]
  337. end
  338. else
  339. line = string.gsub(lib,"[<>]","")
  340. file = io.open("lib/CpplLibs/"..line,"r")
  341. line = ""
  342. g = true
  343. while g do
  344. filledata = file:read()
  345. if filledata == nil then
  346. g = false
  347. else
  348. line = line..filledata.."\n"
  349. end
  350. end
  351. end
  352. edit = false
  353. end
  354. end
  355. end
  356.  
  357. if line ~= nil then
  358. line = string.gsub(line,"}ELSEIF","elseif")
  359. line = string.gsub(line,"}elseif","elseif")
  360. line = string.gsub(line,"ELSEIF","elseif")
  361. line = string.gsub(line," "," ")
  362. program = program..line.."\n"
  363. end
  364.  
  365. if edit == true then
  366. LOG = LOG.."No convert string: "..stringnum.." { "..line.." }\n"
  367. end
  368.  
  369. stringnum = stringnum+1
  370. if noprint == nil then
  371. if edit == true and line ~= "" then
  372. print("Noedit:"..line)
  373. else
  374. print(line)
  375. end
  376. end
  377. end
  378. program = program.."main()"
  379. if printlog then
  380. print(LOG)
  381. end
  382. if tofile == nil then
  383. if retunlog then
  384. return program,LOG
  385. else
  386. return program
  387. end
  388. else
  389. file = io.open(ToFile,"w")
  390. file:write(program)
  391. file:close()
  392. if returnlog then
  393. return LOG
  394. end
  395. end
  396. end
  397.  
  398. return Cppl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement