From 5f70067fa0aae02be767e2a0df94d7cce850405d Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 4 Apr 2017 14:04:35 +0300 Subject: [PATCH] adds instance and class variables to vool --- lib/vool/compiler.rb | 9 +++++++++ lib/vool/variables.rb | 9 +++++++++ test/vool/test_variables.rb | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index e0a2ee0c..028fe347 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -92,6 +92,15 @@ module Vool def on_lvar expression LocalVariable.new(expression.children.first) end + + def on_ivar expression + InstanceVariable.new(expression.children.first.to_s[1 .. -1].to_sym) + end + + def on_cvar expression + ClassVariable.new(expression.children.first.to_s[2 .. -1].to_sym) + end + def on_lvasgn expression name = expression.children[0] value = process(expression.children[1]) diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb index 49af089e..909835ed 100644 --- a/lib/vool/variables.rb +++ b/lib/vool/variables.rb @@ -9,4 +9,13 @@ module Vool class LocalVariable < Statement include Named end + + class InstanceVariable < Statement + include Named + end + + class ClassVariable < Statement + include Named + end + end diff --git a/test/vool/test_variables.rb b/test/vool/test_variables.rb index d42798a7..eb53b6fe 100644 --- a/test/vool/test_variables.rb +++ b/test/vool/test_variables.rb @@ -10,5 +10,26 @@ module Vool # assert_equal SendStatement , lst.class # end + def test_instance_basic + lst = Compiler.compile( "@var" ) + assert_equal InstanceVariable , lst.class + assert_equal :var , lst.name + end + + def test_instance_return + lst = Compiler.compile( "return @var" ) + assert_equal InstanceVariable , lst.return_value.class + end + + def test_class_basic + lst = Compiler.compile( "@@var" ) + assert_equal ClassVariable , lst.class + assert_equal :var , lst.name + end + + def test_class_return + lst = Compiler.compile( "return @@var" ) + assert_equal ClassVariable , lst.return_value.class + end end end