From 0a09477dc6af50209735021ac603882cda8a9e0a Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 19 Jul 2015 10:36:06 +0300 Subject: [PATCH] fix and test compiling classes --- lib/parfait/space.rb | 14 ++++++++++++-- lib/virtual/compiler/module_expression.rb | 2 +- test/virtual/test_methods.rb | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/parfait/space.rb b/lib/parfait/space.rb index cf8c9a38..20a63d85 100644 --- a/lib/parfait/space.rb +++ b/lib/parfait/space.rb @@ -60,8 +60,8 @@ module Parfait kernel.get_instance_method :__init__ end - # this is the way to instantiate classes (not Parfait::Class.new) - # so we get and keep exactly one per name + # get a class by name (symbol) + # return nili if no such class. Use bang version if create should be implicit def get_class_by_name name raise "uups #{name}.#{name.class}" unless name.is_a?(Symbol) c = @classes[name] @@ -69,6 +69,16 @@ module Parfait c end + # get or create the class by the (symbol) name + # notice that this method of creating classes implies Object superclass + def get_class_by_name! name + c = get_class_by_name(name) + return c if c + create_class name , get_class_by_name(:Object) + end + + # this is the way to instantiate classes (not Parfait::Class.new) + # so we get and keep exactly one per name def create_class name , superclass raise "uups #{name.class}" unless name.is_a? Symbol c = Class.new_object(name , superclass) diff --git a/lib/virtual/compiler/module_expression.rb b/lib/virtual/compiler/module_expression.rb index a18d185b..2eccdcf7 100644 --- a/lib/virtual/compiler/module_expression.rb +++ b/lib/virtual/compiler/module_expression.rb @@ -6,7 +6,7 @@ module Virtual end def self.compile_class expression , method - clazz = ::Space.space.get_class_by_name expression.name + clazz = Parfait::Space.object_space.get_class_by_name! expression.name puts "Created class #{clazz.name.inspect}" expression.expressions.each do |expr| # check if it's a function definition and add diff --git a/test/virtual/test_methods.rb b/test/virtual/test_methods.rb index 4d2d7dd1..efd8ae2c 100644 --- a/test/virtual/test_methods.rb +++ b/test/virtual/test_methods.rb @@ -17,6 +17,18 @@ module Virtual end end + def test_module + @string_input = <