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 = 'Store'
12
+
11
13
  @dictionary.add_word( ['▶', 'sto'],
12
- 'Store',
14
+ category,
13
15
  '( content name -- ) store to variable',
14
16
  proc do
15
17
  args = stack_extract( [[RplName], :any] )
@@ -19,7 +21,7 @@ module RplLang
19
21
  end )
20
22
 
21
23
  @dictionary.add_word( ['rcl'],
22
- 'Store',
24
+ category,
23
25
  '( name -- … ) push content of variable name onto stack',
24
26
  proc do
25
27
  args = stack_extract( [[RplName]] )
@@ -30,7 +32,7 @@ module RplLang
30
32
  end )
31
33
 
32
34
  @dictionary.add_word( ['purge'],
33
- 'Store',
35
+ category,
34
36
  '( name -- ) delete variable',
35
37
  proc do
36
38
  args = stack_extract( [[RplName]] )
@@ -39,58 +41,49 @@ module RplLang
39
41
  end )
40
42
 
41
43
  @dictionary.add_word( ['vars'],
42
- 'Store',
44
+ category,
43
45
  '( -- […] ) list variables',
44
46
  proc do
45
- @stack << Types.new_object( RplList, (@dictionary.vars.keys + @dictionary.local_vars_layers.reduce([]) { |memo, layer| memo + layer.keys }).map { |name| Types.new_object( RplName, name ) } )
47
+ @stack << Types.new_object( RplList, (@dictionary.vars.keys + @dictionary.local_vars_layers.reduce([]) { |memo, layer| memo + layer.keys })
48
+ .map { |name| Types.new_object( RplName, name ) } )
46
49
  end )
47
50
 
48
51
  @dictionary.add_word( ['clusr'],
49
- 'Store',
52
+ category,
50
53
  '( -- ) delete all variables',
51
54
  proc do
52
55
  @dictionary.remove_all_vars
53
56
  end )
54
57
 
55
58
  @dictionary.add_word( ['sto+'],
56
- 'Store',
59
+ category,
57
60
  '( a n -- ) add content to variable\'s value',
58
61
  Types.new_object( RplProgram, '« swap over rcl + swap sto »' ) )
59
62
 
60
63
  @dictionary.add_word( ['sto-'],
61
- 'Store',
64
+ category,
62
65
  '( a n -- ) subtract content to variable\'s value',
63
66
  Types.new_object( RplProgram, '« swap over rcl swap - swap sto »' ) )
64
67
 
65
68
  @dictionary.add_word( ['sto×', 'sto*'],
66
- 'Store',
69
+ category,
67
70
  '( a n -- ) multiply content of variable\'s value',
68
71
  Types.new_object( RplProgram, '« swap over rcl * swap sto »' ) )
69
72
 
70
73
  @dictionary.add_word( ['sto÷', 'sto/'],
71
- 'Store',
74
+ category,
72
75
  '( a n -- ) divide content of variable\'s value',
73
76
  Types.new_object( RplProgram, '« swap over rcl swap / swap sto »' ) )
74
77
 
75
78
  @dictionary.add_word( ['sneg'],
76
- 'Store',
79
+ category,
77
80
  '( a n -- ) negate content of variable\'s value',
78
81
  Types.new_object( RplProgram, '« dup rcl chs swap sto »' ) )
79
82
 
80
83
  @dictionary.add_word( ['sinv'],
81
- 'Store',
84
+ category,
82
85
  '( a n -- ) invert content of variable\'s value',
83
86
  Types.new_object( RplProgram, '« dup rcl inv swap sto »' ) )
84
-
85
- @dictionary.add_word( ['↴', 'lsto'],
86
- 'Program',
87
- '( content name -- ) store to local variable',
88
- proc do
89
- args = stack_extract( [[RplName], :any] )
90
-
91
- @dictionary.add_local_var( args[0].value,
92
- args[1] )
93
- end )
94
87
  end
95
88
  end
96
89
  end
@@ -8,124 +8,19 @@ module RplLang
8
8
  def populate_dictionary
