Module: RplLang::Words::List

Includes:
Types
Included in:
Rpl
Defined in:
lib/rpl/words/list.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
# File 'lib/rpl/words/list.rb', line 8

def populate_dictionary
  super

  category = 'Lists'

  @dictionary.add_word( ['→list', '->list'],
                        category,
                        '( … x -- […] ) pack x stacks levels into a list',
                        proc do
                          args = stack_extract( [[RplNumeric]] )
                          args = stack_extract( %i[any] * args[0].value )

                          @stack << Types.new_object( RplList, args.reverse )
                        end )

  @dictionary.add_word( ['list→', 'list->'],
                        category,
                        '( […] -- … ) unpack list on stack',
                        proc do
                          args = stack_extract( [[RplList]] )

                          args[0].value.each do |elt|
                            @stack << elt
                          end
                        end )

  @dictionary.add_word( ['dolist'],
                        category,
                        '( […] prg -- … ) run prg on each element of a list',
                        proc do
                          args = stack_extract( [[RplProgram], [RplList]] )

                          args[1].value.each do |elt|
                            @stack << elt
                            run( args[0].value )
                          end

                          run( "#{args[1].value.length} →list" )
                        end )
end