Class: CodeConsInstruction

Inherits:
Instruction show all
Defined in:
lib/instructions/code/code_cons.rb

Overview

pops the top 2 items from the :code stack; pushes a new :code item containing a block, obtained by consing the other listings, a la Lisp

note: order matters, and the top stack item is the second argument, the second stack item is the first

If the second argument is a block, the result is obtained by inserting the first argument into that block as the new first element; if the second argument isn’t a block, it’s first wrapped in one, and then the first argument is inserted into that block.

For example:

first_argument + second_argument -> cons result
[a, b] + [1,2,3] -> [[a,b],1,2,3]
[a,b] + 1 -> [[a,b],1]
a + [1,2,3] -> [a,1,2,3]
a + 1 -> [a,1]

note: there are several differences between this and code_concatenate

needs: 2 :code

pushes: 1 :code

Instance Attribute Summary

Attributes inherited from Instruction

#context

Instance Method Summary collapse

Methods inherited from Instruction

all_instructions, #go, inherited, #initialize, #needs, #pushes, to_nudgecode

Constructor Details

This class inherits a constructor from Instruction

Instance Method Details

#cleanupObject



43
44
45
# File 'lib/instructions/code/code_cons.rb', line 43

def cleanup
  pushes :code, @result
end

#deriveObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instructions/code/code_cons.rb', line 32

def derive
  t1 = NudgeProgram.new(@arg1)
  t2 = NudgeProgram.new(@arg2)
  if t1.parses? && t2.parses?
    consed_tree = t2.insert_point_before(1,t1.linked_code)
  else
    raise InstructionMethodError,
      "#{self.class.to_nudgecode} cannot parse the arguments"
  end
  @result = ValuePoint.new("code", consed_tree.blueprint)
end

#preconditions?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/instructions/code/code_cons.rb', line 25

def preconditions?
  needs :code, 2
end

#setupObject



28
29
30
31
# File 'lib/instructions/code/code_cons.rb', line 28

def setup
  @arg2 = @context.pop_value(:code)
  @arg1 = @context.pop_value(:code)
end