9
9
  super
10
10
 
11
- @dictionary.add_word( ['→str', '->str'],
12
- 'String',
13
- '( a -- s ) convert element to string',
14
- proc do
15
- args = stack_extract( [:any] )
16
-
17
- @stack << Types.new_object( RplString, "\"#{args[0]}\"" )
18
- end )
19
-
20
- @dictionary.add_word( ['str→', 'str->'],
21
- 'String',
22
- '( s -- a ) convert string to element',
23
- proc do
24
- args = stack_extract( [[RplString]] )
25
-
26
- @stack += Parser.parse( args[0].value )
27
- end )
28
-
29
- @dictionary.add_word( ['→list', '->list'],
30
- 'Lists',
31
- '( … x -- […] ) pack x stacks levels into a list',
32
- proc do
33
- args = stack_extract( [[RplNumeric]] )
34
- args = stack_extract( %i[any] * args[0].value )
35
-
36
- @stack << Types.new_object( RplList, args.reverse )
37
- end )
38
-
39
- @dictionary.add_word( ['list→', 'list->'],
40
- 'Lists',
41
- '( […] -- … ) unpack list on stack',
42
- proc do
43
- args = stack_extract( [[RplList]] )
44
-
45
- args[0].value.each do |elt|
46
- @stack << elt
47
- end
48
- end )
49
-
50
- @dictionary.add_word( ['chr'],
51
- 'String',
52
- '( n -- c ) convert ASCII character code in stack level 1 into a string',
53
- proc do
54
- args = stack_extract( [[RplNumeric]] )
55
-
56
- @stack << Types.new_object( RplString, "\"#{args[0].value.to_i.chr}\"" )
57
- end )
58
-
59
- @dictionary.add_word( ['num'],
60
- 'String',
61
- '( s -- n ) return ASCII code of the first character of the string in stack level 1 as a real number',
62
- proc do
63
- args = stack_extract( [[RplString]] )
64
-
65
- @stack << Types.new_object( RplNumeric, args[0].value.ord )
66
- end )
67
-
68
- @dictionary.add_word( ['size'],
69
- 'String',
70
- '( s -- n ) return the length of the string or list',
71
- proc do
72
- args = stack_extract( [[RplString, RplList]] )
73
-
74
- @stack << Types.new_object( RplNumeric, args[0].value.length )
75
- end )
76
-
77
- @dictionary.add_word( ['pos'],
78
- 'String',
79
- '( s s -- n ) search for the string in level 1 within the string in level 2',
80
- proc do
81
- args = stack_extract( [[RplString], [RplString]] )
82
-
83
- @stack << Types.new_object( RplNumeric, args[1].value.index( args[0].value ) )
84
- end )
85
-
86
- @dictionary.add_word( ['sub'],
87
- 'String',
88
- '( s n n -- s ) return a substring of the string in level 3',
89
- proc do
90
- args = stack_extract( [[RplNumeric], [RplNumeric], [RplString]] )
91
-
92
- @stack << Types.new_object( RplString, "\"#{args[2].value[ (args[1].value - 1)..(args[0].value - 1) ]}\"" )
93
- end )
11
+ category = 'Strings and Lists'
94
12
 
95
13
  @dictionary.add_word( ['rev'],
96
- 'String',
14
+ category,
97
15
  '( s -- s ) reverse string or list',
98
16
  proc do
99
17
  args = stack_extract( [[RplString, RplList]] )
100
18
 
101
- args[0].value.reverse!
102
-
103
- @stack << args[0]
104
- end )
105
-
106
- @dictionary.add_word( ['split'],
107
- 'String',
108
- '( s c -- … ) split string s on character c',
109
- proc do
110
- args = stack_extract( [[RplString], [RplString]] )
111
-
112
- args[1].value.split( args[0].value ).each do |elt|
113
- @stack << Types.new_object( RplString, "\"#{elt}\"" )
114
- end
115
- end )
116
-
117
- @dictionary.add_word( ['dolist'],
118
- 'Lists',
119
- '( […] prg -- … ) run prg on each element of a list',
120
- proc do
121
- args = stack_extract( [[RplProgram], [RplList]] )
122
-
123
- args[1].value.each do |elt|
124
- @stack << elt
125
- run( args[0].value )
126
- end
127
-
128
- run( "#{args[1].value.length} →list" )
19
+ @stack << if args[0].is_a?( RplString )
20
+ Types.new_object( RplString, "\"#{args[0].value.reverse}\"" )
21
+ else
22
+ Types.new_object( args[0].class, "{ #{args[0].value.reverse.join(' ')} }" )
23
+ end
129
24
  end )
