From 93f9d162095cdd08f8c429720bef834e1856f257 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 2 Apr 2017 10:57:39 +0300 Subject: [PATCH] add vool hashes --- lib/vool.rb | 1 + lib/vool/compiler.rb | 9 ++++++++ lib/vool/hash_statement.rb | 17 ++++++++++++++++ test/vool/test_compiler.rb | 1 + test/vool/test_hash_statement.rb | 35 ++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 lib/vool/hash_statement.rb create mode 100644 test/vool/test_hash_statement.rb diff --git a/lib/vool.rb b/lib/vool.rb index ca431eab..a31c4aed 100644 --- a/lib/vool.rb +++ b/lib/vool.rb @@ -6,6 +6,7 @@ end require_relative "vool/array_statement" require_relative "vool/basic_values" require_relative "vool/class_statement" +require_relative "vool/hash_statement" require_relative "vool/method_statement" require_relative "vool/while_statement" diff --git a/lib/vool/compiler.rb b/lib/vool/compiler.rb index 7ba59347..c5b6b316 100644 --- a/lib/vool/compiler.rb +++ b/lib/vool/compiler.rb @@ -71,6 +71,15 @@ module Vool ArrayStatement.new expression.children.collect{ |elem| process(elem) } end + def on_hash expression + hash = HashStatement.new + expression.children.each do |elem| + raise "Hash error, hash contains non pair: #{elem.type}" if elem.type != :pair + hash.add( process(elem.children[0]) , process(elem.children[1]) ) + end + hash + end + def on_return statement w = ReturnStatement.new() w.return_value = process(statement.children.first) diff --git a/lib/vool/hash_statement.rb b/lib/vool/hash_statement.rb new file mode 100644 index 00000000..af2adea3 --- /dev/null +++ b/lib/vool/hash_statement.rb @@ -0,0 +1,17 @@ +module Vool + class HashStatement + attr_reader :hash + + def initialize( ) + @hash = {} + end + + def add(key , value) + @hash[key] = value + end + + def length + @hash.length + end + end +end diff --git a/test/vool/test_compiler.rb b/test/vool/test_compiler.rb index 20890910..be315aab 100644 --- a/test/vool/test_compiler.rb +++ b/test/vool/test_compiler.rb @@ -2,4 +2,5 @@ require_relative "helper" require_relative "test_array_statement" require_relative "test_basic_values" require_relative "test_class_statement" +require_relative "test_hash_statement" require_relative "test_method_statement" diff --git a/test/vool/test_hash_statement.rb b/test/vool/test_hash_statement.rb new file mode 100644 index 00000000..32a0df5e --- /dev/null +++ b/test/vool/test_hash_statement.rb @@ -0,0 +1,35 @@ +require_relative "../helper" + +module Vool + class HashArray < MiniTest::Test + + def test_empty + lst = Compiler.compile( "{}") + assert_equal HashStatement , lst.class + end + def test_empty_length + lst = Compiler.compile( "{}") + assert_equal 0 , lst.length + end + def test_one + lst = Compiler.compile( "{ 1 => 2}") + assert_equal HashStatement , lst.class + end + def test_one_length + lst = Compiler.compile( "{ 1 => 2}") + assert_equal 1 , lst.length + end + def test_one_pair + lst = Compiler.compile( "{ 1 => 2}") + assert_equal 1 , lst.hash.keys.first.value + end + def test_two_length + lst = Compiler.compile( "{ sym: :works , 'string_too' => 2}") + assert_equal 2 , lst.length + end + def test_two_key_one + lst = Compiler.compile( "{ sym: :works , 'string_too' => 2}") + assert_equal :sym , lst.hash.keys.first.value + end + end +end