Advertisement
Narzew

LiteElite 0.05

Sep 9th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.36 KB | None | 0 0
  1. require 'fileutils'
  2. require 'zlib'
  3. require 'open-uri'
  4. require 'thread'
  5. $le_functions_config = "1,write,1,le_write
  6. 2,exit,0,le_exit
  7. 3,spacewrite,0,le_spacewrite
  8. 4,linewrite,0,le_linewrite
  9. 5,keywait,0,le_keywait
  10. 6,fwrite,1,le_fwrite
  11. 7,setvar,2,le_setvar
  12. 8,getvar,1,le_getvar
  13. 9,setglobal,1,le_setglobal
  14. 10,prompt,1,le_prompt
  15. 11,load,1,le_load
  16. 12,ruby,1,le_ruby
  17. 13,vareql,4,le_vareql
  18. 14,varneql,4,le_varneql
  19. 15,+,3,le_plus
  20. 16,-,3,le_minus
  21. 17,*,3,le_mul
  22. 18,/,3,le_div
  23. 19,%,3,le_modulo
  24. 20,^,3,le_xor
  25. 21,||,3,le_or
  26. 22,&&,3,le_and
  27. 23,f+,3,le_fplus
  28. 24,f-,3,le_fminus
  29. 25,f*,3,le_fmul
  30. 26,f/,3,le_fdiv
  31. 27,vartype,2,le_vartype
  32. 28,vargreater,4,le_vargreater
  33. 29,varlower,4,le_varlower
  34. 30,varfgreater,4,le_varfgreater
  35. 31,varflower,4,le_varflower
  36. 32,vargreatereql,4,le_vargreatereql
  37. 33,varlowereql,4,le_varlowereql
  38. 34,varfgreatereql,4,le_varfgreatereql
  39. 35,varflowereql,4,le_varflowereql
  40. 36,loadlib,1,le_loadlib
  41. 37,call,3,le_call
  42. 38,varcall,4,le_varcall
  43. 39,sleep,1,le_sleep
  44. 40,varsleep,1,le_varsleep
  45. 41,clearvar,1,le_clearvar
  46. 42,randomize,3,le_randomize
  47. 43,varrandomize,3,le_varrandomize
  48. 44,vardump,1,le_vardump
  49. 45,loadvar,1,le_loadvar
  50. 46,evalfile,1,le_evalfile
  51. 47,promptval,2,le_promptval
  52. "
  53. $compile = lambda{|x,y| File.open(x,'rb'){|f|$tmp=f.read}
  54.     $result=[]
  55.     $tmp.each_line{|x|next if x[0]=="#"||(x[0]=="/"&&x[1]=="/");eval($list_functions)}
  56.     $tmp=nil
  57.     File.open(y,'wb'){|f|f.write(Zlib::Deflate.deflate(Marshal.dump($result)))}
  58. }.freeze
  59. $interprete = lambda{|x|
  60.     File.open(x,'rb'){|f|$result=Marshal.load(Zlib::Inflate.inflate(f.read))}
  61.     $result.each{|x|$function_args,$function_data,x = x,x[0],x[1];eval($eval_functions)}
  62. }.freeze
  63. module LE
  64.     def self.prepare
  65.         $list_functions = (LE.generate_function_list).freeze
  66.         $eval_functions = (LE.generate_eval_list).freeze
  67.         $globals = {}
  68.         $variables = {}
  69.         $libs = {}
  70.         LE::Functions.set_default_globals
  71.     end
  72.     def self.get_function_name(line)
  73.         return line.split("\x20").at(0)
  74.     end
  75.     def self.get_argument(line,arg_id)
  76.         return line.split("\x20").at(arg_id)
  77.     end
  78.     def self.get_arguments(line)
  79.         return line.split("\x20").delete_at(0)
  80.     end
  81.     def self.get_command_and_arguments(line)
  82.         return line.split("\x20")
  83.     end
  84.     def self.get_one_big_argument(line)
  85.         return line.split("\x20").delete_at(0)
  86.     end
  87.     def self.generate_function_list
  88.         s = ""
  89.         s << "case LE.get_function_name(x)\n"
  90.         $le_functions_config.each_line {|x|
  91.         y = x.split(',')
  92.         s << "when \"#{y.at(1)}\" then $result << [#{y.at(0)}"
  93.         mtcount = y.at(2).to_i
  94.         if mtcount == 0
  95.             s << "]\n"
  96.         else
  97.             mtcount.times{|x|
  98.                 s << ",LE.get_argument(x,#{x+1})"
  99.             }
  100.             s << "]\n"
  101.         end
  102.         }
  103.         s << "else\nraise\"Compile Error!\"\nend\n"
  104.         return s
  105.     end
  106.     def self.generate_eval_list
  107.         s = ""
  108.         s << "case $function_data\n"
  109.         $le_functions_config.each_line{|x|
  110.         y = x.split(',')
  111.         k = y.at(3).delete("\n")
  112.         s << "when #{y.at(0)} then LE::Functions.#{k}"
  113.         mtcount = y.at(2).to_i
  114.         if mtcount == 0
  115.             s << "\n"
  116.         else
  117.             s << "("
  118.             mtcount.times{|x|
  119.             if x == mtcount-1
  120.                 s << "$function_args.at(#{x+1})"
  121.             else
  122.                 s << "$function_args.at(#{x+1}),"
  123.             end
  124.             }
  125.             s << ")\n"
  126.         end
  127.         }
  128.         s << "else\nraise\"Invalid Command!\"\nend\n"
  129.         return s
  130.     end
  131. end
  132. module LE::Functions
  133.     def self.do_nothing
  134.     end
  135.     def self.set_default_globals
  136.         $globals['promptval'] = "Enter a valid value.\n"
  137.     end
  138.     def self.le_write(x)
  139.         print x
  140.     end
  141.     def self.le_exit
  142.         exit
  143.     end
  144.     def self.le_spacewrite
  145.         print " "
  146.     end
  147.     def self.le_linewrite
  148.         print "\n"
  149.     end
  150.     def self.le_keywait
  151.         $stdin.gets
  152.     end
  153.     def self.le_fwrite(x)
  154.         print x.gsub!("+", " ")
  155.     end
  156.     def self.le_setvar(x,y)
  157.         $variables[x] = y
  158.     end
  159.     def self.le_getvar(x)
  160.         print $variables[x]
  161.     end
  162.     def self.le_setglobal(x,y)
  163.         $global[x] = y
  164.     end
  165.     def self.le_prompt(x)
  166.         $variables[x] = $stdin.gets.chomp!
  167.     end
  168.     def self.le_load(x)
  169.         $interprete.call(x)
  170.     end
  171.     def self.le_ruby(x)
  172.         file = File.open(x,'rb')
  173.         data = file.read
  174.         file.close
  175.         eval(data)
  176.     end
  177.     def self.le_vareql(x,y,z,a)
  178.         if $variables[x] == $variables[y]
  179.             LE::Functions.le_load(z)
  180.         elsif a != nil
  181.             LE::Functions.le_load(a)
  182.         end
  183.     end
  184.     def self.le_varneql(x,y,z,a)
  185.         if $variables[x] != $variables[y]
  186.             LE::Functions.le_load(z)
  187.         elsif a != nil
  188.             LE::Functions.le_load(a)
  189.         end
  190.     end
  191.     def self.le_plus(x,y,z)
  192.         $variables[x] = $variables[y].to_i + $variables[z].to_i
  193.     end
  194.     def self.le_minus(x,y,z)
  195.         $variables[x] = $variables[y].to_i - $variables[z].to_i
  196.     end
  197.     def self.le_mul(x,y,z)
  198.         $variables[x] = $variables[y].to_i * $variables[z].to_i
  199.     end
  200.     def self.le_div(x,y,z)
  201.         $variables[x] = $variables[y].to_i / $variables[z].to_i
  202.     end
  203.     def self.le_modulo(x,y,z)
  204.         $variables[x] = $variables[y].to_i % $variables[z].to_i
  205.     end
  206.     def self.le_xor(x,y,z)
  207.         $variables[x] = $variables[y].to_i * $variables[z].to_i
  208.     end
  209.     def self.le_or(x,y,z)
  210.         $variables[x] = $variables[y].to_i / $variables[z].to_i
  211.     end
  212.     def self.le_and(x,y,z)
  213.         $variables[x] = $variables[y].to_i % $variables[z].to_i
  214.     end
  215.     def self.le_fplus(x,y,z)
  216.         $variables[x] = $variables[y].to_f + $variables[z].to_f
  217.     end
  218.     def self.le_fminus(x,y,z)
  219.         $variables[x] = $variables[y].to_f - $variables[z].to_f
  220.     end
  221.     def self.le_fmul(x,y,z)
  222.         $variables[x] = $variables[y].to_f * $variables[z].to_f
  223.     end
  224.     def self.le_fdiv(x,y,z)
  225.         $variables[x] = $variables[y].to_f / $variables[z].to_f
  226.     end
  227.     def self.le_vartype(x,y)
  228.         case y
  229.         when 'int' || 'i'
  230.             $variables[x] = $variables[x].to_i
  231.         when 'float' || 'f'
  232.             $variables[x] = $variables[x].to_f
  233.         when 'string' || 's'
  234.             $variables[x] = $variables[x].to_s
  235.         when 'array' || 'a'
  236.             $variables[x] = $variables[x].to_a
  237.         end
  238.     end
  239.     def self.le_vargreater(x,y,z,a)
  240.         if $variables[x].to_i > $variables[y].to_i
  241.             LE::Functions.le_load(z)
  242.         elsif a != nil
  243.             LE::Functions.le_load(a)
  244.         end
  245.     end
  246.     def self.le_varlower(x,y,z,a)
  247.         if $variables[x].to_i < $variables[y].to_i
  248.             LE::Functions.le_load(z)
  249.         elsif a != nil
  250.             LE::Functions.le_load(a)
  251.         end
  252.     end
  253.     def self.le_varfgreater(x,y,z,a)
  254.         if $variables[x] > $variables[y]
  255.             LE::Functions.le_load(z)
  256.         elsif a != nil
  257.             LE::Functions.le_load(a)
  258.         end
  259.     end
  260.     def self.le_varflower(x,y,z,a)
  261.         if $variables[x] < $variables[y]
  262.             LE::Functions.le_load(z)
  263.         elsif a != nil
  264.             LE::Functions.le_load(a)
  265.         end
  266.     end
  267.     def self.le_vargreatereql(x,y,z,a)
  268.         if $variables[x].to_i >= $variables[y].to_i
  269.             LE::Functions.le_load(z)
  270.         elsif a != nil
  271.             LE::Functions.le_load(a)
  272.         end
  273.     end
  274.     def self.le_varlowereql(x,y,z,a)
  275.         if $variables[x].to_i <= $variables[y].to_i
  276.             LE::Functions.le_load(z)
  277.         elsif a != nil
  278.             LE::Functions.le_load(a)
  279.         end
  280.     end
  281.     def self.le_varfgreatereql(x,y,z,a)
  282.         if $variables[x] >= $variables[y]
  283.             LE::Functions.le_load(z)
  284.         elsif a != nil
  285.             LE::Functions.le_load(a)
  286.         end
  287.     end
  288.     def self.le_varflowereql(x,y,z,a)
  289.         if $variables[x].to_f <= $variables[y].to_f
  290.             LE::Functions.le_load(z)
  291.         elsif a != nil
  292.             LE::Functions.le_load(a)
  293.         end
  294.     end
  295.     def self.le_parse_args(args) #private
  296.         $fargs = []
  297.         args.split(",").each{|x| $fargs << $variables[x] }
  298.     end
  299.     def self.le_loadlib(x)
  300.         file = File.open(x, 'rb')
  301.         $result = Marshal.load(Zlib::Inflate.inflate(file.read))
  302.         file.close
  303.         libname = $result[1][0].to_s
  304.         $libs[libname] = {}
  305.         $result.at(3).each{|x|
  306.             $libs[libname][x[0].to_s] = [$result[2][x[1]][3],$result[2][x[1]][4]]
  307.         }
  308.     end
  309.     def self.le_call(x,y,args)
  310.         LE::Functions.le_parse_args(args)
  311.         if $libs[x][y][0] == 0
  312.             eval($libs[x][y][1])
  313.         elsif $libs[x][y][0] == 1
  314.             eval("Thread.new { LE.interprete_base($libs[x][y][1]) }")
  315.         end
  316.     end
  317.     def self.le_varcall(var,x,y,args)
  318.         $variables[var] = LE::Functions.le_call(x,y,args)
  319.     end
  320.     def self.le_sleep(x)
  321.         sleep x.to_f
  322.     end
  323.     def self.le_varsleep(x)
  324.         sleep($variables[x].to_f)
  325.     end
  326.     def self.le_clearvar(x)
  327.         $variables[x] = nil
  328.     end
  329.     def self.le_randomize(x, min, max)
  330.         $variables[x] = rand(max.to_i - min.to_i + 1)
  331.     end
  332.     def self.le_varrandomize(x, min, max)
  333.         $variables[x] = rand($variables[max].to_i - $variables[min].to_i + 1)
  334.     end
  335.     def self.le_vardump(x)
  336.         file = File.open($variables[x], 'wb')
  337.         file.write(Zlib::Deflate.deflate(Marshal.dump($variables)))
  338.         file.close
  339.     end
  340.     def self.le_loadvar(x)
  341.         file = File.open($variables[x], 'rb')
  342.         $variables = Marshal.load(Zlib::Inflate.inflate(file.read))
  343.         file.close
  344.     end
  345.     def self.le_evalfile(x)
  346.         file = File.open($variables[x],'rb')
  347.         data = file.read
  348.         file.close
  349.         $interprete.call($variables[x])
  350.     end
  351.     def self.le_promptval(x,y)
  352.         val = $stdin.gets.chomp.to_i
  353.         while val > y.to_i
  354.             puts $globals['promptval']
  355.             val = $stdin.gets.chomp.to_i
  356.         end
  357.         $variables[x] = val
  358.     end
  359. end
  360. module LE
  361.     module Library
  362.         def self.extract_config(inp)
  363.             file = File.open(inp,'rb')
  364.             data = file.read
  365.             file.close
  366.             $result = []
  367.             data.each_line{|x|
  368.             a = x.split("\x20")
  369.             $result << [a.at(0).gsub("\n",""),a.at(1).gsub("\n","")]
  370.             }
  371.             return $result
  372.         end        
  373.         def self.compile_library(inp,out)
  374.             $libdata = LE::Library.extract_config(inp)
  375.             $result = [[],[],[],[]]
  376.             $result[0] = 0
  377.             $act_function = 0
  378.             $libdata.each{|x|
  379.                 case x.at(0)
  380.                 when "libname"
  381.                         $result[1][0] = x.at(1)
  382.                 when "libauthor"
  383.                         $result[1][1] = x.at(1)
  384.                 when "libversion"
  385.                         $result[1][2] = x.at(1)
  386.                 when "functionid"
  387.                     $act_function = x.at(1).to_i
  388.                     $result[2][$act_function] = [] if $result[2][$act_function] == nil
  389.                     $result[2][$act_function][0] = $act_function
  390.                 when "functionname"
  391.                     $result[2][$act_function][1] = x.at(1)
  392.                     $result[3] << [x.at(1),$act_function]
  393.                 when "functionauthor"
  394.                     $result[2][$act_function][2] = x.at(1)
  395.                 when "functiontype"
  396.                     $result[2][$act_function][3] = x.at(1).to_i
  397.                 when "functioncode"
  398.                     file = File.open(x.at(1),'rb')
  399.                     $result[2][$act_function][4] = file.read
  400.                     file.close
  401.                 when "libend"
  402.                     break
  403.                 end
  404.             }
  405.             file = File.open(out, 'wb')
  406.             file.write(Zlib::Deflate.deflate(Marshal.dump($result)))
  407.             file.close
  408.         end
  409.     end
  410. end
  411. begin
  412.     LE.prepare
  413.     case ARGV[0]
  414.     when 'c'
  415.         if ARGV[2] == nil
  416.             $compile.call(ARGV[1], ARGV[1].gsub('.lel','.lcc').gsub('.lll','.lcc'))
  417.         else
  418.             $compile.call(ARGV[1], ARGV[2])
  419.         end
  420.     when 'i'
  421.         $interprete.call(ARGV[1])
  422.     when 'l'
  423.         if ARGV[2] == nil
  424.             LE::Library.compile_library(ARGV[1], ARGV[1].gsub('.cfg','.lcl'))
  425.         else
  426.             LE::Library.compile_library(ARGV[1],ARGV[2])
  427.         end
  428.     end
  429. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement