From 2ad24ab0bb7adc2e9b24cb6c991e93ae153412da Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 3 Jul 2018 22:18:19 +0300 Subject: [PATCH] add to_s for statements --- lib/vool/assign_statement.rb | 5 +++++ lib/vool/class_statement.rb | 4 ++++ lib/vool/if_statement.rb | 6 ++++++ lib/vool/logical_statement.rb | 4 ++++ lib/vool/method_statement.rb | 5 +++++ lib/vool/return_statement.rb | 3 +++ lib/vool/send_statement.rb | 5 +++++ lib/vool/statement.rb | 5 +++++ lib/vool/statements.rb | 4 ++++ lib/vool/variables.rb | 6 ++++++ lib/vool/while_statement.rb | 4 ++++ 11 files changed, 51 insertions(+) diff --git a/lib/vool/assign_statement.rb b/lib/vool/assign_statement.rb index eb181cd9..22278965 100644 --- a/lib/vool/assign_statement.rb +++ b/lib/vool/assign_statement.rb @@ -40,6 +40,11 @@ module Vool block.call(self) @value.each(&block) end + + def to_s(depth = 0) + at_depth(depth , "#{@name} = #{@value}") + end + end class IvarAssignment < Assignment diff --git a/lib/vool/class_statement.rb b/lib/vool/class_statement.rb index be80953c..a1e5d112 100644 --- a/lib/vool/class_statement.rb +++ b/lib/vool/class_statement.rb @@ -52,5 +52,9 @@ module Vool @clazz.set_instance_type( Parfait::Type.for_hash( @clazz , ivar_hash ) ) end end + + def to_s(depth = 0) + at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end") + end end end diff --git a/lib/vool/if_statement.rb b/lib/vool/if_statement.rb index 5a677e56..ea981ea0 100644 --- a/lib/vool/if_statement.rb +++ b/lib/vool/if_statement.rb @@ -62,5 +62,11 @@ module Vool @if_true != nil end + def to_s(depth = 0) + parts = ["if (#{@condition})" , @body.to_s(depth + 1) ] + parts += ["else" , "@if_false.to_s(depth + 1)"] if(@false) + parts << "end" + at_depth(depth , *parts ) + end end end diff --git a/lib/vool/logical_statement.rb b/lib/vool/logical_statement.rb index e3d927eb..3dfcde44 100644 --- a/lib/vool/logical_statement.rb +++ b/lib/vool/logical_statement.rb @@ -8,5 +8,9 @@ module Vool @name , @left , @right = name , left , right end + def to_s(depth = 0) + at_depth(depth , "#{left} #{name} #{right}") + end + end end diff --git a/lib/vool/method_statement.rb b/lib/vool/method_statement.rb index b91dcf60..ac9acaf6 100644 --- a/lib/vool/method_statement.rb +++ b/lib/vool/method_statement.rb @@ -35,6 +35,11 @@ module Vool Parfait::NamedList.type_for( type_hash ) end + def to_s(depth = 0) + arg_str = @args.collect{|a| a.to_s}.join(', ') + at_depth(depth , "def #{name}(#{arg_str})" , @body.to_s(depth + 1) , "end") + end + private def make_frame diff --git a/lib/vool/return_statement.rb b/lib/vool/return_statement.rb index 424c2982..53412be1 100644 --- a/lib/vool/return_statement.rb +++ b/lib/vool/return_statement.rb @@ -29,5 +29,8 @@ module Vool ret << Mom::ReturnSequence.new end + def to_s(depth = 0) + at_depth(depth , "return #{@return_value.to_s}") + end end end diff --git a/lib/vool/send_statement.rb b/lib/vool/send_statement.rb index 337972a9..98974d48 100644 --- a/lib/vool/send_statement.rb +++ b/lib/vool/send_statement.rb @@ -129,6 +129,11 @@ module Vool message_setup(in_method,dynamic_call.cache_entry) << dynamic_call end + def to_s(depth = 0) + sen = "#{receiver}.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})" + at_depth(depth , sen) + end + private def receiver_type_definition(in_method) defi = @receiver.slot_definition(in_method) diff --git a/lib/vool/statement.rb b/lib/vool/statement.rb index fc1067b6..f9e1a866 100644 --- a/lib/vool/statement.rb +++ b/lib/vool/statement.rb @@ -36,6 +36,11 @@ module Vool raise "Not implemented for #{self}" end + def at_depth(depth , *strings) + prefix = " " * 2 * depth + strings.collect{|str| prefix + str}.join("\n") + end + end class Expression diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index 720c5d69..c3b83f10 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -55,6 +55,10 @@ module Vool Statements.new(@statements.collect{|s| s.normalize}) end end + def to_s(depth = 0) + at_depth(depth , *@statements.collect{|st| st.to_s(depth)}) + end + end class ScopeStatement < Statements diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb index 8f7a5ff7..0027aebf 100644 --- a/lib/vool/variables.rb +++ b/lib/vool/variables.rb @@ -18,6 +18,9 @@ module Vool end Mom::SlotDefinition.new(:message , [type , @name]) end + def to_s + name.to_s + end end class InstanceVariable < Expression @@ -29,6 +32,9 @@ module Vool def add_ivar( array ) array << @name end + def to_s + "@#{name}" + end end class ClassVariable < Expression diff --git a/lib/vool/while_statement.rb b/lib/vool/while_statement.rb index c3eb58e2..757f3de4 100644 --- a/lib/vool/while_statement.rb +++ b/lib/vool/while_statement.rb @@ -34,5 +34,9 @@ module Vool @body.each(&block) end + def to_s(depth = 0) + at_depth(depth , "while (#{@condition})" , @body.to_s(depth + 1) , "end" ) + end + end end