rpl 0.5.0 → 0.6.0

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.
data/lib/rpl/types.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rpl/types/boolean'
4
+ require 'rpl/types/name'
5
+ require 'rpl/types/list'
6
+ require 'rpl/types/string'
7
+ require 'rpl/types/program'
8
+ require 'rpl/types/numeric'
@@ -1,38 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RplLang
4
- module Core
4
+ module Words
5
5
  module Branch
6
+ include Types
7
+
6
8
  def populate_dictionary
7
9
  super
8
10
 
9
11
  @dictionary.add_word( ['ift'],
10
12
  'Branch',
11
13
  '( t pt -- … ) eval pt or not based on the value of boolean t',
12
- proc do
13
- run( '« nop » ifte' )
14
- end )
14
+ RplProgram.new( '« « nop » ifte »' ) )
15
15
 
16
16
  @dictionary.add_word( ['ifte'],
17
17
  'Branch',
18
18
  '( t pt pf -- … ) eval pt or pf based on the value of boolean t',
19
19
  proc do
20
- args = stack_extract( [:any, :any, %i[boolean]] )
20
+ args = stack_extract( [:any, :any, [RplBoolean]] )
21
21
 
22
- run( args[ args[2][:value] ? 1 : 0 ][:value] )
22
+ run( args[ args[2].value ? 1 : 0 ].value )
23
23
  end )
24
24
 
25
25
  @dictionary.add_word( ['times'],
26
26
  'Branch',
27
27
  '( p n -- … ) eval p n times while pushing counter on stack before',
28
28
  proc do
29
- args = stack_extract( [%i[numeric], :any] )
29
+ args = stack_extract( [[RplNumeric], :any] )
30
30
 
31
- args[0][:value].to_i.times do |i|
32
- counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 }
33
- @stack << counter
31
+ args[0].value.to_i.times do |counter|
32
+ @stack << RplNumeric.new( counter )
34
33
 
35
- run( args[1][:value] )
34
+ run( args[1].value )
36
35
  end
37
36
  end )
38
37
 
@@ -40,13 +39,12 @@ module RplLang
40
39
  'Branch',
41
40
  '( p n1 n2 -- … ) eval p looping from n1 to n2 while pushing counter on stack before',
42
41
  proc do
43
- args = stack_extract( [%i[numeric], %i[numeric], :any] )
42
+ args = stack_extract( [[RplNumeric], [RplNumeric], :any] )
44
43
 
45
- ((args[1][:value].to_i)..(args[0][:value].to_i)).each do |i|
46
- counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 }
47
- @stack << counter
44
+ ((args[1].value.to_i)..(args[0].value.to_i)).each do |counter|
45
+ @stack << RplNumeric.new( counter )
48
46
 
49
- run( args[2][:value] )
47
+ run( args[2].value )
50
48
  end
51
49
  end )
52
50
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RplLang
4
- module Core
4
+ module Words
5
5
  module FileSystem
6
+ include Types
7
+
6
8
  def populate_dictionary
7
9
  super
8
10
 
@@ -10,29 +12,26 @@ module RplLang
10
12
  'Filesystem',
11
13
  '( filename -- content ) read file and put content on stack as string',
12
14
  proc do
13
- args = stack_extract( [%i[string]] )
15
+ args = stack_extract( [[RplString]] )
14
16
 
15
- path = File.absolute_path( args[0][:value] )
17
+ path = File.absolute_path( args[0].value )
16
18
 
17
- @stack << { type: :string,
18
- value: File.read( path ) }
19
+ @stack << RplString.new( "\"#{File.read( path )}\"" )
19
20
  end )
20
21
 
21
22
  @dictionary.add_word( ['feval'],
22
23
  'Filesystem',
23
24
  '( filename -- … ) read and run file',
24
- proc do
25
- run( 'fread eval' )
26
- end )
25
+ RplProgram.new( '« fread eval »' ) )
27
26
 
28
27
  @dictionary.add_word( ['fwrite'],
29
28
  'Filesystem',
30
29
  '( content filename -- ) write content into filename',
31
30
  proc do
32
- args = stack_extract( [%i[string], :any] )
31
+ args = stack_extract( [[RplString], :any] )
33
32
 
34
- File.write( File.absolute_path( args[0][:value] ),
35
- args[1][:value] )
33
+ File.write( File.absolute_path( args[0].value ),
34
+ args[1].value )
36
35
  end )
37
36
  end
38
37
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RplLang
4
- module Core
4
+ module Words
5
5
  module General
6
+ include Types
7
+
6
8
  def populate_dictionary
7
9
  super
8
10
 
