fOOrth 0.6.10 → 0.6.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +13 -2
- data/fOOrth.gemspec +2 -2
- data/integration/array_lib_tests.rb +10 -0
- data/integration/exception_lib_tests.rb +4 -0
- data/integration/hash_lib_tests.rb +9 -0
- data/integration/numeric_lib_tests.rb +326 -321
- data/integration/procedure_lib_tests.rb +16 -0
- data/integration/queue_lib_tests.rb +2 -1
- data/integration/stack_lib_tests.rb +2 -1
- data/integration/string_lib_tests.rb +11 -0
- data/integration/thread_lib_tests.rb +11 -8
- data/lib/fOOrth/compiler/context.rb +64 -64
- data/lib/fOOrth/compiler/context/locals.rb +34 -34
- data/lib/fOOrth/compiler/context/map_name.rb +85 -85
- data/lib/fOOrth/compiler/context/tags.rb +60 -48
- data/lib/fOOrth/compiler/process/procedure.rb +40 -0
- data/lib/fOOrth/library.rb +1 -0
- data/lib/fOOrth/library/array_library.rb +41 -21
- data/lib/fOOrth/library/command_library.rb +1 -1
- data/lib/fOOrth/library/compile_library.rb +266 -266
- data/lib/fOOrth/library/complex_library.rb +82 -80
- data/lib/fOOrth/library/float_library.rb +37 -0
- data/lib/fOOrth/library/hash_library.rb +14 -6
- data/lib/fOOrth/library/mutex_library.rb +4 -2
- data/lib/fOOrth/library/numeric_library.rb +359 -380
- data/lib/fOOrth/library/procedure_library.rb +69 -65
- data/lib/fOOrth/library/queue_library.rb +6 -1
- data/lib/fOOrth/library/rational_library.rb +89 -89
- data/lib/fOOrth/library/stack_library.rb +6 -1
- data/lib/fOOrth/library/string_library.rb +18 -6
- data/lib/fOOrth/monkey_patch/exceptions.rb +2 -6
- data/lib/fOOrth/version.rb +1 -1
- data/tests/compiler/context_tests.rb +188 -177
- data/tests/compiler/file_source_tests.rb +130 -130
- data/tests/compiler/parser_tests.rb +4 -4
- data/tests/compiler/string_source_tests.rb +4 -4
- data/tests/core_tests.rb +138 -138
- data/tests/monkey_patch/complex_test.rb +24 -24
- data/tests/monkey_patch/object_test.rb +49 -49
- data/tests/monkey_patch/string_test.rb +61 -61
- 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',
|
11
|
-
Complex.create_shared_method('.ceil',
|
12
|
-
Complex.create_shared_method('.floor',
|
13
|
-
Complex.create_shared_method('.round',
|
14
|
-
Complex.create_shared_method('<',
|
15
|
-
Complex.create_shared_method('>',
|
16
|
-
Complex.create_shared_method('<=',
|
17
|
-
Complex.create_shared_method('>=',
|
18
|
-
Complex.create_shared_method('<=>',
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
vm.push(
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
# [a]
|
30
|
-
|
31
|
-
begin
|
32
|
-
vm.
|
33
|
-
rescue
|
34
|
-
vm.
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
# [b,a]
|
81
|
-
Numeric.create_shared_method('
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
# [b,a]
|
110
|
-
Numeric.create_shared_method('
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
#
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
#
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
# [
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
# []
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
# [
|
344
|
-
Numeric.create_shared_method('.
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
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
|