From 107e3e6d583d0b2cfffdf8a17a1e8d8943bc8802 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 18 Dec 2016 20:05:11 +0200 Subject: [PATCH] pick up instance variables from the class Crude first set to creating types --- lib/melon/compiler.rb | 6 ++++-- lib/melon/type_collector.rb | 24 ++++++++++++++++++++++++ test/melon/compiler/test_class.rb | 7 +++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 lib/melon/type_collector.rb diff --git a/lib/melon/compiler.rb b/lib/melon/compiler.rb index 99e996d0..cbccbc40 100644 --- a/lib/melon/compiler.rb +++ b/lib/melon/compiler.rb @@ -1,3 +1,4 @@ +require_relative "type_collector" module Melon @@ -15,8 +16,9 @@ module Melon end def on_class statement - name , sup , _ = *statement - Parfait::Space.object_space.create_class(get_name(name) , get_name(sup) ) + name , sup , body = *statement + clazz = Parfait::Space.object_space.create_class(get_name(name) , get_name(sup) ) + clazz.set_instance_names( TypeCollector.new.collect(body) ) end end end diff --git a/lib/melon/type_collector.rb b/lib/melon/type_collector.rb new file mode 100644 index 00000000..0e5991eb --- /dev/null +++ b/lib/melon/type_collector.rb @@ -0,0 +1,24 @@ +module Melon + + class TypeCollector < AST::Processor + + def initialize + @ivar_names = [] + end + + def collect(statement) + process statement + @ivar_names + end + + def on_ivar(statement) + @ivar_names.push statement.children[0].to_s[1..-1].to_sym + end + + def handler_missing(node) + node.children.each do |kid | + process(kid) if kid.is_a?(AST::Node) + end + end + end +end diff --git a/test/melon/compiler/test_class.rb b/test/melon/compiler/test_class.rb index 092c8bb1..14e67f93 100644 --- a/test/melon/compiler/test_class.rb +++ b/test/melon/compiler/test_class.rb @@ -6,6 +6,7 @@ module Melon def setup Register.machine.boot unless Register.machine.booted end + def test_creates_class_without_deriviation Compiler.compile "class Testing ; end" assert t = Parfait::Space.object_space.get_class_by_name(:Testing) , "No classes created" @@ -18,5 +19,11 @@ module Melon assert_equal :List , t.super_class_name end + def test_picks_up_an_ivar_name + Compiler.compile "class TestIvar < Object ; def meth; return @ivar;end; end" + itest = Parfait::Space.object_space.get_class_by_name(:TestIvar) + assert itest.instance_names.include?(:ivar) , itest.instance_names.inspect + end + end end