Class: CodeConcatenateInstruction

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

Overview

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

note: the top stack item (the “attachment”) is concatenated into the second popped item (the “receiver”)

If both items are already blocks, the new block is their concatenation; if the receiver is a block and the attachment isn’t, the attachment is appended to the receiver; if the receiver is not a block, then a surrounding block is constructed that contains both, in order.

For example:

[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]

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



48
49
50
# File 'lib/instructions/code/code_concatenate.rb', line 48

def cleanup
  pushes :code, @result
end

#deriveObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/instructions/code/code_concatenate.rb', line 29

def derive
  t1 = NudgeProgram.new(@arg1)
  t2 = NudgeProgram.new(@arg2)
  if t1.parses? && t2.parses?
    if t1.linked_code.kind_of?(CodeblockPoint)
      if t2.linked_code.kind_of?(CodeblockPoint)
        new_tree = t1[1].contents + t2[1].contents
      else
        new_tree = t1[1].contents << t2.linked_code
      end
    else
      new_tree = [t1.linked_code, t2.linked_code]
    end
    listed = CodeblockPoint.new(new_tree).blueprint
    @result = ValuePoint.new("code", listed)
  else
    @result = ValuePoint.new("code", "block {}")
  end
end

#preconditions?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/instructions/code/code_concatenate.rb', line 22

def preconditions?
  needs :code, 2
end

#setupObject



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

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