Created
August 31, 2008 23:40
-
-
Save collin/8238 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Logical inverter! | |
require 'rubygems' | |
require 'ruby2ruby' | |
class Sexp | |
def to_ruby | |
Ruby2Ruby.new.process(self) | |
end | |
def invert_logic | |
Sexp.from_array(self.map do |op| | |
case op | |
when :or | |
:and | |
when :and | |
:or | |
else | |
if op.is_a? Sexp | |
case op.first | |
when :not | |
op.last.invert_logic | |
when :const | |
Sexp.for("!#{op.to_ruby}") | |
else | |
op.invert_logic | |
end | |
else | |
op | |
end | |
end | |
end.compact) | |
end | |
end | |
if $0 == __FILE__ | |
puts Sexp.for("(A or B)").invert_logic.to_ruby | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment