From d6e346d9c03475bb0d28784ccdb9230c0161db5f Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 4 Apr 2017 18:10:28 +0300 Subject: [PATCH] adds instance assignment to vool --- lib/vool/assignment_statement.rb | 3 ++- lib/vool/compiler.rb | 25 ++++++++++++------------- test/vool/test_assignment_statement.rb | 9 +++++++++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/vool/assignment_statement.rb b/lib/vool/assignment_statement.rb index ef9168df..7e956968 100644 --- a/lib/vool/assignment_statement.rb +++ b/lib/vool/assignment_statement.rb @@ -7,5 +7,6 @@ module Vool end class LocalAssignment < Assignment end - + class InstanceAssignment < Assignment + end end diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 173b5265..c8e3738d 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -94,7 +94,7 @@ module Vool end def on_ivar expression - InstanceVariable.new(expression.children.first.to_s[1 .. -1].to_sym) + InstanceVariable.new(instance_name(expression.children.first)) end def on_cvar expression @@ -109,12 +109,19 @@ module Vool ModuleName.new(expression.children[1]) end + # Assignements def on_lvasgn expression name = expression.children[0] value = process(expression.children[1]) LocalAssignment.new(name,value) end + def on_ivasgn expression + name = expression.children[0] + value = process(expression.children[1]) + InstanceAssignment.new(instance_name(name),value) + end + def on_return statement return_value = process(statement.children.first) ReturnStatement.new( return_value ) @@ -166,10 +173,6 @@ module Vool w end - def on_receiver expression - process expression.children.first - end - def on_send statement kids = statement.children.dup receiver = kids.shift @@ -197,14 +200,6 @@ module Vool w end - # def on_name statement - # NameStatement.new(statement.children.first) - # end - - # def on_class_name expression - # ClassStatement.new(expression.children.first) - # end - def on_assignment statement name , value = *statement w = Assignment.new() @@ -215,6 +210,10 @@ module Vool private + def instance_name sym + sym.to_s[1 .. -1].to_sym + end + def get_name( statement ) return nil unless statement raise "Not const #{statement}" unless statement.type == :const diff --git a/test/vool/test_assignment_statement.rb b/test/vool/test_assignment_statement.rb index f493c620..52a192a8 100644 --- a/test/vool/test_assignment_statement.rb +++ b/test/vool/test_assignment_statement.rb @@ -12,5 +12,14 @@ module Vool assert_equal :foo , lst.name end + def test_instance + lst = Compiler.compile( "@foo = bar") + assert_equal InstanceAssignment , lst.class + end + def test_instance_name + lst = Compiler.compile( "@foo = bar") + assert_equal :foo , lst.name + end + end end