rpl 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,8 +8,10 @@ module RplLang
8
8
  def populate_dictionary
9
9
  super
10
10
 
11
+ category = 'Mode'
12
+
11
13
  @dictionary.add_word( ['prec'],
12
- 'Mode',
14
+ category,
13
15
  '( a -- ) set precision to a',
14
16
  proc do
15
17
  args = stack_extract( [[RplNumeric]] )
@@ -17,18 +19,18 @@ module RplLang
17
19
  RplNumeric.precision = args[0].value
18
20
  end )
19
21
  @dictionary.add_word( ['default'],
20
- 'Mode',
22
+ category,
21
23
  '( -- ) set default precision',
22
24
  proc do
23
25
  RplNumeric.default_precision
24
26
  end )
25
27
  @dictionary.add_word( ['type'],
26
- 'Mode',
28
+ category,
27
29
  '( a -- s ) type of a as a string',
28
30
  proc do
29
31
  args = stack_extract( [:any] )
30
32
 
31
- @stack << Types.new_object( RplString, "\"#{args[0].class.to_s[10..-1]}\"" )
33
+ @stack << Types.new_object( RplString, "\"#{args[0].class.to_s[10..]}\"" )
32
34
  end )
33
35
 
34
36
  # @dictionary.add_word( ['std'],
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RplLang
4
+ module Words
5
+ module OperationsComplexes
6
+ include Types
7
+
8
+ def populate_dictionary
9
+ super
10
+
11
+ category = 'Operations on complexes'
12
+
13
+ @dictionary.add_word( ['re'],
14
+ category,
15
+ '( c -- n ) complex real part',
16
+ proc do
17
+ args = stack_extract( [[RplComplex]] )
18
+
19
+ @stack << RplNumeric.new( args[0].value.real )
20
+ end )
21
+
22
+ @dictionary.add_word( ['im'],
23
+ category,
24
+ '( c -- n ) complex imaginary part',
25
+ proc do
26
+ args = stack_extract( [[RplComplex]] )
27
+
28
+ @stack << RplNumeric.new( args[0].value.imaginary )
29
+ end )
30
+
31
+ @dictionary.add_word( ['conj'],
32
+ category,
33
+ '( c -- c ) complex conjugate',
34
+ proc do
35
+ args = stack_extract( [[RplComplex]] )
36
+
37
+ @stack << RplComplex.new( args[0].value.conjugate )
38
+ end )
39
+
40
+ @dictionary.add_word( ['arg'],
41
+ category,
42
+ '( c -- n ) complex argument in radians',
43
+ proc do
44
+ args = stack_extract( [[RplComplex]] )
45
+
46
+ @stack << RplNumeric.new( args[0].value.arg )
47
+ end )
48
+
49
+ @dictionary.add_word( ['c→r', 'c->r'],
50
+ category,
51
+ '( c -- n n ) transform a complex in 2 reals',
52
+ Types.new_object( RplProgram, '« dup re swap im »' ) )
53
+
54
+ @dictionary.add_word( ['r→c', 'r->c'],
55
+ category,
56
+ '( n n -- c ) transform 2 reals in a complex',
57
+ proc do
58
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
59
+
60
+ complex_as_string = "(#{args[1].value}#{args[0].value.positive? ? '+' : ''}#{args[0].value}i)"
61
+ @stack << RplComplex.new( complex_as_string )
62
+ end )
63
+
64
+ # @dictionary.add_word( ['p→r', 'p->r'],
65
+ # category,
66
+ # 'cartesian to polar',
67
+ # proc do
68
+
69
+ # end )
70
+
71
+ # @dictionary.add_word( ['r→p', 'r->p'],
72
+ # category,
73
+ # 'polar to cartesian',
74
+ # proc do
75
+
76
+ # end )
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RplLang
4
+ module Words
5
+ module OperationsRealsAndComplexes
6
+ include Types
7
+
8
+ def populate_dictionary
9
+ super
10
+
11
+ category = 'Usual operations on reals and complexes'
12
+
13
+ # Usual operations on reals and complexes
14
+ @dictionary.add_word( ['+'],
15
+ category,
16
+ '( a b -- c ) addition',
17
+ proc do
18
+ addable = [RplNumeric, RplString, RplName, RplList]
19
+ args = stack_extract( [addable, addable] )
20
+ # | + | 1 numeric | 1 string | 1 name |v1 list |
21
+ # |-----------+-----------+----------+--------+--------|
22
+ # | 0 numeric | numeric | string | name |vlist |
23
+ # |v0 string |vstring |vstring |vstring |vlist |
24
+ # |v0 name |vstring |vstring |vname |vlist |
25
+ # |v0 list |vlist |vlist |vlist |vlist |
26
+
27
+ args.reverse!
28
+
29
+ result = if args[0].instance_of?( RplList )
30
+ new_list = if args[1].instance_of?( RplList )
31
+ RplList.new( args[0].to_s ).value.concat( args[1].value )
32
+ else
33
+ RplList.new( args[0].to_s ).value.concat( [args[1]] )
34
+ end
35
+
36
+ RplList.new( "{ #{new_list.join(' ')} }" )
37
+
38
+ elsif args[1].instance_of?( RplList )
39
+ new_list = if args[0].instance_of?( RplList )
40
+ RplList.new( args[0].to_s ).value.concat( args[1].value )
41
+ else
42
+ RplList.new( "{ #{args[0]} }" ).value.concat( args[1].value )
43
+ end
44
+
45
+ RplList.new( "{ #{new_list.join(' ')} }" )
46
+
47
+ elsif args[0].instance_of?( RplString )
48
+ RplString.new( if args[1].instance_of?( RplString ) ||
49
+ args[1].instance_of?( RplName )
50
+ "\"#{args[0].value}#{args[1].value}\""
51
+ else
52
+ "\"#{args[0].value}#{args[1]}\""
53
+ end )
54
+
55
+ elsif args[0].instance_of?( RplName )
56
+
57
+ if args[1].instance_of?( RplName )
58
+ RplName.new( "'#{args[0].value}#{args[1].value}'" )
59
+ elsif args[1].instance_of?( RplString )
60
+ Types.new_object( RplString, "\"#{args[0].value}#{args[1].value}\"" )
61
+ elsif args[1].instance_of?( RplNumeric )
62
+ RplName.new( "'#{args[0].value}#{args[1]}'" )
63
+ else
64
+ Types.new_object( RplString, "\"#{args[0]}#{args[1]}\"" )
65
+ end
66
+
67
+ elsif args[0].instance_of?( RplNumeric )
68
+ if args[1].instance_of?( RplNumeric )
69
+ RplNumeric.new( args[0].value + args[1].value, args[0].base )
70
+ elsif args[1].instance_of?( RplString )
71
+ RplString.new( "\"#{args[0]}#{args[1].value}\"" )
72
+ elsif args[1].instance_of?( RplName )
73
+ RplName.new( "'#{args[0]}#{args[1].value}'" )
74
+ end
75
+ end
76
+
77
+ @stack << result
78
+ end )
79
+
80
+ @dictionary.add_word( ['-'],
81
+ category,
82
+ '( a b -- c ) subtraction',
83
+ proc do
84
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
85
+
86
+ @stack << RplNumeric.new( args[1].value - args[0].value, args[1].base )
87
+ end )
88
+
89
+ @dictionary.add_word( ['chs'],
90
+ category,
91
+ '( a -- b ) negate',
92
+ proc do
93
+ run( '-1 *' )
94
+ end )
95
+
96
+ @dictionary.add_word( ['×', '*'],
97
+ category,
98
+ '( a b -- c ) multiplication',
99
+ proc do
100
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
101
+
102
+ @stack << RplNumeric.new( args[1].value * args[0].value, args[1].base )
103
+ end )
104
+
105
+ @dictionary.add_word( ['÷', '/'],
106
+ category,
107
+ '( a b -- c ) division',
108
+ proc do
109
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
110
+
111
+ @stack << RplNumeric.new( args[1].value / args[0].value, args[1].base )
112
+ end )
113
+
114
+ @dictionary.add_word( ['inv'],
115
+ category,
116
+ '( a -- b ) invert numeric',
117
+ proc do
118
+ run( '1.0 swap /' )
119
+ end )
120
+
121
+ @dictionary.add_word( ['^'],
122
+ category,
123
+ '( a b -- c ) a to the power of b',
124
+ proc do
125
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
126
+
127
+ @stack << RplNumeric.new( args[1].value**args[0].value, args[1].base )
128
+ end )
129
+
130
+ @dictionary.add_word( ['√', 'sqrt'],
131
+ category,
132
+ '( a -- b ) square root',
133
+ proc do
134
+ args = stack_extract( [[RplNumeric]] )
135
+
136
+ @stack << RplNumeric.new( BigMath.sqrt( args[0].value, RplNumeric.precision ), args[0].base )
137
+ end )
138
+
139
+ @dictionary.add_word( ['²', 'sq'],
140
+ category,
141
+ '( a -- b ) square',
142
+ proc do
143
+ run( 'dup ×')
144
+ end )
145
+
146
+ @dictionary.add_word( ['abs'],
147
+ category,
148
+ '( a -- b ) absolute value',
149
+ proc do
150
+ args = stack_extract( [[RplNumeric]] )
151
+
152
+ @stack << RplNumeric.new( args[0].value.abs, args[0].base )
153
+ end )
154
+
155
+ @dictionary.add_word( ['dec'],
156
+ category,
157
+ '( a -- a ) set numeric\'s base to 10',
158
+ proc do
159
+ run( '10 base' )
160
+ end )
161
+
162
+ @dictionary.add_word( ['hex'],
163
+ category,
164
+ '( a -- a ) set numeric\'s base to 16',
165
+ proc do
166
+ run( '16 base' )
167
+ end )
168
+
169
+ @dictionary.add_word( ['bin'],
170
+ category,
171
+ '( a -- a ) set numeric\'s base to 2',
172
+ proc do
173
+ run( '2 base' )
174
+ end )
175
+
176
+ @dictionary.add_word( ['base'],
177
+ category,
178
+ '( a b -- a ) set numeric\'s base to b',
179
+ proc do
180
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
181
+
182
+ @stack << RplNumeric.new( args[1].value, args[0].value )
183
+ end )
184
+
185
+ @dictionary.add_word( ['sign'],
186
+ category,
187
+ '( a -- b ) sign of element',
188
+ proc do
189
+ args = stack_extract( [[RplNumeric]] )
190
+
191
+ @stack << RplNumeric.new( if args[0].value.positive?
192
+ 1
193
+ elsif args[0].value.negative?
194
+ -1
195
+ else
196
+ 0
197
+ end )
198
+ end )
199
+ end
200
+ end
201
+ end
202
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RplLang
4
+ module Words
5
+ module OperationsReals
6
+ include Types
7
+
8
+ def populate_dictionary
9
+ super
10
+
11
+ category = 'Operations on reals'
12
+ # Operations on reals
13
+ @dictionary.add_word( ['%'],
14
+ category,
15
+ '( a b -- c ) b% of a',
16
+ proc do
17
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
18
+
19
+ @stack << RplNumeric.new( args[0].value * ( args[1].value / 100.0 ), args[1].base )
20
+ end )
21
+
22
+ @dictionary.add_word( ['%CH'],
23
+ category,
24
+ '( a b -- c ) b is c% of a',
25
+ proc do
26
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
27
+
28
+ @stack << RplNumeric.new( 100.0 * ( args[0].value / args[1].value ), args[1].base )
29
+ end )
30
+
31
+ @dictionary.add_word( ['mod'],
32
+ category,
33
+ '( a b -- c ) modulo',
34
+ proc do
35
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
36
+
37
+ @stack << RplNumeric.new( args[1].value % args[0].value, args[1].base )
38
+ end )
39
+
40
+ @dictionary.add_word( ['!', 'fact'],
41
+ category,
42
+ '( a -- b ) factorial',
43
+ proc do
44
+ args = stack_extract( [[RplNumeric]] )
45
+
46
+ @stack << RplNumeric.new( Math.gamma( args[0].value ), args[0].base )
47
+ end )
48
+
49
+ @dictionary.add_word( ['floor'],
50
+ category,
51
+ '( a -- b ) highest integer under a',
52
+ proc do
53
+ args = stack_extract( [[RplNumeric]] )
54
+
55
+ @stack << RplNumeric.new( args[0].value.floor, args[0].base )
56
+ end )
57
+
58
+ @dictionary.add_word( ['ceil'],
59
+ category,
60
+ '( a -- b ) highest integer over a',
61
+ proc do
62
+ args = stack_extract( [[RplNumeric]] )
63
+
64
+ @stack << RplNumeric.new( args[0].value.ceil, args[0].base )
65
+ end )
66
+
67
+ @dictionary.add_word( ['min'],
68
+ category,
69
+ '( a b -- a/b ) leave lowest of a or b',
70
+ proc do
71
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
72
+
73
+ @stack << ( args[0].value < args[1].value ? args[0] : args[1] )
74
+ end )
75
+
76
+ @dictionary.add_word( ['max'],
77
+ category,
78
+ '( a b -- a/b ) leave highest of a or b',
79
+ proc do
80
+ args = stack_extract( [[RplNumeric], [RplNumeric]] )
81
+
82
+ @stack << ( args[0].value > args[1].value ? args[0] : args[1] )
83
+ end )
84
+
85
+ @dictionary.add_word( ['mant'],
86
+ category,
87
+ 'mantissa of a real number',
88
+ proc do
89
+ args = stack_extract( [[RplNumeric]] )
90
+
91
+ @stack << Types.new_object( RplNumeric, args[0].value.to_s.split('e').first.to_f.abs )
92
+ end )
93
+
94
+ @dictionary.add_word( ['xpon'],
95
+ category,
96
+ 'exponant of a real number',
97
+ proc do
98
+ args = stack_extract( [[RplNumeric]] )
99
+
100
+ @stack << RplNumeric.new( args[0].value.exponent, args[0].base )
101
+ end )
102
+
103
+ @dictionary.add_word( ['ip'],
104
+ category,
105
+ '( n -- i ) integer part',
106
+ proc do
107
+ run( 'dup fp -' )
108
+ end )
109
+
110
+ @dictionary.add_word( ['fp'],
111
+ category,
112
+ '( n -- f ) fractional part',
113
+ proc do
114
+ args = stack_extract( [[RplNumeric]] )
115
+
116
+ @stack << RplNumeric.new( args[0].value.frac, args[0].base )
117
+ end )
118
+ end
119
+ end
120
+ end
121
+ end
@@ -8,8 +8,10 @@ module RplLang
8
8
  def populate_dictionary