130
25
  end
131
26
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RplLang
4
+ module Words
5
+ module String
6
+ include Types
7
+
8
+ def populate_dictionary
9
+ super
10
+
11
+ category = 'String'
12
+
13
+ @dictionary.add_word( ['→str', '->str'],
14
+ category,
15
+ '( a -- s ) convert element to string',
16
+ proc do
17
+ args = stack_extract( [:any] )
18
+
19
+ @stack << Types.new_object( RplString, "\"#{args[0]}\"" )
20
+ end )
21
+
22
+ @dictionary.add_word( ['str→', 'str->'],
23
+ category,
24
+ '( s -- a ) convert string to element',
25
+ proc do
26
+ args = stack_extract( [[RplString]] )
27
+
28
+ @stack += Parser.parse( args[0].value )
29
+ end )
30
+
31
+ @dictionary.add_word( ['chr'],
32
+ category,
33
+ '( n -- c ) convert ASCII character code in stack level 1 into a string',
34
+ proc do
35
+ args = stack_extract( [[RplNumeric]] )
36
+
37
+ @stack << Types.new_object( RplString, "\"#{args[0].value.to_i.chr}\"" )
38
+ end )
39
+
40
+ @dictionary.add_word( ['num'],
41
+ category,
42
+ '( s -- n ) return ASCII code of the first character of the string in stack level 1 as a real number',
43
+ proc do
44
+ args = stack_extract( [[RplString]] )
45
+
46
+ @stack << Types.new_object( RplNumeric, args[0].value.ord )
47
+ end )
48
+
49
+ @dictionary.add_word( ['size'],
50
+ category,
51
+ '( s -- n ) return the length of the string or list',
52
+ proc do
53
+ args = stack_extract( [[RplString, RplList]] )
54
+
55
+ @stack << Types.new_object( RplNumeric, args[0].value.length )
56
+ end )
57
+
58
+ @dictionary.add_word( ['pos'],
59
+ category,
60
+ '( s s -- n ) search for the string in level 1 within the string in level 2',
61
+ proc do
62
+ args = stack_extract( [[RplString], [RplString]] )
63
+
64
+ @stack << Types.new_object( RplNumeric, args[1].value.index( args[0].value ) )
65
+ end )
66
+
67
+ @dictionary.add_word( ['sub'],
68
+ category,
69
+ '( s n n -- s ) return a substring of the string in level 3',
70
+ proc do
71
+ args = stack_extract( [[RplNumeric], [RplNumeric], [RplString]] )
72
+
73
+ @stack << Types.new_object( RplString, "\"#{args[2].value[ (args[1].value - 1)..(args[0].value - 1) ]}\"" )
74
+ end )
75
+
76
+ @dictionary.add_word( ['split'],
77
+ category,
78
+ '( s c -- … ) split string s on character c',
79
+ proc do
80
+ args = stack_extract( [[RplString], [RplString]] )
81
+
82
+ args[1].value.split( args[0].value ).each do |elt|
83
+ @stack << Types.new_object( RplString, "\"#{elt}\"" )
84
+ end
85
+ end )
86
+ end
87
+ end
88
+ end
89
+ end
@@ -8,8 +8,10 @@ module RplLang
8
8
  def populate_dictionary
9
9
  super
10
10
 
