fOOrth 0.6.10 → 0.6.11

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.
Files changed (42) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +13 -2
  3. data/fOOrth.gemspec +2 -2
  4. data/integration/array_lib_tests.rb +10 -0
  5. data/integration/exception_lib_tests.rb +4 -0
  6. data/integration/hash_lib_tests.rb +9 -0
  7. data/integration/numeric_lib_tests.rb +326 -321
  8. data/integration/procedure_lib_tests.rb +16 -0
  9. data/integration/queue_lib_tests.rb +2 -1
  10. data/integration/stack_lib_tests.rb +2 -1
  11. data/integration/string_lib_tests.rb +11 -0
  12. data/integration/thread_lib_tests.rb +11 -8
  13. data/lib/fOOrth/compiler/context.rb +64 -64
  14. data/lib/fOOrth/compiler/context/locals.rb +34 -34
  15. data/lib/fOOrth/compiler/context/map_name.rb +85 -85
  16. data/lib/fOOrth/compiler/context/tags.rb +60 -48
  17. data/lib/fOOrth/compiler/process/procedure.rb +40 -0
  18. data/lib/fOOrth/library.rb +1 -0
  19. data/lib/fOOrth/library/array_library.rb +41 -21
  20. data/lib/fOOrth/library/command_library.rb +1 -1
  21. data/lib/fOOrth/library/compile_library.rb +266 -266
  22. data/lib/fOOrth/library/complex_library.rb +82 -80
  23. data/lib/fOOrth/library/float_library.rb +37 -0
  24. data/lib/fOOrth/library/hash_library.rb +14 -6
  25. data/lib/fOOrth/library/mutex_library.rb +4 -2
  26. data/lib/fOOrth/library/numeric_library.rb +359 -380
  27. data/lib/fOOrth/library/procedure_library.rb +69 -65
  28. data/lib/fOOrth/library/queue_library.rb +6 -1
  29. data/lib/fOOrth/library/rational_library.rb +89 -89
  30. data/lib/fOOrth/library/stack_library.rb +6 -1
  31. data/lib/fOOrth/library/string_library.rb +18 -6
  32. data/lib/fOOrth/monkey_patch/exceptions.rb +2 -6
  33. data/lib/fOOrth/version.rb +1 -1
  34. data/tests/compiler/context_tests.rb +188 -177
  35. data/tests/compiler/file_source_tests.rb +130 -130
  36. data/tests/compiler/parser_tests.rb +4 -4
  37. data/tests/compiler/string_source_tests.rb +4 -4
  38. data/tests/core_tests.rb +138 -138
  39. data/tests/monkey_patch/complex_test.rb +24 -24
  40. data/tests/monkey_patch/object_test.rb +49 -49
  41. data/tests/monkey_patch/string_test.rb +61 -61
  42. metadata +10 -10
@@ -1,81 +1,83 @@
1
- # coding: utf-8
2
-
3
- #* library/complex_library.rb - Numeric support for the fOOrth library.
4
- module XfOOrth
5
-
6
- #Connect the Complex class to the fOOrth class system.
7
- Complex.create_foorth_proxy
8
-
9
- #Some complex stubs.
10
- Complex.create_shared_method('mod', NosSpec, [:stub])
11
- Complex.create_shared_method('.ceil', TosSpec, [:stub])
12
- Complex.create_shared_method('.floor', TosSpec, [:stub])
13
- Complex.create_shared_method('.round', TosSpec, [:stub])
14
- Complex.create_shared_method('<', NosSpec, [:stub])
15
- Complex.create_shared_method('>', NosSpec, [:stub])
16
- Complex.create_shared_method('<=', NosSpec, [:stub])
17
- Complex.create_shared_method('>=', NosSpec, [:stub])
18
- Complex.create_shared_method('<=>', NosSpec, [:stub])
19
-
20
-
21
- #Some conversion words.
22
- # [a b] complex [a+bi]
23
- VirtualMachine.create_shared_method('complex', VmSpec, [], &lambda {|vm|
24
- real,imag = popm(2)
25
-
26
- begin
27
- push(Complex(real,imag));
28
- rescue
29
- error "F40: Cannot coerce a #{real.foorth_name}, #{imag.foorth_name} to a Complex"
30
- end
31
- })
32
-
33
- # [a+bi] .split [a b]
34
- Complex.create_shared_method('.split', TosSpec, [],
35
- &lambda {|vm| vm.push(self.real); vm.push(self.imaginary); })
36
-
37
- # [a] .to_x [compleX or nil]
38
- Object.create_shared_method('.to_x', TosSpec, [], &lambda {|vm|
39
- begin
40
- vm.push(Complex(self))
41
- rescue
42
- vm.push(nil)
43
- end
44
- })
45
-
46
- # [a] .to_x! [compleX]
47
- Object.create_shared_method('.to_x!', TosSpec, [], &lambda {|vm|
48
- begin
49
- vm.push(Complex(self))
50
- rescue
51
- error "F40: Cannot coerce a #{self.foorth_name} to a Complex"
52
- end
53
- })
54
-
55
- # [a+bi] .imaginary [b]
56
- Numeric.create_shared_method('.imaginary', TosSpec, [],
57
- &lambda {|vm| vm.push(self.imaginary); })
58
-
59
- # [a+bi] .real [a]
60
- Numeric.create_shared_method('.real', TosSpec, [],
61
- &lambda {|vm| vm.push(self.real); })
62
-
63
- # [a+bi] .angle [atan2(b,a) or 0]
64
- Numeric.create_shared_method('.angle', TosSpec, [],
65
- &lambda {|vm| vm.push(self.angle); })
66
-
67
- # [a+bi] .magnitude [sqrt(a**2 + b**2)]
68
- Numeric.create_shared_method('.magnitude', TosSpec, [],
69
- &lambda {|vm| vm.push(self.magnitude); })
70
-
71
- # [a+bi] .conjugate [a-bi]
72
- # Complex convicts that behave well are allowed .conjugate visits.
73
- Numeric.create_shared_method('.conjugate', TosSpec, [],
74
- &lambda {|vm| vm.push(self.conjugate); })
75
-
76
- # [a+bi] .polar [magnitude angle]
77
- # Convert a complex number to polar format
78
- Numeric.create_shared_method('.polar', TosSpec, [],
79
- &lambda {|vm| vm.pushm(self.polar); })
80
-
1
+ # coding: utf-8
2
+
3
+ #* library/complex_library.rb - Numeric support for the fOOrth library.
4
+ module XfOOrth
5
+
6
+ #Connect the Complex class to the fOOrth class system.
7
+ Complex.create_foorth_proxy
8
+
9
+ #Some complex stubs.
10
+ Complex.create_shared_method('mod', NosSpec, [:stub])
11
+ Complex.create_shared_method('.ceil', TosSpec, [:stub])
12
+ Complex.create_shared_method('.floor', TosSpec, [:stub])
13
+ Complex.create_shared_method('.round', TosSpec, [:stub])
14
+ Complex.create_shared_method('<', NosSpec, [:stub])
15
+ Complex.create_shared_method('>', NosSpec, [:stub])
16
+ Complex.create_shared_method('<=', NosSpec, [:stub])
17
+ Complex.create_shared_method('>=', NosSpec, [:stub])
18
+ Complex.create_shared_method('<=>', NosSpec, [:stub])
19
+ Complex.create_shared_method('.numerator', TosSpec, [:stub])
20
+ Complex.create_shared_method('.denominator', TosSpec, [:stub])
21
+
22
+
23
+ #Some conversion words.
24
+ # [a b] complex [a+bi]
25
+ VirtualMachine.create_shared_method('complex', VmSpec, [], &lambda {|vm|
26
+ real,imag = popm(2)
27
+
28
+ begin
29
+ push(Complex(real,imag));
30
+ rescue
31
+ error "F40: Cannot coerce a #{real.foorth_name}, #{imag.foorth_name} to a Complex"
32
+ end
33
+ })
34
+
35
+ # [a+bi] .split [a b]
36
+ Complex.create_shared_method('.split', TosSpec, [],
37
+ &lambda {|vm| vm.push(self.real); vm.push(self.imaginary); })
38
+
39
+ # [a] .to_x [compleX or nil]
40
+ Object.create_shared_method('.to_x', TosSpec, [], &lambda {|vm|
41
+ begin
42
+ vm.push(Complex(self))
43
+ rescue
44
+ vm.push(nil)
45
+ end
46
+ })
47
+
48
+ # [a] .to_x! [compleX]
49
+ Object.create_shared_method('.to_x!', TosSpec, [], &lambda {|vm|
50
+ begin
51
+ vm.push(Complex(self))
52
+ rescue
53
+ error "F40: Cannot coerce a #{self.foorth_name} to a Complex"
54
+ end
55
+ })
56
+
57
+ # [a+bi] .imaginary [b]
58
+ Numeric.create_shared_method('.imaginary', TosSpec, [],
59
+ &lambda {|vm| vm.push(self.imaginary); })
60
+
61
+ # [a+bi] .real [a]
62
+ Numeric.create_shared_method('.real', TosSpec, [],
63
+ &lambda {|vm| vm.push(self.real); })
64
+
65
+ # [a+bi] .angle [atan2(b,a) or 0]
66
+ Numeric.create_shared_method('.angle', TosSpec, [],
67
+ &lambda {|vm| vm.push(self.angle); })
68
+
69
+ # [a+bi] .magnitude [sqrt(a**2 + b**2)]
70
+ Numeric.create_shared_method('.magnitude', TosSpec, [],
71
+ &lambda {|vm| vm.push(self.magnitude); })
72
+
73
+ # [a+bi] .conjugate [a-bi]
74
+ # Complex convicts that behave well are allowed .conjugate visits.
75
+ Numeric.create_shared_method('.conjugate', TosSpec, [],
76
+ &lambda {|vm| vm.push(self.conjugate); })
77
+
78
+ # [a+bi] .polar [magnitude angle]
79
+ # Convert a complex number to polar format
80
+ Numeric.create_shared_method('.polar', TosSpec, [],
81
+ &lambda {|vm| vm.pushm(self.polar); })
82
+
81
83
  end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+
3
+ #* library/float_library.rb - Float specific support for the fOOrth library.
4
+ module XfOOrth
5
+
6
+ #Connect the Float class to the fOOrth class system.
7
+ Float.create_foorth_proxy
8
+
9
+ # [a] .to_f [Float or nil]
10
+ Object.create_shared_method('.to_f', TosSpec, [], &lambda {|vm|
11
+ begin
12
+ vm.push(Float(self))
13
+ rescue
14
+ vm.push(nil)
15
+ end
16
+ })
17
+
18
+ # [a] .to_f! [Float]
19
+ Object.create_shared_method('.to_f!', TosSpec, [],
20
+ &lambda {|vm| vm.push(Float.foorth_coerce(self)) })
21
+
22
+ # [num_digits a_number] .round_to [a_float]
23
+ Numeric.create_shared_method('.round_to', TosSpec, [], &lambda {|vm|
24
+ value = Float.foorth_coerce(self)
25
+ digits = Integer.foorth_coerce(vm.pop)
26
+ vm.push(value.round(digits))
27
+ })
28
+
29
+ # [n/d] .numerator [n]
30
+ Float.create_shared_method('.numerator', TosSpec, [],
31
+ &lambda {|vm| vm.push(self.rationalize.numerator); })
32
+
33
+ # [n/d] .denominator [d]
34
+ Float.create_shared_method('.denominator', TosSpec, [],
35
+ &lambda {|vm| vm.push(self.rationalize.denominator); })
36
+
37
+ end
@@ -70,27 +70,35 @@ module XfOOrth
70
70
 
71
71
  # [i h] .[]@ [h[i]]
72
72
  Hash.create_shared_method('.[]@', TosSpec, [],
73
- &lambda {|vm| vm.poke(self[vm.peek]); })
73
+ &lambda {|vm| vm.poke(self[vm.peek]) })
74
74
 
75
75
  # [v i h] .[]! []; h[i]=v
