rpl 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,18 +10,18 @@ module RplLang
10
10
 
11
11
  category = 'Strings and Lists'
12
12
 
13
- @dictionary.add_word( ['rev'],
14
- category,
15
- '( s -- s ) reverse string or list',
16
- proc do
17
- args = stack_extract( [[RplString, RplList]] )
13
+ @dictionary.add_word!( ['rev'],
14
+ category,
15
+ '( s -- s ) reverse string or list',
16
+ proc do
17
+ args = stack_extract( [[RplString, RplList]] )
18
18
 
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
24
- end )
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
24
+ end )
25
25
  end
26
26
  end
27
27
  end
@@ -10,79 +10,79 @@ module RplLang
10
10
 
11
11
  category = 'String'
12
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 )
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
86
  end
87
87
  end
88
88
  end
@@ -10,109 +10,109 @@ module RplLang
10
10
 
11
11
  category = 'Test'
12
12
 
13
- @dictionary.add_word( ['>'],
14
- category,
15
- '( a b -- t ) is a greater than b?',
16
- proc do
17
- args = stack_extract( %i[any any] )
18
-
19
- @stack << Types.new_object( RplBoolean, args[1].value > args[0].value )
20
- end )
21
-
22
- @dictionary.add_word( ['≥', '>='],
23
- category,
24
- '( a b -- t ) is a greater than or equal to b?',
25
- proc do
26
- args = stack_extract( %i[any any] )
27
-
28
- @stack << Types.new_object( RplBoolean, args[1].value >= args[0].value )
29
- end )
30
-
31
- @dictionary.add_word( ['<'],
32
- category,
33
- '( a b -- t ) is a less than b?',
34
- proc do
35
- args = stack_extract( %i[any any] )
36
-
37
- @stack << Types.new_object( RplBoolean, args[1].value < args[0].value )
38
- end )
39
-
40
- @dictionary.add_word( ['≤', '<='],
41
- category,
42
- '( a b -- t ) is a less than or equal to b?',
43
- proc do
44
- args = stack_extract( %i[any any] )
45
-
46
- @stack << Types.new_object( RplBoolean, args[1].value <= args[0].value )
47
- end )
48
-
49
- @dictionary.add_word( ['≠', '!='],
50
- category,
51
- '( a b -- t ) is a not equal to b',
52
- proc do
53
- args = stack_extract( %i[any any] )
54
-
55
- @stack << Types.new_object( RplBoolean, args[1].value != args[0].value )
56
- end )
57
-
58
- @dictionary.add_word( ['==', 'same'],
59
- category,
60
- '( a b -- t ) is a equal to b',
61
- proc do
62
- args = stack_extract( %i[any any] )
63
-
64
- @stack << Types.new_object( RplBoolean, args[1].value == args[0].value )
65
- end )
66
-
67
- @dictionary.add_word( ['and'],
68
- category,
69
- '( a b -- t ) boolean and',
70
- proc do
71
- args = stack_extract( [[RplBoolean], [RplBoolean]] )
72
-
73
- @stack << Types.new_object( RplBoolean, args[1].value && args[0].value )
74
- end )
75
-
76
- @dictionary.add_word( ['or'],
77
- category,
78
- '( a b -- t ) boolean or',
79
- proc do
80
- args = stack_extract( [[RplBoolean], [RplBoolean]] )
81
-
82
- @stack << Types.new_object( RplBoolean, args[1].value || args[0].value )
83
- end )
84
-
85
- @dictionary.add_word( ['xor'],
86
- category,
87
- '( a b -- t ) boolean xor',
88
- proc do
89
- args = stack_extract( [[RplBoolean], [RplBoolean]] )
90
-
91
- @stack << Types.new_object( RplBoolean, args[1].value ^ args[0].value )
92
- end )
93
-
94
- @dictionary.add_word( ['not'],
95
- category,
96
- '( a -- t ) invert boolean value',
97
- proc do
98
- args = stack_extract( [[RplBoolean]] )
99
-
100
- @stack << Types.new_object( RplBoolean, !args[0].value )
101
- end )
102
-
103
- @dictionary.add_word( ['true'],
104
- category,
105
- '( -- t ) push true onto stack',
106
- proc do
107
- @stack << Types.new_object( RplBoolean, true )
108
- end )
109
-
110
- @dictionary.add_word( ['false'],
111
- category,
112
- '( -- t ) push false onto stack',
113
- proc do
114
- @stack << Types.new_object( RplBoolean, false )
115
- end )
13
+ @dictionary.add_word!( ['>'],
14
+ category,
15
+ '( a b -- t ) is a greater than b?',
16
+ proc do
17
+ args = stack_extract( %i[any any] )
18
+
19
+ @stack << Types.new_object( RplBoolean, args[1].value > args[0].value )
20
+ end )
21
+
22
+ @dictionary.add_word!( ['≥', '>='],
23
+ category,
24
+ '( a b -- t ) is a greater than or equal to b?',
25
+ proc do
26
+ args = stack_extract( %i[any any] )
27
+
28
+ @stack << Types.new_object( RplBoolean, args[1].value >= args[0].value )
29
+ end )
30
+
31
+ @dictionary.add_word!( ['<'],
32
+ category,
33
+ '( a b -- t ) is a less than b?',
34
+ proc do
35
+ args = stack_extract( %i[any any] )
36
+
37
+ @stack << Types.new_object( RplBoolean, args[1].value < args[0].value )
38
+ end )
39
+
40
+ @dictionary.add_word!( ['≤', '<='],
41
+ category,
42
+ '( a b -- t ) is a less than or equal to b?',
43
+ proc do
44
+ args = stack_extract( %i[any any] )
45
+
46
+ @stack << Types.new_object( RplBoolean, args[1].value <= args[0].value )
47
+ end )
48
+
49
+ @dictionary.add_word!( ['≠', '!='],
50
+ category,
51
+ '( a b -- t ) is a not equal to b',
52
+ proc do
53
+ args = stack_extract( %i[any any] )
54
+
55
+ @stack << Types.new_object( RplBoolean, args[1].value != args[0].value )
56
+ end )
57
+
58
+ @dictionary.add_word!( ['==', 'same'],
59
+ category,
60
+ '( a b -- t ) is a equal to b',
61
+ proc do
62
+ args = stack_extract( %i[any any] )
63
+
64
+ @stack << Types.new_object( RplBoolean, args[1].value == args[0].value )
65
+ end )
66
+
67
+ @dictionary.add_word!( ['and'],
68
+ category,
69
+ '( a b -- t ) boolean and',
70
+ proc do
71
+ args = stack_extract( [[RplBoolean], [RplBoolean]] )
72
+
73
+ @stack << Types.new_object( RplBoolean, args[1].value && args[0].value )
74
+ end )
75
+
76
+ @dictionary.add_word!( ['or'],
77
+ category,
78
+ '( a b -- t ) boolean or',
79
+ proc do
80
+ args = stack_extract( [[RplBoolean], [RplBoolean]] )
81
+
82
+ @stack << Types.new_object( RplBoolean, args[1].value || args[0].value )
83
+ end )
84
+
85
+ @dictionary.add_word!( ['xor'],
86
+ category,
87
+ '( a b -- t ) boolean xor',
88
+ proc do
89
+ args = stack_extract( [[RplBoolean], [RplBoolean]] )
90
+
91
+ @stack << Types.new_object( RplBoolean, args[1].value ^ args[0].value )
92
+ end )
93
+
94
+ @dictionary.add_word!( ['not'],
95
+ category,
96
+ '( a -- t ) invert boolean value',
97
+ proc do
98
+ args = stack_extract( [[RplBoolean]] )
99
+
100
+ @stack << Types.new_object( RplBoolean, !args[0].value )
101
+ end )
102
+
103
+ @dictionary.add_word!( ['true'],
104
+ category,
105
+ '( -- t ) push true onto stack',
106
+ proc do
107
+ @stack << Types.new_object( RplBoolean, true )
108
+ end )
109
+
110
+ @dictionary.add_word!( ['false'],
111
+ category,
112
+ '( -- t ) push false onto stack',
113
+ proc do
114
+ @stack << Types.new_object( RplBoolean, false )
115
+ end )
116
116
  end