11
+ category = 'Test'
12
+
11
13
  @dictionary.add_word( ['>'],
12
- 'Test',
14
+ category,
13
15
  '( a b -- t ) is a greater than b?',
14
16
  proc do
15
17
  args = stack_extract( %i[any any] )
@@ -18,7 +20,7 @@ module RplLang
18
20
  end )
19
21
 
20
22
  @dictionary.add_word( ['≥', '>='],
21
- 'Test',
23
+ category,
22
24
  '( a b -- t ) is a greater than or equal to b?',
23
25
  proc do
24
26
  args = stack_extract( %i[any any] )
@@ -27,7 +29,7 @@ module RplLang
27
29
  end )
28
30
 
29
31
  @dictionary.add_word( ['<'],
30
- 'Test',
32
+ category,
31
33
  '( a b -- t ) is a less than b?',
32
34
  proc do
33
35
  args = stack_extract( %i[any any] )
@@ -36,7 +38,7 @@ module RplLang
36
38
  end )
37
39
 
38
40
  @dictionary.add_word( ['≤', '<='],
39
- 'Test',
41
+ category,
40
42
  '( a b -- t ) is a less than or equal to b?',
41
43
  proc do
42
44
  args = stack_extract( %i[any any] )
@@ -45,7 +47,7 @@ module RplLang
45
47
  end )
46
48
 
47
49
  @dictionary.add_word( ['≠', '!='],
48
- 'Test',
50
+ category,
49
51
  '( a b -- t ) is a not equal to b',
50
52
  proc do
51
53
  args = stack_extract( %i[any any] )
@@ -54,7 +56,7 @@ module RplLang
54
56
  end )
55
57
 
56
58
  @dictionary.add_word( ['==', 'same'],
57
- 'Test',
59
+ category,
58
60
  '( a b -- t ) is a equal to b',
59
61
  proc do
60
62
  args = stack_extract( %i[any any] )
@@ -63,7 +65,7 @@ module RplLang
63
65
  end )
64
66
 
65
67
  @dictionary.add_word( ['and'],
66
- 'Test',
68
+ category,
67
69
  '( a b -- t ) boolean and',
68
70
  proc do
69
71
  args = stack_extract( [[RplBoolean], [RplBoolean]] )
@@ -72,7 +74,7 @@ module RplLang
72
74
  end )
73
75
 
74
76
  @dictionary.add_word( ['or'],
75
- 'Test',
77
+ category,
76
78
  '( a b -- t ) boolean or',
77
79
  proc do
78
80
  args = stack_extract( [[RplBoolean], [RplBoolean]] )
@@ -81,7 +83,7 @@ module RplLang
81
83
  end )
82
84
 
83
85
  @dictionary.add_word( ['xor'],
84
- 'Test',
86
+ category,
85
87
  '( a b -- t ) boolean xor',
86
88
  proc do
87
89
  args = stack_extract( [[RplBoolean], [RplBoolean]] )
@@ -90,23 +92,23 @@ module RplLang
90
92
  end )
91
93
 
92
94
  @dictionary.add_word( ['not'],
93
- 'Test',
95
+ category,
94
96
  '( a -- t ) invert boolean value',
95
97
  proc do
96
98
  args = stack_extract( [[RplBoolean]] )
97
99
 
98
- @stack << Types.new_object( RplBoolean,!args[0].value )
100
+ @stack << Types.new_object( RplBoolean, !args[0].value )
99
101
  end )
100
102
 
101
103
  @dictionary.add_word( ['true'],
102
- 'Test',
104
+ category,
103
105
  '( -- t ) push true onto stack',
104
106
  proc do
105
107
  @stack << Types.new_object( RplBoolean, true )
106
108
  end )
107
109
 
