Guest User

Untitled

a guest
Feb 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. require 'securerandom'
  2. require 'optparse'
  3.  
  4. INSTRUCTIONS=['>','<','+','-','.',',','[',']','0','1']
  5.  
  6. def _012c(lang,id,count)
  7. case lang
  8. when 'raw'
  9. return id*count
  10. when 'brainfuck'
  11. case id
  12. when '>','<','+','-','.',',','[',']'
  13. return id*count
  14. when '0'
  15. return '[-]'
  16. else
  17. return ''
  18. end
  19. when 'c'
  20. case id
  21. when 'START'
  22. return "#include<stdio.h>nmain(){char d[30000],*p=d,s[30000],*a=s;"
  23. when '>'
  24. if count==1 then
  25. return 'p++;'
  26. else
  27. return "p+=#{count};"
  28. end
  29. when '<'
  30. if count==1 then
  31. return 'p--;'
  32. else
  33. return "p-=#{count};"
  34. end
  35. when '+'
  36. if count==1 then
  37. return '(*p)++;'
  38. else
  39. return "(*p)+=#{count};"
  40. end
  41. when '-'
  42. if count==1 then
  43. return '(*p)--;'
  44. else
  45. return "(*p)-=#{count};"
  46. end
  47. when '.'
  48. return 'putchar(*p);'*count
  49. when ','
  50. if count==1 then
  51. return 'scanf(" %c",&p);'
  52. elsif count==2 then
  53. return 'scanf(" %c",&p);scanf(" %c",&p);'
  54. else
  55. return "for(int i=0;i<#{count};i++)scanf(" %c",p);"
  56. end
  57. when '['
  58. return 'while(*p){'*count
  59. when ']'
  60. return '}'*count
  61. when '0'
  62. return '*p=0;'
  63. when '1'
  64. return 'p=d[0];'
  65. when 'END'
  66. return '}'
  67. end
  68. when 'c++'
  69. case id
  70. when 'START'
  71. return "#include <iostream>nmain(){char d[30000],*p=d,s[30000],*a=s;"
  72. when '>'
  73. if count==1 then
  74. return 'p++;'
  75. else
  76. return "p+=#{count};"
  77. end
  78. when '<'
  79. if count==1 then
  80. return 'p--;'
  81. else
  82. return "p-=#{count};"
  83. end
  84. when '+'
  85. if count==1 then
  86. return '(*p)++;'
  87. else
  88. return "(*p)+=#{count};"
  89. end
  90. when '-'
  91. if count==1 then
  92. return '(*p)--;'
  93. else
  94. return "(*p)-=#{count};"
  95. end
  96. when '.'
  97. if count==1 then
  98. return "std::cout<<*p;"
  99. else
  100. return "std::cout<<std::string(#{count},*p);"
  101. end
  102. when ','
  103. if count==1 then
  104. return 'std::cin>>*p;'
  105. elsif count==2 then
  106. return 'std::cin>>*p;std::cin>>*p;'
  107. else
  108. return "for(int i=0;i<#{count};i++)std::cin>>*p;"
  109. end
  110. when '['
  111. return 'while(*p){'*count
  112. when ']'
  113. return '}'*count
  114. when '0'
  115. return '*p=0;'
  116. when '1'
  117. return 'p=d[0];'
  118. when 'END'
  119. return '}'
  120. end
  121. end
  122. end
  123. def _012(input,output,lang)
  124. id=0
  125. last=''
  126. count=0
  127. File.open(output,'w') do |f2|
  128. f2<<_012c(lang,'START',0)
  129. File.open(input,'r') do |f|
  130. while not f.eof?
  131. c=f.read(1)
  132. if c=='0' then
  133. if last=='1' then
  134. f2<<_012c(lang,INSTRUCTIONS[id],count)
  135. count=1
  136. end
  137. id+=id==INSTRUCTIONS.length-1?-id:1
  138. elsif c=='1' then
  139. if c==last or count==0 then
  140. count+=1
  141. end
  142. end
  143. last=c
  144. end
  145. if last=='1' then
  146. f2<<_012c(lang,INSTRUCTIONS[id],count)
  147. count=1
  148. end
  149. end
  150. f2<<_012c(lang,'END',0)
  151. end
  152. end
  153. def _201(input,output)
  154. last=0
  155. File.open(input,'r') do |f|
  156. File.open(output,'w') do |f2|
  157. while not f.eof?
  158. c=f.read(1)
  159. if not INSTRUCTIONS.include?(c) then
  160. next
  161. end
  162. i=INSTRUCTIONS.index(c)
  163. if last<=i then
  164. f2<<'0'*(i-last)
  165. else
  166. f2<<'0'*(INSTRUCTIONS.length-last+i)
  167. end
  168. f2<<'1'
  169. last=i
  170. end
  171. end
  172. end
  173. end
  174. def minify(input,output)
  175. pipe=SecureRandom.hex(32)
  176. _012(input,pipe,'raw')
  177. _201(pipe,output)
  178. File.delete(pipe)
  179. end
  180.  
  181. OptionParser.new do |parser|
  182. options={}
  183. parser.on('-i','--input INPUT','Set INPUT file for program'){|x|options[:input]=x}
  184. parser.on('-o','--output OUTPUT','Set OUTPUT file for program'){|x|options[:output]=x}
  185. parser.on('-m', '--minify', 'Minify INPUT and get result in OUTPUT') do |x|
  186. if not options[:input] or not options[:output] then
  187. raise OptionParser::MissingArgument
  188. end
  189. minify(options[:input],options[:output])
  190. end
  191. parser.on('-c', '--convert LANG', 'Convert .zo file to other LANG file') do |x|
  192. if not options[:input] or not options[:output] then
  193. raise OptionParser::MissingArgument
  194. end
  195. _012(options[:input],options[:output],x)
  196. end
  197. parser.on('-g', '--generate', 'Convert ASCII to .zo file') do |x|
  198. if not options[:input] or not options[:output] then
  199. raise OptionParser::MissingArgument
  200. end
  201. _201(options[:input],options[:output])
  202. end
  203. end.parse!
Add Comment
Please, Sign In to add comment