76
76
  Hash.create_shared_method('.[]!', TosSpec, [],
77
- &lambda {|vm| value, index = vm.popm(2); self[index] = value; })
77
+ &lambda {|vm| value, index = vm.popm(2); self[index] = value })
78
78
 
79
79
  # [{"a"=>1, "b"=>2}] .length [2]]
80
80
  Hash.create_shared_method('.length', TosSpec, [],
81
- &lambda {|vm| vm.push(self.length); })
81
+ &lambda {|vm| vm.push(self.length) })
82
82
 
83
83
  # [a_hash] .empty? [a_boolean]]
84
84
  Hash.create_shared_method('.empty?', TosSpec, [],
85
- &lambda {|vm| vm.push(self.empty?); })
85
+ &lambda {|vm| vm.push(self.empty?) })
86
+
87
+ # [a_hash] .present? [a_boolean]]
88
+ Hash.create_shared_method('.present?', TosSpec, [],
89
+ &lambda {|vm| vm.push(!self.empty?) })
90
+
91
+ # [a_hash] .clear! []]
92
+ Hash.create_shared_method('.clear!', TosSpec, [],
93
+ &lambda {|vm| self.clear })
86
94
 
87
95
  # [h] .keys [[keys]]
88
96
  Hash.create_shared_method('.keys', TosSpec, [],
89
- &lambda {|vm| vm.push(self.keys); })
97
+ &lambda {|vm| vm.push(self.keys) })
90
98
 
91
99
  # [h] .values [[values]]
92
100
  Hash.create_shared_method('.values', TosSpec, [],
93
- &lambda {|vm| vm.push(self.values); })
101
+ &lambda {|vm| vm.push(self.values) })
94
102
 
95
103
  # [h] .strmax2 [widest_key widest_value]