9
9
  super
10
10
 
11
+ category = 'Program'
12
+
11
13
  @dictionary.add_word( ['eval'],
12
- 'Program',
14
+ category,
13
15
  '( a -- … ) interpret',
14
16
  proc do
15
17
  args = stack_extract( [:any] )
@@ -20,6 +22,16 @@ module RplLang
20
22
  run( args[0].value.to_s )
21
23
  end
22
24
  end )
25
+
26
+ @dictionary.add_word( ['↴', 'lsto'],
27
+ category,
28
+ '( content name -- ) store to local variable',
29
+ proc do
30
+ args = stack_extract( [[RplName], :any] )
31
+
32
+ @dictionary.add_local_var( args[0].value,
33
+ args[1] )
34
+ end )
23
35
  end
24
36
  end
25
37
  end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+
5
+ module RplLang
6
+ module Words
7
+ module REPL
8
+ include Types
9
+
10
+ def populate_dictionary
11
+ super
12
+
13
+ category = 'REPL'
14
+
15
+ @dictionary.add_word( ['words'],
16
+ category,
17
+ 'DEBUG',
18
+ proc do
19
+ @dictionary.words
20
+ .to_a
21
+ .group_by { |word| word.last[:category] }
22
+ .each do |cat, words|
23
+ puts cat
24
+ puts " #{words.map(&:first).join(', ')}"
25
+ end
26
+ end )
27
+
28
+ @dictionary.add_word( ['history'],
29
+ category,
30
+ '',
31
+ proc {} )
32
+
33
+ @dictionary.add_word( ['edit'],
34
+ category,
35
+ '( -- s ) Pop the interpreter\'s complete indentification string',
36
+ proc do
37
+ args = stack_extract( [:any] )
38
+
39
+ value = args[0].to_s
40
+ tempfile = Tempfile.new('rpl')
41
+
42
+ begin
43
+ tempfile.write( value )
44
+ tempfile.rewind
45
+
46
+ `$EDITOR #{tempfile.path}`
47
+
48
+ edited_value = tempfile.read
49
+ ensure
50
+ tempfile.close
51
+ tempfile.unlink
52
+ end
53
+
54
+ @stack << Types.new_object( args[0].class, edited_value )
55
+ end )
56
+
57
+ @dictionary.add_word( ['.s'],
58
+ category,
59
+ 'DEBUG',
60
+ proc { pp @stack } )
61
+
62
+ @dictionary.add_word( ['.d'],
63
+ category,
64
+ 'DEBUG',
65
+ proc { pp @dictionary } )
66
+
67
+ @dictionary.add_word( ['.v'],
68
+ category,
69
+ 'DEBUG',
70
+ proc { pp @dictionary.vars } )
71
+
72
+ @dictionary.add_word( ['.lv'],
73
+ category,
74
+ 'DEBUG',
75
+ proc { pp @dictionary.local_vars_layers } )
76
+ end
77
+ end
78
+ end
79
+ end
@@ -8,8 +8,10 @@ module RplLang
8
8
  def populate_dictionary
