SHARE
TWEET

ROEL

a guest Sep 26th, 2013 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require "ruby_parser"
  2.  
  3. class RoelCompiler
  4.   def compile source
  5.     @concat_enabled = true
  6.     @indent = 0
  7.     compile_line *RubyParser.for_current_ruby.parse(source)
  8.   end
  9.  
  10.   private
  11.   def masgn *_args
  12.     "(" << args(*_args) << ")"
  13.   end
  14.  
  15.   def args *args
  16.     args.map{ |arg|
  17.       case arg
  18.       when Symbol then arg.to_s
  19.       else send(*arg)
  20.       end
  21.     }.join(", ")
  22.   end
  23.  
  24.   def iter call_args, args_args, content = nil
  25.     method_invocation = send *call_args
  26.     args_string = args_args.length > 1 ? " |#{send(*args_args)}|" : ""
  27.     @indent += 1
  28.     content_string = content.nil? ? "" : "\n#{indent}#{compile_line(*content)}\n#{indent -1}"
  29.     @indent -= 1
  30.     "#{method_invocation}{#{args_string}#{content_string}}"
  31.   end
  32.  
  33.   def str value
  34.     value.inspect
  35.   end
  36.  
  37.   def lit value
  38.     value.inspect
  39.   end
  40.  
  41.   def lvar value
  42.     value.to_s
  43.   end
  44.  
  45.   def block *args
  46.     concat_enabled = @concat_enabled
  47.     output = args.map{ |line|
  48.       @concat_enabled = concat_enabled
  49.       compile_line *line
  50.     }.join(" +\n#{indent}")
  51.     output
  52.   end
  53.  
  54.   def indent offset = 0
  55.     "  " * (@indent + offset)
  56.   end
  57.  
  58.   def hash *args
  59.     "{ " << args.each_slice(2).map{ |key_arg, value_arg| "#{compile_line *key_arg} => #{compile_line *value_arg}" }.join(", ") << " }"
  60.   end
  61.  
  62.   def ivar value
  63.     value.to_s
  64.   end
  65.  
  66.   def dstr *args
  67.     prefix = args.shift
  68.     partials = []
  69.     partials << str(prefix) unless prefix.empty?
  70.     args.each{ |arg|
  71.       case arg.first
  72.       when :str
  73.         partials << str(arg.last) unless arg.last.empty?
  74.       else
  75.         partials << "#{compile_line(*arg)}.to_s"
  76.       end
  77.     }
  78.     '('+partials.join("+")+')'
  79.   end
  80.  
  81.   def evstr content
  82.     compile_line(*content)
  83.   end
  84.  
  85.   def block_pass *args
  86.     "&" << compile_line(*args.first)
  87.   end
  88.  
  89.   def const value
  90.     value.to_s
  91.   end
  92.  
  93.   def call target, method_name, *args
  94.     prefix = target.nil? ? "" : "#{compile_line *target}."
  95.     args_string = args.any? ? "(#{args.map{ |arg| compile_line *arg }.join(", ")})" : ""
  96.     "#{prefix}#{method_name}#{args_string}"
  97.   end
  98.  
  99.   def compile_line method, *args
  100.     send(method, *args)
  101.   end
  102. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top