rpl 0.8.0 → 0.9.1
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.
- checksums.yaml +4 -4
- data/bin/rpl +2 -2
- data/lib/rpl/interpreter.rb +36 -9
- data/lib/rpl/parser.rb +30 -4
- data/lib/rpl/types/complex.rb +40 -0
- data/lib/rpl/types/list.rb +1 -1
- data/lib/rpl/types/string.rb +2 -0
- data/lib/rpl/types.rb +2 -0
- data/lib/rpl/words/branch.rb +6 -4
- data/lib/rpl/words/display.rb +29 -0
- data/lib/rpl/words/filesystem.rb +5 -3
- data/lib/rpl/words/general.rb +7 -69
- data/lib/rpl/words/list.rb +50 -0
- data/lib/rpl/words/logarithm.rb +17 -15
- data/lib/rpl/words/mode.rb +6 -4
- data/lib/rpl/words/operations-complexes.rb +80 -0
- data/lib/rpl/words/operations-reals-complexes.rb +202 -0
- data/lib/rpl/words/operations-reals.rb +121 -0
- data/lib/rpl/words/program.rb +13 -1
- data/lib/rpl/words/repl.rb +79 -0
- data/lib/rpl/words/stack.rb +17 -17
- data/lib/rpl/words/store.rb +15 -22
- data/lib/rpl/words/string-list.rb +7 -112
- data/lib/rpl/words/string.rb +89 -0
- data/lib/rpl/words/test.rb +15 -13
- data/lib/rpl/words/time-date.rb +5 -3
- data/lib/rpl/words/trig.rb +11 -9
- data/lib/rpl/words.rb +7 -1
- data/lib/rpl.rb +7 -1
- metadata +10 -3
- data/lib/rpl/words/operations.rb +0 -401
data/lib/rpl/words/operations.rb
DELETED
@@ -1,401 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RplLang
|
4
|
-
module Words
|
5
|
-
module Operations
|
6
|
-
include Types
|
7
|
-
|
8
|
-
def populate_dictionary
|
9
|
-
super
|
10
|
-
|
11
|
-
# Usual operations on reals and complexes
|
12
|
-
@dictionary.add_word( ['+'],
|
13
|
-
'Usual operations on reals and complexes',
|
14
|
-
'( a b -- c ) addition',
|
15
|
-
proc do
|
16
|
-
addable = [RplNumeric, RplString, RplName, RplList]
|
17
|
-
args = stack_extract( [addable, addable] )
|
18
|
-
# | + | 1 numeric | 1 string | 1 name |v1 list |
|
19
|
-
# |-----------+-----------+----------+--------+--------|
|
20
|
-
# | 0 numeric | numeric | string | name |vlist |
|
21
|
-
# |v0 string |vstring |vstring |vstring |vlist |
|
22
|
-
# |v0 name |vstring |vstring |vname |vlist |
|
23
|
-
# |v0 list |vlist |vlist |vlist |vlist |
|
24
|
-
|
25
|
-
args.reverse!
|
26
|
-
|
27
|
-
result = if args[0].instance_of?( RplList )
|
28
|
-
if args[1].instance_of?( RplList )
|
29
|
-
args[0].value.concat( args[1].value )
|
30
|
-
else
|
31
|
-
args[0].value << args[1]
|
32
|
-
end
|
33
|
-
args[0]
|
34
|
-
|
35
|
-
elsif args[1].instance_of?( RplList )
|
36
|
-
if args[0].instance_of?( RplList )
|
37
|
-
args[0].value.concat( args[1].value )
|
38
|
-
args[0]
|
39
|
-
else
|
40
|
-
args[1].value.unshift( args[0] )
|
41
|
-
args[1]
|
42
|
-
end
|
43
|
-
|
44
|
-
elsif args[0].instance_of?( RplString )
|
45
|
-
args[0].value = if args[1].instance_of?( RplString ) ||
|
46
|
-
args[1].instance_of?( RplName )
|
47
|
-
"#{args[0].value}#{args[1].value}"
|
48
|
-
else
|
49
|
-
"#{args[0].value}#{args[1]}"
|
50
|
-
end
|
51
|
-
args[0]
|
52
|
-
|
53
|
-
elsif args[0].instance_of?( RplName )
|
54
|
-
|
55
|
-
if args[1].instance_of?( RplName )
|
56
|
-
args[0].value = "#{args[0].value}#{args[1].value}"
|
57
|
-
args[0]
|
58
|
-
else
|
59
|
-
if args[1].instance_of?( RplString )
|
60
|
-
Types.new_object( RplString, "\"#{args[0].value}#{args[1].value}\"" )
|
61
|
-
elsif args[1].instance_of?( RplNumeric )
|
62
|
-
args[0].value = "#{args[0].value}#{args[1]}"
|
63
|
-
args[0]
|
64
|
-
else
|
65
|
-
Types.new_object( RplString, "\"#{args[0]}#{args[1]}\"" )
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
elsif args[0].instance_of?( RplNumeric )
|
70
|
-
if args[1].instance_of?( RplNumeric )
|
71
|
-
args[0].value += args[1].value
|
72
|
-
args[0]
|
73
|
-
|
74
|
-
elsif args[1].instance_of?( RplString ) ||
|
75
|
-
args[1].instance_of?( RplName )
|
76
|
-
args[1].value = "#{args[0]}#{args[1].value}"
|
77
|
-
args[1]
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
@stack << result
|
82
|
-
end )
|
83
|
-
|
84
|
-
@dictionary.add_word( ['-'],
|
85
|
-
'Usual operations on reals and complexes',
|
86
|
-
'( a b -- c ) subtraction',
|
87
|
-
proc do
|
88
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
89
|
-
|
90
|
-
args[1].value = args[1].value - args[0].value
|
91
|
-
|
92
|
-
@stack << args[1]
|
93
|
-
end )
|
94
|
-
|
95
|
-
@dictionary.add_word( ['chs'],
|
96
|
-
'Usual operations on reals and complexes',
|
97
|
-
'( a -- b ) negate',
|
98
|
-
proc do
|
99
|
-
run( '-1 *' )
|
100
|
-
end )
|
101
|
-
|
102
|
-
@dictionary.add_word( ['×', '*'],
|
103
|
-
'Usual operations on reals and complexes',
|
104
|
-
'( a b -- c ) multiplication',
|
105
|
-
proc do
|
106
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
107
|
-
|
108
|
-
args[1].value = args[1].value * args[0].value
|
109
|
-
|
110
|
-
@stack << args[1]
|
111
|
-
end )
|
112
|
-
|
113
|
-
@dictionary.add_word( ['÷', '/'],
|
114
|
-
'Usual operations on reals and complexes',
|
115
|
-
'( a b -- c ) division',
|
116
|
-
proc do
|
117
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
118
|
-
|
119
|
-
args[1].value = args[1].value / args[0].value
|
120
|
-
|
121
|
-
@stack << args[1]
|
122
|
-
end )
|
123
|
-
|
124
|
-
@dictionary.add_word( ['inv'],
|
125
|
-
'Usual operations on reals and complexes',
|
126
|
-
'( a -- b ) invert numeric',
|
127
|
-
proc do
|
128
|
-
run( '1.0 swap /' )
|
129
|
-
end )
|
130
|
-
|
131
|
-
@dictionary.add_word( ['^'],
|
132
|
-
'Usual operations on reals and complexes',
|
133
|
-
'( a b -- c ) a to the power of b',
|
134
|
-
proc do
|
135
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
136
|
-
|
137
|
-
args[1].value = args[1].value**args[0].value
|
138
|
-
|
139
|
-
@stack << args[1]
|
140
|
-
end )
|
141
|
-
|
142
|
-
@dictionary.add_word( ['√', 'sqrt'],
|
143
|
-
'Usual operations on reals and complexes',
|
144
|
-
'( a -- b ) square root',
|
145
|
-
proc do
|
146
|
-
args = stack_extract( [[RplNumeric]] )
|
147
|
-
|
148
|
-
args[0].value = BigMath.sqrt( args[0].value, RplNumeric.precision )
|
149
|
-
|
150
|
-
@stack << args[0]
|
151
|
-
end )
|
152
|
-
|
153
|
-
@dictionary.add_word( ['²', 'sq'],
|
154
|
-
'Usual operations on reals and complexes',
|
155
|
-
'( a -- b ) square',
|
156
|
-
proc do
|
157
|
-
run( 'dup ×')
|
158
|
-
end )
|
159
|
-
|
160
|
-
@dictionary.add_word( ['abs'],
|
161
|
-
'Usual operations on reals and complexes',
|
162
|
-
'( a -- b ) absolute value',
|
163
|
-
proc do
|
164
|
-
args = stack_extract( [[RplNumeric]] )
|
165
|
-
|
166
|
-
args[0].value = args[0].value.abs
|
167
|
-
|
168
|
-
@stack << args[0]
|
169
|
-
end )
|
170
|
-
|
171
|
-
@dictionary.add_word( ['dec'],
|
172
|
-
'Usual operations on reals and complexes',
|
173
|
-
'( a -- a ) set numeric\'s base to 10',
|
174
|
-
proc do
|
175
|
-
run( '10 base' )
|
176
|
-
end )
|
177
|
-
|
178
|
-
@dictionary.add_word( ['hex'],
|
179
|
-
'Usual operations on reals and complexes',
|
180
|
-
'( a -- a ) set numeric\'s base to 16',
|
181
|
-
proc do
|
182
|
-
run( '16 base' )
|
183
|
-
end )
|
184
|
-
|
185
|
-
@dictionary.add_word( ['bin'],
|
186
|
-
'Usual operations on reals and complexes',
|
187
|
-
'( a -- a ) set numeric\'s base to 2',
|
188
|
-
proc do
|
189
|
-
run( '2 base' )
|
190
|
-
end )
|
191
|
-
|
192
|
-
@dictionary.add_word( ['base'],
|
193
|
-
'Usual operations on reals and complexes',
|
194
|
-
'( a b -- a ) set numeric\'s base to b',
|
195
|
-
proc do
|
196
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
197
|
-
|
198
|
-
args[1].base = args[0].value
|
199
|
-
|
200
|
-
@stack << args[1]
|
201
|
-
end )
|
202
|
-
|
203
|
-
@dictionary.add_word( ['sign'],
|
204
|
-
'Usual operations on reals and complexes',
|
205
|
-
'( a -- b ) sign of element',
|
206
|
-
proc do
|
207
|
-
args = stack_extract( [[RplNumeric]] )
|
208
|
-
args[0].value = if args[0].value.positive?
|
209
|
-
1
|
210
|
-
elsif args[0].value.negative?
|
211
|
-
-1
|
212
|
-
else
|
213
|
-
0
|
214
|
-
end
|
215
|
-
|
216
|
-
@stack << args[0]
|
217
|
-
end )
|
218
|
-
|
219
|
-
# Operations on reals
|
220
|
-
@dictionary.add_word( ['%'],
|
221
|
-
'Operations on reals',
|
222
|
-
'( a b -- c ) b% of a',
|
223
|
-
proc do
|
224
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
225
|
-
|
226
|
-
args[1].value = args[0].value * ( args[1].value / 100.0 )
|
227
|
-
|
228
|
-
@stack << args[1]
|
229
|
-
end )
|
230
|
-
|
231
|
-
@dictionary.add_word( ['%CH'],
|
232
|
-
'Operations on reals',
|
233
|
-
'( a b -- c ) b is c% of a',
|
234
|
-
proc do
|
235
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
236
|
-
|
237
|
-
args[1].value = 100.0 * ( args[0].value / args[1].value )
|
238
|
-
|
239
|
-
@stack << args[1]
|
240
|
-
end )
|
241
|
-
|
242
|
-
@dictionary.add_word( ['mod'],
|
243
|
-
'Operations on reals',
|
244
|
-
'( a b -- c ) modulo',
|
245
|
-
proc do
|
246
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
247
|
-
|
248
|
-
args[1].value = args[1].value % args[0].value
|
249
|
-
|
250
|
-
@stack << args[1]
|
251
|
-
end )
|
252
|
-
|
253
|
-
@dictionary.add_word( ['!', 'fact'],
|
254
|
-
'Operations on reals',
|
255
|
-
'( a -- b ) factorial',
|
256
|
-
proc do
|
257
|
-
args = stack_extract( [[RplNumeric]] )
|
258
|
-
|
259
|
-
args[0].value = Math.gamma( args[0].value )
|
260
|
-
|
261
|
-
@stack << args[0]
|
262
|
-
end )
|
263
|
-
|
264
|
-
@dictionary.add_word( ['floor'],
|
265
|
-
'Operations on reals',
|
266
|
-
'( a -- b ) highest integer under a',
|
267
|
-
proc do
|
268
|
-
args = stack_extract( [[RplNumeric]] )
|
269
|
-
|
270
|
-
args[0].value = args[0].value.floor
|
271
|
-
|
272
|
-
@stack << args[0]
|
273
|
-
end )
|
274
|
-
|
275
|
-
@dictionary.add_word( ['ceil'],
|
276
|
-
'Operations on reals',
|
277
|
-
'( a -- b ) highest integer over a',
|
278
|
-
proc do
|
279
|
-
args = stack_extract( [[RplNumeric]] )
|
280
|
-
|
281
|
-
args[0].value = args[0].value.ceil
|
282
|
-
|
283
|
-
@stack << args[0]
|
284
|
-
end )
|
285
|
-
|
286
|
-
@dictionary.add_word( ['min'],
|
287
|
-
'Operations on reals',
|
288
|
-
'( a b -- a/b ) leave lowest of a or b',
|
289
|
-
proc do
|
290
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
291
|
-
|
292
|
-
@stack << ( args[0].value < args[1].value ? args[0] : args[1] )
|
293
|
-
end )
|
294
|
-
|
295
|
-
@dictionary.add_word( ['max'],
|
296
|
-
'Operations on reals',
|
297
|
-
'( a b -- a/b ) leave highest of a or b',
|
298
|
-
proc do
|
299
|
-
args = stack_extract( [[RplNumeric], [RplNumeric]] )
|
300
|
-
|
301
|
-
@stack << ( args[0].value > args[1].value ? args[0] : args[1] )
|
302
|
-
end )
|
303
|
-
|
304
|
-
@dictionary.add_word( ['mant'],
|
305
|
-
'Operations on reals',
|
306
|
-
'mantissa of a real number',
|
307
|
-
proc do
|
308
|
-
args = stack_extract( [[RplNumeric]] )
|
309
|
-
|
310
|
-
@stack << Types.new_object( RplNumeric, args[0].value.to_s.split('e').first.to_f.abs )
|
311
|
-
end )
|
312
|
-
|
313
|
-
@dictionary.add_word( ['xpon'],
|
314
|
-
'Operations on reals',
|
315
|
-
'exponant of a real number',
|
316
|
-
proc do
|
317
|
-
args = stack_extract( [[RplNumeric]] )
|
318
|
-
|
319
|
-
args[0].value = args[0].value.exponent
|
320
|
-
|
321
|
-
@stack << args[0]
|
322
|
-
end )
|
323
|
-
|
324
|
-
@dictionary.add_word( ['ip'],
|
325
|
-
'Operations on reals',
|
326
|
-
'( n -- i ) integer part',
|
327
|
-
proc do
|
328
|
-
run( 'dup fp -' )
|
329
|
-
end )
|
330
|
-
|
331
|
-
@dictionary.add_word( ['fp'],
|
332
|
-
'Operations on reals',
|
333
|
-
'( n -- f ) fractional part',
|
334
|
-
proc do
|
335
|
-
args = stack_extract( [[RplNumeric]] )
|
336
|
-
|
337
|
-
args[0].value = args[0].value.frac
|
338
|
-
|
339
|
-
@stack << args[0]
|
340
|
-
end )
|
341
|
-
|
342
|
-
# Operations on complexes
|
343
|
-
# @dictionary.add_word( ['re'],
|
344
|
-
# 'Operations on complexes',
|
345
|
-
# 'complex real part',
|
346
|
-
# proc do
|
347
|
-
|
348
|
-
# end )
|
349
|
-
|
350
|
-
# @dictionary.add_word( 'im',
|
351
|
-
# 'Operations on complexes',
|
352
|
-
# 'complex imaginary part',
|
353
|
-
# proc do
|
354
|
-
|
355
|
-
# end )
|
356
|
-
|
357
|
-
# @dictionary.add_word( ['conj'],
|
358
|
-
# 'Operations on complexes',
|
359
|
-
# 'complex conjugate',
|
360
|
-
# proc do
|
361
|
-
|
362
|
-
# end )
|
363
|
-
|
364
|
-
# @dictionary.add_word( 'arg',
|
365
|
-
# 'Operations on complexes',
|
366
|
-
# 'complex argument in radians',
|
367
|
-
# proc do
|
368
|
-
|
369
|
-
# end )
|
370
|
-
|
371
|
-
# @dictionary.add_word( ['c→r', 'c->r'],
|
372
|
-
# 'Operations on complexes',
|
373
|
-
# 'transform a complex in 2 reals',
|
374
|
-
# proc do
|
375
|
-
|
376
|
-
# end )
|
377
|
-
|
378
|
-
# @dictionary.add_word( ['r→c', 'r->c'],
|
379
|
-
# 'Operations on complexes',
|
380
|
-
# 'transform 2 reals in a complex',
|
381
|
-
# proc do
|
382
|
-
|
383
|
-
# end )
|
384
|
-
|
385
|
-
# @dictionary.add_word( ['p→r', 'p->r'],
|
386
|
-
# 'Operations on complexes',
|
387
|
-
# 'cartesian to polar',
|
388
|
-
# proc do
|
389
|
-
|
390
|
-
# end )
|
391
|
-
|
392
|
-
# @dictionary.add_word( ['r→p', 'r->p'],
|
393
|
-
# 'Operations on complexes',
|
394
|
-
# 'polar to cartesian',
|
395
|
-
# proc do
|
396
|
-
|
397
|
-
# end )
|
398
|
-
end
|
399
|
-
end
|
400
|
-
end
|
401
|
-
end
|