Class: Keisan::AST::FunctionAssignment

Inherits:
Object
  • Object
show all
Defined in:
lib/keisan/ast/function_assignment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, lhs, rhs, local) ⇒ FunctionAssignment

Returns a new instance of FunctionAssignment.



6
7
8
9
10
11
12
13
14
15
# File 'lib/keisan/ast/function_assignment.rb', line 6

def initialize(context, lhs, rhs, local)
  @context = context
  @lhs = lhs
  @rhs = rhs
  @local = local

  unless lhs.children.all? {|arg| arg.is_a?(Variable)}
    raise Exceptions::InvalidExpression.new("Left hand side function must have variables as arguments")
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#lhsObject (readonly)

Returns the value of attribute lhs.



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

def lhs
  @lhs
end

#localObject (readonly)

Returns the value of attribute local.



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

def local
  @local
end

#rhsObject (readonly)

Returns the value of attribute rhs.



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

def rhs
  @rhs
end

Instance Method Details

#argument_namesObject



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

def argument_names
  @argument_names ||= lhs.children.map(&:name)
end

#evaluateObject



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

def evaluate
  # Blocks might have local variable/function definitions, so skip check
  verify_rhs_of_function_assignment_is_valid!
  context.register_function!(lhs.name, expression_function, local: local)
  rhs
end

#expression_functionObject



21
22
23
24
25
26
27
28
# File 'lib/keisan/ast/function_assignment.rb', line 21

def expression_function
  Functions::ExpressionFunction.new(
    lhs.name,
    argument_names,
    rhs.evaluate_assignments(context.spawn_child(shadowed: argument_names, transient: true)),
    context.transient_definitions
  )
end