Advertisement
t_a_w

dankcode compiler

Nov 29th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.27 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class DankCode
  4.   def initialize(path)
  5.     @code = File.readlines(path).map(&:chomp)
  6.   end
  7.  
  8.   def parse_expr(expr)
  9.     expr.gsub(/\bisn't\b/, "!=").gsub(/\bis\b/, "==").gsub(/:(\S+)/) { %Q["#{$1}"] }
  10.  end
  11.  
  12.  def transcode_line(line)
  13.    indent = line[/\A\t*/]
  14.    line = line.sub(/\A\t*/, "")
  15.    return nil unless line =~ /\S+/
  16.    ruby = case line
  17.    when /\A>tfw (\w+)\z/
  18.      "return #{$1}"
  19.    when /\A>mfw (\w+)\z/
  20.      "puts #{$1}"
  21.    when /\A>implying (.*)\z/
  22.      "if #{parse_expr($1)}"
  23.    when /\A>be (\w+) like (.*)\z/
  24.      "#{$1} = #{parse_expr($2)}"
  25.    when ">done implying"
  26.      "end"
  27.    when ">or not"
  28.      "else"
  29.    when /\A>wew (\w+\(.*?\))\z/
  30.      "wew = #{$1}"
  31.    when />wewlad (.*)/
  32.      "def #{$1}"
  33.    when ">be me"
  34.      "def main"
  35.    when ">thank mr skeltal"
  36.      "exit"
  37.    else
  38.      warn "Can't parse #{line}"
  39.       ""
  40.     end
  41.     [indent.gsub("\t", "  "), ruby]
  42.   end
  43.  
  44.   def run!
  45.     ruby_lines = @code.map do |line| transcode_line(line) end.compact
  46.     ruby = []
  47.     ruby_lines.each do |i,c|
  48.       ruby << "end" if c =~ /\Adef/
  49.       ruby << "#{i}#{c}"
  50.     end
  51.     ruby << "end"
  52.     ruby << "main"
  53.     puts ruby[1..-1]
  54.   end
  55. end
  56.  
  57. path = ARGV[0]
  58.  
  59. DankCode.new(path).run!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement