rubyx/lib/parser/nodes.rb

88 lines
1.9 KiB
Ruby
Raw Normal View History

# ast classes
module Parser
2014-04-24 16:38:06 +02:00
class Expression
def eval
raise "abstract"
end
def compare other , attributes
2014-04-29 15:22:12 +02:00
return false unless other.class == self.class
attributes.each do |a|
left = send(a)
right = other.send( a)
return false unless left.class == right.class
return false unless left == right
end
return true
end
2014-04-24 16:38:06 +02:00
end
class IntegerExpression < Expression
2014-04-24 16:38:06 +02:00
attr_reader :value
def initialize val
@value = val
end
def == other
compare other , [:value]
end
2014-04-24 14:43:20 +02:00
end
2014-04-24 16:38:06 +02:00
class NameExpression < Expression
attr_reader :name
def initialize name
@name = name
end
def == other
compare other , [:name]
end
2014-04-24 14:43:20 +02:00
end
class StringExpression < Expression
attr_reader :string
def initialize str
@string = str
end
def == other
compare other , [:string]
end
end
2014-04-24 16:38:06 +02:00
class FuncallExpression < Expression
attr_reader :name, :args
def initialize name, args
@name , @args = name , args
end
def == other
compare other , [:name , :args]
end
2014-04-24 14:43:20 +02:00
end
2014-04-24 16:38:06 +02:00
class ConditionalExpression < Expression
attr_reader :cond, :if_true, :if_false
def initialize cond, if_true, if_false
@cond, @if_true, @if_false = cond, if_true, if_false
end
def == other
compare other , [:cond, :if_true, :if_false]
end
end
2014-04-24 14:43:20 +02:00
class AssignmentExpression < Expression
attr_reader :assignee, :assigned
def initialize assignee, assigned
@assignee, @assigned = assignee, assigned
end
def == other
compare other , [:assignee, :assigned]
2014-04-24 14:43:20 +02:00
end
end
2014-04-24 16:38:06 +02:00
class FunctionExpression < Expression
attr_reader :name, :params, :block
def initialize name, params, block
@name, @params, @block = name, params, block
2014-04-24 16:38:06 +02:00
end
def == other
compare other , [:name, :params, :block]
end
2014-04-24 14:43:20 +02:00
end
end