Class: Windstorm::Machine
- Inherits:
-
Object
- Object
- Windstorm::Machine
- Defined in:
- lib/windstorm/machine.rb
Instance Attribute Summary collapse
- #commands ⇒ Object
-
#debug ⇒ Object
writeonly
Sets the attribute debug.
-
#flash ⇒ Object
writeonly
Sets the attribute flash.
-
#loose ⇒ Object
writeonly
Sets the attribute loose.
- #size ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #add_index ⇒ Object
- #add_step ⇒ Object
- #buffer ⇒ Object
- #cache ⇒ Object (also: #result)
- #clip ⇒ Object
- #clip_value ⇒ Object
- #com(ind = nil) ⇒ Object
- #debug? ⇒ Boolean
- #debug_out ⇒ Object
- #decrement ⇒ Object
- #execute ⇒ Object
- #finish? ⇒ Boolean
- #forward ⇒ Object
- #increment ⇒ Object
- #index ⇒ Object
- #input ⇒ Object
- #jump ⇒ Object
- #jump_index(ind) ⇒ Object
- #jump_table ⇒ Object
- #loose? ⇒ Boolean
- #move_point_dec ⇒ Object
- #move_point_inc ⇒ Object
- #output ⇒ Object
- #parse_jump ⇒ Object
- #paste ⇒ Object
- #point ⇒ Object
- #replace_clip_value(val) ⇒ Object
- #replace_value(val, po = nil) ⇒ Object
- #step ⇒ Object
- #step_execute ⇒ Object
- #strict? ⇒ Boolean
- #value(po = nil) ⇒ Object
Instance Attribute Details
#commands ⇒ Object
89 90 91 92 |
# File 'lib/windstorm/machine.rb', line 89 def commands @commands ||= [] @commands end |
#debug=(value) ⇒ Object (writeonly)
Sets the attribute debug
5 6 7 |
# File 'lib/windstorm/machine.rb', line 5 def debug=(value) @debug = value end |
#flash=(value) ⇒ Object (writeonly)
Sets the attribute flash
5 6 7 |
# File 'lib/windstorm/machine.rb', line 5 def flash=(value) @flash = value end |
#loose=(value) ⇒ Object (writeonly)
Sets the attribute loose
5 6 7 |
# File 'lib/windstorm/machine.rb', line 5 def loose=(value) @loose = value end |
#size ⇒ Object
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_index ⇒ Object
99 100 101 102 |
# File 'lib/windstorm/machine.rb', line 99 def add_index @index = index + 1 @index end |
#add_step ⇒ Object
84 85 86 87 |
# File 'lib/windstorm/machine.rb', line 84 def add_step @step = step + 1 step end |
#buffer ⇒ Object
186 187 188 189 |
# File 'lib/windstorm/machine.rb', line 186 def buffer @buffer ||= [] @buffer end |
#cache ⇒ Object Also known as: result
204 205 206 207 |
# File 'lib/windstorm/machine.rb', line 204 def cache @cache ||= "" @cache end |
#clip ⇒ Object
230 231 232 |
# File 'lib/windstorm/machine.rb', line 230 def clip replace_clip_value(value) end |
#clip_value ⇒ Object
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
67 68 69 |
# File 'lib/windstorm/machine.rb', line 67 def debug? @debug ? true : false end |
#debug_out ⇒ Object
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 |
#decrement ⇒ Object
182 183 184 |
# File 'lib/windstorm/machine.rb', line 182 def decrement replace_value(value-1) end |
#execute ⇒ Object
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
115 116 117 |
# File 'lib/windstorm/machine.rb', line 115 def finish? index >= commands.size ? true : false end |
#forward ⇒ Object
109 110 111 112 113 |
# File 'lib/windstorm/machine.rb', line 109 def forward add_step add_index self end |
#increment ⇒ Object
178 179 180 |
# File 'lib/windstorm/machine.rb', line 178 def increment replace_value(value+1) end |
#index ⇒ Object
94 95 96 97 |
# File 'lib/windstorm/machine.rb', line 94 def index @index ||= 0 @index end |
#input ⇒ Object
215 216 217 218 |
# File 'lib/windstorm/machine.rb', line 215 def input c = $stdin.getc replace_value(c.bytes.first) end |
#jump ⇒ Object
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_table ⇒ Object
119 120 121 122 |
# File 'lib/windstorm/machine.rb', line 119 def jump_table @jump_table ||= {} @jump_table end |
#loose? ⇒ Boolean
71 72 73 |
# File 'lib/windstorm/machine.rb', line 71 def loose? @loose ? true : false end |
#move_point_dec ⇒ Object
173 174 175 176 |
# File 'lib/windstorm/machine.rb', line 173 def move_point_dec @point = point - 1 point end |
#move_point_inc ⇒ Object
168 169 170 171 |
# File 'lib/windstorm/machine.rb', line 168 def move_point_inc @point = point + 1 point end |
#output ⇒ Object
210 211 212 213 |
# File 'lib/windstorm/machine.rb', line 210 def output putc value if @flash cache << value.chr end |
#parse_jump ⇒ Object
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 |
#paste ⇒ Object
234 235 236 |
# File 'lib/windstorm/machine.rb', line 234 def paste replace_value(clip_value) end |
#point ⇒ Object
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 |
#step ⇒ Object
79 80 81 82 |
# File 'lib/windstorm/machine.rb', line 79 def step @step ||= 0 @step end |
#step_execute ⇒ Object
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
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 |