Class: RPiet::Machine
- Inherits:
-
Object
- Object
- RPiet::Machine
- 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
-
#block_value ⇒ Object
Returns the value of attribute block_value.
-
#cc ⇒ Object
readonly
Returns the value of attribute cc.
-
#dp ⇒ Object
readonly
Returns the value of attribute dp.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #add ⇒ Object
- #c_in ⇒ Object
- #cout ⇒ Object
- #div ⇒ Object
- #dup ⇒ Object
-
#execute(color1, color2) ⇒ Object
Execute the operation represented by the two colors.
- #gtr ⇒ Object
-
#initialize ⇒ Machine
constructor
A new instance of Machine.
- #inspect ⇒ Object (also: #to_s)
- #mod ⇒ Object
- #mult ⇒ Object
- #n_in ⇒ Object
-
#next_possible(x, y) ⇒ Object
Return the next possible location based on direction pointers facing.
- #noop ⇒ Object
- #not ⇒ Object
- #nout ⇒ Object
-
#orient_elsewhere(i) ⇒ Object
Change either codel chooser or direction pointer to try and look at a different codel.
- #pntr ⇒ Object
- #pop ⇒ Object
- #push ⇒ Object
- #roll ⇒ Object
- #sub ⇒ Object
- #swch ⇒ Object
Constructor Details
#initialize ⇒ Machine
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_value ⇒ Object
Returns the value of attribute block_value.
12 13 14 |
# File 'lib/rpiet/machine.rb', line 12 def block_value @block_value end |
#cc ⇒ Object (readonly)
Returns the value of attribute cc.
11 12 13 |
# File 'lib/rpiet/machine.rb', line 11 def cc @cc end |
#dp ⇒ Object (readonly)
Returns the value of attribute dp.
11 12 13 |
# File 'lib/rpiet/machine.rb', line 11 def dp @dp end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
11 12 13 |
# File 'lib/rpiet/machine.rb', line 11 def stack @stack end |
Instance Method Details
#add ⇒ Object
40 |
# File 'lib/rpiet/machine.rb', line 40 def add; math_op :+; end |
#c_in ⇒ Object
54 |
# File 'lib/rpiet/machine.rb', line 54 def c_in; $stdout.write "> "; c = $stdin.read(1).ord; @stack << c; end |
#cout ⇒ Object
50 |
# File 'lib/rpiet/machine.rb', line 50 def cout; unary_op { |top| print top.chr }; end |
#div ⇒ Object
43 |
# File 'lib/rpiet/machine.rb', line 43 def div; math_op :/; end |
#dup ⇒ Object
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 |
#gtr ⇒ Object
46 |
# File 'lib/rpiet/machine.rb', line 46 def gtr; bin_op { |a, b| @stack << (a > b ? 1 : 0) }; end |
#inspect ⇒ Object 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 |
#mod ⇒ Object
44 |
# File 'lib/rpiet/machine.rb', line 44 def mod; math_op :%; end |
#mult ⇒ Object
42 |
# File 'lib/rpiet/machine.rb', line 42 def mult; math_op :*; end |
#n_in ⇒ Object
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 |
#noop ⇒ Object
36 |
# File 'lib/rpiet/machine.rb', line 36 def noop; end |
#not ⇒ Object
47 |
# File 'lib/rpiet/machine.rb', line 47 def not; unary_op { |top| @stack << (!top || top == 0 ? 1 : 0) }; end |
#nout ⇒ Object
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 |
#pntr ⇒ Object
51 |
# File 'lib/rpiet/machine.rb', line 51 def pntr; unary_op { |top| @dp.rotate! top }; end |
#pop ⇒ Object
38 |
# File 'lib/rpiet/machine.rb', line 38 def pop; @stack.pop; end |
#push ⇒ Object
37 |
# File 'lib/rpiet/machine.rb', line 37 def push; @stack << @block_value; end |
#roll ⇒ Object
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 |
#sub ⇒ Object
41 |
# File 'lib/rpiet/machine.rb', line 41 def sub; math_op :-; end |
#swch ⇒ Object
52 |
# File 'lib/rpiet/machine.rb', line 52 def swch; unary_op { |top| @cc.switch! top }; end |