Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Farley Knight http://blog.farleyknight.com

1 total

On This Page:

  1. 1 Lispify Ruby

Lispify Ruby

// description of your code here

   require 'rubygems'  
   require 'parse_tree'  
   require 'ruby2ruby'  
     
   Sexp.class_eval do  
     def unbox  
       if length == 1  
         self.first  
       else  
         self  
       end  
     end  
   end  
     
   class Lispify < SexpProcessor  
     def initialize  
       super  
       self.auto_shift_type = true  
       self.require_empty   = false  
     end  
     
     def process_vcall(expr)  
       s(expr.first)  
     end  
     
     def process_call(expr)  
       expr = Sexp.from_array(expr)  
     
       first  = process(expr[0]).unbox  
       second = process(expr[2]).unbox  
     
       s(expr[1], first, second)  
     end  
     
     def process_array(expr)  
       process(expr.first)  
     end  
   end  


Which results in:


>> Lispify.new.process(ParseTree.translate(%q{ x + x * x - x }))
=> s(:-, s(:+, :x, s(:*, :x, :x)), :x)

1 total

On This Page:

  1. 1 Lispify Ruby