Module: RplLang::Words::OperationsRealsAndComplexes

Includes:
Types
Included in:
Rpl
Defined in:
lib/rpl/words/operations-reals-complexes.rb

Instance Method Summary collapse

Methods included from Types

new_object

Instance Method Details

#populate_dictionaryObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
81
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
110
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
# File 'lib/rpl/words/operations-reals-complexes.rb', line 8

def populate_dictionary
  super

  category = 'Usual operations on reals and complexes'

  # Usual operations on reals and complexes
  @dictionary.add_word( ['+'],
                        category,
                        '( a b -- c ) addition',
                        proc do
                          addable = [RplNumeric, RplString, RplName, RplList]
                          args = stack_extract( [addable, addable] )
                          # | +         | 1 numeric | 1 string | 1 name |v1 list |
                          # |-----------+-----------+----------+--------+--------|
                          # | 0 numeric | numeric   | string   | name   |vlist   |
                          # |v0 string  |vstring    |vstring   |vstring |vlist   |
                          # |v0 name    |vstring    |vstring   |vname   |vlist   |
                          # |v0 list    |vlist      |vlist     |vlist   |vlist   |

                          args.reverse!

                          result = if args[0].instance_of?( RplList )
                                     new_list = if args[1].instance_of?( RplList )
                                                  RplList.new( args[0].to_s ).value.concat( args[1].value )
                                                else
                                                  RplList.new( args[0].to_s ).value.concat( [args[1]] )
                                                end

                                     RplList.new( "{ #{new_list.join(' ')} }" )

                                   elsif args[1].instance_of?( RplList )
                                     new_list = if args[0].instance_of?( RplList )
                                                  RplList.new( args[0].to_s ).value.concat( args[1].value )
                                                else
                                                  RplList.new( "{ #{args[0]} }" ).value.concat( args[1].value )
                                                end

                                     RplList.new( "{ #{new_list.join(' ')} }" )

                                   elsif args[0].instance_of?( RplString )
                                     RplString.new( if args[1].instance_of?( RplString ) ||
                                                       args[1].instance_of?( RplName )
                                                      "\"#{args[0].value}#{args[1].value}\""
                                                    else
                                                      "\"#{args[0].value}#{args[1]}\""
                                                    end )

                                   elsif args[0].instance_of?( RplName )

                                     if args[1].instance_of?( RplName )
                                       RplName.new( "'#{args[0].value}#{args[1].value}'" )
                                     elsif args[1].instance_of?( RplString )
                                       Types.new_object( RplString, "\"#{args[0].value}#{args[1].value}\"" )
                                     elsif args[1].instance_of?( RplNumeric )
                                       RplName.new( "'#{args[0].value}#{args[1]}'" )
                                     else
                                       Types.new_object( RplString, "\"#{args[0]}#{args[1]}\"" )
                                     end

                                   elsif args[0].instance_of?( RplNumeric )
                                     if args[1].instance_of?( RplNumeric )
                                       RplNumeric.new( args[0].value + args[1].value, args[0].base )
                                     elsif args[1].instance_of?( RplString )
                                       RplString.new( "\"#{args[0]}#{args[1].value}\"" )
                                     elsif args[1].instance_of?( RplName )
                                       RplName.new( "'#{args[0]}#{args[1].value}'" )
                                     end
                                   end

                          @stack << result
                        end )

  @dictionary.add_word( ['-'],
                        category,
                        '( a b -- c ) subtraction',
                        proc do
                          args = stack_extract( [[RplNumeric], [RplNumeric]] )

                          @stack << RplNumeric.new( args[1].value - args[0].value, args[1].base )
                        end )

  @dictionary.add_word( ['chs'],
                        category,
                        '( a -- b ) negate',
                        proc do
                          run( '-1 *' )
                        end )

  @dictionary.add_word( ['×', '*'],
                        category,
                        '( a b -- c ) multiplication',
                        proc do
                          args = stack_extract( [[RplNumeric], [RplNumeric]] )

                          @stack << RplNumeric.new( args[1].value * args[0].value, args[1].base )
                        end )

  @dictionary.add_word( ['÷', '/'],
                        category,
                        '( a b -- c ) division',
                        proc do
                          args = stack_extract( [[RplNumeric], [RplNumeric]] )

                          @stack << RplNumeric.new( args[1].value / args[0].value, args[1].base )
                        end )

  @dictionary.add_word( ['inv'],
                        category,
                        '( a -- b ) invert numeric',
                        proc do
                          run( '1.0 swap /' )
                        end )

  @dictionary.add_word( ['^'],
                        category,
                        '( a b -- c ) a to the power of b',
                        proc do
                          args = stack_extract( [[RplNumeric], [RplNumeric]] )

                          @stack << RplNumeric.new( args[1].value**args[0].value, args[1].base )
                        end )

  @dictionary.add_word( ['', 'sqrt'],
                        category,
                        '( a -- b ) square root',
                        proc do
                          args = stack_extract( [[RplNumeric]] )

                          @stack << RplNumeric.new( BigMath.sqrt( args[0].value, RplNumeric.precision ), args[0].base )
                        end )

  @dictionary.add_word( ['²', 'sq'],
                        category,
                        '( a -- b ) square',
                        proc do
                          run( 'dup ×')
                        end )

  @dictionary.add_word( ['abs'],
                        category,
                        '( a -- b ) absolute value',
                        proc do
                          args = stack_extract( [[RplNumeric]] )

                          @stack << RplNumeric.new( args[0].value.abs, args[0].base )
                        end )

  @dictionary.add_word( ['dec'],
                        category,
                        '( a -- a ) set numeric\'s base to 10',
                        proc do
                          run( '10 base' )
                        end )

  @dictionary.add_word( ['hex'],
                        category,
                        '( a -- a ) set numeric\'s base to 16',
                        proc do
                          run( '16 base' )
                        end )

  @dictionary.add_word( ['bin'],
                        category,
                        '( a -- a ) set numeric\'s base to 2',
                        proc do
                          run( '2 base' )
                        end )

  @dictionary.add_word( ['base'],
                        category,
                        '( a b -- a ) set numeric\'s base to b',
                        proc do
                          args = stack_extract( [[RplNumeric], [RplNumeric]] )

                          @stack << RplNumeric.new( args[1].value, args[0].value )
                        end )

  @dictionary.add_word( ['sign'],
                        category,
                        '( a -- b ) sign of element',
                        proc do
                          args = stack_extract( [[RplNumeric]] )

                          @stack << RplNumeric.new( if args[0].value.positive?
                                                      1
                                                    elsif args[0].value.negative?
                                                      -1
                                                    else
                                                      0
                                                    end )
                        end )
end