Module: RplLang::Words::Stack

Includes:
Types
Included in:
Rpl
Defined in:
lib/rpl/words/stack.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rpl/words/stack.rb', line 8

def populate_dictionary
  super

  category = 'Stack'

  @dictionary.add_word( ['swap'],
                        category,
                        '( a b -- b a ) swap 2 first stack elements',
                        proc do
                          args = stack_extract( %i[any any] )

                          @stack << args[0] << args[1]
                        end )

  @dictionary.add_word( ['drop'],
                        category,
                        '( a -- ) drop first stack element',
                        Types.new_object( RplProgram, '« 1 dropn »' ) )

  @dictionary.add_word( ['drop2'],
                        category,
                        '( a b -- ) drop first two stack elements',
                        Types.new_object( RplProgram, '« 2 dropn »' ) )

  @dictionary.add_word( ['dropn'],
                        category,
                        '( a b … n -- ) drop first n stack elements',
                        proc do
                          args = stack_extract( [[RplNumeric]] )

                          _args = stack_extract( %i[any] * args[0].value )
                        end )

  @dictionary.add_word( ['del'],
                        category,
                        '( a b … -- ) drop all stack elements',
                        proc do
                          @stack = []
                        end)

  @dictionary.add_word( ['rot'],
                        category,
                        '( a b c -- b c a ) rotate 3 first stack elements',
                        proc do
                          args = stack_extract( %i[any any any] )

                          @stack << args[1] << args[0] << args[2]
                        end )

  @dictionary.add_word( ['dup'],
                        category,
                        '( a -- a a ) duplicate first stack element',
                        Types.new_object( RplProgram, '« 1 dupn »' ) )

  @dictionary.add_word( ['dup2'],
                        category,
                        '( a b -- a b a b ) duplicate first two stack elements',
                        Types.new_object( RplProgram, '« 2 dupn »' ) )

  @dictionary.add_word( ['dupn'],
                        category,
                        '( a b … n -- a b … a b … ) duplicate first n stack elements',
                        proc do
                          args = stack_extract( [[RplNumeric]] )
                          n = args[0].value.to_i
                          args = stack_extract( %i[any] * args[0].value )

                          args.reverse!

                          2.times do
                            n.times.each do |i|
                              @stack << Marshal.load(Marshal.dump( args[i] ))
                            end
                          end
                        end )

  @dictionary.add_word( ['pick'],
                        category,
                        '( … b … n -- … b … b ) push a copy of the given stack level onto the stack',
                        proc do
                          args = stack_extract( [[RplNumeric]] )
                          n = args[0].value.to_i
                          args = stack_extract( %i[any] * args[0].value )

                          args.reverse!

                          n.times.each do |i|
                            @stack << args[ i ]
                          end

                          @stack << args[0]
                        end )

  @dictionary.add_word( ['depth'],
                        category,
                        '( … -- … n ) push stack depth onto the stack',
                        proc do
                          @stack << Types.new_object( RplNumeric, stack.size )
                        end )

  @dictionary.add_word( ['roll'],
                        category,
                        '( … a -- a … ) move a stack element to the top of the stack',
                        proc do
                          args = stack_extract( [[RplNumeric]] )
                          n = args[0].value
                          args = stack_extract( %i[any] * args[0].value )

                          args.reverse!

                          (1..(n - 1)).each do |i|
                            @stack << args[ i ]
                          end

                          @stack << args[0]
                        end )

  @dictionary.add_word( ['rolld'],
                        category,
                        '( a … -- … a ) move the element on top of the stack to a higher stack position',
                        proc do
                          args = stack_extract( [[RplNumeric]] )
                          n = args[0].value
                          args = stack_extract( %i[any] * args[0].value )

                          args.reverse!

                          @stack << args[n - 1]

                          (0..(n - 2)).each do |i|
                            @stack << args[ i ]
                          end
                        end )

  @dictionary.add_word( ['over'],
                        category,
                        '( a b -- a b a ) push a copy of the element in stack level 2 onto the stack',
                        Types.new_object( RplProgram, '« 2 pick »' ) )
end