koi-vm-ruby 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README.rdoc +64 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/architecture.rdoc +112 -0
- data/examples/blastoff.rb +20 -0
- data/examples/hello_world.rb +8 -0
- data/koi-vm.gemspec +198 -0
- data/lib/.gitkeep +0 -0
- data/lib/koi-vm-ruby.rb +10 -0
- data/lib/koi-vm-ruby/accessors.rb +63 -0
- data/lib/koi-vm-ruby/core.rb +48 -0
- data/lib/koi-vm-ruby/exceptions.rb +8 -0
- data/lib/koi-vm-ruby/helpers.rb +5 -0
- data/lib/koi-vm-ruby/opcodes/_opcode_constants.rb +68 -0
- data/lib/koi-vm-ruby/opcodes/_value_constants.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/comparative_operations/equal.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/comparative_operations/greater_than.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/comparative_operations/invert.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/comparative_operations/less_than.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/control_operations/exit.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/control_operations/no_op.rb +9 -0
- data/lib/koi-vm-ruby/opcodes/flow_control_operations/jump.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/flow_control_operations/jump_if.rb +16 -0
- data/lib/koi-vm-ruby/opcodes/flow_control_operations/jump_unless.rb +16 -0
- data/lib/koi-vm-ruby/opcodes/function_operations/call.rb +13 -0
- data/lib/koi-vm-ruby/opcodes/function_operations/push_function.rb +19 -0
- data/lib/koi-vm-ruby/opcodes/function_operations/return.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/function_operations/tailcall.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/get_key.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/has_key.rb +18 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/length.rb +19 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/num_pairs.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/pairs.rb +22 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/push_hash.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/hash_operations/set_key.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/io_operations/gets.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/io_operations/print.rb +13 -0
- data/lib/koi-vm-ruby/opcodes/math_operations/add.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/math_operations/divide.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/math_operations/multiply.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/math_operations/subtract.rb +14 -0
- data/lib/koi-vm-ruby/opcodes/push_operations/push_bool.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/push_operations/push_float.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/push_operations/push_int.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/push_operations/push_nil.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/push_operations/push_string.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/dup.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/pop.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/stksize.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/swap.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/top.rb +13 -0
- data/lib/koi-vm-ruby/opcodes/stack_operations/typeof.rb +18 -0
- data/lib/koi-vm-ruby/opcodes/string_operations/concat.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/string_operations/strlen.rb +12 -0
- data/lib/koi-vm-ruby/opcodes/string_operations/to_string.rb +16 -0
- data/lib/koi-vm-ruby/opcodes/variable_operations/get_global.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/variable_operations/get_local.rb +10 -0
- data/lib/koi-vm-ruby/opcodes/variable_operations/set_global.rb +11 -0
- data/lib/koi-vm-ruby/opcodes/variable_operations/set_local.rb +11 -0
- data/test/.gitkeep +0 -0
- data/test/performance/.gitignore +1 -0
- data/test/performance/simple_benchmark.rb +87 -0
- data/test/setup/test_unit_extensions.rb +21 -0
- data/test/test_helper.rb +13 -0
- data/test/unit/opcodes/comparative_operations/equal_test.rb +116 -0
- data/test/unit/opcodes/comparative_operations/greater_than_test.rb +53 -0
- data/test/unit/opcodes/comparative_operations/invert_test.rb +44 -0
- data/test/unit/opcodes/comparative_operations/less_than_test.rb +53 -0
- data/test/unit/opcodes/control_operations/exit_test.rb +20 -0
- data/test/unit/opcodes/control_operations/no_op_test.rb +17 -0
- data/test/unit/opcodes/flow_control_operations/jump_if_test.rb +50 -0
- data/test/unit/opcodes/flow_control_operations/jump_test.rb +36 -0
- data/test/unit/opcodes/flow_control_operations/jump_unless_test.rb +50 -0
- data/test/unit/opcodes/function_operations/call_test.rb +39 -0
- data/test/unit/opcodes/function_operations/push_function_test.rb +41 -0
- data/test/unit/opcodes/function_operations/return_test.rb +31 -0
- data/test/unit/opcodes/function_operations/tailcall_test.rb +41 -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/io_operations/gets_test.rb +38 -0
- data/test/unit/opcodes/io_operations/print_test.rb +42 -0
- data/test/unit/opcodes/math_operations/add_test.rb +62 -0
- data/test/unit/opcodes/math_operations/divide_test.rb +62 -0
- data/test/unit/opcodes/math_operations/multiply_test.rb +62 -0
- data/test/unit/opcodes/math_operations/subtract_test.rb +62 -0
- data/test/unit/opcodes/push_operations/push_bool_test.rb +36 -0
- data/test/unit/opcodes/push_operations/push_float_test.rb +27 -0
- data/test/unit/opcodes/push_operations/push_int_test.rb +27 -0
- data/test/unit/opcodes/push_operations/push_nil_test.rb +16 -0
- data/test/unit/opcodes/push_operations/push_string_test.rb +27 -0
- data/test/unit/opcodes/stack_operations/dup_test.rb +25 -0
- data/test/unit/opcodes/stack_operations/pop_test.rb +28 -0
- data/test/unit/opcodes/stack_operations/stksize_test.rb +20 -0
- data/test/unit/opcodes/stack_operations/swap_test.rb +36 -0
- data/test/unit/opcodes/stack_operations/top_test.rb +45 -0
- data/test/unit/opcodes/stack_operations/typeof_test.rb +79 -0
- data/test/unit/opcodes/string_operations/concat_test.rb +65 -0
- data/test/unit/opcodes/string_operations/strlen_test.rb +44 -0
- data/test/unit/opcodes/string_operations/to_string_test.rb +70 -0
- data/test/unit/opcodes/variable_operations/get_global_test.rb +18 -0
- data/test/unit/opcodes/variable_operations/get_local_test.rb +18 -0
- data/test/unit/opcodes/variable_operations/set_global_test.rb +27 -0
- data/test/unit/opcodes/variable_operations/set_local_test.rb +27 -0
- data/test/unit/vm_initialization_test.rb +86 -0
- metadata +224 -0
@@ -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 KoiVMRuby
|
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
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class GetsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
require "stringio"
|
8
|
+
|
9
|
+
test "should get string from stdin and push it onto stack as a string" do
|
10
|
+
input = "Test"
|
11
|
+
old_stdin = $stdin
|
12
|
+
buffer = StringIO.new
|
13
|
+
$stdin = buffer
|
14
|
+
buffer.string = input
|
15
|
+
vm = VM.new
|
16
|
+
vm.run [
|
17
|
+
GETS
|
18
|
+
]
|
19
|
+
assert_equal [[STRING_, input]], vm.data_stack
|
20
|
+
$stdin = old_stdin
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should get newline separated strings from stdin and push onto stack as a string" do
|
24
|
+
input = "Test\nstring"
|
25
|
+
old_stdin = $stdin
|
26
|
+
buffer = StringIO.new
|
27
|
+
$stdin = buffer
|
28
|
+
buffer.string = input
|
29
|
+
vm = VM.new
|
30
|
+
vm.run [
|
31
|
+
GETS,
|
32
|
+
GETS
|
33
|
+
]
|
34
|
+
assert_equal [[STRING_, "Test\n"], [STRING_, "string"]], vm.data_stack
|
35
|
+
$stdin = old_stdin
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class PrintTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
require "stringio"
|
8
|
+
|
9
|
+
test "should print topmost string value from stack" do
|
10
|
+
old_stdout = $stdout
|
11
|
+
buffer = StringIO.new
|
12
|
+
$stdout = buffer
|
13
|
+
vm = VM.new
|
14
|
+
vm.data_stack = [[STRING_, "test"]]
|
15
|
+
vm.run [
|
16
|
+
PRINT
|
17
|
+
]
|
18
|
+
assert_equal [], vm.data_stack
|
19
|
+
assert_equal "test", buffer.string
|
20
|
+
$stdout = old_stdout
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should raise StackError if topmost item is not a string" do
|
24
|
+
assert_raises StackError do
|
25
|
+
vm = VM.new
|
26
|
+
vm.data_stack = [[INTEGER_, 1]]
|
27
|
+
vm.run [
|
28
|
+
PRINT
|
29
|
+
]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
test "should raise StackError if there are no items on the stack" do
|
34
|
+
assert_raises StackError do
|
35
|
+
vm = VM.new
|
36
|
+
vm.run [
|
37
|
+
PRINT
|
38
|
+
]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class AddTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should add top two integers on stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[INTEGER_, 9], [INTEGER_, 9]]
|
10
|
+
vm.run [
|
11
|
+
ADD
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 18]], vm.data_stack
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should add top two floats on stack" do
|
17
|
+
vm = VM.new
|
18
|
+
vm.data_stack = [[FLOAT_, 9.0], [FLOAT_, 9.0]]
|
19
|
+
vm.run [
|
20
|
+
ADD
|
21
|
+
]
|
22
|
+
assert_equal [[FLOAT_, 18.0]], vm.data_stack
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should add top two numbers on stack and implicitly convert them to floats" do
|
26
|
+
vm = VM.new
|
27
|
+
vm.data_stack = [[INTEGER_, 9], [FLOAT_, 9.0]]
|
28
|
+
vm.run [
|
29
|
+
ADD
|
30
|
+
]
|
31
|
+
assert_equal [[FLOAT_, 18.0]], vm.data_stack
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should add top two numbers on stack and implicitly convert them to floats if receiver is a float" do
|
35
|
+
vm = VM.new
|
36
|
+
vm.data_stack = [[FLOAT_, 9.0], [INTEGER_, 9]]
|
37
|
+
vm.run [
|
38
|
+
ADD
|
39
|
+
]
|
40
|
+
assert_equal [[FLOAT_, 18.0]], vm.data_stack
|
41
|
+
end
|
42
|
+
|
43
|
+
test "should raise StackError if there is only one item on the stack" do
|
44
|
+
assert_raises StackError do
|
45
|
+
vm = VM.new
|
46
|
+
vm.data_stack = [1]
|
47
|
+
vm.run [
|
48
|
+
ADD
|
49
|
+
]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should raise StackError if there are no items on the stack" do
|
54
|
+
assert_raises StackError do
|
55
|
+
vm = VM.new
|
56
|
+
vm.run [
|
57
|
+
ADD
|
58
|
+
]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class DivideTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should divide top two integers on stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[INTEGER_, 10], [INTEGER_, 3]]
|
10
|
+
vm.run [
|
11
|
+
DIVIDE
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 3]], vm.data_stack
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should divide top two floats on stack" do
|
17
|
+
vm = VM.new
|
18
|
+
vm.data_stack = [[FLOAT_, 10.0], [FLOAT_, 3.0]]
|
19
|
+
vm.run [
|
20
|
+
DIVIDE
|
21
|
+
]
|
22
|
+
assert_equal [[FLOAT_, 10.0 / 3.0]], vm.data_stack
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should divide top two numbers on stack and implicitly convert them to floats" do
|
26
|
+
vm = VM.new
|
27
|
+
vm.data_stack = [[INTEGER_, 10], [FLOAT_, 3.0]]
|
28
|
+
vm.run [
|
29
|
+
DIVIDE
|
30
|
+
]
|
31
|
+
assert_equal [[FLOAT_, 10.0 / 3.0]], vm.data_stack
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should divide top two numbers on stack and implicitly convert them to floats if receiver is a float" do
|
35
|
+
vm = VM.new
|
36
|
+
vm.data_stack = [[FLOAT_, 10.0], [INTEGER_, 3]]
|
37
|
+
vm.run [
|
38
|
+
DIVIDE
|
39
|
+
]
|
40
|
+
assert_equal [[FLOAT_, 10.0 / 3.0]], vm.data_stack
|
41
|
+
end
|
42
|
+
|
43
|
+
test "should raise StackError if there is only one item on the stack" do
|
44
|
+
assert_raises StackError do
|
45
|
+
vm = VM.new
|
46
|
+
vm.data_stack = [1]
|
47
|
+
vm.run [
|
48
|
+
DIVIDE
|
49
|
+
]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should raise StackError if there are no items on the stack" do
|
54
|
+
assert_raises StackError do
|
55
|
+
vm = VM.new
|
56
|
+
vm.run [
|
57
|
+
DIVIDE
|
58
|
+
]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class MultiplyTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should multiply top two integers on stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[INTEGER_, 5], [INTEGER_, 2]]
|
10
|
+
vm.run [
|
11
|
+
MULTIPLY
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 10]], vm.data_stack
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should multiply top two floats on stack" do
|
17
|
+
vm = VM.new
|
18
|
+
vm.data_stack = [[FLOAT_, 5.0], [FLOAT_, 2.0]]
|
19
|
+
vm.run [
|
20
|
+
MULTIPLY
|
21
|
+
]
|
22
|
+
assert_equal [[FLOAT_, 10.0]], vm.data_stack
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should multiply top two numbers on stack and implicitly convert them to floats" do
|
26
|
+
vm = VM.new
|
27
|
+
vm.data_stack = [[INTEGER_, 5], [FLOAT_, 2.0]]
|
28
|
+
vm.run [
|
29
|
+
MULTIPLY
|
30
|
+
]
|
31
|
+
assert_equal [[FLOAT_, 10.0]], vm.data_stack
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should multiply top two numbers on stack and implicitly convert them to floats if receiver is a float" do
|
35
|
+
vm = VM.new
|
36
|
+
vm.data_stack = [[FLOAT_, 5.0], [INTEGER_, 2]]
|
37
|
+
vm.run [
|
38
|
+
MULTIPLY
|
39
|
+
]
|
40
|
+
assert_equal [[FLOAT_, 10.0]], vm.data_stack
|
41
|
+
end
|
42
|
+
|
43
|
+
test "should raise StackError if there is only one item on the stack" do
|
44
|
+
assert_raises StackError do
|
45
|
+
vm = VM.new
|
46
|
+
vm.data_stack = [1]
|
47
|
+
vm.run [
|
48
|
+
MULTIPLY
|
49
|
+
]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should raise StackError if there are no items on the stack" do
|
54
|
+
assert_raises StackError do
|
55
|
+
vm = VM.new
|
56
|
+
vm.run [
|
57
|
+
MULTIPLY
|
58
|
+
]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class SubtractTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should subtract top two integers on stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.data_stack = [[INTEGER_, 5], [INTEGER_, 2]]
|
10
|
+
vm.run [
|
11
|
+
SUBTRACT
|
12
|
+
]
|
13
|
+
assert_equal [[INTEGER_, 3]], vm.data_stack
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should subtract top two floats on stack" do
|
17
|
+
vm = VM.new
|
18
|
+
vm.data_stack = [[FLOAT_, 5.0], [FLOAT_, 2.0]]
|
19
|
+
vm.run [
|
20
|
+
SUBTRACT
|
21
|
+
]
|
22
|
+
assert_equal [[FLOAT_, 3.0]], vm.data_stack
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should subtract top two numbers on stack and implicitly convert them to floats" do
|
26
|
+
vm = VM.new
|
27
|
+
vm.data_stack = [[INTEGER_, 5], [FLOAT_, 2.0]]
|
28
|
+
vm.run [
|
29
|
+
SUBTRACT
|
30
|
+
]
|
31
|
+
assert_equal [[FLOAT_, 3.0]], vm.data_stack
|
32
|
+
end
|
33
|
+
|
34
|
+
test "should subtract top two numbers on stack and implicitly convert them to floats if receiver is a float" do
|
35
|
+
vm = VM.new
|
36
|
+
vm.data_stack = [[FLOAT_, 5.0], [INTEGER_, 2]]
|
37
|
+
vm.run [
|
38
|
+
SUBTRACT
|
39
|
+
]
|
40
|
+
assert_equal [[FLOAT_, 3.0]], vm.data_stack
|
41
|
+
end
|
42
|
+
|
43
|
+
test "should raise StackError if there is only one item on the stack" do
|
44
|
+
assert_raises StackError do
|
45
|
+
vm = VM.new
|
46
|
+
vm.data_stack = [1]
|
47
|
+
vm.run [
|
48
|
+
SUBTRACT
|
49
|
+
]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should raise StackError if there are no items on the stack" do
|
54
|
+
assert_raises StackError do
|
55
|
+
vm = VM.new
|
56
|
+
vm.run [
|
57
|
+
SUBTRACT
|
58
|
+
]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class PushBoolTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should put true onto stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.run [
|
10
|
+
PUSH_BOOL, true
|
11
|
+
]
|
12
|
+
assert_equal [[BOOL_, true]], vm.data_stack
|
13
|
+
assert_equal 2, vm.instruction_pointer
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should put false onto stack" do
|
17
|
+
vm = VM.new
|
18
|
+
vm.run [
|
19
|
+
PUSH_BOOL, false
|
20
|
+
]
|
21
|
+
assert_equal [[BOOL_, false]], vm.data_stack
|
22
|
+
assert_equal 2, vm.instruction_pointer
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should raise error if operand is not a boolean value" do
|
26
|
+
[1,2.0,"string", [1], {:t=>1}].each do |x|
|
27
|
+
assert_raises OperandError do
|
28
|
+
vm = VM.new
|
29
|
+
vm.run [
|
30
|
+
PUSH_BOOL, x
|
31
|
+
]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper.rb'))
|
2
|
+
|
3
|
+
class PushFloatTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include KoiVMRuby
|
6
|
+
|
7
|
+
test "should push float onto stack" do
|
8
|
+
vm = VM.new
|
9
|
+
vm.run [
|
10
|
+
PUSH_FLOAT, 99.9
|
11
|
+
]
|
12
|
+
assert_equal [[FLOAT_, 99.9]], vm.data_stack
|
13
|
+
assert_equal 2, vm.instruction_pointer
|
14
|
+
end
|
15
|
+
|
16
|
+
test "should raise error if operand is not a float" do
|
17
|
+
[false, 2, "string", [1], {:t=>1}].each do |x|
|
18
|
+
assert_raises OperandError do
|
19
|
+
vm = VM.new
|
20
|
+
vm.run [
|
21
|
+
PUSH_FLOAT, x
|
22
|
+
]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|