From 363d1cb36f31f4c2b707130975d9a01f02596752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Fri, 6 Sep 2019 21:00:37 +0300 Subject: [PATCH] fix module handling at ruby level Was returning arrays instead of Statements, which messed things up --- lib/ruby/ruby_block_statement.rb | 4 +++- lib/ruby/ruby_compiler.rb | 17 +++++++++++++---- lib/ruby/statements.rb | 4 +++- lib/ruby/variables.rb | 8 ++++---- test/ruby/test_module_name.rb | 9 ++++++--- 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/ruby/ruby_block_statement.rb b/lib/ruby/ruby_block_statement.rb index 69f9edf9..1cf8f938 100644 --- a/lib/ruby/ruby_block_statement.rb +++ b/lib/ruby/ruby_block_statement.rb @@ -26,6 +26,8 @@ module Ruby sendd.arguments << lambda ret end - + def to_s(depth = 0) + at_depth(depth , "{|#{@args.join(',')}| #{@body}}") + end end end diff --git a/lib/ruby/ruby_compiler.rb b/lib/ruby/ruby_compiler.rb index 1811f89a..8755e25f 100644 --- a/lib/ruby/ruby_compiler.rb +++ b/lib/ruby/ruby_compiler.rb @@ -38,7 +38,7 @@ module Ruby begin self.new.process(ast) rescue => e - puts "Error processing #{ast}" + puts "Error processing \n#{ast}" raise e end end @@ -139,7 +139,11 @@ module Ruby not_implemented(expression) end def on_kwbegin statement - ScopeStatement.new process_all( statement.children ) + scope = ScopeStatement.new([]) + statement.children.each do |kid| #do the loop to catch errors (not process_all) + scope << process(kid) + end + scope end alias :on_begin :on_kwbegin @@ -268,8 +272,13 @@ module Ruby kids = statement.children.dup name = kids.shift if(name.type == :const and - name.children[1] == :Parfait) - process_all(kids) + name.children[1] == :Parfait) + raise "No empty modules for now #{statement}" if kids.empty? + if(kids.length == 1) + process(kids.first) + else + on_kwbegin(kids) + end else not_implemented(statement) end diff --git a/lib/ruby/statements.rb b/lib/ruby/statements.rb index e8182307..c50f3f8d 100644 --- a/lib/ruby/statements.rb +++ b/lib/ruby/statements.rb @@ -2,7 +2,8 @@ module Ruby class Statements < Statement attr_reader :statements def initialize(statements) - @statements = statements + @statements = [] + statements.each{|st| self << st} end def empty? @@ -30,6 +31,7 @@ module Ruby @statements[i] end def <<(o) + raise "Not Statement #{o.class}=#{o.to_s[0..100]}" unless o.is_a?(Statement) @statements << o self end diff --git a/lib/ruby/variables.rb b/lib/ruby/variables.rb index 2efefe9c..efddac64 100644 --- a/lib/ruby/variables.rb +++ b/lib/ruby/variables.rb @@ -9,12 +9,12 @@ module Ruby def to_vool vool_brother.new(@name) end + def to_s(depth=0) + name.to_s + end end class LocalVariable < Variable - def to_s - name.to_s - end end class InstanceVariable < Variable @@ -24,7 +24,7 @@ module Ruby array << @name end def to_s(depth = 0) - at_depth(depth , "@#{name}" ) + "@#{name}" end end diff --git a/test/ruby/test_module_name.rb b/test/ruby/test_module_name.rb index e5261230..20241565 100644 --- a/test/ruby/test_module_name.rb +++ b/test/ruby/test_module_name.rb @@ -4,11 +4,14 @@ module Ruby class ModuleNameTest < Minitest::Test include RubyTests - def test_parfait_module - assert_equal [nil], compile("module Parfait ; end") + def test_parfait_module_scoped + lst = compile("module Parfait ; 1 ; 1 ; end") + assert_equal ScopeStatement, lst.class + assert_equal IntegerConstant, lst.first.class + assert_equal 2, lst.length end def test_parfait_module_const - assert_equal IntegerConstant, compile("module Parfait ; 1;end").first.class + assert_equal IntegerConstant, compile("module Parfait ; 1;end").class end def test_module_parfait_removed