9
9
  super
10
10
 
11
+ category = 'Stack'
12
+
11
13
  @dictionary.add_word( ['swap'],
12
- 'Stack',
14
+ category,
13
15
  '( a b -- b a ) swap 2 first stack elements',
14
16
  proc do
15
17
  args = stack_extract( %i[any any] )
@@ -18,19 +20,17 @@ module RplLang
18
20
  end )
19
21
 
20
22
  @dictionary.add_word( ['drop'],
21
- 'Stack',
23
+ category,
22
24
  '( a -- ) drop first stack element',
23
25
  Types.new_object( RplProgram, '« 1 dropn »' ) )
24
26
 
25
27
  @dictionary.add_word( ['drop2'],
26
- 'Stack',
28
+ category,
27
29
  '( a b -- ) drop first two stack elements',
28
- proc do
29
- run( '2 dropn' )
30
- end )
30
+ Types.new_object( RplProgram, '« 2 dropn »' ) )
31
31
 
32
32
  @dictionary.add_word( ['dropn'],
33
- 'Stack',
33
+ category,
34
34
  '( a b … n -- ) drop first n stack elements',
35
35
  proc do
36
36
  args = stack_extract( [[RplNumeric]] )
@@ -39,14 +39,14 @@ module RplLang
39
39
  end )
