From 539ab692a30fc321481d1bb37d7cc529fa9e4e85 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 2 Apr 2017 13:24:09 +0300 Subject: [PATCH] add scope and kwbegin scope is just a list of statements --- lib/vool.rb | 2 ++ lib/vool/compiler.rb | 7 ++++++- lib/vool/statements.rb | 6 ++++++ lib/vool/variables.rb | 12 ++++++++++++ test/vool/test_basic_values.rb | 11 ++++++++++- test/vool/test_send_statement.rb | 1 - test/vool/test_variables.rb | 14 ++++++++++++++ 7 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 lib/vool/variables.rb create mode 100644 test/vool/test_variables.rb diff --git a/lib/vool.rb b/lib/vool.rb index 2eaf0f7c..37a6b5fc 100644 --- a/lib/vool.rb +++ b/lib/vool.rb @@ -13,8 +13,10 @@ require_relative "vool/method_statement" require_relative "vool/while_statement" require_relative "vool/if_statement" require_relative "vool/return_statement" +require_relative "vool/return_statement" require_relative "vool/statements" require_relative "vool/send_statement" require_relative "vool/operator_expression" +require_relative "vool/variables" require_relative "vool/compiler" diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 7be45c35..088d26f6 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -8,7 +8,7 @@ module Vool # default to error, so non implemented stuff shows early def handler_missing(node) - raise "Not implemented #{node.type}" + raise "Not implemented #{node.type} #{node}" end def on_class( statement ) @@ -69,6 +69,9 @@ module Vool def on_dsym raise "Not implemented dynamix symbols (with interpolation)" end + def on_kwbegin expression + ScopeStatement.new process_all( expression.children ) + end # Array + Hashes def on_array expression @@ -86,6 +89,7 @@ module Vool #Variables def on_lvasgn expression + puts "EXP #{expression}" name = expression.children[0] value = process(expression.children[1]) LocalAssignment.new(name,value) @@ -144,6 +148,7 @@ module Vool end def on_send statement + puts "SEND #{statement}" kids = statement.children.dup receiver = kids.shift name = kids.shift diff --git a/lib/vool/statements.rb b/lib/vool/statements.rb index ffa51a9d..f7281801 100644 --- a/lib/vool/statements.rb +++ b/lib/vool/statements.rb @@ -1,5 +1,11 @@ module Vool class Statements < Statement attr_accessor :statements + def initialize(statements) + @statements = statements + end + end + + class ScopeStatement < Statements end end diff --git a/lib/vool/variables.rb b/lib/vool/variables.rb new file mode 100644 index 00000000..49af089e --- /dev/null +++ b/lib/vool/variables.rb @@ -0,0 +1,12 @@ +module Vool + module Named + attr_accessor :name + def initialize name + @name = name + end + end + + class LocalVariable < Statement + include Named + end +end diff --git a/test/vool/test_basic_values.rb b/test/vool/test_basic_values.rb index ac896847..e6db31d9 100644 --- a/test/vool/test_basic_values.rb +++ b/test/vool/test_basic_values.rb @@ -5,7 +5,7 @@ module Vool def test_self lst = Compiler.compile( "self") - assert_equal SelfStatement , lst.class + assert_equal SelfStatement , lst.class end def test_nil lst = Compiler.compile( "nil") @@ -36,5 +36,14 @@ module Vool Compiler.compile( '"dstr#{self}"') end end + + def test_scope + lst = Compiler.compile( "begin ; 1 ; end") + assert_equal ScopeStatement , lst.class , lst.inspect + end + def test_scope_contents + lst = Compiler.compile( "begin ; 1 ; end") + assert_equal 1 , lst.statements.first.value + end end end diff --git a/test/vool/test_send_statement.rb b/test/vool/test_send_statement.rb index 7eef28e2..fc14461b 100644 --- a/test/vool/test_send_statement.rb +++ b/test/vool/test_send_statement.rb @@ -36,6 +36,5 @@ module Vool lst = Compiler.compile( "bar(1)") assert_equal 1 , lst.arguments.first.value end - end end diff --git a/test/vool/test_variables.rb b/test/vool/test_variables.rb new file mode 100644 index 00000000..793b3bb9 --- /dev/null +++ b/test/vool/test_variables.rb @@ -0,0 +1,14 @@ +require_relative "../helper" + +module Vool + class TestVariables < MiniTest::Test + + # "free standing" local can not be tested as it will result in send + # in other words ther is no way of knowing if a name is variable or method + def ptest_send_to_local + lst = Compiler.compile( "foo = 1 ; foo.bar") + assert_equal LocalVariable , lst.receiver + end + + end +end