Created
August 23, 2009 07:59
-
-
Save jedediah/173198 to your computer and use it in GitHub Desktop.
This file contains 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
class Object | |
def to_ast; self; end | |
end | |
module DAL | |
class Call | |
attr_accessor :message, :name, :args, :block | |
def initialize context, name, *args, &block | |
@context = context | |
@name = name | |
@args = args | |
@block = Block.new &block if block | |
end | |
def method_missing meth, *args, &block | |
@context.remove_statements *args | |
@message = Call.new @context, meth, *args, &block | |
self | |
end | |
def include? node | |
self == node or | |
(@message and @message.include? node) or | |
(@args and @args.include? node) or | |
(@block and @block.include? node) | |
end | |
def to_ast | |
{ :type => :call, | |
:name => @name, | |
:args => @args.map(&:to_ast), | |
:block => @block.to_ast, | |
:message => @message.to_ast } | |
end | |
end | |
class Constant | |
attr_accessor :name | |
def initialize name | |
@name = name | |
end | |
def to_ast | |
{ :type => :constant, | |
:name => @name } | |
end | |
end | |
class Block | |
attr_accessor :statements | |
def initialize &block | |
@statements = [] | |
v = instance_eval &block | |
@statements << v unless @statements.last.include? v | |
end | |
def include? node | |
@statements.include? node | |
end | |
def remove_statements *a | |
a.each{|x| @statements.delete x } | |
end | |
def method_missing meth, *args, &block | |
e = Call.new self, meth, *args, &block | |
remove_statements *args | |
@statements << e | |
e | |
end | |
def self.const_missing x | |
c = Constant.new x | |
end | |
def to_ast | |
{ :type => :block, | |
:statements => @statements.map(&:to_ast) } | |
end | |
end | |
def self.block &block | |
Block.new &block | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment