2018-07-19 13:46:51 +02:00
|
|
|
module Ruby
|
|
|
|
|
|
|
|
class Assignment < Statement
|
|
|
|
attr_reader :name , :value
|
|
|
|
def initialize(name , value )
|
|
|
|
@name , @value = name , value
|
|
|
|
end
|
|
|
|
|
2018-07-19 13:59:10 +02:00
|
|
|
def to_vool()
|
2018-07-19 13:46:51 +02:00
|
|
|
raise "not named left #{name.class}" unless name.is_a?(Symbol)
|
|
|
|
case value
|
2018-07-20 19:53:35 +02:00
|
|
|
when Variable , Constant
|
2018-07-20 17:13:58 +02:00
|
|
|
return self.vool_brother.new(name,@value.to_vool)
|
2018-07-30 13:44:14 +02:00
|
|
|
when SendStatement , YieldStatement
|
2018-07-19 13:46:51 +02:00
|
|
|
return normalize_send
|
2018-07-25 10:06:42 +02:00
|
|
|
when BlockStatement
|
|
|
|
return normalize_block
|
2018-07-19 13:46:51 +02:00
|
|
|
else
|
|
|
|
raise "unsupported right #{value}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-25 10:06:42 +02:00
|
|
|
# Ruby BlockStatements have the block and the send. Normalize the send
|
|
|
|
# and assign it (it is the last in the list)
|
|
|
|
def normalize_block
|
|
|
|
statements = value.to_vool
|
|
|
|
index = statements.length - 1
|
|
|
|
snd = statements.statements[index]
|
|
|
|
raise "Expecting Send #{snd.class}:#{snd}" unless snd.is_a?( Vool::SendStatement)
|
|
|
|
statements.statements[index] = assignment( snd )
|
|
|
|
statements
|
|
|
|
end
|
|
|
|
|
2018-07-20 17:13:58 +02:00
|
|
|
# sends may have complex args that get hoisted in vool:ing them
|
|
|
|
# in which case we have to assign the simplified, otherwise the
|
|
|
|
# plain send
|
2018-07-20 12:43:37 +02:00
|
|
|
def normalize_send
|
|
|
|
statements = value.to_vool
|
2018-07-30 13:44:14 +02:00
|
|
|
return assignment( statements ) if statements.is_a?(Vool::CallStatement)
|
2018-07-20 17:13:58 +02:00
|
|
|
# send has hoisted assigns, so we make an assign out of the "pure" send
|
|
|
|
statements << assignment(statements.statements.pop)
|
2018-07-19 13:46:51 +02:00
|
|
|
statements
|
|
|
|
end
|
|
|
|
|
2018-07-20 17:13:58 +02:00
|
|
|
# create same type assignment with the value (a send)
|
|
|
|
def assignment(value)
|
|
|
|
value ||= @value
|
|
|
|
self.vool_brother.new(name,value)
|
|
|
|
end
|
|
|
|
|
2018-07-19 13:46:51 +02:00
|
|
|
def to_s(depth = 0)
|
|
|
|
at_depth(depth , "#{@name} = #{@value}")
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class IvarAssignment < Assignment
|
|
|
|
|
2018-07-19 13:59:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class LocalAssignment < Assignment
|
2018-07-19 13:46:51 +02:00
|
|
|
|
|
|
|
end
|
2018-07-19 13:59:10 +02:00
|
|
|
|
2018-07-19 13:46:51 +02:00
|
|
|
end
|