96
104
  Hash.create_shared_method('.strmax2', TosSpec, [], &lambda {|vm|
@@ -9,7 +9,7 @@ module XfOOrth
9
9
  # [Mutex] .do{{ ... }} [] (Releases a lock on the mutex)
10
10
  Mutex.create_exclusive_method('.do{{', NosSpec, [], &lambda {|vm|
11
11
  block = vm.pop
12
- @foorth_shared_mutex.synchronize { block.call(vm, nil, nil) }
12
+ foorth_shared_mutex.synchronize { block.call(vm, nil, nil) }
13
13
  })
14
14
 
15
15
  # [a_mutex] .lock [] (Acquires a lock on the mutex)
@@ -32,5 +32,7 @@ end
32
32
 
33
33
  # Create a shared mutex instance.
34
34
  class Mutex
35
- @foorth_shared_mutex = Mutex.new
35
+ def self.foorth_shared_mutex
36
+ @foorth_shared_mutex ||= Mutex.new
37
+ end
36
38
  end
@@ -1,380 +1,359 @@
1
- # coding: utf-8
2
-
3
- #* This meaningless entry exists to shut up rdoc!
4
- module Math #:nodoc: don't document this
5
- end
6
-
7
- #* library/numeric_library.rb - Numeric support for the fOOrth library.
8
- module XfOOrth
9
-
10
- #Connect the Numeric classes to the fOOrth class system.
11
- Numeric.create_foorth_proxy
12
- Float.create_foorth_proxy
13
-
14
- # Some conversion words.
15
- # [a] .to_n [Number or nil]
16
- Object.create_shared_method('.to_n', TosSpec, [],
17
- &lambda {|vm| vm.push(self.to_foorth_n); })
18
-
19
- # Some conversion words.
20
- # [a] .to_n! [Number]
21
- Object.create_shared_method('.to_n!', TosSpec, [], &lambda {|vm|
22
- if (result = self.to_foorth_n)
23
- vm.push(result);
24
- else
25
- error "F40: Cannot convert a #{self.foorth_name} to a Numeric instance"
26
- end
27
- })
28
-
29
- # [a] .to_f [Float or nil]
30
- Object.create_shared_method('.to_f', TosSpec, [], &lambda {|vm|
31
- begin
32
- vm.push(Float(self))
33
- rescue
34
- vm.push(nil)
35
- end
36
- })
37
-
38
- # [num_digits a_number] .round_to [a_float]
39
- Numeric.create_shared_method('.round_to', TosSpec, [], &lambda {|vm|
40
- value = Float.foorth_coerce(self)
41
- digits = Integer.foorth_coerce(vm.pop)
42
- vm.push(value.round(digits))
43
- })
44
-
45
- # [a] .to_f! [Float]
46
- Object.create_shared_method('.to_f!', TosSpec, [],
47
- &lambda {|vm| vm.push(Float.foorth_coerce(self)) })
48
-
49
- # Some comparison words.
50
- # [b,a] > if b > a then [true] else [false]
51
- Numeric.create_shared_method('>', NosSpec, [], &lambda {|vm|
52
- begin
53
- vm.poke(self > self.foorth_coerce(vm.peek))
54
- rescue
55
- vm.data_stack.pop
56
- raise
57
- end
58
- })
59
-
60
- # [b,a] < if b < a then [true] else [false]
61
- Numeric.create_shared_method('<', NosSpec, [], &lambda {|vm|
62
- begin
63
- vm.poke(self < self.foorth_coerce(vm.peek))
64
- rescue
65
- vm.data_stack.pop
66
- raise
67
- end
68
- })
69
-
70
- # [b,a] >= if b >= a then [true] else [false]
71
- Numeric.create_shared_method('>=', NosSpec, [], &lambda {|vm|
72
- begin
73
- vm.poke(self >= self.foorth_coerce(vm.peek))
74
- rescue
75
- vm.data_stack.pop
76
- raise
77
- end
78
- })
79
-
80
- # [b,a] <= if b <= a then [true] else [false]
81
- Numeric.create_shared_method('<=', NosSpec, [], &lambda {|vm|
82
- begin
83
- vm.poke(self <= self.foorth_coerce(vm.peek))
84
- rescue
85
- vm.data_stack.pop
86
- raise
87
- end
88
- })
89
-
90
- # [b,a] <=> if b <=> a then [true] else [false]
91
- Numeric.create_shared_method('<=>', NosSpec, [], &lambda {|vm|
92
- begin
93
- vm.poke(self <=> self.foorth_coerce(vm.peek))
94
- rescue
95
- vm.data_stack.pop
96
- raise
97
- end
98
- })
99
-
100
- # Some comparison with zero words.
101
- # [b,a] 0= if b == 0 then [true] else [false]
102
- Numeric.create_shared_method('0=', TosSpec, [],
103
- &lambda {|vm| vm.push(self.zero?); })
104
-
105
- # [b,a] 0<> if b != 0 then [true] else [false]
106
- Numeric.create_shared_method('0<>', TosSpec, [],
107
- &lambda {|vm| vm.push(!self.zero?); })
108
-
109
- # [b,a] 0> if b > 0 then [true] else [false]
110
- Numeric.create_shared_method('0>', TosSpec, [],
111
- &lambda {|vm| vm.push(self > 0); })
112
-
113
- # [b,a] 0< if b < 0 then [true] else [false]
114
- Numeric.create_shared_method('0<', TosSpec, [],
115
- &lambda {|vm| vm.push(self < 0); })
116
-
117
- # [b,a] 0>= if b >= 0 then [true] else [false]
118
- Numeric.create_shared_method('0>=', TosSpec, [],
119
- &lambda {|vm| vm.push(self >= 0); })
120
-
121
- # [b,a] 0<= if b <= 0 then [true] else [false]
122
- Numeric.create_shared_method('0<=', TosSpec, [],
123
- &lambda {|vm| vm.push(self <= 0); })
124
-
125
- # [b] 0<=> b < 0 [-1], b = 0 [0], b > 0 [1]
126
- Numeric.create_shared_method('0<=>', TosSpec, [],
127
- &lambda {|vm| vm.push(self <=> 0); })
128
-
129
- # Some stack arithmetic words.
130
- # [b,a] + [b+a]
131
- Numeric.create_shared_method('+', NosSpec, [], &lambda {|vm|
132
- begin
133
- vm.poke(self + self.foorth_coerce(vm.peek))
134
- rescue
135
- vm.data_stack.pop
136
- raise
137
- end
138
- })
139
-
140
- # [b,a] - [b-a]
141
- Numeric.create_shared_method('-', NosSpec, [], &lambda {|vm|
142
- begin
143
- vm.poke(self - self.foorth_coerce(vm.peek))
144
- rescue
145
- vm.data_stack.pop
146
- raise
147
- end
148
- })
149
-
150
- # [b,a] * [b*a]
151
- Numeric.create_shared_method('*', NosSpec, [], &lambda {|vm|
152
- begin
153
- vm.poke(self * self.foorth_coerce(vm.peek))
154
- rescue
155
- vm.data_stack.pop
156
- raise
157
- end
158
- })
159
-
160
- # [b,a] ** [b**a]
161
- Numeric.create_shared_method('**', NosSpec, [], &lambda {|vm|
162
- begin
163
- vm.poke(self ** Float.foorth_coerce(vm.peek))
164
- rescue
165
- vm.data_stack.pop
166
- raise
167
- end
168
- })
169
-
170
- # [b,a] / [b/a]
171
- Numeric.create_shared_method('/', NosSpec, [], &lambda {|vm|
172
- begin
173
- vm.poke(self / self.foorth_coerce(vm.peek))
174
- rescue
175
- vm.data_stack.pop
176
- raise
177
- end
178
- })
179
-
180
- # [b,a] mod [b%a]
181
- Numeric.create_shared_method('mod', NosSpec, [], &lambda {|vm|
182
- begin
183
- vm.poke(self % self.foorth_coerce(vm.peek))
184
- rescue
185
- vm.data_stack.pop
186
- raise
187
- end
188
- })
189
-
190
- # [a] neg [-a]
191
- Numeric.create_shared_method('neg', TosSpec, [],
192
- &lambda {|vm| vm.push(-self); })
193
-
194
- # [a] .1/x [-a]
195
- Numeric.create_shared_method('.1/x', TosSpec, [],
196
- &lambda {|vm| vm.push(1/self); })
197
-
198
- # [a] .abs [|a|]
199
- Numeric.create_shared_method('.abs', TosSpec, [],
200
- &lambda {|vm| vm.push(self.abs); })
201
-
202
- # [a] 1+ [a+1]
203
- Numeric.create_shared_method('1+', TosSpec, [],
204
- &lambda {|vm| vm.push(self+1); })
205
-
206
- # [a] 1- [a-1]
207
- Numeric.create_shared_method('1-', TosSpec, [],
208
- &lambda {|vm| vm.push(self-1); })
209
-
210
- # [a] 2+ [a+2]
211
- Numeric.create_shared_method('2+', TosSpec, [],
212
- &lambda {|vm| vm.push(self+2); })
213
-
214
- # [a] 2- [a-2]
215
- Numeric.create_shared_method('2-', TosSpec, [],
216
- &lambda {|vm| vm.push(self-2); })
217
-
218
- # [a] 2* [a*2]
219
- Numeric.create_shared_method('2*', TosSpec, [],
220
- &lambda {|vm| vm.push(self*2); })
221
-
222
- # [a] 2/ [a/2]
223
- Numeric.create_shared_method('2/', TosSpec, [],
224
- &lambda {|vm| vm.push(self/2); })
225
-
226
- # [a] .ceil [a']; where a' is the closest integer >= a
227
- Numeric.create_shared_method('.ceil', TosSpec, [],
228
- &lambda {|vm| vm.push(self.ceil); })
229
-
230
- # [a] .floor [a']; where a' is the closest integer <= a
231
- Numeric.create_shared_method('.floor', TosSpec, [],
232
- &lambda {|vm| vm.push(self.floor); })
233
-
234
- # [a] .round [a']; where a' is the integer closest to a
235
- Numeric.create_shared_method('.round', TosSpec, [],
236
- &lambda {|vm| vm.push(self.round); })
237
-
238
-
239
- #Advanced math stuff!
240
- # [] pi [3.141592653589793]
241
- VirtualMachine.create_shared_method('pi', MacroSpec, [:macro, "vm.push(Math::PI)"])
242
-
243
- # [] e [2.718281828459045]
244
- VirtualMachine.create_shared_method('e', MacroSpec, [:macro, "vm.push(Math::E)"])
245
-
246
- #The number of degrees in one radian.
247
- DegreesPerRadian = 180.0/Math::PI
248
-
249
- # [] dpr [2.718281828459045]
250
- VirtualMachine.create_shared_method('dpr', MacroSpec,
251
- [:macro, "vm.push(DegreesPerRadian)"])
252
-
253
- # [degrees] .d2r [radians]
254
- Numeric.create_shared_method('.d2r', TosSpec, [],
255
- &lambda {|vm| vm.push(Float.foorth_coerce(self)/DegreesPerRadian); })
256
-
257
- # [radians] .r2d [degrees]
258
- Numeric.create_shared_method('.r2d', TosSpec, [],
259
- &lambda {|vm| vm.push(Float.foorth_coerce(self)*DegreesPerRadian); })
260
-
261
- # [radians] .cos [cos(radians)]
262
- Numeric.create_shared_method('.cos', TosSpec, [],
263
- &lambda {|vm| vm.push(Math::cos(Float.foorth_coerce(self))); })
264
-
265
- # [radians] .sin [sin(radians)]
266
- Numeric.create_shared_method('.sin', TosSpec, [],
267
- &lambda {|vm| vm.push(Math::sin(Float.foorth_coerce(self))); })
268
-
269
- # [radians] .tan [tan(radians)]
270
- Numeric.create_shared_method('.tan', TosSpec, [],
271
- &lambda {|vm| vm.push(Math::tan(Float.foorth_coerce(self))); })
272
-
273
- # [cos(radians)] .acos [radians]
274
- Numeric.create_shared_method('.acos', TosSpec, [],
275
- &lambda {|vm| vm.push(Math::acos(Float.foorth_coerce(self))); })
276
-
277
- # [sin(radians)] .asin [radians]
278
- Numeric.create_shared_method('.asin', TosSpec, [],
279
- &lambda {|vm| vm.push(Math::asin(Float.foorth_coerce(self))); })
280
-
281
- # [y/x] .atan [radians]
282
- Numeric.create_shared_method('.atan', TosSpec, [],
283
- &lambda {|vm| vm.push(Math::atan(Float.foorth_coerce(self))); })
284
-
285
- # [y x] .atan2 [radians]
286
- Numeric.create_shared_method('.atan2', TosSpec, [],
287
- &lambda {|vm| vm.poke(Math::atan2(Float.foorth_coerce(vm.peek), Float.foorth_coerce(self))); })
288
-
289
- # [radians] .cosh [cosh(radians)]
290
- Numeric.create_shared_method('.cosh', TosSpec, [],
291
- &lambda {|vm| vm.push(Math::cosh(Float.foorth_coerce(self))); })
292
-
293
- # [radians] .sinh [sinh(radians)]
294
- Numeric.create_shared_method('.sinh', TosSpec, [],
295
- &lambda {|vm| vm.push(Math::sinh(Float.foorth_coerce(self))); })
296
-
297
- # [radians] .tanh [tanh(radians)]
298
- Numeric.create_shared_method('.tanh', TosSpec, [],
299
- &lambda {|vm| vm.push(Math::tanh(Float.foorth_coerce(self))); })
300
-
301
- # [cosh(radians)] .acosh [radians]
302
- Numeric.create_shared_method('.acosh', TosSpec, [],
303
- &lambda {|vm| vm.push(Math::acosh(Float.foorth_coerce(self))); })
304
-
305
- # [sinh(radians)] .asinh [radians]
306
- Numeric.create_shared_method('.asinh', TosSpec, [],
307
- &lambda {|vm| vm.push(Math::asinh(Float.foorth_coerce(self))); })
308
-
309
- # [y/x] .atanh [radians]
310
- Numeric.create_shared_method('.atanh', TosSpec, [],
311
- &lambda {|vm| vm.push(Math::atanh(Float.foorth_coerce(self))); })
312
-
313
- # [x] .e** [e**x]
314
- Numeric.create_shared_method('.e**', TosSpec, [],
315
- &lambda {|vm| vm.push(Math::exp(self)); })
316
- Complex.create_shared_method('.e**', TosSpec, [],
317
- &lambda {|vm| vm.push(Math::E ** self); })
318
-
319
- # [x] .ln [ln(x)]
320
- Numeric.create_shared_method('.ln', TosSpec, [],
321
- &lambda {|vm| vm.push(Math::log(Float.foorth_coerce(self))); })
322
-
323
- # [x] .10** [10**x]
324
- Numeric.create_shared_method('.10**', TosSpec, [],
325
- &lambda {|vm| vm.push(10.0**self); })
326
-
327
- # [x] .log10 [log10(x)]
328
- Numeric.create_shared_method('.log10', TosSpec, [],
329
- &lambda {|vm| vm.push(Math::log10(Float.foorth_coerce(self))); })
330
-
331
- # [x] .2** [2**x]
332
- Numeric.create_shared_method('.2**', TosSpec, [],
333
- &lambda {|vm| vm.push(2.0**self); })
334
-
335
- # [x] .log2 [log2(x)]
336
- Numeric.create_shared_method('.log2', TosSpec, [],
337
- &lambda {|vm| vm.push(Math::log2(Float.foorth_coerce(self))); })
338
-
339
- # [x] .sqr [square(x)]
340
- Numeric.create_shared_method('.sqr', TosSpec, [],
341
- &lambda {|vm| vm.push(self*self); })
342
-
343
- # [x] .sqrt [square root(x)]
344
- Numeric.create_shared_method('.sqrt', TosSpec, [],
345
- &lambda {|vm| vm.push(Math::sqrt(self)); })
346
- Complex.create_shared_method('.sqrt', TosSpec, [],
347
- &lambda {|vm| vm.push(self ** 0.5); })
348
-
349
- # [x] .cube [cube(x)]
350
- Numeric.create_shared_method('.cube', TosSpec, [],
351
- &lambda {|vm| vm.push(self*self*self); })
352
-
353
- # [x] .cbrt [cube root(x)]
354
- Numeric.create_shared_method('.cbrt', TosSpec, [],
355
- &lambda {|vm| vm.push(Math::cbrt(self)); })
356
- Complex.create_shared_method('.cbrt', TosSpec, [],
357
- &lambda {|vm| vm.push(self ** Rational(1,3)); })
358
-
359
- # [x y] .hypot [sqrt(x**2 + y**2)]
360
- Numeric.create_shared_method('.hypot', TosSpec, [], &lambda {|vm|
361
- vm.poke(Math::hypot(Float.foorth_coerce(self), Float.foorth_coerce(vm.peek)));
362
- })
363
-
364
- # [r t] .p2c [x y]; Polar to Cartesian.
365
- Numeric.create_shared_method('.p2c', TosSpec, [], &lambda {|vm|
366
- radius = Float.foorth_coerce(vm.pop)
367
- theta = Float.foorth_coerce(self)
368
- vm.push(radius * Math::cos(theta))
369
- vm.push(radius * Math::sin(theta))
370
- })
371
-
372
- # [x y] .c2p [r t]; Cartesian to Polar.
373
- Numeric.create_shared_method('.c2p', TosSpec, [], &lambda {|vm|
374
- real = Float.foorth_coerce(vm.pop)
375
- imag = Float.foorth_coerce(self)
376
- vm.push(Math::hypot(real,imag))
377
- vm.push(Math::atan2(imag,real))
378
- })
379
-
380
- end
1
+ # coding: utf-8
2
+
3
+ #* This meaningless entry exists to shut up rdoc!
4
+ module Math #:nodoc: don't document this
5
+ end
6
+
7
+ #* library/numeric_library.rb - Numeric support for the fOOrth library.
8
+ module XfOOrth
9
+
10
+ #Connect the Numeric classes to the fOOrth class system.
11
+ Numeric.create_foorth_proxy
12
+
13
+ # Some conversion words.
14
+ # [a] .to_n [Number or nil]
15
+ Object.create_shared_method('.to_n', TosSpec, [],
16
+ &lambda {|vm| vm.push(self.to_foorth_n); })
17
+
18
+ # Some conversion words.
19
+ # [a] .to_n! [Number]
20
+ Object.create_shared_method('.to_n!', TosSpec, [], &lambda {|vm|
21
+ if (result = self.to_foorth_n)
22
+ vm.push(result);
23
+ else
24
+ error "F40: Cannot convert a #{self.foorth_name} to a Numeric instance"
25
+ end
26
+ })
27
+
28
+ # Some comparison words.
29
+ # [b,a] > if b > a then [true] else [false]
30
+ Numeric.create_shared_method('>', NosSpec, [], &lambda {|vm|
31
+ begin
32
+ vm.poke(self > self.foorth_coerce(vm.peek))
33
+ rescue
34
+ vm.data_stack.pop
35
+ raise
36
+ end
37
+ })
38
+
39
+ # [b,a] < if b < a then [true] else [false]
40
+ Numeric.create_shared_method('<', NosSpec, [], &lambda {|vm|
41
+ begin
42
+ vm.poke(self < self.foorth_coerce(vm.peek))
43
+ rescue
44
+ vm.data_stack.pop
45
+ raise
46
+ end
47
+ })
48
+
49
+ # [b,a] >= if b >= a then [true] else [false]
50
+ Numeric.create_shared_method('>=', NosSpec, [], &lambda {|vm|
51
+ begin
52
+ vm.poke(self >= self.foorth_coerce(vm.peek))
53
+ rescue
54
+ vm.data_stack.pop
55
+ raise
56
+ end
57
+ })
58
+
59
+ # [b,a] <= if b <= a then [true] else [false]
60
+ Numeric.create_shared_method('<=', NosSpec, [], &lambda {|vm|
61
+ begin
62
+ vm.poke(self <= self.foorth_coerce(vm.peek))
63
+ rescue
64
+ vm.data_stack.pop
65
+ raise
66
+ end
67
+ })
68
+
69
+ # [b,a] <=> if b <=> a then [true] else [false]
70
+ Numeric.create_shared_method('<=>', NosSpec, [], &lambda {|vm|
71
+ begin
72
+ vm.poke(self <=> self.foorth_coerce(vm.peek))
73
+ rescue
74
+ vm.data_stack.pop
75
+ raise
76
+ end
77
+ })
78
+
79
+ # Some comparison with zero words.
80
+ # [b,a] 0= if b == 0 then [true] else [false]
81
+ Numeric.create_shared_method('0=', TosSpec, [],
82
+ &lambda {|vm| vm.push(self.zero?); })
83
+
84
+ # [b,a] 0<> if b != 0 then [true] else [false]
85
+ Numeric.create_shared_method('0<>', TosSpec, [],
86
+ &lambda {|vm| vm.push(!self.zero?); })
87
+
88
+ # [b,a] 0> if b > 0 then [true] else [false]
89
+ Numeric.create_shared_method('0>', TosSpec, [],
90
+ &lambda {|vm| vm.push(self > 0); })
91
+
92
+ # [b,a] 0< if b < 0 then [true] else [false]
93
+ Numeric.create_shared_method('0<', TosSpec, [],
94
+ &lambda {|vm| vm.push(self < 0); })
95
+
96
+ # [b,a] 0>= if b >= 0 then [true] else [false]
97
+ Numeric.create_shared_method('0>=', TosSpec, [],
98
+ &lambda {|vm| vm.push(self >= 0); })
99
+
100
+ # [b,a] 0<= if b <= 0 then [true] else [false]
101
+ Numeric.create_shared_method('0<=', TosSpec, [],
102
+ &lambda {|vm| vm.push(self <= 0); })
103
+
104
+ # [b] 0<=> b < 0 [-1], b = 0 [0], b > 0 [1]
105
+ Numeric.create_shared_method('0<=>', TosSpec, [],
106
+ &lambda {|vm| vm.push(self <=> 0); })
107
+
108
+ # Some stack arithmetic words.
109
+ # [b,a] + [b+a]
110
+ Numeric.create_shared_method('+', NosSpec, [], &lambda {|vm|
111
+ begin
112
+ vm.poke(self + self.foorth_coerce(vm.peek))
113
+ rescue
114
+ vm.data_stack.pop
115
+ raise
116
+ end
117
+ })
118
+
119
+ # [b,a] - [b-a]
120
+ Numeric.create_shared_method('-', NosSpec, [], &lambda {|vm|
121
+ begin
122
+ vm.poke(self - self.foorth_coerce(vm.peek))
123
+ rescue
124
+ vm.data_stack.pop
125
+ raise
126
+ end
127
+ })
128
+
129
+ # [b,a] * [b*a]
130
+ Numeric.create_shared_method('*', NosSpec, [], &lambda {|vm|
131
+ begin
132
+ vm.poke(self * self.foorth_coerce(vm.peek))
133
+ rescue
134
+ vm.data_stack.pop
135
+ raise
136
+ end
137
+ })
138
+
139
+ # [b,a] ** [b**a]
140
+ Numeric.create_shared_method('**', NosSpec, [], &lambda {|vm|
141
+ begin
142
+ vm.poke(self ** Float.foorth_coerce(vm.peek))
143
+ rescue
144
+ vm.data_stack.pop
145
+ raise
146
+ end
147
+ })
148
+
149
+ # [b,a] / [b/a]
150
+ Numeric.create_shared_method('/', NosSpec, [], &lambda {|vm|
151
+ begin
152
+ vm.poke(self / self.foorth_coerce(vm.peek))
153
+ rescue
154
+ vm.data_stack.pop
155
+ raise
156
+ end
157
+ })
158
+
159
+ # [b,a] mod [b%a]
160
+ Numeric.create_shared_method('mod', NosSpec, [], &lambda {|vm|
161
+ begin
162
+ vm.poke(self % self.foorth_coerce(vm.peek))
163
+ rescue
164
+ vm.data_stack.pop
165
+ raise
166
+ end
167
+ })
168
+
169
+ # [a] neg [-a]
170
+ Numeric.create_shared_method('neg', TosSpec, [],
171
+ &lambda {|vm| vm.push(-self); })
172
+
173
+ # [a] .1/x [-a]
174
+ Numeric.create_shared_method('.1/x', TosSpec, [],
175
+ &lambda {|vm| vm.push(1/self); })
176
+
177
+ # [a] .abs [|a|]
178
+ Numeric.create_shared_method('.abs', TosSpec, [],
179
+ &lambda {|vm| vm.push(self.abs); })
180
+
181
+ # [a] 1+ [a+1]
182
+ Numeric.create_shared_method('1+', TosSpec, [],
183
+ &lambda {|vm| vm.push(self+1); })
184
+
185
+ # [a] 1- [a-1]
186
+ Numeric.create_shared_method('1-', TosSpec, [],
187
+ &lambda {|vm| vm.push(self-1); })
188
+
189
+ # [a] 2+ [a+2]
190
+ Numeric.create_shared_method('2+', TosSpec, [],
191
+ &lambda {|vm| vm.push(self+2); })
192
+
193
+ # [a] 2- [a-2]
194
+ Numeric.create_shared_method('2-', TosSpec, [],
195
+ &lambda {|vm| vm.push(self-2); })
196
+
197
+ # [a] 2* [a*2]
198
+ Numeric.create_shared_method('2*', TosSpec, [],
199
+ &lambda {|vm| vm.push(self*2); })
200
+
201
+ # [a] 2/ [a/2]
202
+ Numeric.create_shared_method('2/', TosSpec, [],
203
+ &lambda {|vm| vm.push(self/2); })
204
+
205
+ # [a] .ceil [a']; where a' is the closest integer >= a
206
+ Numeric.create_shared_method('.ceil', TosSpec, [],
207
+ &lambda {|vm| vm.push(self.ceil); })
208
+
209
+ # [a] .floor [a']; where a' is the closest integer <= a
210
+ Numeric.create_shared_method('.floor', TosSpec, [],
211
+ &lambda {|vm| vm.push(self.floor); })
212
+
213
+ # [a] .round [a']; where a' is the integer closest to a
214
+ Numeric.create_shared_method('.round', TosSpec, [],
215
+ &lambda {|vm| vm.push(self.round); })
216
+
217
+
218
+ #Advanced math stuff!
219
+ # [] pi [3.141592653589793]
220
+ VirtualMachine.create_shared_method('pi', MacroSpec, [:macro, "vm.push(Math::PI)"])
221
+
222
+ # [] e [2.718281828459045]
223
+ VirtualMachine.create_shared_method('e', MacroSpec, [:macro, "vm.push(Math::E)"])
224
+
225
+ #The number of degrees in one radian.
226
+ DegreesPerRadian = 180.0/Math::PI
227
+
228
+ # [] dpr [2.718281828459045]
229
+ VirtualMachine.create_shared_method('dpr', MacroSpec,
230
+ [:macro, "vm.push(DegreesPerRadian)"])
231
+
232
+ # [degrees] .d2r [radians]
233
+ Numeric.create_shared_method('.d2r', TosSpec, [],
234
+ &lambda {|vm| vm.push(Float.foorth_coerce(self)/DegreesPerRadian); })
235
+
236
+ # [radians] .r2d [degrees]
237
+ Numeric.create_shared_method('.r2d', TosSpec, [],
238
+ &lambda {|vm| vm.push(Float.foorth_coerce(self)*DegreesPerRadian); })
239
+
240
+ # [radians] .cos [cos(radians)]
241
+ Numeric.create_shared_method('.cos', TosSpec, [],
242
+ &lambda {|vm| vm.push(Math::cos(Float.foorth_coerce(self))); })
243
+
244
+ # [radians] .sin [sin(radians)]
245
+ Numeric.create_shared_method('.sin', TosSpec, [],
246
+ &lambda {|vm| vm.push(Math::sin(Float.foorth_coerce(self))); })
247
+
248
+ # [radians] .tan [tan(radians)]
249
+ Numeric.create_shared_method('.tan', TosSpec, [],
250
+ &lambda {|vm| vm.push(Math::tan(Float.foorth_coerce(self))); })
251
+
252
+ # [cos(radians)] .acos [radians]
253
+ Numeric.create_shared_method('.acos', TosSpec, [],
254
+ &lambda {|vm| vm.push(Math::acos(Float.foorth_coerce(self))); })
255
+
256
+ # [sin(radians)] .asin [radians]
257
+ Numeric.create_shared_method('.asin', TosSpec, [],
258
+ &lambda {|vm| vm.push(Math::asin(Float.foorth_coerce(self))); })
259
+
260
+ # [y/x] .atan [radians]
261
+ Numeric.create_shared_method('.atan', TosSpec, [],
262
+ &lambda {|vm| vm.push(Math::atan(Float.foorth_coerce(self))); })
263
+
264
+ # [y x] .atan2 [radians]
265
+ Numeric.create_shared_method('.atan2', TosSpec, [],
266
+ &lambda {|vm| vm.poke(Math::atan2(Float.foorth_coerce(vm.peek), Float.foorth_coerce(self))); })
267
+
268
+ # [radians] .cosh [cosh(radians)]
269
+ Numeric.create_shared_method('.cosh', TosSpec, [],
270
+ &lambda {|vm| vm.push(Math::cosh(Float.foorth_coerce(self))); })
271
+
272
+ # [radians] .sinh [sinh(radians)]
273
+ Numeric.create_shared_method('.sinh', TosSpec, [],
274
+ &lambda {|vm| vm.push(Math::sinh(Float.foorth_coerce(self))); })
275
+
276
+ # [radians] .tanh [tanh(radians)]
277
+ Numeric.create_shared_method('.tanh', TosSpec, [],
278
+ &lambda {|vm| vm.push(Math::tanh(Float.foorth_coerce(self))); })
279
+
280
+ # [cosh(radians)] .acosh [radians]
281
+ Numeric.create_shared_method('.acosh', TosSpec, [],
282
+ &lambda {|vm| vm.push(Math::acosh(Float.foorth_coerce(self))); })
283
+
284
+ # [sinh(radians)] .asinh [radians]
285
+ Numeric.create_shared_method('.asinh', TosSpec, [],
286
+ &lambda {|vm| vm.push(Math::asinh(Float.foorth_coerce(self))); })
287
+
288
+ # [y/x] .atanh [radians]
289
+ Numeric.create_shared_method('.atanh', TosSpec, [],
290
+ &lambda {|vm| vm.push(Math::atanh(Float.foorth_coerce(self))); })
291
+
292
+ # [x] .e** [e**x]
293
+ Numeric.create_shared_method('.e**', TosSpec, [],
294
+ &lambda {|vm| vm.push(Math::exp(self)); })
295
+ Complex.create_shared_method('.e**', TosSpec, [],
296
+ &lambda {|vm| vm.push(Math::E ** self); })
297
+
298
+ # [x] .ln [ln(x)]
299
+ Numeric.create_shared_method('.ln', TosSpec, [],
300
+ &lambda {|vm| vm.push(Math::log(Float.foorth_coerce(self))); })
301
+
302
+ # [x] .10** [10**x]
303
+ Numeric.create_shared_method('.10**', TosSpec, [],
304
+ &lambda {|vm| vm.push(10.0**self); })
305
+
306
+ # [x] .log10 [log10(x)]
307
+ Numeric.create_shared_method('.log10', TosSpec, [],
308
+ &lambda {|vm| vm.push(Math::log10(Float.foorth_coerce(self))); })
309
+
310
+ # [x] .2** [2**x]
311
+ Numeric.create_shared_method('.2**', TosSpec, [],
312
+ &lambda {|vm| vm.push(2.0**self); })
313
+
314
+ # [x] .log2 [log2(x)]
315
+ Numeric.create_shared_method('.log2', TosSpec, [],
316
+ &lambda {|vm| vm.push(Math::log2(Float.foorth_coerce(self))); })
317
+
318
+ # [x] .sqr [square(x)]
319
+ Numeric.create_shared_method('.sqr', TosSpec, [],
320
+ &lambda {|vm| vm.push(self*self); })
321
+
322
+ # [x] .sqrt [square root(x)]
323
+ Numeric.create_shared_method('.sqrt', TosSpec, [],
324
+ &lambda {|vm| vm.push(Math::sqrt(self)); })
325
+ Complex.create_shared_method('.sqrt', TosSpec, [],
326
+ &lambda {|vm| vm.push(self ** 0.5); })
327
+
328
+ # [x] .cube [cube(x)]
329
+ Numeric.create_shared_method('.cube', TosSpec, [],
330
+ &lambda {|vm| vm.push(self*self*self); })
331
+
332
+ # [x] .cbrt [cube root(x)]
333
+ Numeric.create_shared_method('.cbrt', TosSpec, [],
334
+ &lambda {|vm| vm.push(Math::cbrt(self)); })
335
+ Complex.create_shared_method('.cbrt', TosSpec, [],
336
+ &lambda {|vm| vm.push(self ** Rational(1,3)); })
337
+
338
+ # [x y] .hypot [sqrt(x**2 + y**2)]
339
+ Numeric.create_shared_method('.hypot', TosSpec, [], &lambda {|vm|
340
+ vm.poke(Math::hypot(Float.foorth_coerce(self), Float.foorth_coerce(vm.peek)));
341
+ })
342
+
343
+ # [r t] .p2c [x y]; Polar to Cartesian.
344
+ Numeric.create_shared_method('.p2c', TosSpec, [], &lambda {|vm|
345
+ radius = Float.foorth_coerce(vm.pop)
346
+ theta = Float.foorth_coerce(self)
347
+ vm.push(radius * Math::cos(theta))
348
+ vm.push(radius * Math::sin(theta))
349
+ })
350
+
351
+ # [x y] .c2p [r t]; Cartesian to Polar.
352
+ Numeric.create_shared_method('.c2p', TosSpec, [], &lambda {|vm|
353
+ real = Float.foorth_coerce(vm.pop)
354
+ imag = Float.foorth_coerce(self)
355
+ vm.push(Math::hypot(real,imag))
356
+ vm.push(Math::atan2(imag,real))
357
+ })
358
+
359
+ end