From f126aa52dfbc9acf3b967c8473aa5531a689ba6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Fri, 6 Sep 2019 13:59:33 +0300 Subject: [PATCH] Descope Parfait in the compiler Compiler now removes the module Parfait scope and also the ::Parfait:: Scope in module names Which means we can compile scoped code and get unscoped code. for Parfait Handy for tests too --- lib/ruby/ruby_compiler.rb | 22 +++++++++++++++++++++- test/ruby/test_module_name.rb | 21 +++++++++++++++++++++ test/ruby/test_not_implemented.rb | 10 +++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/ruby/test_module_name.rb diff --git a/lib/ruby/ruby_compiler.rb b/lib/ruby/ruby_compiler.rb index d614b822..1811f89a 100644 --- a/lib/ruby/ruby_compiler.rb +++ b/lib/ruby/ruby_compiler.rb @@ -170,10 +170,17 @@ module Ruby ClassVariable.new(expression.children.first.to_s[2 .. -1].to_sym) end + # remove global parfait module scopes from consts + # Other modules _scopes_ not implemented def on_const expression scope = expression.children.first if scope - not_implemented(expression) unless scope.type == :cbase + unless(scope.type == :const and + scope.children.first and + scope.children.first.type == :cbase and + scope.children[1] == :Parfait) + not_implemented(expression) unless scope.type == :cbase + end end ModuleName.new(expression.children[1]) end @@ -254,6 +261,19 @@ module Ruby w end + # Unscope stuff out of the parfait module + # less magic in requires + # Other modules still not implemented + def on_module(statement) + kids = statement.children.dup + name = kids.shift + if(name.type == :const and + name.children[1] == :Parfait) + process_all(kids) + else + not_implemented(statement) + end + end private def instance_name sym diff --git a/test/ruby/test_module_name.rb b/test/ruby/test_module_name.rb new file mode 100644 index 00000000..e5261230 --- /dev/null +++ b/test/ruby/test_module_name.rb @@ -0,0 +1,21 @@ +require_relative "helper" + +module Ruby + class ModuleNameTest < Minitest::Test + include RubyTests + + def test_parfait_module + assert_equal [nil], compile("module Parfait ; end") + end + def test_parfait_module_const + assert_equal IntegerConstant, compile("module Parfait ; 1;end").first.class + end + + def test_module_parfait_removed + exp = compile( "::Parfait::Object" ) + assert_equal ModuleName , exp.class + assert_equal :Object , exp.name + end + + end +end diff --git a/test/ruby/test_not_implemented.rb b/test/ruby/test_not_implemented.rb index 7dac9b0c..a996998f 100644 --- a/test/ruby/test_not_implemented.rb +++ b/test/ruby/test_not_implemented.rb @@ -4,17 +4,25 @@ module Ruby class NotImplemented < Minitest::Test include RubyTests + # random module names don't work, only global Parfait def assert_handler_missing(input) assert_raises(Ruby::ProcessError){ compile(input) } end def test_module - assert_handler_missing "module Name ; end" + assert_handler_missing( "module Mod ; end" ) end def test_module_module_scoped assert_handler_missing( "M::Module" ) end + def test_module_parfait_scoped + assert_handler_missing( "Parfait::Module" ) + end + + def test_module_parfait_scoped + assert_handler_missing( "module Mod ; end" ) + end end end