40
40
 
41
41
  @dictionary.add_word( ['del'],
42
- 'Stack',
42
+ category,
43
43
  '( a b … -- ) drop all stack elements',
44
44
  proc do
45
45
  @stack = []
46
46
  end)
47
47
 
48
48
  @dictionary.add_word( ['rot'],
49
- 'Stack',
49
+ category,
50
50
  '( a b c -- b c a ) rotate 3 first stack elements',
51
51
  proc do
52
52
  args = stack_extract( %i[any any any] )
@@ -55,17 +55,17 @@ module RplLang
55
55
  end )
56
56
 
57
57
  @dictionary.add_word( ['dup'],
58
- 'Stack',
58
+ category,
59
59
  '( a -- a a ) duplicate first stack element',
60
60
  Types.new_object( RplProgram, '« 1 dupn »' ) )
61
61
 
62
62
  @dictionary.add_word( ['dup2'],
63
- 'Stack',
63
+ category,
64
64
  '( a b -- a b a b ) duplicate first two stack elements',
65
65
  Types.new_object( RplProgram, '« 2 dupn »' ) )
66
66
 
67
67
  @dictionary.add_word( ['dupn'],
68
- 'Stack',
68
+ category,
69
69
  '( a b … n -- a b … a b … ) duplicate first n stack elements',
70
70
  proc do
71
71
  args = stack_extract( [[RplNumeric]] )
@@ -82,7 +82,7 @@ module RplLang
82
82
  end )
