Module: RplLang::Words::Branch

Includes:
Types
Included in:
Rpl
Defined in:
lib/rpl/words/branch.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
48
49
50
51
52
# File 'lib/rpl/words/branch.rb', line 8

def populate_dictionary
  super

  category = 'Branch'

  @dictionary.add_word!( ['ift'],
                         category,
                         '( t pt -- … ) eval pt or not based on the value of boolean t',
                         Types.new_object( RplProgram, '« « nop » ifte »' ) )

  @dictionary.add_word!( ['ifte'],
                         category,
                         '( t pt pf -- … ) eval pt or pf based on the value of boolean t',
                         proc do
                           args = stack_extract( [:any, :any, [RplBoolean]] )

                           run!( args[ args[2].value ? 1 : 0 ].value )
                         end )

  @dictionary.add_word!( ['times'],
                         category,
                         '( p n -- … ) eval p n times while pushing counter on stack before',
                         proc do
                           args = stack_extract( [[RplNumeric], :any] )

                           args[0].value.to_i.times do |counter|
                             @stack << Types.new_object( RplNumeric, counter )

                             run!( args[1].value )
                           end
                         end )

  @dictionary.add_word!( ['loop'],
                         category,
                         '( p n1 n2 -- … ) eval p looping from n1 to n2 while pushing counter on stack before',
                         proc do
                           args = stack_extract( [[RplNumeric], [RplNumeric], :any] )

                           ((args[1].value.to_i)..(args[0].value.to_i)).each do |counter|
                             @stack << Types.new_object( RplNumeric, counter )

                             run!( args[2].value )
                           end
                         end )
end