117
117
  end
118
118
  end
@@ -12,26 +12,26 @@ module RplLang
12
12
 
13
13
  category = 'Time and date'
14
14
 
15
- @dictionary.add_word( ['time'],
16
- category,
17
- '( -- t ) push current time',
18
- proc do
19
- @stack << Types.new_object( RplString, "\"#{DateTime.now.iso8601.to_s.split('T').last[0..7]}\"" )
20
- end )
21
- @dictionary.add_word( ['date'],
22
- category,
23
- '( -- d ) push current date',
24
- proc do
25
- @stack << Types.new_object( RplString, "\"#{Date.today.iso8601}\"" )
26
- end )
27
- @dictionary.add_word( ['ticks'],
28
- category,
29
- '( -- t ) push datetime as ticks',
30
- proc do
31
- ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000
32
- now = Time.now
33
- @stack << Types.new_object( RplNumeric, now.to_i * 10_000_000 + now.nsec / 100 - ticks_since_epoch )
34
- end )
15
+ @dictionary.add_word!( ['time'],
16
+ category,
17
+ '( -- t ) push current time',
18
+ proc do
19
+ @stack << Types.new_object( RplString, "\"#{DateTime.now.iso8601.to_s.split('T').last[0..7]}\"" )
20
+ end )
21
+ @dictionary.add_word!( ['date'],
22
+ category,
23
+ '( -- d ) push current date',
24
+ proc do
25
+ @stack << Types.new_object( RplString, "\"#{Date.today.iso8601}\"" )
26
+ end )
27
+ @dictionary.add_word!( ['ticks'],
28
+ category,
29
+ '( -- t ) push datetime as ticks',
30
+ proc do
31
+ ticks_since_epoch = Time.utc( 1, 1, 1 ).to_i * 10_000_000
32
+ now = Time.now
33
+ @stack << Types.new_object( RplNumeric, now.to_i * 10_000_000 + now.nsec / 100 - ticks_since_epoch )
34
+ end )
35
35
  end
