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, #deep_dup, #equal, #evaluate_assignments, #evaluated, #false?, #not_equal, #or, #simplified, #to_cell, #to_node, #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



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

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

#differentiate(variable, context = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/keisan/ast/variable.rb', line 70

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/keisan/ast/variable.rb', line 37

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



62
63
64
65
66
67
68
# File 'lib/keisan/ast/variable.rb', line 62

def replace(variable, replacement)
  if name == variable.name
    replacement
  else
    self
  end
end

#simplify(context = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/keisan/ast/variable.rb', line 53

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



33
34
35
# File 'lib/keisan/ast/variable.rb', line 33

def to_s
  name.to_s
end

#unbound_variables(context = nil) ⇒ Object



19
20
21
22
# File 'lib/keisan/ast/variable.rb', line 19

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
# File 'lib/keisan/ast/variable.rb', line 14

def value(context = nil)
  context ||= Context.new
  variable_node_from_context(context).value(context)
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