Class: Windstorm::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/windstorm/machine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandsObject



89
90
91
92
# File 'lib/windstorm/machine.rb', line 89

def commands
  @commands ||= []
  @commands
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



5
6
7
# File 'lib/windstorm/machine.rb', line 5

def debug=(value)
  @debug = value
end

#flash=(value) ⇒ Object (writeonly)

Sets the attribute flash

Parameters:

  • value

    the value to set the attribute flash to.



5
6
7
# File 'lib/windstorm/machine.rb', line 5

def flash=(value)
  @flash = value
end

#loose=(value) ⇒ Object (writeonly)

Sets the attribute loose

Parameters:

  • value

    the value to set the attribute loose to.



5
6
7
# File 'lib/windstorm/machine.rb', line 5

def loose=(value)
  @loose = value
end

#sizeObject



156
157
158
159
160
# File 'lib/windstorm/machine.rb', line 156

def size
  @size ||= BUFFER_DEFAULT_SIZE
  @size = BUFFER_DEFAULT_SIZE if @size <= 0
  @size
end

Class Method Details

.create(coms, params = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/windstorm/machine.rb', line 9

def create(coms, params = nil)
  params ||= {}
  s = params[:size].to_i

  m = self.new
  m.commands = coms
  m.size = s if s > 0
  m.debug = true if params[:debug]
  m.flash = true if params[:flash]
  m.loose = true if params[:loose]
  m
end

.execute(coms, params = nil) ⇒ Object



22
23
24
25
# File 'lib/windstorm/machine.rb', line 22

def execute(coms, params = nil)
  m = create(coms, params)
  m.execute
end

Instance Method Details

#add_indexObject



99
100
101
102
# File 'lib/windstorm/machine.rb', line 99

def add_index
  @index = index + 1
  @index
end

#add_stepObject



84
85
86
87
# File 'lib/windstorm/machine.rb', line 84

def add_step
  @step = step + 1
  step
end

#bufferObject



186
187
188
189
# File 'lib/windstorm/machine.rb', line 186

def buffer
  @buffer ||= []
  @buffer
end

#cacheObject Also known as: result



204
205
206
207
# File 'lib/windstorm/machine.rb', line 204

def cache
  @cache ||= ""
  @cache
end

#clipObject



230
231
232
# File 'lib/windstorm/machine.rb', line 230

def clip
  replace_clip_value(value)
end

#clip_valueObject



220
221
222
223
# File 'lib/windstorm/machine.rb', line 220

def clip_value
  @clip_value ||= 0
  @clip_value
end

#com(ind = nil) ⇒ Object



104
105
106
107
# File 'lib/windstorm/machine.rb', line 104

def com(ind = nil)
  ind ||= index
  commands[ind]
end

#debug?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/windstorm/machine.rb', line 67

def debug?
  @debug ? true : false
end

#debug_outObject



238
239
240
# File 'lib/windstorm/machine.rb', line 238

def debug_out
  puts "step:#{step} com:#{com} index:#{index} point:#{point} buffer:#{buffer.inspect} clip:#{clip_value} result:#{result}"
end

#decrementObject



182
183
184
# File 'lib/windstorm/machine.rb', line 182

def decrement
  replace_value(value-1)
end

#executeObject



29
30
31
32
33
34
35
36
37
# File 'lib/windstorm/machine.rb', line 29

def execute
  return result if finish?
  parse_jump
  loop do
    break if finish?
    step_execute
  end
  result
end

#finish?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/windstorm/machine.rb', line 115

def finish?
  index >= commands.size ? true : false
end

#forwardObject



109
110
111
112
113
# File 'lib/windstorm/machine.rb', line 109

def forward
  add_step
  add_index
  self
end

#incrementObject



178
179
180
# File 'lib/windstorm/machine.rb', line 178

def increment
  replace_value(value+1)
end

#indexObject



94
95
96
97
# File 'lib/windstorm/machine.rb', line 94

def index
  @index ||= 0
  @index
end

#inputObject



215
216
217
218
# File 'lib/windstorm/machine.rb', line 215

def input
  c = $stdin.getc
  replace_value(c.bytes.first)
end

#jumpObject



152
153
154
# File 'lib/windstorm/machine.rb', line 152

def jump
  jump_index(jump_table[index])
end

#jump_index(ind) ⇒ Object



146
147
148
149
150
# File 'lib/windstorm/machine.rb', line 146

def jump_index(ind)
  error 'jump index is nil' unless ind
  strict_error 'out of command index' if ind < 0 || ind >= commands.size
  @index = ind
end

#jump_tableObject



119
120
121
122
# File 'lib/windstorm/machine.rb', line 119

def jump_table
  @jump_table ||= {}
  @jump_table
end

#loose?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/windstorm/machine.rb', line 71

def loose?
  @loose ? true : false
end

#move_point_decObject



173
174
175
176
# File 'lib/windstorm/machine.rb', line 173

def move_point_dec
  @point = point - 1
  point
end

#move_point_incObject



168
169
170
171
# File 'lib/windstorm/machine.rb', line 168

def move_point_inc
  @point = point + 1
  point
end

#outputObject



210
211
212
213
# File 'lib/windstorm/machine.rb', line 210

def output
  putc value if @flash
  cache << value.chr
end

#parse_jumpObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/windstorm/machine.rb', line 124

def parse_jump
  return if commands.empty?
  inds = []
  commands.each.with_index do |com, i|
    case com
    when :jmp
      inds << i
    when :ret
      hind = inds.delete_at(-1)
      raise 'jump couples invalid' unless hind
      jump_table[hind] = i
      jump_table[i] = hind
    end
  end
  raise 'jump couples invalid' unless inds.empty?
  if debug? && !jump_table.empty?
    puts 'jump table:'
    puts jump_table
  end
  self
end

#pasteObject



234
235
236
# File 'lib/windstorm/machine.rb', line 234

def paste
  replace_value(clip_value)
end

#pointObject



162
163
164
165
166
# File 'lib/windstorm/machine.rb', line 162

def point
  @point ||= 0
  strict_error 'point over' if (@point < 0 || @point >= size)
  @point
end

#replace_clip_value(val) ⇒ Object



225
226
227
228
# File 'lib/windstorm/machine.rb', line 225

def replace_clip_value(val)
  strict_error 'value under 0' if val < 0
  @clip_value = val
end

#replace_value(val, po = nil) ⇒ Object



197
198
199
200
201
202
# File 'lib/windstorm/machine.rb', line 197

def replace_value(val, po = nil)
  po ||= point
  strict_error 'value under 0' if val < 0
  strict_error 'point over' if (po < 0 || po >= size)
  buffer[po] = val
end

#stepObject



79
80
81
82
# File 'lib/windstorm/machine.rb', line 79

def step
  @step ||= 0
  @step
end

#step_executeObject



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
# File 'lib/windstorm/machine.rb', line 39

def step_execute
  case com
  when :pinc
    move_point_inc
  when :pdec
    move_point_dec
  when :inc
    increment
  when :dec
    decrement
  when :out
    output
  when :inp
    input
  when :jmp
    jump if value == 0
  when :ret
    jump unless value == 0
  when :clip
    clip
  when :paste
    paste
  end
  debug_out if debug?
  forward
  self
end

#strict?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/windstorm/machine.rb', line 75

def strict?
  ! loose?
end

#value(po = nil) ⇒ Object



191
192
193
194
195
# File 'lib/windstorm/machine.rb', line 191

def value(po = nil)
  po ||= point
  strict_error 'point over' if (po < 0 || po >= size)
  buffer[po] || 0
end