Class: RPiet::Machine

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

Overview

This is a simple piet runtime to be controled by interp.

dp - Direction Pointer (right, down, left, up) cc - Codel Chooser (left, right)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMachine

Returns a new instance of Machine.



14
15
16
17
# File 'lib/rpiet/machine.rb', line 14

def initialize
  @stack, @dp, @cc = [], DirectionPointer.new, CodelChooser.new
  @block_value = 1
end

Instance Attribute Details

#block_valueObject

Returns the value of attribute block_value.



12
13
14
# File 'lib/rpiet/machine.rb', line 12

def block_value
  @block_value
end

#ccObject (readonly)

Returns the value of attribute cc.



11
12
13
# File 'lib/rpiet/machine.rb', line 11

def cc
  @cc
end

#dpObject (readonly)

Returns the value of attribute dp.



11
12
13
# File 'lib/rpiet/machine.rb', line 11

def dp
  @dp
end

#stackObject (readonly)

Returns the value of attribute stack.



11
12
13
# File 'lib/rpiet/machine.rb', line 11

def stack
  @stack
end

Instance Method Details

#addObject



40
# File 'lib/rpiet/machine.rb', line 40

def add; math_op :+; end

#c_inObject



54
# File 'lib/rpiet/machine.rb', line 54

def c_in; $stdout.write "> "; c = $stdin.read(1).ord; @stack << c; end

#coutObject



50
# File 'lib/rpiet/machine.rb', line 50

def cout; unary_op { |top| print top.chr }; end

#divObject



43
# File 'lib/rpiet/machine.rb', line 43

def div; math_op :/; end

#dupObject



48
# File 'lib/rpiet/machine.rb', line 48

def dup; @stack << @stack[-1]; end

#execute(color1, color2) ⇒ Object

Execute the operation represented by the two colors



74
75
76
77
78
79
# File 'lib/rpiet/machine.rb', line 74

def execute(color1, color2)
  dh = color1.hue.delta color2.hue
  dd = color1.lightness.delta color2.lightness
  operation = OPERATION[dh][dd]
  __send__(operation)
end

#gtrObject



46
# File 'lib/rpiet/machine.rb', line 46

def gtr; bin_op { |a, b| @stack << (a > b ? 1 : 0) }; end

#inspectObject Also known as: to_s



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

def inspect
  "dp: #{@dp.ascii}, cc: #{@cc.ascii(@dp)}, bv: #{@block_value}, st: #{@stack}"
end

#modObject



44
# File 'lib/rpiet/machine.rb', line 44

def mod; math_op :%; end

#multObject



42
# File 'lib/rpiet/machine.rb', line 42

def mult; math_op :*; end

#n_inObject



53
# File 'lib/rpiet/machine.rb', line 53

def n_in; puts "Enter an integer: "; @stack << $stdin.gets.to_i; end

#next_possible(x, y) ⇒ Object

Return the next possible location based on direction pointers facing



21
22
23
# File 'lib/rpiet/machine.rb', line 21

def next_possible(x, y)
  @dp.next_possible(x, y)
end

#noopObject



36
# File 'lib/rpiet/machine.rb', line 36

def noop; end

#notObject



47
# File 'lib/rpiet/machine.rb', line 47

def not; unary_op { |top| @stack << (!top || top == 0 ? 1 : 0) }; end

#noutObject



49
# File 'lib/rpiet/machine.rb', line 49

def nout; unary_op { |top| print top }; end

#orient_elsewhere(i) ⇒ Object

Change either codel chooser or direction pointer to try and look at a different codel.



28
29
30
31
32
33
34
# File 'lib/rpiet/machine.rb', line 28

def orient_elsewhere(i)
  if i.even?
    dp.rotate!
  else
    cc.switch!
  end
end

#pntrObject



51
# File 'lib/rpiet/machine.rb', line 51

def pntr; unary_op { |top| @dp.rotate! top }; end

#popObject



38
# File 'lib/rpiet/machine.rb', line 38

def pop; @stack.pop; end

#pushObject



37
# File 'lib/rpiet/machine.rb', line 37

def push;  @stack << @block_value; end

#rollObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rpiet/machine.rb', line 55

def roll
  bin_op do |depth, num| 
    num %= depth
    return if depth <= 0 || num == 0
    if num > 0
      @stack[-depth..-1] = @stack[-num..-1] + @stack[-depth...-num]
    elsif num < 0
      @stack[-depth..-1] = @stack[-depth...-num] + @stack[-num..-1]
    end
  end
end

#subObject



41
# File 'lib/rpiet/machine.rb', line 41

def sub; math_op :-; end

#swchObject



52
# File 'lib/rpiet/machine.rb', line 52

def swch; unary_op { |top| @cc.switch! top }; end