Class: Braingasm::Machine

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

Overview

A Machine keeps the state of a running program, and exposes various operations to modify this state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMachine

Returns a new instance of Machine.



11
12
13
14
15
# File 'lib/braingasm/machine.rb', line 11

def initialize
  @tape = Array.new(10) { 0 }
  @dp = 0           # data pointer
  @ip = 0           # instruction pointer
end

Instance Attribute Details

#dpObject

Returns the value of attribute dp.



9
10
11
# File 'lib/braingasm/machine.rb', line 9

def dp
  @dp
end

#ipObject

Returns the value of attribute ip.



9
10
11
# File 'lib/braingasm/machine.rb', line 9

def ip
  @ip
end

#programObject

Returns the value of attribute program.



9
10
11
# File 'lib/braingasm/machine.rb', line 9

def program
  @program
end

#tapeObject

Returns the value of attribute tape.



9
10
11
# File 'lib/braingasm/machine.rb', line 9

def tape
  @tape
end

Instance Method Details

#inst_dec(n = 1) ⇒ Object



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

def inst_dec(n=1)
  @tape[@dp] -= n
  wrap_cell
end

#inst_inc(n = 1) ⇒ Object



62
63
64
65
# File 'lib/braingasm/machine.rb', line 62

def inst_inc(n=1)
  @tape[@dp] += n
  wrap_cell
end

#inst_jump(to) ⇒ Object

Raises:



72
73
74
# File 'lib/braingasm/machine.rb', line 72

def inst_jump(to)
  raise JumpSignal.new(to)
end

#inst_jump_if_zero(to) ⇒ Object

Raises:



76
77
78
# File 'lib/braingasm/machine.rb', line 76

def inst_jump_if_zero(to)
  raise JumpSignal.new(to) if @tape[@dp] == 0
end

#inst_left(n = 1) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/braingasm/machine.rb', line 45

def inst_left(n=1)
  new_dp = @dp - n

  if new_dp < 0
    n.times do
      @tape.unshift 0
    end
    new_dp = 0
  end

  @dp = new_dp
end

#inst_print_cellObject



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

def inst_print_cell
  putc @tape[@dp]
end

#inst_print_tapeObject



58
59
60
# File 'lib/braingasm/machine.rb', line 58

def inst_print_tape
  p @tape
end

#inst_read_byteObject



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

def inst_read_byte
  @tape[@dp] = ARGF.getbyte || Options[:eof] || @tape[@dp]
end

#inst_right(n = 1) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/braingasm/machine.rb', line 33

def inst_right(n=1)
  new_dp = @dp + n
  no_cells = @tape.length

  if new_dp >= no_cells
    grow = new_dp * 3 / 2
    @tape.fill(0, no_cells..grow)
  end

  @dp = new_dp
end

#runObject



17
18
19
20
21
22
23
24
# File 'lib/braingasm/machine.rb', line 17

def run
  return if @program.empty?

  loop do
    continue = step
    break unless continue && @ip < @program.size
  end
end

#stepObject



26
27
28
29
30
31
# File 'lib/braingasm/machine.rb', line 26

def step
  @program[@ip].call(self)
  @ip += 1
rescue JumpSignal => jump
  @ip = jump.to
end