@@ -15,12 +17,24 @@ module RplLang
15
17
  'General',
16
18
  '( w -- s ) pop help string of the given word',
17
19
  proc do
18
- args = stack_extract( [%i[name]] )
20
+ args = stack_extract( [[RplName]] )
21
+
22
+ word = @dictionary.words[ args[0].value ]
19
23
 
20
- word = @dictionary.words[ args[0][:value] ]
24
+ @stack << RplString.new( "\"#{args[0].value}: #{word.nil? ? 'not a core word' : word[:help]}\"" )
25
+ end )
21
26
 
22
- @stack << { type: :string,
23
- value: "#{args[0][:value]}: #{word.nil? ? 'not a core word' : word[:help]}" }
27
+ @dictionary.add_word( ['words'],
28
+ 'REPL',
29
+ 'DEBUG',
30
+ proc do
31
+ @dictionary.words
32
+ .to_a
33
+ .group_by { |word| word.last[:category] }
34
+ .each do |cat, words|
35
+ puts cat
36
+ puts " #{words.map { |word| word.first }.join(', ')}"
37
+ end
24
38
  end )
25
39
 
26
40
  @dictionary.add_word( ['quit'],
@@ -32,14 +46,14 @@ module RplLang
32
46
  'General',
33
47
  '( -- n ) Pop the interpreter\'s version number',
34
48
  proc do
35
- @stack += parse( @version.to_s )
49
+ @stack += Parser.parse( @version.to_s )
36
50
  end )
37
51
 
38
52
  @dictionary.add_word( ['uname'],
39
53
  'General',
40
54
  '( -- s ) Pop the interpreter\'s complete indentification string',
41
55
  proc do
42
- @stack += parse( "\"Rpl Interpreter version #{@version}\"" )
56
+ @stack += Parser.parse( "\"Rpl Interpreter version #{@version}\"" )
43
57
  end )
44
58
 
45
59
  @dictionary.add_word( ['history'],
@@ -51,6 +65,21 @@ module RplLang
51
65
  'REPL',
52
66
  'DEBUG',
53
67
  proc { pp @stack } )
68
+
69
+ @dictionary.add_word( ['.d'],
70
+ 'REPL',
71
+ 'DEBUG',
72
+ proc { pp @dictionary } )
73
+
74
+ @dictionary.add_word( ['.v'],
75
+ 'REPL',
76
+ 'DEBUG',
77
+ proc { pp @dictionary.vars } )
78
+
79
+ @dictionary.add_word( ['.lv'],
80
+ 'REPL',
81
+ 'DEBUG',
82
+ proc { pp @dictionary.local_vars_layers } )
54
83
  end
55
84
  end
56
85
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RplLang
4
- module Core
4
+ module Words
5
5
  module Logarithm
6
+ include Types
7
+
6
8
  def populate_dictionary
7
9
  super
8
10
 
@@ -10,9 +12,7 @@ module RplLang
10
12
  'Logs on reals and complexes',
11
13
  '( … -- ℇ ) push ℇ',
12
14
  proc do
13
- @stack << { type: :numeric,
14
- base: 10,
15
- value: BigMath.E( @precision ) }
15
+ @stack << RplNumeric.new( BigMath.E( RplNumeric.precision ) )
16
16
  end )
17
17
 
18
18
  # @dictionary.add_word( ['ln'],
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RplLang
4
- module Core
4
+ module Words
5
5
  module Mode
6
+ include Types
7
+
6
8
  def populate_dictionary
7
9
  super
8
10
 
@@ -10,15 +12,15 @@ module RplLang
10
12
  'Mode',
11
13
  '( a -- ) set precision to a',
12
14
  proc do
13
- args = stack_extract( [%i[numeric]] )
15
+ args = stack_extract( [[RplNumeric]] )
14
16
 
15
- @precision = args[0][:value]
17
+ RplNumeric.precision = args[0].value
16
18
  end )
17
19
  @dictionary.add_word( ['default'],
18
20
  'Mode',
19
21
  '( -- ) set default precision',
20
22
  proc do
21
- @precision = default_precision
23
+ RplNumeric.default_precision
22
24
  end )
23
25
  @dictionary.add_word( ['type'],
24
26
  'Mode',
@@ -26,8 +28,7 @@ module RplLang
26
28
  proc do
27
29
  args = stack_extract( [:any] )
28
30
 
29
- @stack << { type: :string,
30
- value: args[0][:type].to_s }
31
+ @stack << RplString.new( "\"#{args[0].class.to_s[10..-1]}\"" )
31
32
  end )
32
33
 
33
34
  # @dictionary.add_word( ['std'],