Class: Keisan::AST::Assignment

Inherits:
Operator show all
Defined in:
lib/keisan/ast/assignment.rb

Constant Summary

Constants inherited from Operator

Operator::ARITIES, Operator::ARITY_PRIORITY_ASSOCIATIVITY, Operator::ASSOCIATIVITIES, Operator::ASSOCIATIVITY_OF_PRIORITY, Operator::PRIORITIES

Instance Attribute Summary collapse

Attributes inherited from Parent

#children

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operator

arity, #arity, associativity, #associativity, associativity_of_priority, #blank_value, #priority, priority, #to_s, #value

Methods inherited from Parent

#==, #deep_dup, #freeze, #replace, #traverse

Methods inherited from Node

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

Constructor Details

#initialize(children = [], parsing_operators = [], local: false, compound_operator: nil) ⇒ Assignment

Returns a new instance of Assignment.



10
11
12
13
14
# File 'lib/keisan/ast/assignment.rb', line 10

def initialize(children = [], parsing_operators = [], local: false, compound_operator: nil)
  super(children, parsing_operators)
  @local = local
  @compound_operator = compound_operator
end

Instance Attribute Details

#compound_operatorObject (readonly)

Returns the value of attribute compound_operator.



8
9
10
# File 'lib/keisan/ast/assignment.rb', line 8

def compound_operator
  @compound_operator
end

#localObject (readonly)

Returns the value of attribute local.



8
9
10
# File 'lib/keisan/ast/assignment.rb', line 8

def local
  @local
end

Class Method Details

.symbolObject



16
17
18
# File 'lib/keisan/ast/assignment.rb', line 16

def self.symbol
  :"="
end

Instance Method Details

#evaluate(context = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/keisan/ast/assignment.rb', line 24

def evaluate(context = nil)
  context ||= Context.new

  lhs = children.first
  rhs = children.last

  if is_variable_definition?
    evaluate_variable_assignment(context, lhs, rhs)
  elsif is_function_definition?
    evaluate_function_assignment(context, lhs, rhs)
  else
    # Try cell assignment
    evaluate_cell_assignment(context, lhs, rhs)
  end
end

#evaluate_assignments(context = nil) ⇒ Object



44
45
46
# File 'lib/keisan/ast/assignment.rb', line 44

def evaluate_assignments(context = nil)
  evaluate(context)
end

#is_function_definition?Boolean

Returns:



70
71
72
# File 'lib/keisan/ast/assignment.rb', line 70

def is_function_definition?
  children.first.is_a?(Function)
end

#is_variable_definition?Boolean

Returns:



66
67
68
# File 'lib/keisan/ast/assignment.rb', line 66

def is_variable_definition?
  children.first.is_a?(Variable)
end

#simplify(context = nil) ⇒ Object



40
41
42
# File 'lib/keisan/ast/assignment.rb', line 40

def simplify(context = nil)
  evaluate(context)
end

#symbolObject



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

def symbol
  :"#{compound_operator}="
end

#unbound_functions(context = nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/keisan/ast/assignment.rb', line 57

def unbound_functions(context = nil)
  functions = super(context)
  if is_function_definition?
    functions.delete(children.first.name)
  else
    functions
  end
end

#unbound_variables(context = nil) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/keisan/ast/assignment.rb', line 48

def unbound_variables(context = nil)
  variables = super(context)
  if is_variable_definition?
    variables.delete(children.first.name)
  else
    variables
  end
end