rubyx/lib/vool/assign_statement.rb

65 lines
1.4 KiB
Ruby
Raw Normal View History

2017-04-01 20:28:57 +02:00
module Vool
2017-04-01 20:28:57 +02:00
class Assignment < Statement
attr_reader :name , :value
2017-04-02 11:59:07 +02:00
def initialize(name , value )
@name , @value = name , value
2017-04-01 20:28:57 +02:00
end
def normalize()
raise "not named left #{name.class}" unless name.is_a?(Symbol)
case value
when Named , Constant
return copy
when SendStatement
return normalize_send
else
raise "unsupported right #{value}"
end
end
def copy(value = nil)
value ||= @value
self.class.new(name,value)
end
def normalize_send
statements = value.normalize()
return copy( statements ) if statements.is_a?(SendStatement)
assign = statements.statements.pop
statements << copy(assign)
statements
end
def chain_assign(assign , method)
return assign unless @value.is_a?(SendStatement)
@value.to_mom(method) << assign
end
def each(&block)
block.call(self)
@value.each(&block)
2017-04-08 11:10:42 +02:00
end
2018-07-03 21:18:19 +02:00
def to_s(depth = 0)
at_depth(depth , "#{@name} = #{@value}")
end
2017-04-01 20:28:57 +02:00
end
2017-04-12 13:45:02 +02:00
class IvarAssignment < Assignment
def normalize()
super()
return IvarAssignment.new(@name , @value)
2017-04-08 11:18:10 +02:00
end
def to_mom( method )
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
from = @value.slot_definition(method)
return chain_assign( Mom::SlotLoad.new(to,from) , method)
end
2017-04-04 17:10:28 +02:00
end
2017-04-01 20:28:57 +02:00
end