108
110
  @dictionary.add_word( ['false'],
109
- 'Test',
111
+ category,
110
112
  '( -- t ) push false onto stack',
111
113
  proc do
112
114
  @stack << Types.new_object( RplBoolean, false )
@@ -10,20 +10,22 @@ module RplLang
10
10
  def populate_dictionary
11
11
  super
12
12
 
13
+ category = 'Time and date'
14
+
13
15
  @dictionary.add_word( ['time'],
14
- 'Time and date',
16
+ category,
15
17
  '( -- t ) push current time',
16
18
  proc do
17
19
  @stack << Types.new_object( RplString, "\"#{DateTime.now.iso8601.to_s.split('T').last[0..7]}\"" )
18
20
  end )
19
21
  @dictionary.add_word( ['date'],
20
- 'Time and date',
22
+ category,
21
23
  '( -- d ) push current date',
22
24
  proc do
23
25
  @stack << Types.new_object( RplString, "\"#{Date.today.iso8601}\"" )
24
26
  end )
25
27
  @dictionary.add_word( ['ticks'],
26
- 'Time and date',
28
+ category,
27
29
  '( -- t ) push datetime as ticks',
28
30
  proc do
29
31
  ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000
@@ -10,15 +10,17 @@ module RplLang
10
10
  def populate_dictionary
11
11
  super
12
12
 
13
+ category = 'Trig on reals and complexes'
14
+
13
15
  @dictionary.add_word( ['𝛑', 'pi'],
14
- 'Trig on reals and complexes',
16
+ category,
15
17
  '( … -- 𝛑 ) push 𝛑',
16
18
  proc do
17
19
  @stack << Types.new_object( RplNumeric, BigMath.PI( RplNumeric.precision ) )
18
20
  end )
19
21
 
20
22
  @dictionary.add_word( ['sin'],
21
- 'Trig on reals and complexes',
23
+ category,
22
24
  '( n -- m ) compute sinus of n',
23
25
  proc do
24
26
  args = stack_extract( [[RplNumeric]] )
@@ -27,7 +29,7 @@ module RplLang
27
29
  end )
28
30
 
29
31
  @dictionary.add_word( ['asin'],
30
- 'Trig on reals and complexes',
32
+ category,
31
33
  '( n -- m ) compute arg-sinus of n',
32
34
  Types.new_object( RplProgram, '« dup abs 1 ==
33
35
  « 𝛑 2 / * »
@@ -35,7 +37,7 @@ module RplLang
35
37
  ifte »' ) )
36
38
 
37
39
  @dictionary.add_word( ['cos'],
38
- 'Trig on reals and complexes',
40
+ category,
39
41
  '( n -- m ) compute cosinus of n',
40
42
  proc do
41
43
  args = stack_extract( [[RplNumeric]] )
@@ -43,7 +45,7 @@ module RplLang
43
45
  @stack << Types.new_object( RplNumeric, BigMath.cos( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
44
46
  end )
45
47
  @dictionary.add_word( ['acos'],
46
- 'Trig on reals and complexes',
48
+ category,
47
49
  '( n -- m ) compute arg-cosinus of n',
48
50
  Types.new_object( RplProgram, '« dup 0 ==
49
51
  « drop 𝛑 2 / »
@@ -56,12 +58,12 @@ module RplLang
56
58
  ifte »' ) )
57
59
 
58
60
  @dictionary.add_word( ['tan'],
59
- 'Trig on reals and complexes',
61
+ category,
60
62
  '( n -- m ) compute tangent of n',
61
63
  Types.new_object( RplProgram, '« dup sin swap cos / »' ) )
62
64
 
63
65
  @dictionary.add_word( ['atan'],
64
- 'Trig on reals and complexes',
66
+ category,
65
67
  '( n -- m ) compute arc-tangent of n',
66
68
  proc do
67
69
  args = stack_extract( [[RplNumeric]] )
@@ -70,12 +72,12 @@ module RplLang
70
72
  end )
71
73
 
72
74
  @dictionary.add_word( ['d→r', 'd->r'],
73
- 'Trig on reals and complexes',
75
+ category,
74
76
  '( n -- m ) convert degree to radian',
75
77
  Types.new_object( RplProgram, '« 180 / 𝛑 * »' ) )
76
78
 
77
79
  @dictionary.add_word( ['r→d', 'r->d'],
78
- 'Trig on reals and complexes',
80
+ category,
79
81
  '( n -- m ) convert radian to degree',
80
82
  Types.new_object( RplProgram, '« 𝛑 180 / / »' ) )
81
83
  end
data/lib/rpl/words.rb CHANGED
@@ -1,15 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rpl/words/branch'
4
+ require 'rpl/words/display'
4
5
  require 'rpl/words/general'
5
6
  require 'rpl/words/mode'
6
- require 'rpl/words/operations'
7
+ require 'rpl/words/operations-reals'
8
+ require 'rpl/words/operations-complexes'
9
+ require 'rpl/words/operations-reals-complexes'
7
10
  require 'rpl/words/program'
11
+ require 'rpl/words/repl'
8
12
  require 'rpl/words/stack'
9
13
  require 'rpl/words/store'
14
+ require 'rpl/words/string'
10
15
  require 'rpl/words/string-list'
11
16
  require 'rpl/words/test'
12
17
  require 'rpl/words/time-date'
13
18
  require 'rpl/words/trig'
14
19
  require 'rpl/words/logarithm'
15
20
  require 'rpl/words/filesystem'
21
+ require 'rpl/words/list'
data/lib/rpl.rb CHANGED
@@ -14,14 +14,20 @@ class Rpl < Interpreter
14
14
  end
15
15
 
16
16
  prepend RplLang::Words::Branch
17
+ prepend RplLang::Words::Display
17
18
  prepend RplLang::Words::FileSystem
18
19
  prepend RplLang::Words::General
20
+ prepend RplLang::Words::List
19
21
  prepend RplLang::Words::Logarithm
20
22
  prepend RplLang::Words::Mode
21
- prepend RplLang::Words::Operations
23
+ prepend RplLang::Words::OperationsReals
24
+ prepend RplLang::Words::OperationsComplexes
25
+ prepend RplLang::Words::OperationsRealsAndComplexes
22
26
  prepend RplLang::Words::Program
27
+ prepend RplLang::Words::REPL
23
28
  prepend RplLang::Words::Stack
24
29
  prepend RplLang::Words::Store
30
+ prepend RplLang::Words::String
25
31
  prepend RplLang::Words::StringAndList
26
32
  prepend RplLang::Words::Test
27
33
  prepend RplLang::Words::TimeAndDate
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gwenhael Le Moine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-12 00:00:00.000000000 Z
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A language inspired by HP's RPL and https://github.com/louisrubet/rpn/
14
14
  email: gwenhael@le-moine.org
@@ -24,6 +24,7 @@ files:
24
24
  - lib/rpl/parser.rb
25
25
  - lib/rpl/types.rb
26
26
  - lib/rpl/types/boolean.rb
27
+ - lib/rpl/types/complex.rb
27
28
  - lib/rpl/types/list.rb
28
29
  - lib/rpl/types/name.rb
29
30
  - lib/rpl/types/numeric.rb
@@ -31,15 +32,21 @@ files:
31
32
  - lib/rpl/types/string.rb
32
33
  - lib/rpl/words.rb
33
34
  - lib/rpl/words/branch.rb
35
+ - lib/rpl/words/display.rb
34
36
  - lib/rpl/words/filesystem.rb
35
37
  - lib/rpl/words/general.rb
38
+ - lib/rpl/words/list.rb
36
39
  - lib/rpl/words/logarithm.rb
37
40
  - lib/rpl/words/mode.rb
38
- - lib/rpl/words/operations.rb
41
+ - lib/rpl/words/operations-complexes.rb
42
+ - lib/rpl/words/operations-reals-complexes.rb
43
+ - lib/rpl/words/operations-reals.rb
39
44
  - lib/rpl/words/program.rb
45
+ - lib/rpl/words/repl.rb
40
46
  - lib/rpl/words/stack.rb
41
47
  - lib/rpl/words/store.rb
42
48
  - lib/rpl/words/string-list.rb
49
+ - lib/rpl/words/string.rb
43
50
  - lib/rpl/words/test.rb
44
51
  - lib/rpl/words/time-date.rb
45
52
  - lib/rpl/words/trig.rb