Class: Keisan::Functions::ExpressionFunction

Inherits:
Keisan::Function show all
Defined in:
lib/keisan/functions/expression_function.rb

Instance Attribute Summary collapse

Attributes inherited from Keisan::Function

#name

Instance Method Summary collapse

Constructor Details

#initialize(name, arguments, expression, local_context) ⇒ ExpressionFunction

Returns a new instance of ExpressionFunction.



6
7
8
9
10
11
# File 'lib/keisan/functions/expression_function.rb', line 6

def initialize(name, arguments, expression, local_context)
  super(name)
  @expression = expression.deep_dup
  @arguments = arguments
  @local_context = local_context
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



4
5
6
# File 'lib/keisan/functions/expression_function.rb', line 4

def arguments
  @arguments
end

#expressionObject (readonly)

Returns the value of attribute expression.



4
5
6
# File 'lib/keisan/functions/expression_function.rb', line 4

def expression
  @expression
end

Instance Method Details

#call(context, *args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/keisan/functions/expression_function.rb', line 13

def call(context, *args)
  unless @arguments.count == args.count
    raise Keisan::Exceptions::InvalidFunctionError.new("Invalid number of arguments for #{name} function")
  end

  local = @local_context.spawn_child
  arguments.each.with_index do |arg_name, i|
    local.register_variable!(arg_name, args[i])
  end

  expression.value(local)
end

#evaluate(ast_function, context = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/keisan/functions/expression_function.rb', line 32

def evaluate(ast_function, context = nil)
  context ||= Keisan::Context.new

  ast_function.instance_variable_set(
    :@children,
    ast_function.children.map {|child| child.evaluate(context)}
  )

  if ast_function.children.all? {|child| child.well_defined?(context)}
    value(ast_function, context).to_node.evaluate(context)
  else
    ast_function
  end
end

#simplify(ast_function, context = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/keisan/functions/expression_function.rb', line 47

def simplify(ast_function, context = nil)
  context ||= Context.new

  ast_function.instance_variable_set(
    :@children,
    ast_function.children.map {|child| child.evaluate(context)}
  )

  if ast_function.children.all? {|child| child.is_a?(Keisan::AST::ConstantLiteral)}
    value(ast_function, context).to_node.simplify(context)
  else
    ast_function
  end
end

#value(ast_function, context = nil) ⇒ Object



26
27
28
29
30
# File 'lib/keisan/functions/expression_function.rb', line 26

def value(ast_function, context = nil)
  context ||= Keisan::Context.new
  argument_values = ast_function.children.map {|child| child.value(context)}
  call(context, *argument_values)
end