fOOrth 0.6.6 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +5 -5
  2. data/CODE_OF_CONDUCT.md +49 -0
  3. data/README.md +32 -1
  4. data/fOOrth.gemspec +3 -3
  5. data/integration/array_lib_tests.rb +10 -0
  6. data/integration/compile_lib_tests.rb +67 -1
  7. data/integration/exception_lib_tests.rb +4 -0
  8. data/integration/hash_lib_tests.rb +9 -0
  9. data/integration/numeric_lib_tests.rb +326 -321
  10. data/integration/procedure_lib_tests.rb +16 -0
  11. data/integration/queue_lib_tests.rb +2 -1
  12. data/integration/stack_lib_tests.rb +2 -1
  13. data/integration/stdio_lib_tests.rb +62 -0
  14. data/integration/string_lib_tests.rb +11 -0
  15. data/integration/thread_lib_tests.rb +19 -5
  16. data/lib/fOOrth.rb +0 -2
  17. data/lib/fOOrth/compiler/context.rb +64 -64
  18. data/lib/fOOrth/compiler/context/locals.rb +34 -34
  19. data/lib/fOOrth/compiler/context/map_name.rb +85 -74
  20. data/lib/fOOrth/compiler/context/tags.rb +60 -48
  21. data/lib/fOOrth/compiler/process/generate.rb +1 -1
  22. data/lib/fOOrth/compiler/process/procedure.rb +40 -0
  23. data/lib/fOOrth/compiler/word_specs.rb +3 -3
  24. data/lib/fOOrth/core/object.rb +1 -1
  25. data/lib/fOOrth/library.rb +3 -0
  26. data/lib/fOOrth/library/alias_library.rb +126 -0
  27. data/lib/fOOrth/library/array_library.rb +41 -21
  28. data/lib/fOOrth/library/command_library.rb +1 -1
  29. data/lib/fOOrth/library/compile_library.rb +266 -264
  30. data/lib/fOOrth/library/complex_library.rb +82 -80
  31. data/lib/fOOrth/library/float_library.rb +37 -0
  32. data/lib/fOOrth/library/formatting/array.rb +90 -0
  33. data/lib/fOOrth/library/formatting/bullets.rb +15 -79
  34. data/lib/fOOrth/library/formatting/columns.rb +20 -42
  35. data/lib/fOOrth/library/formatting/hash.rb +29 -0
  36. data/lib/fOOrth/library/formatting/nil.rb +13 -0
  37. data/lib/fOOrth/library/formatting/object.rb +18 -0
  38. data/lib/fOOrth/library/formatting/string.rb +46 -0
  39. data/lib/fOOrth/library/hash_library.rb +14 -6
  40. data/lib/fOOrth/library/introspection/class.rb +20 -18
  41. data/lib/fOOrth/library/introspection/context.rb +3 -2
  42. data/lib/fOOrth/library/introspection/object.rb +42 -20
  43. data/lib/fOOrth/library/introspection/string.rb +21 -5
  44. data/lib/fOOrth/library/introspection/vm.rb +17 -29
  45. data/lib/fOOrth/library/mutex_library.rb +8 -1
  46. data/lib/fOOrth/library/numeric_library.rb +359 -380
  47. data/lib/fOOrth/library/procedure_library.rb +69 -65
  48. data/lib/fOOrth/library/queue_library.rb +6 -1
  49. data/lib/fOOrth/library/rational_library.rb +89 -89
  50. data/lib/fOOrth/library/stack_library.rb +6 -1
  51. data/lib/fOOrth/library/stdio_library.rb +11 -8
  52. data/lib/fOOrth/library/string_library.rb +21 -6
  53. data/lib/fOOrth/library/stubs_library.rb +49 -0
  54. data/lib/fOOrth/monkey_patch/exceptions.rb +2 -6
  55. data/lib/fOOrth/monkey_patch/object.rb +7 -0
  56. data/lib/fOOrth/version.rb +1 -1
  57. data/reek.txt +1 -59
  58. data/sire.rb +0 -1
  59. data/tests/compiler/context_tests.rb +188 -177
  60. data/tests/compiler/file_source_tests.rb +130 -130
  61. data/tests/compiler/parser_tests.rb +4 -4
  62. data/tests/compiler/string_source_tests.rb +4 -4
  63. data/tests/core_tests.rb +138 -138
  64. data/tests/monkey_patch/complex_test.rb +24 -24
  65. data/tests/monkey_patch/object_test.rb +49 -49
  66. data/tests/monkey_patch/string_test.rb +61 -61
  67. metadata +20 -13
@@ -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