Class: Keisan::AST::Variable

Inherits:
Literal show all
Defined in:
lib/keisan/ast/variable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#!, #%, #&, #*, #**, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #>, #>=, #>>, #^, #and, #coerce, #contains_a?, #deep_dup, #differentiated, #equal, #evaluate_assignments, #evaluated, #false?, #is_constant?, #not_equal, #or, #replaced, #simplified, #to_cell, #to_node, #traverse, #true?, #unbound_functions, #well_defined?, #|, #~

Constructor Details

#initialize(name) ⇒ Variable

Returns a new instance of Variable.



6
7
8
# File 'lib/keisan/ast/variable.rb', line 6

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/keisan/ast/variable.rb', line 4

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/keisan/ast/variable.rb', line 30

def ==(other)
  case other
  when Variable
    name == other.name
  else
    false
  end
end

#differentiate(variable, context = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/keisan/ast/variable.rb', line 73

def differentiate(variable, context = nil)
  context ||= Context.new

  if name == variable.name && !context.has_variable?(name)
    1.to_node
  else
    0.to_node
  end
end

#evaluate(context = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/keisan/ast/variable.rb', line 43

def evaluate(context = nil)
  context ||= Context.new
  if context.has_variable?(name)
    variable = variable_node_from_context(context)

    # The variable might just be a variable, i.e. probably in function definition
    if variable.is_a?(Node)
      variable.is_a?(Variable) ? variable : variable.evaluate(context)
    else
      variable
    end
  else
    self
  end
end

#replace(variable, replacement) ⇒ Object



68
69
70
71
# File 'lib/keisan/ast/variable.rb', line 68

def replace(variable, replacement)
  to_replace_name = variable.is_a?(::String) ? variable : variable.name
  name == to_replace_name ? replacement : self
end

#simplify(context = nil) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/keisan/ast/variable.rb', line 59

def simplify(context = nil)
  context ||= Context.new
  if context.has_variable?(name)
    context.variable(name).to_node.simplify(context)
  else
    self
  end
end

#to_sObject



39
40
41
# File 'lib/keisan/ast/variable.rb', line 39

def to_s
  name.to_s
end

#unbound_variables(context = nil) ⇒ Object



25
26
27
28
# File 'lib/keisan/ast/variable.rb', line 25

def unbound_variables(context = nil)
  context ||= Context.new
  context.has_variable?(name) ? Set.new : Set.new([name])
end

#value(context = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/keisan/ast/variable.rb', line 14

def value(context = nil)
  context ||= Context.new
  node = variable_node_from_context(context)
  case node
  when Variable
    node
  else
    node.value(context)
  end
end

#variable_truthy?(context) ⇒ Boolean

Returns:



10
11
12
# File 'lib/keisan/ast/variable.rb', line 10

def variable_truthy?(context)
  context.has_variable?(name) && context.variable(name).true?
end