add vool hashes
This commit is contained in:
parent
d5af6dad42
commit
93f9d16209
@ -6,6 +6,7 @@ end
|
|||||||
require_relative "vool/array_statement"
|
require_relative "vool/array_statement"
|
||||||
require_relative "vool/basic_values"
|
require_relative "vool/basic_values"
|
||||||
require_relative "vool/class_statement"
|
require_relative "vool/class_statement"
|
||||||
|
require_relative "vool/hash_statement"
|
||||||
require_relative "vool/method_statement"
|
require_relative "vool/method_statement"
|
||||||
|
|
||||||
require_relative "vool/while_statement"
|
require_relative "vool/while_statement"
|
||||||
|
@ -71,6 +71,15 @@ module Vool
|
|||||||
ArrayStatement.new expression.children.collect{ |elem| process(elem) }
|
ArrayStatement.new expression.children.collect{ |elem| process(elem) }
|
||||||
end
|
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
|
def on_return statement
|
||||||
w = ReturnStatement.new()
|
w = ReturnStatement.new()
|
||||||
w.return_value = process(statement.children.first)
|
w.return_value = process(statement.children.first)
|
||||||
|
17
lib/vool/hash_statement.rb
Normal file
17
lib/vool/hash_statement.rb
Normal file
@ -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
|
@ -2,4 +2,5 @@ require_relative "helper"
|
|||||||
require_relative "test_array_statement"
|
require_relative "test_array_statement"
|
||||||
require_relative "test_basic_values"
|
require_relative "test_basic_values"
|
||||||
require_relative "test_class_statement"
|
require_relative "test_class_statement"
|
||||||
|
require_relative "test_hash_statement"
|
||||||
require_relative "test_method_statement"
|
require_relative "test_method_statement"
|
||||||
|
35
test/vool/test_hash_statement.rb
Normal file
35
test/vool/test_hash_statement.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user