koi-vm 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/koi-vm.gemspec +22 -1
- data/lib/koi-vm/opcodes/_opcode_constants.rb +9 -0
- data/lib/koi-vm/opcodes/_value_constants.rb +1 -0
- data/lib/koi-vm/opcodes/hash_operations/get_key.rb +14 -0
- data/lib/koi-vm/opcodes/hash_operations/has_key.rb +18 -0
- data/lib/koi-vm/opcodes/hash_operations/length.rb +19 -0
- data/lib/koi-vm/opcodes/hash_operations/num_pairs.rb +12 -0
- data/lib/koi-vm/opcodes/hash_operations/pairs.rb +22 -0
- data/lib/koi-vm/opcodes/hash_operations/push_hash.rb +10 -0
- data/lib/koi-vm/opcodes/hash_operations/set_key.rb +14 -0
- data/lib/koi-vm/opcodes/stack_operations/typeof.rb +2 -0
- data/test/unit/opcodes/hash_operations/get_key_test.rb +56 -0
- data/test/unit/opcodes/hash_operations/has_key_test.rb +56 -0
- data/test/unit/opcodes/hash_operations/length_test.rb +46 -0
- data/test/unit/opcodes/hash_operations/num_pairs_test.rb +46 -0
- data/test/unit/opcodes/hash_operations/pairs_test.rb +67 -0
- data/test/unit/opcodes/hash_operations/push_hash_test.rb +16 -0
- data/test/unit/opcodes/hash_operations/set_key_test.rb +56 -0
- data/test/unit/opcodes/stack_operations/typeof_test.rb +18 -0
- metadata +23 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/koi-vm.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{koi-vm}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Aaron Gough"]
|
@@ -45,6 +45,13 @@ Gem::Specification.new do |s|
|
|
45
45
|
"lib/koi-vm/opcodes/function_operations/push_function.rb",
|
46
46
|
"lib/koi-vm/opcodes/function_operations/return.rb",
|
47
47
|
"lib/koi-vm/opcodes/function_operations/tailcall.rb",
|
48
|
+
"lib/koi-vm/opcodes/hash_operations/get_key.rb",
|
49
|
+
"lib/koi-vm/opcodes/hash_operations/has_key.rb",
|
50
|
+
"lib/koi-vm/opcodes/hash_operations/length.rb",
|
51
|
+
"lib/koi-vm/opcodes/hash_operations/num_pairs.rb",
|
52
|
+
"lib/koi-vm/opcodes/hash_operations/pairs.rb",
|
53
|
+
"lib/koi-vm/opcodes/hash_operations/push_hash.rb",
|
54
|
+
"lib/koi-vm/opcodes/hash_operations/set_key.rb",
|
48
55
|
"lib/koi-vm/opcodes/io_operations/gets.rb",
|
49
56
|
"lib/koi-vm/opcodes/io_operations/print.rb",
|
50
57
|
"lib/koi-vm/opcodes/math_operations/add.rb",
|
@@ -87,6 +94,13 @@ Gem::Specification.new do |s|
|
|
87
94
|
"test/unit/opcodes/function_operations/push_function_test.rb",
|
88
95
|
"test/unit/opcodes/function_operations/return_test.rb",
|
89
96
|
"test/unit/opcodes/function_operations/tailcall_test.rb",
|
97
|
+
"test/unit/opcodes/hash_operations/get_key_test.rb",
|
98
|
+
"test/unit/opcodes/hash_operations/has_key_test.rb",
|
99
|
+
"test/unit/opcodes/hash_operations/length_test.rb",
|
100
|
+
"test/unit/opcodes/hash_operations/num_pairs_test.rb",
|
101
|
+
"test/unit/opcodes/hash_operations/pairs_test.rb",
|
102
|
+
"test/unit/opcodes/hash_operations/push_hash_test.rb",
|
103
|
+
"test/unit/opcodes/hash_operations/set_key_test.rb",
|
90
104
|
"test/unit/opcodes/io_operations/gets_test.rb",
|
91
105
|
"test/unit/opcodes/io_operations/print_test.rb",
|
92
106
|
"test/unit/opcodes/math_operations/add_test.rb",
|
@@ -135,6 +149,13 @@ Gem::Specification.new do |s|
|
|
135
149
|
"test/unit/opcodes/function_operations/push_function_test.rb",
|
136
150
|
"test/unit/opcodes/function_operations/return_test.rb",
|
137
151
|
"test/unit/opcodes/function_operations/tailcall_test.rb",
|
152
|
+
"test/unit/opcodes/hash_operations/get_key_test.rb",
|
153
|
+
"test/unit/opcodes/hash_operations/has_key_test.rb",
|
154
|
+
"test/unit/opcodes/hash_operations/length_test.rb",
|
155
|
+
"test/unit/opcodes/hash_operations/num_pairs_test.rb",
|
156
|
+
"test/unit/opcodes/hash_operations/pairs_test.rb",
|
157
|
+
"test/unit/opcodes/hash_operations/push_hash_test.rb",
|
158
|
+
"test/unit/opcodes/hash_operations/set_key_test.rb",
|
138
159
|
"test/unit/opcodes/io_operations/gets_test.rb",
|
139
160
|
"test/unit/opcodes/io_operations/print_test.rb",
|
140
161
|
"test/unit/opcodes/math_operations/add_test.rb",
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[GET_KEY] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least two items on the data stack" unless(vm.data_stack.length > 1)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-2][0] == HASH_)
|
7
|
+
key = vm.data_stack.pop
|
8
|
+
vm.data_stack.push( vm.data_stack.pop[1][key] )
|
9
|
+
vm.data_stack[-1] = [NIL_, nil] if(vm.data_stack[-1] == nil)
|
10
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[HAS_KEY] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least two items on the data stack" unless(vm.data_stack.length > 1)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-2][0] == HASH_)
|
7
|
+
key = vm.data_stack.pop
|
8
|
+
hash = vm.data_stack.pop[1]
|
9
|
+
if(hash.has_key?(key))
|
10
|
+
vm.data_stack.push([BOOL_, true])
|
11
|
+
else
|
12
|
+
vm.data_stack.push([BOOL_, false])
|
13
|
+
end
|
14
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[LENGTH] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least one item on the data stack" unless(vm.data_stack.length > 0)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-1][0] == HASH_)
|
7
|
+
hash = vm.data_stack.pop[1]
|
8
|
+
integer_keys = hash.keys.reject{|x| x[0] != INTEGER_ }.map{|x| x[1]}
|
9
|
+
highest_key = integer_keys.sort.last
|
10
|
+
if(highest_key.nil?)
|
11
|
+
vm.data_stack.push([INTEGER_, 0])
|
12
|
+
else
|
13
|
+
vm.data_stack.push([INTEGER_, highest_key + 1])
|
14
|
+
end
|
15
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[NUM_PAIRS] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least one item on the data stack" unless(vm.data_stack.length > 0)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-1][0] == HASH_)
|
7
|
+
vm.data_stack.push([INTEGER_, vm.data_stack.pop[1].length ])
|
8
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[PAIRS] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least one item on the data stack" unless(vm.data_stack.length > 0)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-1][0] == HASH_)
|
7
|
+
hash = vm.data_stack.pop[1]
|
8
|
+
hashed_hash = {}
|
9
|
+
index = 0
|
10
|
+
hash.each_pair do |k,v|
|
11
|
+
hashed_hash[[INTEGER_, index]] = [HASH_, {
|
12
|
+
[INTEGER_, 0] => k,
|
13
|
+
[INTEGER_, 1] => v
|
14
|
+
}]
|
15
|
+
index += 1
|
16
|
+
end
|
17
|
+
vm.data_stack.push([HASH_, hashed_hash])
|
18
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module KoiVM
|
2
|
+
class VM
|
3
|
+
|
4
|
+
@@instruction[SET_KEY] = Proc.new() do |vm|
|
5
|
+
raise StackError, "Expecting at least three items on the data stack" unless(vm.data_stack.length > 2)
|
6
|
+
raise StackError, "Expecting a hash" unless(vm.data_stack[-3][0] == HASH_)
|
7
|
+
value = vm.data_stack.pop
|
8
|
+
key = vm.data_stack.pop
|
9
|
+
vm.data_stack[-1][1][key] = value
|
10
|
+
vm.instruction_pointer = vm.instruction_pointer + 1
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -9,6 +9,8 @@ module KoiVM
|
|
9
9
|
vm.data_stack.push([STRING_, "integer"]) if(value[0] == INTEGER_)
|
10
10
|
vm.data_stack.push([STRING_, "float"]) if(value[0] == FLOAT_)
|
11
11
|
vm.data_stack.push([STRING_, "string"]) if(value[0] == STRING_)
|
12
|
+
vm.data_stack.push([STRING_, "hash"]) if(value[0] == HASH_)
|
13
|
+
vm.data_stack.push([STRING_, "function"]) if(value[0] == FUNCTION_)
|
12
14
|
vm.instruction_pointer = vm.instruction_pointer + 1
|
13
15
|
end
|
14
16
|
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class GetKeyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should get key in hash" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[HASH_, {[INTEGER_, 1] => [STRING_, "yay"]}], [INTEGER_, 1]]
|
10
|
+
vm.run [
|
11
|
+
GET_KEY
|
12
|
+
]
|
13
|
+
assert_equal [[STRING_, "yay"]], vm.data_stack
|
14
|
+
assert_equal 1, vm.instruction_pointer
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should get unset key in hash" do
|
18
|
+
vm = VM.new
|
19
|
+
vm.data_stack = [[HASH_, {}], [INTEGER_, 1]]
|
20
|
+
vm.run [
|
21
|
+
GET_KEY
|
22
|
+
]
|
23
|
+
assert_equal [[NIL_, nil]], vm.data_stack
|
24
|
+
assert_equal 1, vm.instruction_pointer
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should raise StackError if stack[-2] is not a hash" do
|
28
|
+
assert_raises StackError do
|
29
|
+
vm = VM.new
|
30
|
+
vm.data_stack = [[INTEGER_, 1], [INTEGER_, 1]]
|
31
|
+
vm.run [
|
32
|
+
GET_KEY
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should raise StackError if there is only 1 item on the stack" do
|
38
|
+
assert_raises StackError do
|
39
|
+
vm = VM.new
|
40
|
+
vm.data_stack = [[HASH_, {}]]
|
41
|
+
vm.run [
|
42
|
+
GET_KEY
|
43
|
+
]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test "should raise StackError if there are no items on the stack" do
|
48
|
+
assert_raises StackError do
|
49
|
+
vm = VM.new
|
50
|
+
vm.run [
|
51
|
+
GET_KEY
|
52
|
+
]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class HasKeyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should return true as hash has key" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[HASH_, {[INTEGER_, 1] => [STRING_, "yay"]}], [INTEGER_, 1]]
|
10
|
+
vm.run [
|
11
|
+
HAS_KEY
|
12
|
+
]
|
13
|
+
assert_equal [[BOOL_, true]], vm.data_stack
|
14
|
+
assert_equal 1, vm.instruction_pointer
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should return false as hash does not have key" do
|
18
|
+
vm = VM.new
|
19
|
+
vm.data_stack = [[HASH_, {}], [INTEGER_, 1]]
|
20
|
+
vm.run [
|
21
|
+
HAS_KEY
|
22
|
+
]
|
23
|
+
assert_equal [[BOOL_, false]], vm.data_stack
|
24
|
+
assert_equal 1, vm.instruction_pointer
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should raise StackError if stack[-2] is not a hash" do
|
28
|
+
assert_raises StackError do
|
29
|
+
vm = VM.new
|
30
|
+
vm.data_stack = [[INTEGER_, 1], [INTEGER_, 1]]
|
31
|
+
vm.run [
|
32
|
+
HAS_KEY
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should raise StackError if there is only 1 item on the stack" do
|
38
|
+
assert_raises StackError do
|
39
|
+
vm = VM.new
|
40
|
+
vm.data_stack = [[HASH_, {}]]
|
41
|
+
vm.run [
|
42
|
+
HAS_KEY
|
43
|
+
]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test "should raise StackError if there are no items on the stack" do
|
48
|
+
assert_raises StackError do
|
49
|
+
vm = VM.new
|
50
|
+
vm.run [
|
51
|
+
HAS_KEY
|
52
|
+
]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class LengthTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should return value of highest integer key + 1" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[HASH_, {[INTEGER_, 1] => [STRING_, "yay"], [INTEGER_, 2] => [STRING_, "yay"], [INTEGER_, 6] => [STRING_, "yay"]}]]
|
10
|
+
vm.run [
|
11
|
+
LENGTH
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 7]], vm.data_stack
|
14
|
+
assert_equal 1, vm.instruction_pointer
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should return 0" do
|
18
|
+
vm = VM.new
|
19
|
+
vm.data_stack = [[HASH_, {[STRING_, "1"] => [STRING_, "yay"], [STRING_, "2"] => [STRING_, "yay"], [STRING_, "3"] => [STRING_, "yay"]}]]
|
20
|
+
vm.run [
|
21
|
+
LENGTH
|
22
|
+
]
|
23
|
+
assert_equal [[INTEGER_, 0]], vm.data_stack
|
24
|
+
assert_equal 1, vm.instruction_pointer
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should raise StackError if stack[-1] is not a hash" do
|
28
|
+
assert_raises StackError do
|
29
|
+
vm = VM.new
|
30
|
+
vm.data_stack = [[INTEGER_, 1]]
|
31
|
+
vm.run [
|
32
|
+
LENGTH
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should raise StackError if there are no items on the stack" do
|
38
|
+
assert_raises StackError do
|
39
|
+
vm = VM.new
|
40
|
+
vm.run [
|
41
|
+
LENGTH
|
42
|
+
]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class NumPairsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should return number of key-value pairs in hash" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[HASH_, {[INTEGER_, 1] => [STRING_, "yay"], [INTEGER_, 2] => [STRING_, "yay"], [INTEGER_, 6] => [STRING_, "yay"]}]]
|
10
|
+
vm.run [
|
11
|
+
NUM_PAIRS
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 3]], vm.data_stack
|
14
|
+
assert_equal 1, vm.instruction_pointer
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should return 0" do
|
18
|
+
vm = VM.new
|
19
|
+
vm.data_stack = [[HASH_, {}]]
|
20
|
+
vm.run [
|
21
|
+
NUM_PAIRS
|
22
|
+
]
|
23
|
+
assert_equal [[INTEGER_, 0]], vm.data_stack
|
24
|
+
assert_equal 1, vm.instruction_pointer
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should raise StackError if stack[-1] is not a hash" do
|
28
|
+
assert_raises StackError do
|
29
|
+
vm = VM.new
|
30
|
+
vm.data_stack = [[INTEGER_, 1]]
|
31
|
+
vm.run [
|
32
|
+
NUM_PAIRS
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should raise StackError if there are no items on the stack" do
|
38
|
+
assert_raises StackError do
|
39
|
+
vm = VM.new
|
40
|
+
vm.run [
|
41
|
+
NUM_PAIRS
|
42
|
+
]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class PairsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should return new hash containing all the pairs from the previous hash with numeric indices" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [
|
10
|
+
[HASH_, {
|
11
|
+
[INTEGER_, 1] => [STRING_, "yay"],
|
12
|
+
[INTEGER_, 2] => [STRING_, "yay"],
|
13
|
+
[INTEGER_, 6] => [STRING_, "yay"]
|
14
|
+
}]
|
15
|
+
]
|
16
|
+
vm.run [
|
17
|
+
PAIRS
|
18
|
+
]
|
19
|
+
assert_equal [
|
20
|
+
[HASH_, {
|
21
|
+
[INTEGER_, 0] => [ HASH_, {
|
22
|
+
[INTEGER_, 0] => [INTEGER_, 1],
|
23
|
+
[INTEGER_, 1] => [STRING_, "yay"]
|
24
|
+
}],
|
25
|
+
[INTEGER_, 1] => [ HASH_, {
|
26
|
+
[INTEGER_, 0] => [INTEGER_, 2],
|
27
|
+
[INTEGER_, 1] => [STRING_, "yay"]
|
28
|
+
}],
|
29
|
+
[INTEGER_, 2] => [ HASH_, {
|
30
|
+
[INTEGER_, 0] => [INTEGER_, 6],
|
31
|
+
[INTEGER_, 1] => [STRING_, "yay"]
|
32
|
+
}]
|
33
|
+
}]
|
34
|
+
], vm.data_stack
|
35
|
+
assert_equal 1, vm.instruction_pointer
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should return empty hash" do
|
39
|
+
vm = VM.new
|
40
|
+
vm.data_stack = [[HASH_, {}]]
|
41
|
+
vm.run [
|
42
|
+
PAIRS
|
43
|
+
]
|
44
|
+
assert_equal [[HASH_, {}]], vm.data_stack
|
45
|
+
assert_equal 1, vm.instruction_pointer
|
46
|
+
end
|
47
|
+
|
48
|
+
test "should raise StackError if stack[-1] is not a hash" do
|
49
|
+
assert_raises StackError do
|
50
|
+
vm = VM.new
|
51
|
+
vm.data_stack = [[INTEGER_, 1]]
|
52
|
+
vm.run [
|
53
|
+
PAIRS
|
54
|
+
]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
test "should raise StackError if there are no items on the stack" do
|
59
|
+
assert_raises StackError do
|
60
|
+
vm = VM.new
|
61
|
+
vm.run [
|
62
|
+
PAIRS
|
63
|
+
]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class PushHashTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should push hash onto stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.run [
|
10
|
+
PUSH_HASH
|
11
|
+
]
|
12
|
+
assert_equal [[HASH_, {}]], vm.data_stack
|
13
|
+
assert_equal 1, vm.instruction_pointer
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class SetKeyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVM
|
6
|
+
|
7
|
+
test "should set key in hash" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[HASH_, {}], [INTEGER_, 1], [STRING_, "yay"]]
|
10
|
+
vm.run [
|
11
|
+
SET_KEY
|
12
|
+
]
|
13
|
+
assert_equal [[HASH_, {[INTEGER_, 1] => [STRING_, "yay"]}]], vm.data_stack
|
14
|
+
assert_equal 1, vm.instruction_pointer
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should raise StackError if stack[-3] is not a hash" do
|
18
|
+
assert_raises StackError do
|
19
|
+
vm = VM.new
|
20
|
+
vm.data_stack = [[INTEGER_, 1], [INTEGER_, 1], [STRING_, "yay"]]
|
21
|
+
vm.run [
|
22
|
+
SET_KEY
|
23
|
+
]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should raise StackError if there are only 2 items on the stack" do
|
28
|
+
assert_raises StackError do
|
29
|
+
vm = VM.new
|
30
|
+
vm.data_stack = [[HASH_, {}], [INTEGER_, 1]]
|
31
|
+
vm.run [
|
32
|
+
SET_KEY
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should raise StackError if there is only 1 item on the stack" do
|
38
|
+
assert_raises StackError do
|
39
|
+
vm = VM.new
|
40
|
+
vm.data_stack = [[HASH_, {}]]
|
41
|
+
vm.run [
|
42
|
+
SET_KEY
|
43
|
+
]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test "should raise StackError if there are no items on the stack" do
|
48
|
+
assert_raises StackError do
|
49
|
+
vm = VM.new
|
50
|
+
vm.run [
|
51
|
+
SET_KEY
|
52
|
+
]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -49,6 +49,24 @@ class TypeofTest < Test::Unit::TestCase
|
|
49
49
|
assert_equal [[STRING_, "string"]], vm.data_stack
|
50
50
|
end
|
51
51
|
|
52
|
+
test "should push string representing type of topmost stack item onto the stack (hash)" do
|
53
|
+
vm = VM.new
|
54
|
+
vm.data_stack = [[HASH_, {}]]
|
55
|
+
vm.run [
|
56
|
+
TYPEOF
|
57
|
+
]
|
58
|
+
assert_equal [[STRING_, "hash"]], vm.data_stack
|
59
|
+
end
|
60
|
+
|
61
|
+
test "should push string representing type of topmost stack item onto the stack (function)" do
|
62
|
+
vm = VM.new
|
63
|
+
vm.data_stack = [[FUNCTION_, 12]]
|
64
|
+
vm.run [
|
65
|
+
TYPEOF
|
66
|
+
]
|
67
|
+
assert_equal [[STRING_, "function"]], vm.data_stack
|
68
|
+
end
|
69
|
+
|
52
70
|
test "should raise StackError if there are no items on the stack" do
|
53
71
|
assert_raises StackError do
|
54
72
|
vm = VM.new
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Aaron Gough
|
@@ -56,6 +56,13 @@ files:
|
|
56
56
|
- lib/koi-vm/opcodes/function_operations/push_function.rb
|
57
57
|
- lib/koi-vm/opcodes/function_operations/return.rb
|
58
58
|
- lib/koi-vm/opcodes/function_operations/tailcall.rb
|
59
|
+
- lib/koi-vm/opcodes/hash_operations/get_key.rb
|
60
|
+
- lib/koi-vm/opcodes/hash_operations/has_key.rb
|
61
|
+
- lib/koi-vm/opcodes/hash_operations/length.rb
|
62
|
+
- lib/koi-vm/opcodes/hash_operations/num_pairs.rb
|
63
|
+
- lib/koi-vm/opcodes/hash_operations/pairs.rb
|
64
|
+
- lib/koi-vm/opcodes/hash_operations/push_hash.rb
|
65
|
+
- lib/koi-vm/opcodes/hash_operations/set_key.rb
|
59
66
|
- lib/koi-vm/opcodes/io_operations/gets.rb
|
60
67
|
- lib/koi-vm/opcodes/io_operations/print.rb
|
61
68
|
- lib/koi-vm/opcodes/math_operations/add.rb
|
@@ -98,6 +105,13 @@ files:
|
|
98
105
|
- test/unit/opcodes/function_operations/push_function_test.rb
|
99
106
|
- test/unit/opcodes/function_operations/return_test.rb
|
100
107
|
- test/unit/opcodes/function_operations/tailcall_test.rb
|
108
|
+
- test/unit/opcodes/hash_operations/get_key_test.rb
|
109
|
+
- test/unit/opcodes/hash_operations/has_key_test.rb
|
110
|
+
- test/unit/opcodes/hash_operations/length_test.rb
|
111
|
+
- test/unit/opcodes/hash_operations/num_pairs_test.rb
|
112
|
+
- test/unit/opcodes/hash_operations/pairs_test.rb
|
113
|
+
- test/unit/opcodes/hash_operations/push_hash_test.rb
|
114
|
+
- test/unit/opcodes/hash_operations/set_key_test.rb
|
101
115
|
- test/unit/opcodes/io_operations/gets_test.rb
|
102
116
|
- test/unit/opcodes/io_operations/print_test.rb
|
103
117
|
- test/unit/opcodes/math_operations/add_test.rb
|
@@ -174,6 +188,13 @@ test_files:
|
|
174
188
|
- test/unit/opcodes/function_operations/push_function_test.rb
|
175
189
|
- test/unit/opcodes/function_operations/return_test.rb
|
176
190
|
- test/unit/opcodes/function_operations/tailcall_test.rb
|
191
|
+
- test/unit/opcodes/hash_operations/get_key_test.rb
|
192
|
+
- test/unit/opcodes/hash_operations/has_key_test.rb
|
193
|
+
- test/unit/opcodes/hash_operations/length_test.rb
|
194
|
+
- test/unit/opcodes/hash_operations/num_pairs_test.rb
|
195
|
+
- test/unit/opcodes/hash_operations/pairs_test.rb
|
196
|
+
- test/unit/opcodes/hash_operations/push_hash_test.rb
|
197
|
+
- test/unit/opcodes/hash_operations/set_key_test.rb
|
177
198
|
- test/unit/opcodes/io_operations/gets_test.rb
|
178
199
|
- test/unit/opcodes/io_operations/print_test.rb
|
179
200
|
- test/unit/opcodes/math_operations/add_test.rb
|