Class: Cauldron::NumberAdditionTemplate::AddFive

Inherits:
Object
  • Object
show all
Defined in:
lib/cauldron/number_addition_template/add_five.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexes) ⇒ AddFive

Returns a new instance of AddFive.



9
10
11
# File 'lib/cauldron/number_addition_template/add_five.rb', line 9

def initialize(indexes)
  @indexes = indexes
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



7
8
9
# File 'lib/cauldron/number_addition_template/add_five.rb', line 7

def indexes
  @indexes
end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/cauldron/number_addition_template/add_five.rb', line 63

def branch?
  false
end

#clone_statementObject



51
52
53
54
55
56
57
# File 'lib/cauldron/number_addition_template/add_five.rb', line 51

def clone_statement
  # TODO Need to clone the sexp methods
  # o = DynamicOperator.new(@information, @sexp_methods)
  # o.instance_eval(Sorcerer.source(@sexp_methods, indent: true))
  # o
  self.class.new(@indexes.clone)
end

#context_realizable?(context) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cauldron/number_addition_template/add_five.rb', line 13

def context_realizable?(context)
  
  vars = context.keys.select {|x| x.match(/var\d/) }
  var_names = vars.collect(&:to_s)
  
  first_variable = 'var'+@indexes[0].to_s

  a = %Q{
  def function(#{first_variable})
    #{Sorcerer.source(to_sexp(Cauldron::Scope.new(var_names), []), indent: true)}
  end
  }     
  
  o = Object.new
  o.instance_eval(a)

  begin
    #o.function(*vars.collect {|x| context[x] })  
    o.function context[first_variable.to_sym]
  rescue NoMethodError => e
    return false
  rescue StandardError => e
    puts e
    return false
  end
  return true
   
end

#to_ruby(scope, operators) ⇒ Object



59
60
61
# File 'lib/cauldron/number_addition_template/add_five.rb', line 59

def to_ruby(scope, operators)
  Sorcerer.source to_sexp(scope, operators)
end

#to_sexp(scope, children) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cauldron/number_addition_template/add_five.rb', line 42

def to_sexp(scope, children)
  first_variable = 'var'+@indexes[0].to_s
  #binding.pry
  #'var0 + 5'
  #''
  puts %Q{#{first_variable} + 5}
  Ripper::SexpBuilder.new(%Q{#{first_variable} + 5}).parse
end