From 113ba8607cfecf33457b7f2afe933a6eb81007ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Thu, 19 Sep 2019 20:48:21 +0300 Subject: [PATCH] fix to_s (mostly) --- lib/ruby/class_method_statement.rb | 2 +- lib/ruby/if_statement.rb | 11 ++++++----- lib/ruby/method_statement.rb | 5 ++--- lib/ruby/statement.rb | 3 ++- lib/ruby/statements.rb | 2 +- lib/vool/class_expression.rb | 5 +++-- lib/vool/class_method_expression.rb | 2 +- lib/vool/if_statement.rb | 11 ++++++----- lib/vool/method_expression.rb | 2 +- lib/vool/statement.rb | 3 ++- lib/vool/statements.rb | 2 +- lib/vool/variables.rb | 3 +++ test/ruby/test_if_statement.rb | 2 +- test/rubyx/rt_parfait/test_object.rb | 4 +++- 14 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/ruby/class_method_statement.rb b/lib/ruby/class_method_statement.rb index 7bc63db9..fa4b9ba3 100644 --- a/lib/ruby/class_method_statement.rb +++ b/lib/ruby/class_method_statement.rb @@ -8,7 +8,7 @@ module Ruby def to_s(depth = 0) arg_str = @args.collect{|a| a.to_s}.join(', ') - at_depth(depth , "def self.#{name}(#{arg_str})" , @body.to_s(depth + 1) , "end") + at_depth(depth , "def self.#{name}(#{arg_str})\n#{@body.to_s(depth + 1)}\nend") end end diff --git a/lib/ruby/if_statement.rb b/lib/ruby/if_statement.rb index b8ef7d77..509e93a7 100644 --- a/lib/ruby/if_statement.rb +++ b/lib/ruby/if_statement.rb @@ -37,11 +37,12 @@ module Ruby end def to_s(depth = 0) - parts = ["if(#{@condition})" ] - parts << @if_true.to_s(depth + 1) if(@if_true) - parts += ["else" , @if_false.to_s(depth + 1)] if(@if_false) - parts << "end" - at_depth(depth , *parts ) + parts = "if(#{@condition})\n" + parts += " #{@if_true.to_s(depth + 1)}\n" if(@if_true) + parts += "else\n" if(@if_false) + parts += " #{@if_false.to_s(depth + 1)}\n" if(@if_false) + parts += "end\n" + at_depth(depth , parts ) end end end diff --git a/lib/ruby/method_statement.rb b/lib/ruby/method_statement.rb index 4f75194a..122b5ddb 100644 --- a/lib/ruby/method_statement.rb +++ b/lib/ruby/method_statement.rb @@ -1,11 +1,10 @@ module Ruby class MethodStatement < Statement - attr_reader :name, :args , :body , :clazz + attr_reader :name, :args , :body - def initialize( name , args , body , clazz = nil) + def initialize( name , args , body) @name , @args , @body = name , args , body raise "no bod" unless @body - @clazz = clazz end # At the moment normalizing means creating implicit returns for some cases diff --git a/lib/ruby/statement.rb b/lib/ruby/statement.rb index 17ea1247..2a4e8704 100644 --- a/lib/ruby/statement.rb +++ b/lib/ruby/statement.rb @@ -24,8 +24,9 @@ module Ruby # helper method for formatting source code # depth is the depth in the tree (os the ast) # and the string are the ones to be indented (2 spaces) - def at_depth(depth , *strings) + def at_depth(depth , lines) prefix = " " * 2 * depth + strings = lines.split("\n") strings.collect{|str| prefix + str}.join("\n") end end diff --git a/lib/ruby/statements.rb b/lib/ruby/statements.rb index c50f3f8d..5a914d0c 100644 --- a/lib/ruby/statements.rb +++ b/lib/ruby/statements.rb @@ -45,7 +45,7 @@ module Ruby end def to_s(depth = 0) - at_depth(depth , *@statements.collect{|st| st.to_s(depth)}) + at_depth(depth , @statements.collect{|st| st.to_s(depth)}.join("\n")) end end diff --git a/lib/vool/class_expression.rb b/lib/vool/class_expression.rb index e18b0621..2a1234f1 100644 --- a/lib/vool/class_expression.rb +++ b/lib/vool/class_expression.rb @@ -65,7 +65,7 @@ module Vool # adding each to the respective type, ie class or meta_class, depending # on if they are instance or class instance variables. # - # Class variables are deemed a design mistake, ie not implemented (yet) + # Class variables are deemed a design mistake, ie not implemented (yet) def create_types self.body.statements.each do |node| case node @@ -87,7 +87,8 @@ module Vool end end def to_s(depth = 0) - at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end") + derive = super_class_name ? "< #{super_class_name}" : "" + at_depth(depth , "class #{name} #{derive}\n#{@body.to_s(depth + 1)}\nend") end end end diff --git a/lib/vool/class_method_expression.rb b/lib/vool/class_method_expression.rb index b4045767..d4a293b7 100644 --- a/lib/vool/class_method_expression.rb +++ b/lib/vool/class_method_expression.rb @@ -29,7 +29,7 @@ module Vool 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") + at_depth(depth , "def self.#{name}(#{arg_str})\n#{@body.to_s(depth + 1)}end") end private diff --git a/lib/vool/if_statement.rb b/lib/vool/if_statement.rb index 5c83a833..a4d3e365 100644 --- a/lib/vool/if_statement.rb +++ b/lib/vool/if_statement.rb @@ -50,11 +50,12 @@ module Vool end def to_s(depth = 0) - parts = ["if (#{@condition.to_s(0)})" ] - parts << " #{@if_true}" if @if_true - parts += [ "else" , " #{@if_false}"] if(@false) - parts << "end" - at_depth(depth , *parts ) + parts = "if (#{@condition.to_s(0)})\n" + parts += " #{@if_true}\n" if @if_true + parts += "else\n" if(@false) + parts += " #{@if_false}\n" if(@false) + parts += "end\n" + at_depth(depth , parts ) end end end diff --git a/lib/vool/method_expression.rb b/lib/vool/method_expression.rb index 357fc086..4557a8ad 100644 --- a/lib/vool/method_expression.rb +++ b/lib/vool/method_expression.rb @@ -43,7 +43,7 @@ module Vool 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") + at_depth(depth , "def #{name}(#{arg_str})\n#{@body.to_s(depth + 1)}\nend") end private diff --git a/lib/vool/statement.rb b/lib/vool/statement.rb index bf2fa101..a3d6319c 100644 --- a/lib/vool/statement.rb +++ b/lib/vool/statement.rb @@ -28,8 +28,9 @@ module Vool raise "Not implemented for #{self}" end - def at_depth(depth , *strings) + def at_depth(depth , lines) prefix = " " * 2 * depth + strings = lines.split("\n") strings.collect{|str| prefix + str}.join("\n") end diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index 39d717d1..e15f10b9 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -83,7 +83,7 @@ module Vool end def to_s(depth = 0) - at_depth(depth , *@statements.collect{|st| st.to_s(depth)}) + at_depth(depth , @statements.collect{|st| st.to_s(depth)}.join("\n")) end end diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb index 8b2ad1e4..2d34149c 100644 --- a/lib/vool/variables.rb +++ b/lib/vool/variables.rb @@ -60,5 +60,8 @@ module Vool def each(&block) block.call(self) end + def to_s + @name.to_s + end end end diff --git a/test/ruby/test_if_statement.rb b/test/ruby/test_if_statement.rb index b4fb45a5..45b32d52 100644 --- a/test/ruby/test_if_statement.rb +++ b/test/ruby/test_if_statement.rb @@ -39,7 +39,7 @@ module Ruby end def test_to_s lst = compile( double_if ) - assert_equal "if(false);true;else;false;end" , lst.to_s.gsub("\n",";") + assert_equal "if(false); true;else; false;end" , lst.to_s.gsub("\n",";") end end end diff --git a/test/rubyx/rt_parfait/test_object.rb b/test/rubyx/rt_parfait/test_object.rb index d645a014..d365fb92 100644 --- a/test/rubyx/rt_parfait/test_object.rb +++ b/test/rubyx/rt_parfait/test_object.rb @@ -1,7 +1,7 @@ require_relative "rt_helper" module RubyX - class ObjectSourceTest < MiniTest::Test + class ObjectSourceTest #< MiniTest::Test include ParfaitHelper def setup @input = load_parfait(:object) + load_parfait_test(:object) @@ -55,6 +55,7 @@ module RubyX def self.runnable_methods input = load_parfait(:object) + load_parfait_test(:object) vool = Ruby::RubyCompiler.compile(input).to_vool + #puts vool.to_s tests = [ ] vool[2].body.statements.each do |method| tests << method.name @@ -72,6 +73,7 @@ MAIN # ticks = run_input(code) # assert_equal "" , @interpreter.stdout end + break end tests end