razyk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb DELETED
@@ -1,5 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
- require 'razyk'
5
- require 'rspec'
data/spec/vm_spec.rb DELETED
@@ -1,128 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require_relative "spec_helper"
4
-
5
- require "razyk/vm"
6
- require "stringio"
7
-
8
- include RazyK
9
-
10
- describe VM do
11
- it "should reduce I combinator" do
12
- i = Combinator.new(:I)
13
- x = Combinator.new(:X)
14
- cons = Pair.new(i, x)
15
- vm = VM.new(cons)
16
- vm.tree.should == cons
17
- x.from.size.should == 1
18
- x.from[0].should == cons
19
- vm.reduce
20
- vm.tree.should == x
21
- x.from.size.should == 1
22
- x.from[0].should_not == cons
23
- end
24
-
25
- it "should reduce K combinator" do
26
- k = Combinator.new(:K)
27
- x = Combinator.new(:X)
28
- y = Combinator.new(:Y)
29
- cons1 = Pair.new(k, x)
30
- cons2 = Pair.new(cons1, y)
31
- vm = VM.new(cons2)
32
- vm.tree.should == cons2
33
- x.from.size.should == 1
34
- x.from[0].should == cons1
35
- vm.reduce
36
- vm.tree.should == x
37
- x.from.size.should == 1
38
- x.from[0].should_not == cons1
39
- end
40
-
41
- it "should reduce S combinator" do
42
- s = Combinator.new(:S)
43
- x = Combinator.new(:X)
44
- y = Combinator.new(:Y)
45
- z = Combinator.new(:Z)
46
- cons1 = Pair.new(s, x)
47
- cons2 = Pair.new(cons1, y)
48
- cons3 = Pair.new(cons2, z)
49
- vm = VM.new(cons3)
50
- vm.tree.should == cons3
51
- x.from.should == [ cons1 ]
52
- y.from.should == [ cons2 ]
53
- z.from.should == [ cons3 ]
54
- vm.reduce
55
- vm.tree.inspect.should == "((X Z) (Y Z))"
56
- x.from.size.should == 1
57
- x.from.should_not == [ cons1 ]
58
- y.from.size.should == 1
59
- y.from.should_not == [ cons2 ]
60
- z.from.size.should == 2
61
- end
62
-
63
- it "should reduce CONS and CAR combinator" do
64
- cons = Combinator.new(:CONS)
65
- a = Combinator.new(:A)
66
- b = Combinator.new(:B)
67
- list = Pair.new(Pair.new(cons, a), b)
68
- car = Combinator.new(:CAR)
69
- root = Pair.new(car, list)
70
- vm = VM.new(root)
71
- vm.evaluate(vm.tree)
72
- vm.tree.should == a
73
- a.from.size.should == 1
74
- end
75
-
76
- it "should reduce CONS and CDR combinator" do
77
- cons = Combinator.new(:CONS)
78
- a = Combinator.new(:A)
79
- b = Combinator.new(:B)
80
- list = Pair.new(Pair.new(cons, a), b)
81
- cdr = Combinator.new(:CDR)
82
- root = Pair.new(cdr, list)
83
- vm = VM.new(root)
84
- vm.evaluate(vm.tree)
85
- vm.tree.should == b
86
- b.from.size.should == 1
87
- end
88
-
89
- it "should reduce IN combinator (CAR)" do
90
- input = Combinator.new(:IN)
91
- car = Combinator.new(:CAR)
92
- root = Pair.new(car, input)
93
- buf = StringIO.new([100, 200].pack("C"))
94
- vm = VM.new(root, buf)
95
- vm.evaluate(vm.tree)
96
- vm.tree.should be_is_a(Combinator)
97
- vm.tree.label.should == 100
98
- buf.pos.should == 1
99
- end
100
-
101
- it "should reduce IN combinator (CDR)" do
102
- input = Combinator.new(:IN)
103
- cdr = Combinator.new(:CDR)
104
- root = Pair.new(cdr, input)
105
- buf = StringIO.new([100, 200].pack("C*"))
106
- vm = VM.new(root, buf)
107
- vm.evaluate(vm.tree)
108
- vm.tree.should be_is_a(Pair)
109
- vm.tree.cdr.label == :IN
110
- vm.tree.from.size.should == 1
111
- buf.pos.should == 2
112
- end
113
-
114
- it "should reduce PUTC combinator" do
115
- putc = Combinator.new(:PUTC)
116
- a = Combinator.new(97)
117
- x = Combinator.new(:X)
118
- decode = Pair.new(Pair.new(a, :INC), 0)
119
- root = Pair.new(Pair.new(putc, decode), x)
120
- ibuf = StringIO.new("")
121
- obuf = StringIO.new("")
122
- vm = VM.new(root,ibuf, obuf)
123
- vm.evaluate(vm.tree)
124
- obuf.string.should == "a"
125
- vm.tree.should == x
126
- x.from.size.should == 1
127
- end
128
- end