Guest User

Untitled

a guest
May 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. # class name MUST match /[AnythingCamelCased]Instruction/
  2.  
  3. class MyNewNudgeInstruction < Instruction
  4.  
  5. # preconditions? should return true if everything works
  6. #
  7. # use the #needs method for simple argument requirements checking:
  8. # test will pass if there are at least that many items on the named stack(s)
  9. def preconditions?
  10. needs :foo, 2
  11. needs :bar, 1
  12. end
  13.  
  14.  
  15.  
  16. # use #setup to pop arguments from the stacks or gather them
  17. # from the environment more generally
  18. # (we won't reach here anyway if we don't pass #preconditions?)
  19. # convenience methods include:
  20. # #pop_value(stack_name) -> pop an item AND return its #value attribute
  21. # #pop(stack_name) -> pop an item
  22. # #peek_value(stack_name)-> peek at the top item AND return its #value attribute
  23. # #peek (stack_name) -> peek at the top item
  24. # #depth(stack_name)
  25.  
  26. # the values will be returned by way of [NudgeType].from_string, so for example
  27. # peek_value(:int) -> an integer, not a string
  28.  
  29. def setup
  30. @arg3 = @context.pop_value(:foo)
  31. @arg2 = @context.pop_value(:foo)
  32. @arg1 = @context.pop_value(:bar)
  33.  
  34. # remember the convention here is that the original CODE read like "1 2 divide", so
  35. # the first argument was pushed before the second one
  36. end
  37.  
  38.  
  39.  
  40. # use #derive to build the values or set up whatever else you need to determine what to do;
  41. # if you need to create new Interpreter instances, or access file storage, or
  42. # invoke external scripts, this is where to do it
  43. #
  44. # remember that you are going to be building a ValuePoint object, not the actual result
  45. # of your calculations
  46. #
  47. # Exceptions raised by instruction execution are by convention converted into ValuePoints
  48. # of type :error and pushed to that stack (by the Interpreter's error catching methods)
  49. #
  50. # a few include NotEnoughStackItems (raised when #needs fails), InstructionMethodError (for
  51. # complex failures that might come up, like trying to change the value of a variable),
  52. # NaNResultError (for the obvious math errors like div0 and float overflow),
  53. # MissingInstructionError (in case your instruction calls another, and it's not defined),
  54. # CodeOversizeError (used to enforce a Push3 rule which makes sense). Add your own!
  55.  
  56. def derive
  57. input_image = GetFromHardDrive(@arg1)
  58. mask = MyFancyLibrary.to_mask(@arg2)
  59. alpha = MyFancyLibrary.alphaize(@arg3)
  60.  
  61. begin
  62. munged = MyFancyLibrary.munge_now!(input_image, mask, ginger=alpha)
  63. rescue LibraryFuckupError => e
  64. raise InstructionMethodError, "#{self.class} had a foobie bletch"
  65. end
  66.  
  67. @result1 = ValuePoint.new(:bar, munged)
  68. @result2 = ValuePoint.new(:foo, munged.width)
  69. end
  70.  
  71.  
  72. # #cleanup is when stuff is actually written to the stacks
  73. #
  74. # don't be worried about exception handling; stacks are created on demand
  75. # and nobody ever checks the values you push unless another Instruction
  76. # pops them and tries to read what they are
  77. #
  78. # use the #pushes convenience method for the obvious thing
  79.  
  80. def cleanup
  81. pushes :bar, @result1
  82. pushes :foo, @result2
  83. end
  84. end
Add Comment
Please, Sign In to add comment