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.



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

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.



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

def compound_operator
  @compound_operator
end

#localObject (readonly)

Returns the value of attribute local.



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

def local
  @local
end

Class Method Details

.symbolObject



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

def self.symbol
  :"="
end

Instance Method Details

#evaluate(context = nil) ⇒ Object



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

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)
  elsif is_list_assignment?
    evaluate_list_assignment(context, lhs, rhs)
  else
    # Try cell assignment
    evaluate_cell_assignment(context, lhs, rhs)
  end
end

#evaluate_assignments(context = nil) ⇒ Object



47
48
49
# File 'lib/keisan/ast/assignment.rb', line 47

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

#is_function_definition?Boolean

Returns:



73
74
75
# File 'lib/keisan/ast/assignment.rb', line 73

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

#is_list_assignment?Boolean

Returns:



77
78
79
# File 'lib/keisan/ast/assignment.rb', line 77

def is_list_assignment?
  children.first.is_a?(List)
end

#is_variable_definition?Boolean

Returns:



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

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

#simplify(context = nil) ⇒ Object



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

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

#symbolObject



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

def symbol
  :"#{compound_operator}="
end

#unbound_functions(context = nil) ⇒ Object



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

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



51
52
53
54
55
56
57
58
# File 'lib/keisan/ast/assignment.rb', line 51

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