Class: Mapleseed::OpRing

Inherits:
Ring
  • Object
show all
Defined in:
lib/mapleseed/ring.rb

Overview

command ring for whirl operations

Instance Attribute Summary

Attributes inherited from Ring

#commands, #direction, #position, #value

Instance Method Summary collapse

Methods inherited from Ring

#execute, #rotate, #switch_direction

Constructor Details

#initialize(interpreter) ⇒ OpRing

initialize commands for operation ring



42
43
44
45
# File 'lib/mapleseed/ring.rb', line 42

def initialize(interpreter)
  super(interpreter)
  @commands = [:noop, :exit, :one, :zero, :load, :store, :p_add, :d_add, :logic, :if, :int_io, :asc_io]
end

Instance Method Details

#asc_ioObject

if ring value is 0, set memory value to character input, otherwise print memory value as an character



119
120
121
122
123
124
125
126
# File 'lib/mapleseed/ring.rb', line 119

def asc_io
  if @value == 0
    str = @interpreter.input_stream.getc
    @interpreter.memory.set(@interpreter.memory_position, str)
  else
    @interpreter.output_stream.putc @interpreter.memory.get(@interpreter.memory_position)
  end
end

#d_addObject

change location in memory



86
87
88
# File 'lib/mapleseed/ring.rb', line 86

def d_add
  @interpreter.memory_position += @value
end

#exitObject

exit the program



53
54
55
# File 'lib/mapleseed/ring.rb', line 53

def exit
  throw :quit
end

#ifObject

if value in memory is not 0, add ring value to program position



100
101
102
103
104
# File 'lib/mapleseed/ring.rb', line 100

def if
  if @interpreter.memory.get(@interpreter.memory_position) != 0
    p_add
  end
end

#int_ioObject

if ring value is 0, set memory value to integer input, otherwise print memory value as an integer



108
109
110
111
112
113
114
115
# File 'lib/mapleseed/ring.rb', line 108

def int_io
  if @value == 0
    str = @interpreter.input_stream.gets.chomp
    @interpreter.memory.set(@interpreter.memory_position, str.to_i)
  else
    @interpreter.output_stream.print @interpreter.memory.get(@interpreter.memory_position)
  end
end

#loadObject

set the ring value to the current memory value



68
69
70
# File 'lib/mapleseed/ring.rb', line 68

def load
  @value = @interpreter.memory.get(@interpreter.memory_position)
end

#logicObject

logical AND



91
92
93
94
95
96
97
# File 'lib/mapleseed/ring.rb', line 91

def logic
  if @interpreter.memory.get(@interpreter.memory_position) != 0 and @value != 0
    @value = 1
  else
    @value = 0
  end
end

#noopObject

do nothing



48
49
50
# File 'lib/mapleseed/ring.rb', line 48

def noop
  #...

end

#oneObject

set the ring value to 1



58
59
60
# File 'lib/mapleseed/ring.rb', line 58

def one
  @value = 1
end

#p_addObject

change location in the program



78
79
80
81
82
83
# File 'lib/mapleseed/ring.rb', line 78

def p_add
  @interpreter.program_position += @value - 1
  if @interpreter.program_position < 0
    @interpreter.program_position = 0
  end
end

#storeObject

set the current value in memory to the ring value



73
74
75
# File 'lib/mapleseed/ring.rb', line 73

def store
  @interpreter.memory.set(@interpreter.memory_position, @value)
end

#zeroObject

set the ring value to 0



63
64
65
# File 'lib/mapleseed/ring.rb', line 63

def zero
  @value = 0
end