83
83
 
84
84
  @dictionary.add_word( ['pick'],
85
- 'Stack',
85
+ category,
86
86
  '( … b … n -- … b … b ) push a copy of the given stack level onto the stack',
87
87
  proc do
88
88
  args = stack_extract( [[RplNumeric]] )
@@ -99,14 +99,14 @@ module RplLang
99
99
  end )
100
100
 
101
101
  @dictionary.add_word( ['depth'],
102
- 'Stack',
102
+ category,
103
103
  '( … -- … n ) push stack depth onto the stack',
104
104
  proc do
105
105
  @stack << Types.new_object( RplNumeric, stack.size )
106
106
  end )
107
107
 
108
108
  @dictionary.add_word( ['roll'],
109
- 'Stack',
109
+ category,
110
110
  '( … a -- a … ) move a stack element to the top of the stack',
111
111
  proc do
112
112
  args = stack_extract( [[RplNumeric]] )
@@ -123,7 +123,7 @@ module RplLang
123
123
  end )
124
124
 
125
125
  @dictionary.add_word( ['rolld'],
126
- 'Stack',
126
+ category,
127
127
  '( a … -- … a ) move the element on top of the stack to a higher stack position',
128
128
  proc do
129
129
  args = stack_extract( [[RplNumeric]] )
@@ -140,7 +140,7 @@ module RplLang
140
140
  end )
141
141
 
142
142
  @dictionary.add_word( ['over'],
143
- 'Stack',
143
+ category,
144
144
  '( a b -- a b a ) push a copy of the element in stack level 2 onto the stack',
145
145
  Types.new_object( RplProgram, '« 2 pick »' ) )
146
146
  end