36
36
  end
37
37
  end
@@ -12,42 +12,42 @@ module RplLang
12
12
 
13
13
  category = 'Trig on reals and complexes'
14
14
 
15
- @dictionary.add_word( ['𝛑', 'pi'],
16
- category,
17
- '( … -- 𝛑 ) push 𝛑',
18
- proc do
19
- @stack << Types.new_object( RplNumeric, BigMath.PI( RplNumeric.precision ) )
20
- end )
15
+ @dictionary.add_word!( ['𝛑', 'pi'],
16
+ category,
17
+ '( … -- 𝛑 ) push 𝛑',
18
+ proc do
19
+ @stack << Types.new_object( RplNumeric, BigMath.PI( RplNumeric.precision ) )
20
+ end )
21
21
 
22
- @dictionary.add_word( ['sin'],
23
- category,
24
- '( n -- m ) compute sinus of n',
25
- proc do
26
- args = stack_extract( [[RplNumeric]] )
22
+ @dictionary.add_word!( ['sin'],
23
+ category,
24
+ '( n -- m ) compute sinus of n',
25
+ proc do
26
+ args = stack_extract( [[RplNumeric]] )
27
27
 
28
- @stack << Types.new_object( RplNumeric, BigMath.sin( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
29
- end )
28
+ @stack << Types.new_object( RplNumeric, BigMath.sin( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
29
+ end )
30
30
 
31
- @dictionary.add_word( ['asin'],
32
- category,
33
- '( n -- m ) compute arg-sinus of n',
34
- Types.new_object( RplProgram, '« dup abs 1 ==
31
+ @dictionary.add_word!( ['asin'],
32
+ category,
33
+ '( n -- m ) compute arg-sinus of n',
34
+ Types.new_object( RplProgram, '« dup abs 1 ==
35
35
  « 𝛑 2 / * »
36
36
  « dup sq 1 swap - sqrt / atan »
37
37
  ifte »' ) )
38
38
 
39
- @dictionary.add_word( ['cos'],
40
- category,
41
- '( n -- m ) compute cosinus of n',
42
- proc do
43
- args = stack_extract( [[RplNumeric]] )
39
+ @dictionary.add_word!( ['cos'],
40
+ category,
41
+ '( n -- m ) compute cosinus of n',
42
+ proc do
43
+ args = stack_extract( [[RplNumeric]] )
44
44
 
45
- @stack << Types.new_object( RplNumeric, BigMath.cos( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
46
- end )
47
- @dictionary.add_word( ['acos'],
48
- category,
49
- '( n -- m ) compute arg-cosinus of n',
50
- Types.new_object( RplProgram, '« dup 0 ==
45
+ @stack << Types.new_object( RplNumeric, BigMath.cos( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
46
+ end )
47
+ @dictionary.add_word!( ['acos'],
48
+ category,
49
+ '( n -- m ) compute arg-cosinus of n',
50
+ Types.new_object( RplProgram, '« dup 0 ==
51
51
  « drop 𝛑 2 / »
52
52
  «
53
53
  dup sq 1 swap - sqrt / atan
@@ -57,29 +57,29 @@ module RplLang
57
57
  »
58
58
  ifte »' ) )
59
59
 
60
- @dictionary.add_word( ['tan'],
61
- category,
62
- '( n -- m ) compute tangent of n',
63
- Types.new_object( RplProgram, '« dup sin swap cos / »' ) )
60
+ @dictionary.add_word!( ['tan'],
61
+ category,
62
+ '( n -- m ) compute tangent of n',
63
+ Types.new_object( RplProgram, '« dup sin swap cos / »' ) )
64
64
 
65
- @dictionary.add_word( ['atan'],
66
- category,
67
- '( n -- m ) compute arc-tangent of n',
68
- proc do
69
- args = stack_extract( [[RplNumeric]] )
65
+ @dictionary.add_word!( ['atan'],
66
+ category,
67
+ '( n -- m ) compute arc-tangent of n',
68
+ proc do
69
+ args = stack_extract( [[RplNumeric]] )
70
70
 
71
- @stack << Types.new_object( RplNumeric, BigMath.atan( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
72
- end )
71
+ @stack << Types.new_object( RplNumeric, BigMath.atan( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
72
+ end )
73
73
 
74
- @dictionary.add_word( ['d→r', 'd->r'],
75
- category,
76
- '( n -- m ) convert degree to radian',
77
- Types.new_object( RplProgram, '« 180 / 𝛑 * »' ) )
74
+ @dictionary.add_word!( ['d→r', 'd->r'],
75
+ category,
76
+ '( n -- m ) convert degree to radian',
77
+ Types.new_object( RplProgram, '« 180 / 𝛑 * »' ) )
78
78
 
79
- @dictionary.add_word( ['r→d', 'r->d'],
80
- category,
81
- '( n -- m ) convert radian to degree',
82
- Types.new_object( RplProgram, '« 𝛑 180 / / »' ) )
79
+ @dictionary.add_word!( ['r→d', 'r->d'],
80
+ category,
81
+ '( n -- m ) convert radian to degree',
82
+ Types.new_object( RplProgram, '« 𝛑 180 / / »' ) )
83
83
  end
84
84
  end
85
85
  end
data/lib/rpl.rb CHANGED
@@ -5,7 +5,7 @@ require 'rpl/types'
5
5
  require 'rpl/words'
6
6
 
7
7
  class Rpl < Interpreter
8
- VERSION = '0.12.0'
8
+ VERSION = '0.13.0'
9
9
 
10
10
  include Types
11
11
 
@@ -31,7 +31,7 @@ class Rpl < Interpreter
31
31
  FileUtils.mkdir_p( File.dirname( @persistence_filename ) )
32
32
  FileUtils.touch( @persistence_filename )
33
33
 
34
- run "\"#{@persistence_filename}\" feval"
34
+ run!( "\"#{@persistence_filename}\" feval" )
35
35
  end
36
36
 
37
37
  def persist_state
@@ -42,7 +42,7 @@ class Rpl < Interpreter
42
42
  end
43
43
  end
44
44
 
45
- def run( input )
45
+ def run!( input )
46
46
  stack = super
47
47
 
48
48
  persist_state if @live_persistence
metadata CHANGED
@@ -1,22 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rpl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.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-10-11 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A language inspired by HP's RPL and https://github.com/louisrubet/rpn/
13
+ description: Reverse-Polish-Lisp inspired language in ruby
14
14
  email: gwenhael@le-moine.org
15
15
  executables:
16
16
  - rpl
17
17
  extensions: []
18
- extra_rdoc_files: []
18
+ extra_rdoc_files:
19
+ - README.md
19
20
  files:
21
+ - README.md
20
22
  - bin/rpl
21
23
  - lib/rpl.rb
22
24
  - lib/rpl/dictionary.rb